Search in sources :

Example 1 with DatabaseUtility

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

the class DBGroupByNodeModel method createOutSpec.

/**
 * @param inSpec Spec of the input table
 * @param checkRetrieveMetadata
 * @return Spec of the output table
 * @throws InvalidSettingsException if settings do not match the input specification
 */
private DataTableSpec createOutSpec(final DataTableSpec inSpec, final DatabaseConnectionSettings settings, final String query, final boolean ignoreExceptions) throws InvalidSettingsException {
    // Try get spec from database
    try {
        DatabaseQueryConnectionSettings querySettings = new DatabaseQueryConnectionSettings(settings, query);
        DatabaseReaderConnection conn = new DatabaseReaderConnection(querySettings);
        return conn.getDataTableSpec(getCredentialsProvider());
    } catch (SQLException e) {
        NodeLogger.getLogger(getClass()).info("Could not determine table spec from database, trying to guess now", e);
        if (!ignoreExceptions) {
            throw new InvalidSettingsException("Error in automatically build sql statement: " + e.getMessage());
        }
    // Otherwise guess spec
    }
    List<DataColumnSpec> colSpecs = new ArrayList<>();
    // Add all group by columns
    for (String col : m_groupByCols.getIncludeList()) {
        colSpecs.add(inSpec.getColumnSpec(col));
    }
    // Add aggregated columns
    for (int i = 0; i < m_aggregatedColumns.length; i++) {
        String col = m_aggregatedColumns[i];
        String method = m_aggregationMethods[i];
        if (inSpec.getColumnSpec(col) == null) {
            throw new InvalidSettingsException("Column '" + col + "' in aggregation " + method + " does not exist");
        }
        final DatabaseUtility databaseUtility = settings.getUtility();
        final DBAggregationFunction function = databaseUtility.getAggregationFunction(method);
        // Get type of column after aggregation
        DataType type = function.getType(inSpec.getColumnSpec(col).getType());
        colSpecs.add(new DataColumnSpecCreator(generateColumnName(col, method), type).createSpec());
    }
    return new DataTableSpec(colSpecs.toArray(new DataColumnSpec[colSpecs.size()]));
}
Also used : DatabaseUtility(org.knime.core.node.port.database.DatabaseUtility) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DatabaseReaderConnection(org.knime.core.node.port.database.DatabaseReaderConnection) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) DataType(org.knime.core.data.DataType)

Example 2 with DatabaseUtility

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

the class DBGroupByAggregationPanel method loadSettingsFrom.

/**
 * @param settings The settings to load from
 * @param dbspec {@link DatabasePortObjectSpec} with the database information e.g. the supported
 * {@link DBAggregationFunction}s
 * @param spec The spec containing the available columns
 * @throws NotConfigurableException if the connection settings cannot retrieved from the
 * {@link DatabasePortObjectSpec}.
 */
void loadSettingsFrom(final NodeSettingsRO settings, final DatabasePortObjectSpec dbspec, final DataTableSpec spec) throws NotConfigurableException {
    m_dbspec = dbspec;
    try {
        final DatabaseUtility utility = m_dbspec.getConnectionSettings(null).getUtility();
        final Collection<DBAggregationFunction> aggregationFunctions = utility.getAggregationFunctions();
        setSupportedAggregationFunctions(aggregationFunctions);
    } catch (final InvalidSettingsException e) {
        throw new NotConfigurableException(e.getMessage());
    }
    m_spec = spec;
    m_aggregatedColumnsModel.setRowCount(0);
    String[] columns = settings.getStringArray(DBGroupByNodeModel.CFG_AGGREGATED_COLUMNS, new String[0]);
    String[] methods = settings.getStringArray(DBGroupByNodeModel.CFG_AGGREGATION_METHODS, new String[0]);
    for (int i = 0; i < columns.length; i++) {
        m_aggregatedColumnsModel.addRow(new Object[] { columns[i], methods[i] });
    }
    refreshAvailableColumns();
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) DatabaseUtility(org.knime.core.node.port.database.DatabaseUtility) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction)

Example 3 with DatabaseUtility

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

the class DBPatternAggregationFunctionRow method loadFunctions.

/**
 * Loads the functions and handles invalid aggregation functions graceful.
 * @param settings {@link NodeSettingsRO}
 * @param key the config key
 * @param dbIdentifier the {@link AggregationFunctionProvider}
 * @param tableSpec the input {@link DataTableSpec}
 * @return {@link List} of {@link DBPatternAggregationFunctionRow}s
 * @throws InvalidSettingsException if the settings are invalid
 */
public static List<DBPatternAggregationFunctionRow> loadFunctions(final NodeSettingsRO settings, final String key, final String dbIdentifier, final DataTableSpec tableSpec) throws InvalidSettingsException {
    if (key == null || key.isEmpty()) {
        throw new IllegalArgumentException("key must not be null");
    }
    if (dbIdentifier == null || dbIdentifier.isEmpty()) {
        throw new IllegalArgumentException("dbIdentifier must not be empty");
    }
    if (settings == null || !settings.containsKey(key)) {
        return Collections.EMPTY_LIST;
    }
    final DatabaseUtility utility = DatabaseUtility.getUtility(dbIdentifier);
    DBAggregationFunctionProvider functionProvider = new DBAggregationFunctionProvider(utility);
    final NodeSettingsRO root = settings.getNodeSettings(key);
    final Set<String> settingsKeys = root.keySet();
    final List<DBPatternAggregationFunctionRow> colAggrList = new ArrayList<>(settingsKeys.size());
    for (String settingsKey : settingsKeys) {
        final NodeSettingsRO cfg = root.getNodeSettings(settingsKey);
        final String inputPattern = cfg.getString(CNFG_INPUT_PATTERN);
        final boolean isRegex = cfg.getBoolean(CNFG_IS_REGEX);
        final DBAggregationFunction function = AbstractDBAggregationFunctionRow.loadFunction(tableSpec, functionProvider, cfg);
        final DBPatternAggregationFunctionRow aggrFunctionRow = new DBPatternAggregationFunctionRow(inputPattern, isRegex, function);
        colAggrList.add(aggrFunctionRow);
    }
    return colAggrList;
}
Also used : DatabaseUtility(org.knime.core.node.port.database.DatabaseUtility) DBAggregationFunctionProvider(org.knime.base.node.io.database.groupby.dialog.DBAggregationFunctionProvider) ArrayList(java.util.ArrayList) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) NodeSettingsRO(org.knime.core.node.NodeSettingsRO)

Example 4 with DatabaseUtility

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

the class DBColumnAggregationFunctionPanel method loadSettingsFrom.

/**
 * @param settings the settings to read from
 * @param dbIdentifier the database identifier
 * @param spec initializes the component
 * @throws InvalidSettingsException if the settings are invalid
 */
public void loadSettingsFrom(final NodeSettingsRO settings, final String dbIdentifier, final DataTableSpec spec) throws InvalidSettingsException {
    final DatabaseUtility utility = DatabaseUtility.getUtility(dbIdentifier);
    setAggregationFunctionProvider(new DBAggregationFunctionProvider(utility));
    initialize(spec, DBColumnAggregationFunctionRow.loadFunctions(settings, m_key, dbIdentifier, spec));
}
Also used : DatabaseUtility(org.knime.core.node.port.database.DatabaseUtility) DBAggregationFunctionProvider(org.knime.base.node.io.database.groupby.dialog.DBAggregationFunctionProvider)

Example 5 with DatabaseUtility

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

the class DBPatternAggregationFunctionPanel method loadSettingsFrom.

/**
 * @param settings the settings to read from
 * @param dbIdentifier the database identifier
 * @param spec initializes the component
 * @throws InvalidSettingsException if the settings are invalid
 */
public void loadSettingsFrom(final NodeSettingsRO settings, final String dbIdentifier, final DataTableSpec spec) throws InvalidSettingsException {
    final DatabaseUtility utility = DatabaseUtility.getUtility(dbIdentifier);
    setAggregationFunctionProvider(new DBAggregationFunctionProvider(utility));
    initialize(spec, DBPatternAggregationFunctionRow.loadFunctions(settings, m_key, dbIdentifier, spec));
}
Also used : DatabaseUtility(org.knime.core.node.port.database.DatabaseUtility) DBAggregationFunctionProvider(org.knime.base.node.io.database.groupby.dialog.DBAggregationFunctionProvider)

Aggregations

DatabaseUtility (org.knime.core.node.port.database.DatabaseUtility)11 DBAggregationFunctionProvider (org.knime.base.node.io.database.groupby.dialog.DBAggregationFunctionProvider)6 DBAggregationFunction (org.knime.core.node.port.database.aggregation.DBAggregationFunction)6 ArrayList (java.util.ArrayList)5 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)5 DataType (org.knime.core.data.DataType)4 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)4 SQLException (java.sql.SQLException)3 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 DataTableSpec (org.knime.core.data.DataTableSpec)2 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)2 DatabaseQueryConnectionSettings (org.knime.core.node.port.database.DatabaseQueryConnectionSettings)2 File (java.io.File)1 IOException (java.io.IOException)1 ExceptionUtils (org.apache.commons.lang.exception.ExceptionUtils)1 DBColumnAggregationFunctionRow (org.knime.base.node.io.database.groupby.dialog.column.DBColumnAggregationFunctionRow)1 ColumnNamePolicy (org.knime.base.node.preproc.groupby.ColumnNamePolicy)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1