Search in sources :

Example 1 with StatementManipulator

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

the class DBSamplingNodeModel method createDbOutSpec.

/**
 * @param inSpec Spec of the input database object
 * @param exec The {@link ExecutionMonitor}
 * @param checkRetrieveMetadata true if the retrieveMetadataInConfigure settings should be respected,
 *            <code>false</code> if the metadata should be retrieved in any case (for execute)
 * @return Spec of the output database object
 * @throws InvalidSettingsException if the current settings are invalid
 * @throws CanceledExecutionException if execution is canceled
 */
private DatabasePortObjectSpec createDbOutSpec(final DatabasePortObjectSpec inSpec, final ExecutionMonitor exec, final boolean checkRetrieveMetadata) throws InvalidSettingsException, CanceledExecutionException {
    final DatabaseQueryConnectionSettings connectionSettings = inSpec.getConnectionSettings(getCredentialsProvider());
    final StatementManipulator statementManipulator = connectionSettings.getUtility().getStatementManipulator();
    try {
        // Connection connection = connectionSettings.createConnection(getCredentialsProvider());
        final String newQuery = connectionSettings.execute(getCredentialsProvider(), conn -> {
            return createQuery(conn, connectionSettings.getQuery(), statementManipulator, inSpec.getDataTableSpec(), exec);
        });
        DatabaseQueryConnectionSettings resultSettings = createDBQueryConnection(inSpec, newQuery);
        DatabaseQueryConnectionSettings querySettings = new DatabaseQueryConnectionSettings(resultSettings, newQuery);
        DatabaseReaderConnection conn = new DatabaseReaderConnection(querySettings);
        DataTableSpec tableSpec;
        exec.setMessage("Retrieving result specification.");
        tableSpec = conn.getDataTableSpec(getCredentialsProvider());
        return new DatabasePortObjectSpec(tableSpec, resultSettings.createConnectionModel());
    } catch (SQLException e1) {
        throw new InvalidSettingsException("Failure during query generation. Error: " + e1.getMessage(), e1);
    }
}
Also used : DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataTableSpec(org.knime.core.data.DataTableSpec) DatabaseReaderConnection(org.knime.core.node.port.database.DatabaseReaderConnection) SQLException(java.sql.SQLException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) StatementManipulator(org.knime.core.node.port.database.StatementManipulator)

Example 2 with StatementManipulator

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

the class DBPivotNodeModel method createDbOutSpec.

/**
 * @param inSpec Spec of the input database object
 * @param exec The {@link ExecutionMonitor}
 * @return Spec of the output database object
 * @throws InvalidSettingsException if the current settings are invalid
 * @throws CanceledExecutionException if execution is canceled
 */
private DatabasePortObjectSpec createDbOutSpec(final DatabasePortObjectSpec inSpec, final ExecutionMonitor exec) throws InvalidSettingsException, CanceledExecutionException {
    DatabaseQueryConnectionSettings connectionSettings = inSpec.getConnectionSettings(getCredentialsProvider());
    final StatementManipulator statementManipulator = connectionSettings.getUtility().getStatementManipulator();
    try {
        // Connection connection = connectionSettings.createConnection(getCredentialsProvider());
        String newQuery = createQuery(connectionSettings, inSpec.getDataTableSpec(), exec);
        connectionSettings = createDBQueryConnection(inSpec, newQuery);
        DatabaseQueryConnectionSettings querySettings = new DatabaseQueryConnectionSettings(connectionSettings, newQuery);
        DBReader reader = querySettings.getUtility().getReader(querySettings);
        DataTableSpec tableSpec;
        exec.setMessage("Retrieving result specification.");
        tableSpec = reader.getDataTableSpec(getCredentialsProvider());
        return new DatabasePortObjectSpec(tableSpec, connectionSettings.createConnectionModel());
    } catch (SQLException e1) {
        throw new InvalidSettingsException("Failure during query generation. Error: " + e1.getMessage(), e1);
    }
}
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) SQLException(java.sql.SQLException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) StatementManipulator(org.knime.core.node.port.database.StatementManipulator)

Example 3 with StatementManipulator

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

the class DBAutoBinnerNodeModel method createDatabasePortObject.

private DatabasePortObject createDatabasePortObject(final DatabasePortObjectSpec inSpec, DatabaseQueryConnectionSettings connectionSettings, final PMMLPortObject pmmlPortObject) throws InvalidSettingsException {
    final StatementManipulator statementManipulator = connectionSettings.getUtility().getStatementManipulator();
    String newQuery = createQuery(connectionSettings.getQuery(), statementManipulator, inSpec.getDataTableSpec(), pmmlPortObject);
    connectionSettings = createDBQueryConnection(inSpec, newQuery);
    DatabaseQueryConnectionSettings querySettings = new DatabaseQueryConnectionSettings(connectionSettings, newQuery);
    DatabaseReaderConnection conn = new DatabaseReaderConnection(querySettings);
    DatabasePortObjectSpec databasePortObjectSpec = null;
    try {
        databasePortObjectSpec = new DatabasePortObjectSpec(conn.getDataTableSpec(getCredentialsProvider()), connectionSettings.createConnectionModel());
    } catch (SQLException e) {
        throw new InvalidSettingsException("Failure during query generation. Error: " + e.getMessage());
    }
    DatabasePortObject databasePortObject = new DatabasePortObject(databasePortObjectSpec);
    return databasePortObject;
}
Also used : DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DatabaseReaderConnection(org.knime.core.node.port.database.DatabaseReaderConnection) SQLException(java.sql.SQLException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) StatementManipulator(org.knime.core.node.port.database.StatementManipulator)

Example 4 with StatementManipulator

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

the class DBNumericBinnerNodeModel method createDatabasePortObjectSpec.

private DatabasePortObjectSpec createDatabasePortObjectSpec(DatabaseQueryConnectionSettings connectionSettings, final DatabasePortObjectSpec inDatabasePortObjectSpec, final DBBinnerMaps binnerMaps) throws InvalidSettingsException, SQLException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, IOException {
    final StatementManipulator statementManipulator = connectionSettings.getUtility().getStatementManipulator();
    final String newQuery = createQuery(connectionSettings.getQuery(), statementManipulator, inDatabasePortObjectSpec.getDataTableSpec(), binnerMaps);
    connectionSettings = createDBQueryConnection(inDatabasePortObjectSpec, newQuery);
    final DatabaseQueryConnectionSettings querySettings = new DatabaseQueryConnectionSettings(connectionSettings, newQuery);
    DBReader reader = querySettings.getUtility().getReader(querySettings);
    DataTableSpec outDataTableSpec = reader.getDataTableSpec(getCredentialsProvider());
    DatabasePortObjectSpec outDatabasePortObjectSpec = new DatabasePortObjectSpec(outDataTableSpec, connectionSettings.createConnectionModel());
    return outDatabasePortObjectSpec;
}
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) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) StatementManipulator(org.knime.core.node.port.database.StatementManipulator)

Example 5 with StatementManipulator

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

StatementManipulator (org.knime.core.node.port.database.StatementManipulator)13 DatabaseQueryConnectionSettings (org.knime.core.node.port.database.DatabaseQueryConnectionSettings)9 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)8 DatabasePortObjectSpec (org.knime.core.node.port.database.DatabasePortObjectSpec)8 DataTableSpec (org.knime.core.data.DataTableSpec)6 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)6 SQLException (java.sql.SQLException)5 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)4 DatabasePortObject (org.knime.core.node.port.database.DatabasePortObject)4 LinkedHashMap (java.util.LinkedHashMap)3 DBReader (org.knime.core.node.port.database.reader.DBReader)3 IOException (java.io.IOException)2 ResultSet (java.sql.ResultSet)2 DataColumnSpec (org.knime.core.data.DataColumnSpec)2 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)2 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)2 PortObject (org.knime.core.node.port.PortObject)2 DatabaseReaderConnection (org.knime.core.node.port.database.DatabaseReaderConnection)2 CredentialsProvider (org.knime.core.node.workflow.CredentialsProvider)2 Pair (org.knime.core.util.Pair)2