Search in sources :

Example 6 with DatabaseConnectionPortObjectSpec

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

the class DBTableSelectorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    if ((m_settings.getQuery() == null) || m_settings.getQuery().isEmpty()) {
        throw new InvalidSettingsException("No query configured");
    }
    DatabaseConnectionPortObjectSpec incomingConnection = (DatabaseConnectionPortObjectSpec) inSpecs[0];
    DatabaseConnectionSettings connSettings = incomingConnection.getConnectionSettings(getCredentialsProvider());
    String sql = FlowVariableResolver.parse(m_settings.getQuery(), this);
    DatabaseQueryConnectionSettings querySettings = new DatabaseQueryConnectionSettings(connSettings, sql);
    final DBReader conn = connSettings.getUtility().getReader(querySettings);
    if (!connSettings.getRetrieveMetadataInConfigure()) {
        return new PortObjectSpec[1];
    }
    try {
        DataTableSpec tableSpec = conn.getDataTableSpec(getCredentialsProvider());
        return new PortObjectSpec[] { new DatabasePortObjectSpec(tableSpec, querySettings) };
    } catch (SQLException ex) {
        Throwable cause = ExceptionUtils.getRootCause(ex);
        if (cause == null) {
            cause = ex;
        }
        throw new InvalidSettingsException("Error while validating SQL query '" + sql + "' : " + cause.getMessage(), ex);
    }
}
Also used : DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataTableSpec(org.knime.core.data.DataTableSpec) DBReader(org.knime.core.node.port.database.reader.DBReader) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) SQLException(java.sql.SQLException) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)

Example 7 with DatabaseConnectionPortObjectSpec

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

the class DBDeleteRowsDialogPane method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final PortObjectSpec[] specs) throws NotConfigurableException {
    DataTableSpec tableSpec = (DataTableSpec) specs[0];
    // load login setting
    m_loginPanel.loadSettingsFrom(settings, specs, getCredentialsProvider());
    // load table name
    m_tableName.setText(settings.getString(DBDeleteRowsNodeModel.KEY_TABLE_NAME, ""));
    // load WHERE column panel
    DataColumnSpecFilterConfiguration configWhere = new DataColumnSpecFilterConfiguration(DBDeleteRowsNodeModel.KEY_WHERE_FILTER_COLUMN);
    configWhere.loadConfigurationInDialog(settings, tableSpec);
    m_columnsInWhereClause.loadConfiguration(configWhere, tableSpec);
    // load batch size
    final int batchSize = settings.getInt(DBDeleteRowsNodeModel.KEY_BATCH_SIZE, 1);
    m_batchSize.setText(Integer.toString(batchSize));
    if ((specs.length > 1) && (specs[1] instanceof DatabaseConnectionPortObjectSpec)) {
        m_loginPanel.setVisible(false);
    } else {
        m_loginPanel.setVisible(true);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecFilterConfiguration(org.knime.core.node.util.filter.column.DataColumnSpecFilterConfiguration) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)

Example 8 with DatabaseConnectionPortObjectSpec

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

the class DBDeleteRowsNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
    // check table name
    if (m_tableName == null || m_tableName.trim().isEmpty()) {
        throw new InvalidSettingsException("Configure node and enter a valid table name.");
    }
    final List<String> whereIncludes = new ArrayList<String>(Arrays.asList(m_configWHERE.applyTo(tableSpec).getIncludes()));
    if (whereIncludes.isEmpty()) {
        throw new InvalidSettingsException("No WHERE column selected.");
    }
    // check optional incoming connection
    if ((inSpecs.length > 1) && (inSpecs[1] instanceof DatabaseConnectionPortObjectSpec)) {
        DatabaseConnectionSettings connSettings = ((DatabaseConnectionPortObjectSpec) inSpecs[1]).getConnectionSettings(getCredentialsProvider());
        if ((connSettings.getJDBCUrl() == null) || connSettings.getJDBCUrl().isEmpty() || (connSettings.getDriver() == null) || connSettings.getDriver().isEmpty()) {
            throw new InvalidSettingsException("No valid database connection provided via second input port");
        }
        if (!connSettings.getUtility().supportsDelete()) {
            throw new InvalidSettingsException("Connected database does not support delete operations");
        }
    } else {
        if (!m_loginConfig.getUtility().supportsDelete()) {
            throw new InvalidSettingsException("Selected database does not support delete operations");
        }
    }
    // forward input spec with one additional update (=int) column
    final ColumnRearranger colre = createColumnRearranger(tableSpec, new int[] {});
    return new DataTableSpec[] { colre.createSpec() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) ArrayList(java.util.ArrayList) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)

Example 9 with DatabaseConnectionPortObjectSpec

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

the class DBReaderNodeModel method getResultSpec.

/**
 * @param inSpecs input spec
 * @return the {@link DataTableSpec} of the result table
 * @throws InvalidSettingsException if the connection settings are invalid
 * @throws SQLException if the query is invalid
 */
protected DataTableSpec getResultSpec(final PortObjectSpec[] inSpecs) throws InvalidSettingsException, SQLException {
    String query = parseQuery(m_settings.getQuery());
    DatabaseQueryConnectionSettings connSettings;
    if (hasDatabaseInputConnection(inSpecs)) {
        DatabaseConnectionPortObjectSpec connSpec = (DatabaseConnectionPortObjectSpec) inSpecs[getNrInPorts() - 1];
        connSettings = new DatabaseQueryConnectionSettings(connSpec.getConnectionSettings(getCredentialsProvider()), query);
    } else {
        connSettings = new DatabaseQueryConnectionSettings(m_settings, query);
    }
    DBReader reader = connSettings.getUtility().getReader(connSettings);
    final DataTableSpec resultSpec = reader.getDataTableSpec(getCredentialsProvider());
    return resultSpec;
}
Also used : DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataTableSpec(org.knime.core.data.DataTableSpec) DBReader(org.knime.core.node.port.database.reader.DBReader) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)

Example 10 with DatabaseConnectionPortObjectSpec

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

the class DatabaseLoopingNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    final DataTableSpec lastSpec = getLastSpec();
    if (lastSpec != null) {
        return new DataTableSpec[] { lastSpec };
    }
    final DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
    String column = m_columnModel.getStringValue();
    if (column == null) {
        throw new InvalidSettingsException("No column selected.");
    }
    final int colIdx = tableSpec.findColumnIndex(column);
    if (colIdx < 0) {
        throw new InvalidSettingsException("Column '" + column + "' not found in input data.");
    }
    if ((inSpecs.length > 1) && (inSpecs[1] instanceof DatabaseConnectionPortObjectSpec)) {
        DatabaseConnectionSettings connSettings = ((DatabaseConnectionPortObjectSpec) inSpecs[1]).getConnectionSettings(getCredentialsProvider());
        m_settings.setValidateQuery(connSettings.getRetrieveMetadataInConfigure());
    } else {
        m_settings.setValidateQuery(true);
    }
    if (!m_settings.getValidateQuery()) {
        setLastSpec(null);
        return new DataTableSpec[] { null };
    }
    final String oQuery = getQuery();
    PortObjectSpec[] spec = null;
    try {
        final String newQuery;
        newQuery = createDummyValueQuery(tableSpec, colIdx, oQuery);
        setQuery(newQuery);
        spec = new DataTableSpec[] { getResultSpec(inSpecs) };
    } catch (InvalidSettingsException e) {
        setLastSpec(null);
        throw e;
    } catch (SQLException ex) {
        setLastSpec(null);
        Throwable cause = ExceptionUtils.getRootCause(ex);
        if (cause == null) {
            cause = ex;
        }
        throw new InvalidSettingsException("Could not determine table spec from database query: " + cause.getMessage(), ex);
    } finally {
        setQuery(oQuery);
    }
    if (spec[0] == null) {
        return spec;
    } else {
        final DataTableSpec resultSpec = createSpec((DataTableSpec) spec[0], tableSpec.getColumnSpec(column));
        setLastSpec(resultSpec);
        return new DataTableSpec[] { resultSpec };
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) SQLException(java.sql.SQLException) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)

Aggregations

DatabaseConnectionPortObjectSpec (org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)13 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)10 DataTableSpec (org.knime.core.data.DataTableSpec)9 DatabaseConnectionSettings (org.knime.core.node.port.database.DatabaseConnectionSettings)7 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)4 SQLException (java.sql.SQLException)3 DatabaseQueryConnectionSettings (org.knime.core.node.port.database.DatabaseQueryConnectionSettings)3 ArrayList (java.util.ArrayList)2 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2 DBReader (org.knime.core.node.port.database.reader.DBReader)2 DataColumnSpecFilterConfiguration (org.knime.core.node.util.filter.column.DataColumnSpecFilterConfiguration)2 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 DataType (org.knime.core.data.DataType)1 DoubleValue (org.knime.core.data.DoubleValue)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)1 NotConfigurableException (org.knime.core.node.NotConfigurableException)1