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()]));
}
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();
}
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;
}
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));
}
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));
}
Aggregations