use of org.knime.core.node.port.database.tablecreator.DBColumn in project knime-core by knime.
the class KeysPanel method createSelectKeysContain.
/**
* Creates table pop up menu item to select keys that contain the specified column
*
* @param menu the parent pop up menu to attach the menu items
*/
private void createSelectKeysContain(final JPopupMenu menu) {
final JMenu subMenu = new JMenu("Select keys contain column");
menu.add(subMenu);
Map<String, List<Integer>> columnKeyIdxMapping = new TreeMap<>();
for (int i = 0; i < getTableModel().getRowCount(); i++) {
KeyElement key = (KeyElement) getTableModel().getElement(i);
for (DBColumn col : key.getColumns()) {
final String colName = col.getName();
List<Integer> indices = columnKeyIdxMapping.get(colName);
if (indices == null) {
indices = new ArrayList<>();
indices.add(i);
columnKeyIdxMapping.put(colName, indices);
} else {
indices.add(i);
}
}
}
for (Entry<String, List<Integer>> entry : columnKeyIdxMapping.entrySet()) {
final JMenuItem item = new JMenuItem(entry.getKey());
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
getTable().clearSelection();
for (int idx : entry.getValue()) {
getTable().addRowSelectionInterval(idx, idx);
}
}
});
subMenu.add(item);
}
}
use of org.knime.core.node.port.database.tablecreator.DBColumn in project knime-core by knime.
the class DBTableCreatorConfiguration method createRowElement.
/**
* A helper method to create a new instance of RowElement from NodeSettingsRO instance
*
* @param cfgKey key to determine which kind of RowElement to create
* @param settings NodeSettingsRO instance used to create a new RowElement
* @return a new instance of RowElement
*/
private RowElement createRowElement(final String cfgKey, final NodeSettingsRO settings) {
switch(cfgKey) {
case CFG_COLUMNS_SETTINGS:
return new ColumnElement(settings);
case CFG_KEYS_SETTINGS:
KeyElement elem = new KeyElement(settings);
Set<ColumnElement> columns = new HashSet<>();
for (DBColumn dbCol : elem.getColumns()) {
boolean isFound = false;
for (RowElement el : getRowElements(CFG_COLUMNS_SETTINGS)) {
ColumnElement colElem = (ColumnElement) el;
if (dbCol.getName().equalsIgnoreCase(colElem.getName())) {
columns.add(colElem);
isFound = true;
break;
}
}
if (!isFound) {
throw new IllegalArgumentException(String.format("Column '%s' is undefined", dbCol.getName()));
}
}
elem.setColumnElements(columns);
return elem;
case CFG_NAME_BASED_TYPE_MAPPING:
return new NameBasedMappingElement(settings);
case CFG_KNIME_BASED_TYPE_MAPPING:
return new KNIMEBasedMappingElement(settings);
case CFG_NAME_BASED_KEYS:
return new NameBasedKeysElement(settings);
default:
return null;
}
}
use of org.knime.core.node.port.database.tablecreator.DBColumn in project knime-core by knime.
the class KeyElement method loadSettingsFrom.
/**
* {@inheritDoc}
*/
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings) {
try {
final String name = settings.getString(CFG_KEY_NAME);
// Load all columns
final NodeSettingsRO cfg = settings.getNodeSettings(CFG_KEY_COLUMNS);
final Set<DBColumn> dbColumns = new LinkedHashSet<>();
for (String settingsKey : cfg.keySet()) {
final String colName = cfg.getString(settingsKey);
DBColumn col = new DBColumn(colName, "", false);
dbColumns.add(col);
}
final boolean primaryKey = settings.getBoolean(CFG_KEY_PRIMARY);
m_key = new DBKey(name, dbColumns, primaryKey);
} catch (InvalidSettingsException ex) {
// Do nothing if no settings are found
}
}
use of org.knime.core.node.port.database.tablecreator.DBColumn in project knime-core by knime.
the class KeyElement method getColumnsString.
/**
* Returns a string representation of the columns used to define this key
*
* @return a string representation of the columns used to define this key
*/
String getColumnsString() {
final StringBuilder builder = new StringBuilder();
List<DBColumn> columns = new ArrayList<>(getColumns());
Collections.sort(columns, new Comparator<DBColumn>() {
@Override
public int compare(final DBColumn o1, final DBColumn o2) {
return o1.getName().compareTo(o2.getName());
}
});
for (DBColumn column : columns) {
// remove ', ' at the end of the string
builder.append(column.getName() + ", ");
}
final String columnsString = builder.toString();
return columnsString.substring(0, columnsString.length() - 2);
}
use of org.knime.core.node.port.database.tablecreator.DBColumn in project knime-core by knime.
the class DBTableCreatorNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
if (inSpecs[0] == null && !(inSpecs[0] instanceof DatabaseConnectionPortObjectSpec)) {
throw new InvalidSettingsException("No valid database connection available.");
}
final DatabaseConnectionPortObjectSpec dbSpec = (DatabaseConnectionPortObjectSpec) inSpecs[0];
m_config.setTableSpec((DataTableSpec) inSpecs[1]);
final boolean isColumnsEmpty = m_config.getColumns().isEmpty();
if (m_config.getTableSpec() != null && (m_config.useDynamicSettings() || isColumnsEmpty)) {
m_config.loadColumnSettingsFromTableSpec(m_config.getTableSpec());
m_config.updateKeysWithDynamicSettings();
}
if (m_config.getTableSpec() == null && m_config.useDynamicSettings()) {
throw new InvalidSettingsException("Dynamic settings enabled but no input table available.");
}
if (isColumnsEmpty) {
throw new InvalidSettingsException("At least one column must be defined.");
}
final DatabaseConnectionSettings conn = dbSpec.getConnectionSettings(getCredentialsProvider());
final DBTableCreator tableCreator = conn.getUtility().getTableCreator(m_config.getSchema(), getTableName(), m_config.isTempTable());
final List<DBColumn> columns = m_config.getColumns();
final List<DBKey> keys = m_config.getKeys();
try {
tableCreator.validateSettings(m_config.ifNotExists(), columns.toArray(new DBColumn[columns.size()]), keys.toArray(new DBKey[keys.size()]), m_config.getAdditionalOptions());
} catch (Exception e) {
throw new InvalidSettingsException(e.getMessage());
}
pushFlowVariables(tableCreator.getSchema(), tableCreator.getTableName());
return new PortObjectSpec[] { dbSpec };
}
Aggregations