Search in sources :

Example 6 with DatabaseConnectionPortObject

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

the class DBReaderNodeModel method loadConnectionSettings.

/**
 * @param dbPortObject
 * @return
 * @throws InvalidSettingsException
 */
protected DBReader loadConnectionSettings(final PortObject dbPortObject) throws InvalidSettingsException {
    String query = parseQuery(m_settings.getQuery());
    DatabaseQueryConnectionSettings connSettings;
    if ((dbPortObject instanceof DatabaseConnectionPortObject)) {
        DatabaseConnectionPortObject dbObj = (DatabaseConnectionPortObject) dbPortObject;
        connSettings = new DatabaseQueryConnectionSettings(dbObj.getConnectionSettings(getCredentialsProvider()), query);
    } else {
        connSettings = new DatabaseQueryConnectionSettings(m_settings, query);
    }
    final DBReader load = connSettings.getUtility().getReader(new DatabaseQueryConnectionSettings(connSettings, query));
    return load;
}
Also used : DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DBReader(org.knime.core.node.port.database.reader.DBReader)

Example 7 with DatabaseConnectionPortObject

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

the class DBSQLExecutorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected final PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    DatabaseConnectionPortObject dbObj = (DatabaseConnectionPortObject) inData[0];
    DatabaseConnectionSettings conn = dbObj.getConnectionSettings(getCredentialsProvider());
    String[] statements = parseStatementAndReplaceVariables().split(DBReader.SQL_QUERY_SEPARATOR);
    final double max = statements.length;
    int i = 0;
    for (String statement : statements) {
        exec.checkCanceled();
        statement = statement.trim();
        exec.setProgress(i++ / max, "Executing '" + statement + "'");
        if (!statement.isEmpty()) {
            conn.execute(statement, getCredentialsProvider());
        }
    }
    return inData;
}
Also used : DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings)

Example 8 with DatabaseConnectionPortObject

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

the class DBUpdateNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    final BufferedDataTable inTable = (BufferedDataTable) inData[0];
    final DataTableSpec inSpec = inTable.getSpec();
    final String[] setIncludes = m_configSET.applyTo(inSpec).getIncludes();
    final String[] whereIncludes = m_configWHERE.applyTo(inSpec).getIncludes();
    DatabaseConnectionSettings connSettings;
    if ((inData.length > 1) && (inData[1] instanceof DatabaseConnectionPortObject)) {
        connSettings = ((DatabaseConnectionPortObject) inData[1]).getConnectionSettings(getCredentialsProvider());
    } else {
        connSettings = m_loginConfig;
    }
    // UPDATE table
    final int[] updateStatus = new int[inTable.getRowCount()];
    final DBWriter dbWriter = connSettings.getUtility().getWriter(connSettings);
    final String errMsg = dbWriter.updateTable(null, m_tableName, inTable, setIncludes, whereIncludes, updateStatus, exec, getCredentialsProvider(), m_batchSize);
    // set warning message generated during updating rows
    if (errMsg != null) {
        setWarningMessage(errMsg);
    }
    // create out table with update column
    final ColumnRearranger colre = createColumnRearranger(inTable.getSpec(), updateStatus);
    final BufferedDataTable outTable = exec.createColumnRearrangeTable(inTable, colre, exec.createSubProgress(1.0));
    return new BufferedDataTable[] { outTable };
}
Also used : DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) BufferedDataTable(org.knime.core.node.BufferedDataTable) DBWriter(org.knime.core.node.port.database.writer.DBWriter)

Example 9 with DatabaseConnectionPortObject

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

the class DatabaseLoopingNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    final BufferedDataTable inputTable = (BufferedDataTable) inData[0];
    final long rowCount = inputTable.size();
    final String column = m_columnModel.getStringValue();
    final DataTableSpec spec = inputTable.getDataTableSpec();
    final int colIdx = spec.findColumnIndex(column);
    if (colIdx < 0) {
        throw new InvalidSettingsException("Column " + column + " not found in input table.");
    }
    final Set<DataCell> values = new HashSet<>();
    BufferedDataContainer buf = null;
    final String oQuery = getQuery();
    final Collection<DataCell> curSet = new LinkedHashSet<>();
    final DBReader load = loadConnectionSettings(inData[getNrInPorts() - 1]);
    try {
        final int noValues = m_noValues.getIntValue();
        MutableInteger rowCnt = new MutableInteger(0);
        for (Iterator<DataRow> it = inputTable.iterator(); it.hasNext(); ) {
            exec.checkCanceled();
            DataCell cell = it.next().getCell(colIdx);
            if (values.contains(cell) && !it.hasNext() && curSet.isEmpty()) {
                continue;
            }
            values.add(cell);
            curSet.add(cell);
            if (curSet.size() == noValues || !it.hasNext()) {
                StringBuilder queryValues = new StringBuilder();
                for (DataCell v : curSet) {
                    if (queryValues.length() > 0) {
                        queryValues.append("','");
                    }
                    queryValues.append(v.toString());
                }
                String newQuery = parseQuery(oQuery.replaceAll(IN_PLACE_HOLDER, queryValues.toString()));
                load.updateQuery(newQuery);
                exec.setProgress(values.size() * (double) noValues / rowCount, "Selecting all values \"" + queryValues + "\"...");
                final BufferedDataTable table = getResultTable(exec, inData, load);
                if (buf == null) {
                    DataTableSpec resSpec = table.getDataTableSpec();
                    buf = exec.createDataContainer(createSpec(resSpec, spec.getColumnSpec(column)));
                }
                if (m_aggByRow.getBooleanValue()) {
                    aggregate(table, rowCnt, buf, CollectionCellFactory.createListCell(curSet));
                } else {
                    notAggregate(table, rowCnt, buf, CollectionCellFactory.createListCell(curSet));
                }
                curSet.clear();
            }
        }
        if (buf == null) {
            // create empty dummy container with spec generated during #configure
            final PortObjectSpec[] inSpec;
            if ((inData.length > 1) && (inData[1] instanceof DatabaseConnectionPortObject)) {
                DatabaseConnectionPortObject dbPort = (DatabaseConnectionPortObject) inData[1];
                inSpec = new PortObjectSpec[] { inputTable.getSpec(), dbPort.getSpec() };
            } else {
                inSpec = new PortObjectSpec[] { inputTable.getSpec() };
            }
            final String newQuery = createDummyValueQuery(spec, colIdx, oQuery);
            setQuery(newQuery);
            final DataTableSpec resultSpec = getResultSpec(inSpec);
            final DataTableSpec outSpec = createSpec(resultSpec, spec.getColumnSpec(column));
            buf = exec.createDataContainer(outSpec);
        }
        buf.close();
    } catch (CanceledExecutionException cee) {
        throw cee;
    } catch (Exception e) {
        setLastSpec(null);
        throw e;
    } finally {
        // reset query to original
        setQuery(oQuery);
    }
    final BufferedDataTable resultTable = buf.getTable();
    setLastSpec(resultTable.getDataTableSpec());
    return new BufferedDataTable[] { resultTable };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) MutableInteger(org.knime.core.util.MutableInteger) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) SQLException(java.sql.SQLException) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DBReader(org.knime.core.node.port.database.reader.DBReader) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec) DataCell(org.knime.core.data.DataCell) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 10 with DatabaseConnectionPortObject

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

the class DBTableSelectorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    exec.setMessage("Retrieving metadata from database");
    DatabaseConnectionPortObject incomingConnection = (DatabaseConnectionPortObject) inObjects[0];
    DatabaseConnectionSettings connSettings = incomingConnection.getConnectionSettings(getCredentialsProvider());
    String sql = FlowVariableResolver.parse(m_settings.getQuery(), this);
    DatabaseQueryConnectionSettings querySettings = new DatabaseQueryConnectionSettings(connSettings, sql);
    DBReader conn = querySettings.getUtility().getReader(querySettings);
    try {
        DataTableSpec tableSpec = conn.getDataTableSpec(getCredentialsProvider());
        return new PortObject[] { new DatabasePortObject(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: " + cause.getMessage(), ex);
    }
}
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) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) SQLException(java.sql.SQLException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) DatabaseConnectionPortObject(org.knime.core.node.port.database.DatabaseConnectionPortObject) DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) PortObject(org.knime.core.node.port.PortObject)

Aggregations

DatabaseConnectionPortObject (org.knime.core.node.port.database.DatabaseConnectionPortObject)12 DatabaseConnectionSettings (org.knime.core.node.port.database.DatabaseConnectionSettings)10 PortObject (org.knime.core.node.port.PortObject)6 DataTableSpec (org.knime.core.data.DataTableSpec)5 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)5 SQLException (java.sql.SQLException)4 BufferedDataTable (org.knime.core.node.BufferedDataTable)4 DBReader (org.knime.core.node.port.database.reader.DBReader)4 DBWriter (org.knime.core.node.port.database.writer.DBWriter)4 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 DatabaseQueryConnectionSettings (org.knime.core.node.port.database.DatabaseQueryConnectionSettings)3 IOException (java.io.IOException)2 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2 ExecutionContext (org.knime.core.node.ExecutionContext)2 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)2 DatabaseConnectionPortObjectSpec (org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)2 DatabasePortObject (org.knime.core.node.port.database.DatabasePortObject)2 DatabasePortObjectSpec (org.knime.core.node.port.database.DatabasePortObjectSpec)2 DataTableRowInput (org.knime.core.node.streamable.DataTableRowInput)2