Search in sources :

Example 1 with DBTableCreator

use of org.knime.core.node.port.database.tablecreator.DBTableCreator in project knime-core by knime.

the class DBTableCreatorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    if (inSpecs[0] == null && !(inSpecs[0] instanceof DatabaseConnectionPortObjectSpec)) {
        throw new InvalidSettingsException("No valid database connection available.");
    }
    final DatabaseConnectionPortObjectSpec dbSpec = (DatabaseConnectionPortObjectSpec) inSpecs[0];
    m_config.setTableSpec((DataTableSpec) inSpecs[1]);
    final boolean isColumnsEmpty = m_config.getColumns().isEmpty();
    if (m_config.getTableSpec() != null && (m_config.useDynamicSettings() || isColumnsEmpty)) {
        m_config.loadColumnSettingsFromTableSpec(m_config.getTableSpec());
        m_config.updateKeysWithDynamicSettings();
    }
    if (m_config.getTableSpec() == null && m_config.useDynamicSettings()) {
        throw new InvalidSettingsException("Dynamic settings enabled but no input table available.");
    }
    if (isColumnsEmpty) {
        throw new InvalidSettingsException("At least one column must be defined.");
    }
    final DatabaseConnectionSettings conn = dbSpec.getConnectionSettings(getCredentialsProvider());
    final DBTableCreator tableCreator = conn.getUtility().getTableCreator(m_config.getSchema(), getTableName(), m_config.isTempTable());
    final List<DBColumn> columns = m_config.getColumns();
    final List<DBKey> keys = m_config.getKeys();
    try {
        tableCreator.validateSettings(m_config.ifNotExists(), columns.toArray(new DBColumn[columns.size()]), keys.toArray(new DBKey[keys.size()]), m_config.getAdditionalOptions());
    } catch (Exception e) {
        throw new InvalidSettingsException(e.getMessage());
    }
    pushFlowVariables(tableCreator.getSchema(), tableCreator.getTableName());
    return new PortObjectSpec[] { dbSpec };
}
Also used : DBTableCreator(org.knime.core.node.port.database.tablecreator.DBTableCreator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec) DBColumn(org.knime.core.node.port.database.tablecreator.DBColumn) DBKey(org.knime.core.node.port.database.tablecreator.DBKey) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)

Example 2 with DBTableCreator

use of org.knime.core.node.port.database.tablecreator.DBTableCreator in project knime-core by knime.

the class DBTableCreatorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    exec.setMessage("Creating table");
    final DatabaseConnectionPortObject dbConn = (DatabaseConnectionPortObject) inData[0];
    final DatabaseConnectionSettings conn = dbConn.getConnectionSettings(getCredentialsProvider());
    final DBTableCreator tableCreator = conn.getUtility().getTableCreator(m_config.getSchema(), getTableName(), m_config.isTempTable());
    final List<DBColumn> columns = m_config.getColumns();
    final List<DBKey> keys = m_config.getKeys();
    tableCreator.createTable(conn, getCredentialsProvider(), m_config.ifNotExists(), columns.toArray(new DBColumn[columns.size()]), keys.toArray(new DBKey[keys.size()]), m_config.getAdditionalOptions());
    pushFlowVariables(tableCreator.getSchema(), tableCreator.getTableName());
    final String warning = tableCreator.getWarning();
    if (!StringUtils.isBlank(warning)) {
        setWarningMessage(warning);
    }
    return new PortObject[] { dbConn };
}
Also used : DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DBTableCreator(org.knime.core.node.port.database.tablecreator.DBTableCreator) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) DBColumn(org.knime.core.node.port.database.tablecreator.DBColumn) DBKey(org.knime.core.node.port.database.tablecreator.DBKey) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) PortObject(org.knime.core.node.port.PortObject)

Aggregations

DatabaseConnectionSettings (org.knime.core.node.port.database.DatabaseConnectionSettings)2 DBColumn (org.knime.core.node.port.database.tablecreator.DBColumn)2 DBKey (org.knime.core.node.port.database.tablecreator.DBKey)2 DBTableCreator (org.knime.core.node.port.database.tablecreator.DBTableCreator)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1 PortObject (org.knime.core.node.port.PortObject)1 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)1 DatabaseConnectionPortObject (org.knime.core.node.port.database.DatabaseConnectionPortObject)1 DatabaseConnectionPortObjectSpec (org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)1