Search in sources :

Example 41 with DatabaseQueryConnectionSettings

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

the class DBGroupByNodeDialog2 method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final PortObjectSpec[] specs) throws NotConfigurableException {
    if (specs == null || specs.length < 1 || specs[0] == null) {
        throw new NotConfigurableException("No input connection found.");
    }
    final DatabasePortObjectSpec dbspec = (DatabasePortObjectSpec) specs[0];
    final DataTableSpec spec = dbspec.getDataTableSpec();
    try {
        final DatabaseQueryConnectionSettings connectionSettings = dbspec.getConnectionSettings(null);
        m_columnNamePolicy.loadSettingsFrom(settings);
        final String dbIdentifier = connectionSettings.getDatabaseIdentifier();
        m_aggregationPanel.loadSettingsFrom(settings, dbIdentifier, spec);
        m_patternPanel.loadSettingsFrom(settings, dbIdentifier, spec);
        m_typePanel.loadSettingsFrom(settings, dbIdentifier, spec);
        m_addCountStar.loadSettingsFrom(settings);
        m_countStarColName.loadSettingsFrom(settings);
        final DBAggregationFunctionProvider functionProvider = new DBAggregationFunctionProvider(connectionSettings.getUtility());
        m_descriptionTab.removeAll();
        final GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.CENTER;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        m_descriptionTab.add(functionProvider.getDescriptionPane(), c);
    } catch (final InvalidSettingsException e) {
        throw new NotConfigurableException(e.getMessage());
    }
    m_groupCol.loadSettingsFrom(settings, new DataTableSpec[] { spec });
    columnsChanged();
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) DataTableSpec(org.knime.core.data.DataTableSpec) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) GridBagConstraints(java.awt.GridBagConstraints) DBAggregationFunctionProvider(org.knime.base.node.io.database.groupby.dialog.DBAggregationFunctionProvider) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) DialogComponentString(org.knime.core.node.defaultnodesettings.DialogComponentString) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 42 with DatabaseQueryConnectionSettings

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

the class DBGroupByNodeModel2 method createDbOutSpec.

/**
 * @param inSpec Spec of the input database object
 * @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
 */
private DatabasePortObjectSpec createDbOutSpec(final DatabasePortObjectSpec inSpec, final boolean checkRetrieveMetadata) throws InvalidSettingsException {
    DatabaseQueryConnectionSettings connection = inSpec.getConnectionSettings(getCredentialsProvider());
    final StatementManipulator statementManipulator = connection.getUtility().getStatementManipulator();
    final String newQuery = createQuery(connection, connection.getQuery(), statementManipulator);
    connection = createDBQueryConnection(inSpec, newQuery);
    if (checkRetrieveMetadata && !connection.getRetrieveMetadataInConfigure()) {
        return null;
    }
    final DataTableSpec tableSpec = createOutSpec(inSpec.getDataTableSpec(), connection, newQuery, statementManipulator, checkRetrieveMetadata);
    return new DatabasePortObjectSpec(tableSpec, connection.createConnectionModel());
}
Also used : DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataTableSpec(org.knime.core.data.DataTableSpec) 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 43 with DatabaseQueryConnectionSettings

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

the class DBGroupByNodeModel2 method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    final DatabasePortObjectSpec dbSpec = (DatabasePortObjectSpec) inSpecs[0];
    final DataTableSpec tableSpec = dbSpec.getDataTableSpec();
    final DatabaseQueryConnectionSettings connection = dbSpec.getConnectionSettings(null);
    final String dbIdentifier = connection.getDatabaseIdentifier();
    final List<DBColumnAggregationFunctionRow> columnFunctions = DBColumnAggregationFunctionRow.loadFunctions(m_settings, DBGroupByNodeModel2.CFG_AGGREGATION_FUNCTIONS, dbIdentifier, tableSpec);
    final ArrayList<DBColumnAggregationFunctionRow> invalidColAggrs = new ArrayList<>(1);
    final Set<String> usedColNames = new HashSet<>(tableSpec.getNumColumns());
    usedColNames.addAll(m_groupByCols.getIncludeList());
    m_aggregationFunction2Use.clear();
    for (DBColumnAggregationFunctionRow row : columnFunctions) {
        final DataColumnSpec columnSpec = row.getColumnSpec();
        final DataColumnSpec inputSpec = tableSpec.getColumnSpec(columnSpec.getName());
        final AggregationFunction function = row.getFunction();
        if (inputSpec == null || !inputSpec.getType().equals(columnSpec.getType())) {
            invalidColAggrs.add(row);
            continue;
        }
        if (function instanceof InvalidAggregationFunction) {
            throw new InvalidSettingsException(((InvalidAggregationFunction) function).getErrorMessage());
        }
        if (function.hasOptionalSettings()) {
            try {
                function.configure(tableSpec);
            } catch (InvalidSettingsException e) {
                throw new InvalidSettingsException("Exception in aggregation function " + function.getLabel() + " of column " + row.getColumnSpec().getName() + ": " + e.getMessage());
            }
        }
        usedColNames.add(row.getColumnSpec().getName());
        m_aggregationFunction2Use.add(row);
    }
    final List<DBPatternAggregationFunctionRow> patternFunctions = DBPatternAggregationFunctionRow.loadFunctions(m_settings, CFG_PATTERN_AGGREGATION_FUNCTIONS, dbIdentifier, tableSpec);
    if (tableSpec.getNumColumns() > usedColNames.size() && !patternFunctions.isEmpty()) {
        for (final DataColumnSpec spec : tableSpec) {
            if (!usedColNames.contains(spec.getName())) {
                for (final DBPatternAggregationFunctionRow patternFunction : patternFunctions) {
                    final Pattern pattern = patternFunction.getRegexPattern();
                    final DBAggregationFunction function = patternFunction.getFunction();
                    if (pattern != null && pattern.matcher(spec.getName()).matches() && function.isCompatible(spec.getType())) {
                        final DBColumnAggregationFunctionRow row = new DBColumnAggregationFunctionRow(spec, patternFunction.getFunction());
                        m_aggregationFunction2Use.add(row);
                        usedColNames.add(spec.getName());
                    }
                }
            }
        }
    }
    final List<DBDataTypeAggregationFunctionRow> typeFunctions = DBDataTypeAggregationFunctionRow.loadFunctions(m_settings, CFG_TYPE_AGGREGATION_FUNCTIONS, dbIdentifier, tableSpec);
    // check if some columns are left
    if (tableSpec.getNumColumns() > usedColNames.size() && !typeFunctions.isEmpty()) {
        for (final DataColumnSpec spec : tableSpec) {
            if (!usedColNames.contains(spec.getName())) {
                final DataType dataType = spec.getType();
                for (final DBDataTypeAggregationFunctionRow typeAggregator : typeFunctions) {
                    if (typeAggregator.isCompatibleType(dataType)) {
                        final DBColumnAggregationFunctionRow row = new DBColumnAggregationFunctionRow(spec, typeAggregator.getFunction());
                        m_aggregationFunction2Use.add(row);
                        usedColNames.add(spec.getName());
                    }
                }
            }
        }
    }
    if (m_groupByCols.getIncludeList().isEmpty() && m_aggregationFunction2Use.isEmpty() && !m_addCountStar.getBooleanValue()) {
        throw new InvalidSettingsException("Please select at least one group or aggregation function or the " + "COUNT(*) option.");
    }
    if (!invalidColAggrs.isEmpty()) {
        setWarningMessage(invalidColAggrs.size() + " aggregation functions ignored due to incompatible columns.");
    }
    return new PortObjectSpec[] { createDbOutSpec(dbSpec, true) };
}
Also used : Pattern(java.util.regex.Pattern) DataTableSpec(org.knime.core.data.DataTableSpec) InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) ArrayList(java.util.ArrayList) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DBPatternAggregationFunctionRow(org.knime.base.node.io.database.groupby.dialog.pattern.DBPatternAggregationFunctionRow) InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) AggregationFunction(org.knime.core.node.port.database.aggregation.AggregationFunction) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DBColumnAggregationFunctionRow(org.knime.base.node.io.database.groupby.dialog.column.DBColumnAggregationFunctionRow) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) DataType(org.knime.core.data.DataType) DBDataTypeAggregationFunctionRow(org.knime.base.node.io.database.groupby.dialog.type.DBDataTypeAggregationFunctionRow) HashSet(java.util.HashSet)

Example 44 with DatabaseQueryConnectionSettings

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

the class ParameterizedDBQueryNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    final BufferedDataTable inTable = (BufferedDataTable) inData[0];
    final DatabasePortObject dbObject = (DatabasePortObject) inData[1];
    DatabaseQueryConnectionSettings conn = dbObject.getConnectionSettings(getCredentialsProvider());
    final String newQuery = parseSQLStatement(inTable.getDataTableSpec(), conn.getQuery());
    conn = createDBQueryConnection(dbObject.getSpec(), newQuery);
    final DBReader reader = conn.getUtility().getReader(conn);
    final DataTableRowInput data = new DataTableRowInput(inTable);
    final BufferedDataTable outTable = reader.loopTable(exec, getCredentialsProvider(), data, inTable.size(), m_failIfExceptionModel.getBooleanValue(), m_appendInputColumnsModel.getBooleanValue(), m_includeEmptyResultsModel.getBooleanValue(), m_retainAllColumnsModel.getBooleanValue(), m_dataColumns.toArray(new String[m_dataColumns.size()])).getDataTable();
    final BufferedDataTable errorTable = reader.getErrorDataTable();
    return new BufferedDataTable[] { outTable, errorTable };
}
Also used : DatabasePortObject(org.knime.core.node.port.database.DatabasePortObject) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DBReader(org.knime.core.node.port.database.reader.DBReader) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput)

Example 45 with DatabaseQueryConnectionSettings

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

the class DBPivotNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    final DatabasePortObjectSpec dbSpec = (DatabasePortObjectSpec) inSpecs[0];
    final DataTableSpec tableSpec = dbSpec.getDataTableSpec();
    final DatabaseQueryConnectionSettings connection = dbSpec.getConnectionSettings(getCredentialsProvider());
    final String dbIdentifier = connection.getDatabaseIdentifier();
    final List<DBColumnAggregationFunctionRow> columnFunctions = DBColumnAggregationFunctionRow.loadFunctions(m_settings, DBPivotNodeModel.CFG_AGGREGATION_FUNCTIONS, dbIdentifier, tableSpec);
    final ArrayList<DBColumnAggregationFunctionRow> invalidColAggrs = new ArrayList<>(1);
    final Set<String> usedColNames = new HashSet<>(tableSpec.getNumColumns());
    usedColNames.addAll(m_groupByCols.getIncludeList());
    usedColNames.addAll(m_pivotCols.getIncludeList());
    m_aggregationFunction2Use.clear();
    for (DBColumnAggregationFunctionRow row : columnFunctions) {
        final DataColumnSpec columnSpec = row.getColumnSpec();
        final DataColumnSpec inputSpec = tableSpec.getColumnSpec(columnSpec.getName());
        final AggregationFunction function = row.getFunction();
        if (inputSpec == null || !inputSpec.getType().equals(columnSpec.getType())) {
            invalidColAggrs.add(row);
            continue;
        }
        if (function instanceof InvalidAggregationFunction) {
            throw new InvalidSettingsException(((InvalidAggregationFunction) function).getErrorMessage());
        }
        if (function.hasOptionalSettings()) {
            try {
                function.configure(tableSpec);
            } catch (InvalidSettingsException e) {
                throw new InvalidSettingsException("Wrong aggregation function configuration '" + function.getLabel() + "' of column '" + row.getColumnSpec().getName() + "': " + e.getMessage(), e);
            }
        }
        usedColNames.add(row.getColumnSpec().getName());
        m_aggregationFunction2Use.add(row);
    }
    if (m_aggregationFunction2Use.isEmpty()) {
        throw new InvalidSettingsException("No aggregation columns selected.");
    }
    if (m_groupByCols.getIncludeList().isEmpty()) {
        setWarningMessage("No grouping column included. Aggregate complete table");
    }
    if (m_pivotCols.getIncludeList().isEmpty()) {
        throw new InvalidSettingsException("No pivot columns selected.");
    }
    if (!invalidColAggrs.isEmpty()) {
        setWarningMessage(invalidColAggrs.size() + " aggregation functions ignored due to incompatible columns.");
    }
    final DatabasePortObjectSpec resultSpec;
    if (connection.getRetrieveMetadataInConfigure()) {
        try {
            resultSpec = createDbOutSpec(dbSpec, new ExecutionMonitor());
        } catch (CanceledExecutionException e) {
            throw new InvalidSettingsException(e.getMessage());
        }
    } else {
        resultSpec = null;
    }
    return new PortObjectSpec[] { resultSpec };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) ArrayList(java.util.ArrayList) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) AggregationFunction(org.knime.core.node.port.database.aggregation.AggregationFunction) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) DBColumnAggregationFunctionRow(org.knime.base.node.io.database.groupby.dialog.column.DBColumnAggregationFunctionRow) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) HashSet(java.util.HashSet)

Aggregations

DatabaseQueryConnectionSettings (org.knime.core.node.port.database.DatabaseQueryConnectionSettings)49 DatabasePortObjectSpec (org.knime.core.node.port.database.DatabasePortObjectSpec)33 DataTableSpec (org.knime.core.data.DataTableSpec)29 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)28 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)17 DatabasePortObject (org.knime.core.node.port.database.DatabasePortObject)17 DBReader (org.knime.core.node.port.database.reader.DBReader)17 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)12 SQLException (java.sql.SQLException)11 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)11 PortObject (org.knime.core.node.port.PortObject)11 StatementManipulator (org.knime.core.node.port.database.StatementManipulator)9 DataColumnSpec (org.knime.core.data.DataColumnSpec)5 ArrayList (java.util.ArrayList)4 DBAggregationFunction (org.knime.core.node.port.database.aggregation.DBAggregationFunction)4 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 DBColumnAggregationFunctionRow (org.knime.base.node.io.database.groupby.dialog.column.DBColumnAggregationFunctionRow)3 RowIterator (org.knime.core.data.RowIterator)3