Search in sources :

Example 6 with StatementManipulator

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

the class DBColumnRenameNodeModel method createDBOutSpec.

private DatabasePortObjectSpec createDBOutSpec(final DatabasePortObjectSpec inSpec) throws InvalidSettingsException {
    if (m_config == null) {
        throw new InvalidSettingsException("No configuration available");
    }
    final DataTableSpec tableSpec = inSpec.getDataTableSpec();
    final DataTableSpec outSpec = m_config.getNewSpec(tableSpec);
    final List<String> missingColumnNames = m_config.getMissingColumnNames();
    if (missingColumnNames != null && !missingColumnNames.isEmpty()) {
        setWarningMessage("The following columns are configured but no longer exist: " + ConvenienceMethods.getShortStringFrom(missingColumnNames, 5));
    }
    DatabaseQueryConnectionSettings conn = inSpec.getConnectionSettings(getCredentialsProvider());
    final StatementManipulator statementManipulator = conn.getUtility().getStatementManipulator();
    String newQuery = createQuery(conn.getQuery(), tableSpec, statementManipulator);
    conn = createDBQueryConnection(inSpec, newQuery);
    return new DatabasePortObjectSpec(outSpec, conn.createConnectionModel());
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) StatementManipulator(org.knime.core.node.port.database.StatementManipulator)

Example 7 with StatementManipulator

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

the class DBColumnFilterNodeModel method createQuery.

private String createQuery(final DatabaseQueryConnectionSettings conn, final DataTableSpec resultSpec) {
    final String query = conn.getQuery();
    final StatementManipulator manipulator = conn.getUtility().getStatementManipulator();
    final StringBuilder buf = new StringBuilder();
    final String[] queries = query.split(DBReader.SQL_QUERY_SEPARATOR);
    for (int i = 0; i < queries.length - 1; i++) {
        buf.append(queries[i]);
        buf.append(DBReader.SQL_QUERY_SEPARATOR);
    }
    // build SELECT statement
    buf.append("SELECT ");
    if (m_filter.getExcludeList().isEmpty()) {
        super.setWarningMessage("All columns retained.");
        // selects all columns
        buf.append("*");
    } else {
        int i = 0;
        for (final DataColumnSpec colSpec : resultSpec) {
            final String colName = colSpec.getName();
            if (i++ > 0) {
                buf.append(",");
            }
            buf.append(manipulator.quoteColumn(colName));
        }
    }
    final String selectQuery = queries[queries.length - 1];
    buf.append(" FROM (" + selectQuery + ") table_" + System.identityHashCode(this));
    return buf.toString();
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) StatementManipulator(org.knime.core.node.port.database.StatementManipulator)

Example 8 with StatementManipulator

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

the class DBConnectionWriterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    DatabasePortObject dbObj = (DatabasePortObject) inData[0];
    exec.setProgress("Opening database connection...");
    String tableName = m_tableName.getStringValue();
    DatabaseQueryConnectionSettings conn = dbObj.getConnectionSettings(getCredentialsProvider());
    CredentialsProvider cp = getCredentialsProvider();
    final StatementManipulator statementManipulator = conn.getUtility().getStatementManipulator();
    try {
        // use the statement manipulator to create the drop table statement
        conn.execute(statementManipulator.dropTable(tableName, false), cp);
    } catch (Exception e) {
    // suppress exception thrown when table does not exist in database
    }
    String[] stmts = statementManipulator.createTableAsSelect(tableName, conn.getQuery());
    for (final String stmt : stmts) {
        conn.execute(stmt, cp);
    }
    return new BufferedDataTable[0];
}
Also used : DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) BufferedDataTable(org.knime.core.node.BufferedDataTable) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) CredentialsProvider(org.knime.core.node.workflow.CredentialsProvider) StatementManipulator(org.knime.core.node.port.database.StatementManipulator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 9 with StatementManipulator

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

the class DBJoinerNodeModel method createDbOutSpec.

/**
 * @param inSpecLeft Spec of the left database object
 * @param inSpecRight Spec of the right database object
 * @return Spec of the output database object
 * @throws InvalidSettingsException If the current settings are invalid
 */
private DatabasePortObjectSpec createDbOutSpec(final DatabasePortObjectSpec inSpecLeft, final DatabasePortObjectSpec inSpecRight) throws InvalidSettingsException {
    DatabaseQueryConnectionSettings connectionLeft = inSpecLeft.getConnectionSettings(getCredentialsProvider());
    DatabaseQueryConnectionSettings connectionRight = inSpecRight.getConnectionSettings(getCredentialsProvider());
    StatementManipulator statementManipulator = connectionLeft.getUtility().getStatementManipulator();
    // Check first if connections are equal, otherwise abort
    checkIfDBConnectionsAreEqual(connectionLeft, connectionRight);
    // Calculate output columns
    Pair<Map<String, String>, Map<String, String>> columnMaps = createOutputColumnMaps(inSpecLeft.getDataTableSpec(), inSpecRight.getDataTableSpec(), statementManipulator);
    if (columnMaps.getFirst().size() + columnMaps.getSecond().size() < 1) {
        throw new InvalidSettingsException("No column selected");
    }
    // Create output query
    String newQuery = createQuery(connectionLeft.getQuery(), connectionRight.getQuery(), statementManipulator, columnMaps);
    // Create output spec
    DataTableSpec tableSpec = createOutSpec(inSpecLeft.getDataTableSpec(), inSpecRight.getDataTableSpec(), columnMaps, statementManipulator);
    DatabaseQueryConnectionSettings connection = createDBQueryConnection(inSpecLeft, newQuery);
    return new DatabasePortObjectSpec(tableSpec, connection.createConnectionModel());
}
Also used : DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) StatementManipulator(org.knime.core.node.port.database.StatementManipulator) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with StatementManipulator

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

the class DBAutoBinner method createPMMLPrepocDiscretize.

/**
 * This method creates a {@link PMMLPreprocDiscretize} object and is used in {@link DBAutoBinnerNodeModel}
 *
 * @param cp {@link CredentialsProvider}
 * @param connectionSettings {@link DatabaseQueryConnectionSettings}
 * @param dataTableSpec DataTableSpec of incoming {@link BufferedDataTable}
 * @return a {@link PMMLPreprocDiscretize} object containing required parameters for binning operation
 * @throws SQLException
 */
public PMMLPreprocDiscretize createPMMLPrepocDiscretize(final CredentialsProvider cp, final DatabaseQueryConnectionSettings connectionSettings, final DataTableSpec dataTableSpec) throws SQLException {
    final String query = connectionSettings.getQuery();
    final StatementManipulator statementManipulator = connectionSettings.getUtility().getStatementManipulator();
    AutoBinnerLearnSettings settings = getSettings();
    String[] includeCols = settings.getFilterConfiguration().applyTo(dataTableSpec).getIncludes();
    if (includeCols.length == 0) {
        return createDisretizeOp(new LinkedHashMap<>());
    }
    StringBuilder minMaxQuery = new StringBuilder();
    minMaxQuery.append("SELECT");
    for (int i = 0; i < includeCols.length; i++) {
        minMaxQuery.append(" MAX(" + statementManipulator.quoteIdentifier(includeCols[i]) + ") " + statementManipulator.quoteIdentifier("max_" + includeCols[i]) + ",");
        minMaxQuery.append(" MIN(" + statementManipulator.quoteIdentifier(includeCols[i]) + ") " + statementManipulator.quoteIdentifier("min_" + includeCols[i]));
        if (i < includeCols.length - 1) {
            minMaxQuery.append(",");
        }
    }
    minMaxQuery.append(" FROM (" + query + ") T");
    HashMap<String, Pair<Double, Double>> maxAndMin = connectionSettings.execute(cp, conn -> {
        double max = 0;
        double min = 0;
        HashMap<String, Pair<Double, Double>> maxMinMap = new LinkedHashMap<>();
        try (ResultSet valueSet = conn.createStatement().executeQuery(minMaxQuery.toString())) {
            while (valueSet.next()) {
                for (int i = 0; i < includeCols.length; i++) {
                    max = valueSet.getDouble("max_" + includeCols[i]);
                    min = valueSet.getDouble("min_" + includeCols[i]);
                    maxMinMap.put(includeCols[i], new Pair<Double, Double>(min, max));
                }
            }
        }
        return maxMinMap;
    });
    int number = settings.getBinCount();
    Map<String, double[]> edgesMap = new LinkedHashMap<>();
    for (Entry<String, Pair<Double, Double>> entry : maxAndMin.entrySet()) {
        double[] edges = AutoBinner.calculateBounds(number, entry.getValue().getFirst(), entry.getValue().getSecond());
        if (settings.getIntegerBounds()) {
            edges = AutoBinner.toIntegerBoundaries(edges);
        }
        edgesMap.put(entry.getKey(), edges);
    }
    return createDisretizeOp(edgesMap);
}
Also used : StatementManipulator(org.knime.core.node.port.database.StatementManipulator) LinkedHashMap(java.util.LinkedHashMap) AutoBinnerLearnSettings(org.knime.base.node.preproc.autobinner3.AutoBinnerLearnSettings) ResultSet(java.sql.ResultSet) Pair(org.knime.core.util.Pair)

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