Search in sources :

Example 6 with DatabaseConnectionSettings

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

the class DBUpdateNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
    // 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().supportsUpdate()) {
            throw new InvalidSettingsException("Connected database does not support update operations");
        }
    } else {
        if (!m_loginConfig.getUtility().supportsUpdate()) {
            throw new InvalidSettingsException("Selected database does not support update operations");
        }
    }
    // check table name
    if ((m_tableName == null) || m_tableName.trim().isEmpty()) {
        throw new InvalidSettingsException("Configure node and enter a valid table name.");
    }
    // check SET and WHERE for overlapping columns
    final List<String> setIncludes = new ArrayList<>(Arrays.asList(m_configSET.applyTo(tableSpec).getIncludes()));
    if (setIncludes.isEmpty()) {
        throw new InvalidSettingsException("No SET column selected.");
    }
    final List<String> whereIncludes = new ArrayList<>(Arrays.asList(m_configWHERE.applyTo(tableSpec).getIncludes()));
    if (whereIncludes.isEmpty()) {
        throw new InvalidSettingsException("No WHERE column selected.");
    }
    if (!setIncludes.retainAll(whereIncludes)) {
        throw new InvalidSettingsException("SET and WHERE column list contain duplicate columns.");
    }
    // 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) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ArrayList(java.util.ArrayList) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)

Example 7 with DatabaseConnectionSettings

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

the class DBUpdateNodeModel method validateSettings.

/**
 * {@inheritDoc}
 */
@Override
protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException {
    // validate login settings
    new DatabaseConnectionSettings(settings, getCredentialsProvider());
    // validate table name
    final String tableName = settings.getString(KEY_TABLE_NAME);
    if (tableName == null || tableName.trim().isEmpty()) {
        throw new InvalidSettingsException("Configure node and enter a valid table name.");
    }
    // validate SET columns
    final DataColumnSpecFilterConfiguration confSET = new DataColumnSpecFilterConfiguration(KEY_SET_FILTER_COLUMN);
    confSET.loadConfigurationInModel(settings);
    // validate WHERE columns
    final DataColumnSpecFilterConfiguration confWHERE = new DataColumnSpecFilterConfiguration(KEY_WHERE_FILTER_COLUMN);
    confWHERE.loadConfigurationInModel(settings);
    // validate batch size
    final int batchSize = settings.getInt(KEY_BATCH_SIZE);
    if (batchSize <= 0) {
        throw new InvalidSettingsException("Batch size must be greater than 0, is " + batchSize);
    }
}
Also used : DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataColumnSpecFilterConfiguration(org.knime.core.node.util.filter.column.DataColumnSpecFilterConfiguration)

Example 8 with DatabaseConnectionSettings

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

the class SQLInjectNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    exec.setMessage("Retrieving metadata from database");
    final String varName = m_flowVariableName.getStringValue();
    if (varName == null) {
        throw new InvalidSettingsException("No flow variable for the SQL statement selected");
    }
    final String sql = peekFlowVariableString(varName);
    if (sql == null) {
        throw new InvalidSettingsException("The selected flow variable is not assigned");
    }
    DatabaseConnectionPortObject dbIn = (DatabaseConnectionPortObject) inData[0];
    DatabaseConnectionSettings inSettings = dbIn.getConnectionSettings(getCredentialsProvider());
    // Attach the SQL query
    DatabaseQueryConnectionSettings outSettings = new DatabaseQueryConnectionSettings(inSettings, sql);
    // Probe the database to see how the result table looks like
    DBReader load = outSettings.getUtility().getReader(outSettings);
    DataTableSpec tableSpec = load.getDataTableSpec(getCredentialsProvider());
    DatabasePortObjectSpec outSpec = new DatabasePortObjectSpec(tableSpec, outSettings.createConnectionModel());
    return new PortObject[] { new DatabasePortObject(outSpec) };
}
Also used : DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) 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) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) PortObject(org.knime.core.node.port.PortObject)

Example 9 with DatabaseConnectionSettings

use of org.knime.core.node.port.database.DatabaseConnectionSettings 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 10 with DatabaseConnectionSettings

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

the class DBDropTableNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    exec.setMessage("Droping table");
    final DatabaseConnectionPortObject incomingConnection = (DatabaseConnectionPortObject) inObjects[0];
    final CredentialsProvider cp = getCredentialsProvider();
    final DatabaseConnectionSettings connSettings = incomingConnection.getConnectionSettings(cp);
    final DatabaseUtility dbUtility = connSettings.getUtility();
    final StatementManipulator manipulator = dbUtility.getStatementManipulator();
    final String table2Drop = m_tableName.getStringValue();
    try {
        if (m_failIfNotExists.getBooleanValue() || connSettings.execute(cp, conn -> {
            return dbUtility.tableExists(conn, table2Drop);
        })) {
            connSettings.execute(manipulator.dropTable(table2Drop, m_cascade.getBooleanValue()), cp);
            exec.setMessage("Table " + table2Drop + " sucessful droped");
        } else {
            exec.setMessage("Table " + table2Drop + " does not exist in db");
        }
    } catch (SQLException ex) {
        Throwable cause = ExceptionUtils.getRootCause(ex);
        if (cause == null) {
            cause = ex;
        }
        throw new InvalidSettingsException("Error while validating drop statement: " + cause.getMessage(), ex);
    }
    return inObjects;
}
Also used : ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExceptionUtils(org.apache.commons.lang.exception.ExceptionUtils) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) IOException(java.io.IOException) NodeModel(org.knime.core.node.NodeModel) File(java.io.File) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) ExecutionContext(org.knime.core.node.ExecutionContext) SQLException(java.sql.SQLException) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) CredentialsProvider(org.knime.core.node.workflow.CredentialsProvider) StatementManipulator(org.knime.core.node.port.database.StatementManipulator) PortObject(org.knime.core.node.port.PortObject) PortType(org.knime.core.node.port.PortType) DatabaseUtility(org.knime.core.node.port.database.DatabaseUtility) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DatabaseUtility(org.knime.core.node.port.database.DatabaseUtility) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) SQLException(java.sql.SQLException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CredentialsProvider(org.knime.core.node.workflow.CredentialsProvider) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) StatementManipulator(org.knime.core.node.port.database.StatementManipulator)

Aggregations

DatabaseConnectionSettings (org.knime.core.node.port.database.DatabaseConnectionSettings)26 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)14 DataTableSpec (org.knime.core.data.DataTableSpec)12 DatabaseConnectionPortObject (org.knime.core.node.port.database.DatabaseConnectionPortObject)10 SQLException (java.sql.SQLException)7 DatabaseConnectionPortObjectSpec (org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)7 PortObject (org.knime.core.node.port.PortObject)6 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)4 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)4 DBWriter (org.knime.core.node.port.database.writer.DBWriter)4 DataColumnSpecFilterConfiguration (org.knime.core.node.util.filter.column.DataColumnSpecFilterConfiguration)4 PreparedStatement (java.sql.PreparedStatement)3 ArrayList (java.util.ArrayList)3 TimeZone (java.util.TimeZone)3 DataCell (org.knime.core.data.DataCell)3 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 DataRow (org.knime.core.data.DataRow)3 BufferedDataTable (org.knime.core.node.BufferedDataTable)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 DatabasePortObjectSpec (org.knime.core.node.port.database.DatabasePortObjectSpec)3