Search in sources :

Example 46 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class DatabaseHelper method createTableSpec.

/**
 * Creates <code>DataTableSpec</code> from the given <code>ResultSetMetaData</code>
 * @param meta the <code>ResultSetMetaData</code> used to create <code>DataTableSpec</code>
 * @return a {@link DataTableSpec} created from the given {@link ResultSetMetaData}
 * @throws SQLException
 */
protected DataTableSpec createTableSpec(final ResultSetMetaData meta) throws SQLException {
    int cols = meta.getColumnCount();
    if (cols == 0) {
        return new DataTableSpec("database");
    }
    StatementManipulator manipulator = m_conn.getUtility().getStatementManipulator();
    DataTableSpec spec = null;
    for (int i = 0; i < cols; i++) {
        int dbIdx = i + 1;
        String name = manipulator.unquoteColumn(meta.getColumnLabel(dbIdx));
        int type = meta.getColumnType(dbIdx);
        DataType newType = getKNIMEType(type, meta, dbIdx);
        if (spec == null) {
            spec = new DataTableSpec("database", new DataColumnSpecCreator(name, newType).createSpec());
        } else {
            name = DataTableSpec.getUniqueColumnName(spec, name);
            spec = new DataTableSpec("database", spec, new DataTableSpec(new DataColumnSpecCreator(name, newType).createSpec()));
        }
    }
    return spec;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataType(org.knime.core.data.DataType)

Example 47 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class DatabaseReaderConnection method createTableSpec.

private DataTableSpec createTableSpec(final ResultSetMetaData meta) throws SQLException {
    int cols = meta.getColumnCount();
    if (cols == 0) {
        return new DataTableSpec("database");
    }
    StatementManipulator manipulator = m_conn.getUtility().getStatementManipulator();
    DataTableSpec spec = null;
    for (int i = 0; i < cols; i++) {
        int dbIdx = i + 1;
        String name = manipulator.unquoteColumn(meta.getColumnLabel(dbIdx));
        int type = meta.getColumnType(dbIdx);
        DataType newType;
        switch(type) {
            // all types that can be interpreted as integer
            case Types.BIT:
            case Types.BOOLEAN:
                newType = BooleanCell.TYPE;
                break;
            // all types that can be interpreted as integer
            case Types.TINYINT:
            case Types.SMALLINT:
            case Types.INTEGER:
                newType = IntCell.TYPE;
                break;
            // all types that can be interpreted as long
            case Types.BIGINT:
                newType = LongCell.TYPE;
                break;
            // all types that can be interpreted as double
            case Types.FLOAT:
            case Types.DOUBLE:
            case Types.NUMERIC:
            case Types.DECIMAL:
            case Types.REAL:
                newType = DoubleCell.TYPE;
                break;
            // all types that can be interpreted as data-and-time
            case Types.TIME:
            case Types.DATE:
            case Types.TIMESTAMP:
                newType = DateAndTimeCell.TYPE;
                break;
            // all types that can be interpreted as binary object
            case Types.BLOB:
            case Types.LONGVARBINARY:
            case Types.BINARY:
                newType = BinaryObjectDataCell.TYPE;
                break;
            // fallback string
            default:
                newType = StringCell.TYPE;
        }
        if (spec == null) {
            spec = new DataTableSpec("database", new DataColumnSpecCreator(name, newType).createSpec());
        } else {
            name = DataTableSpec.getUniqueColumnName(spec, name);
            spec = new DataTableSpec("database", spec, new DataTableSpec(new DataColumnSpecCreator(name, newType).createSpec()));
        }
    }
    return spec;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataType(org.knime.core.data.DataType)

Example 48 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class ColumnRearranger method ensureColumnIsConverted.

/**
 * @param converter
 * @param index
 * @since 2.7
 */
public final void ensureColumnIsConverted(final DataCellTypeConverter converter, final int index) {
    SpecAndFactoryObject current = m_includes.get(index);
    DataType converterOutputType = converter.getOutputType();
    DataColumnSpec colSpec = current.getColSpec();
    if (converterOutputType.equals(colSpec.getType())) {
        LOGGER.debug("Converting column \"" + colSpec.getName() + "\" not required.");
    } else {
        LOGGER.debug("Converting column \"" + colSpec.getName() + "\" required.");
        DataColumnSpecCreator c = new DataColumnSpecCreator(colSpec);
        c.setType(converterOutputType);
        DataCellTypeConverterCellFactory cellConverter = new DataCellTypeConverterCellFactory(c.createSpec(), converter, index);
        replace(cellConverter, index);
    }
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataType(org.knime.core.data.DataType)

Example 49 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class ColumnSelectionPanel method update.

/**
 * Updates this filter panel by removing all current items and adding the
 * columns according to the content of the argument <code>spec</code>. If
 * a column name is provided and it is not filtered out the corresponding
 * item in the combo box will be selected.
 *
 * @param spec To get the column names, types and the current index from.
 * @param selColName The column name to be set as chosen.
 * @param addSelColIfInvalid adds the selected column although it does not
 * not exist in the given spec or is filtered by the filter.
 * The column is added at the end and rendered with a red border.
 * @param useRowID set this parameter to <code>true</code> and the
 * selColName to <code>null</code> if the the row id option should be
 * selected
 * @throws NotConfigurableException If the spec does not contain at least
 * one compatible type.
 * @since 2.10
 */
public final void update(final DataTableSpec spec, final String selColName, final boolean useRowID, final boolean addSelColIfInvalid) throws NotConfigurableException {
    m_spec = spec;
    m_chooser.removeAllItems();
    if (m_addNoneColOption) {
        final String noneOption = DataTableSpec.getUniqueColumnName(spec, NONE_OPTION_LABEL);
        m_noneColSpec = new DataColumnSpecCreator(noneOption, DataType.getMissingCell().getType()).createSpec();
        m_chooser.addItem(m_noneColSpec);
        m_chooser.setToolTipText("Select " + noneOption + " for no column");
    }
    if (m_addRowIDOption) {
        final String rowIDOption = DataTableSpec.getUniqueColumnName(spec, ROWID_OPTION_LABEL);
        m_rowIDColSpec = new DataColumnSpecCreator(rowIDOption, DataType.getMissingCell().getType()).createSpec();
        m_chooser.addItem(m_rowIDColSpec);
        m_chooser.setToolTipText("Select " + rowIDOption + " for RowID");
    }
    DataColumnSpec selectMe = null;
    if (spec != null) {
        for (DataColumnSpec current : spec) {
            if (m_columnFilter.includeColumn(current)) {
                m_chooser.addItem(current);
                if (current.getName().equals(selColName)) {
                    selectMe = current;
                }
            }
        }
        if (selectMe != null) {
            m_chooser.setSelectedItem(selectMe);
        } else {
            if (addSelColIfInvalid && selColName != null) {
                m_chooser.addItem(DataColumnSpecListCellRenderer.createInvalidSpec(selColName));
            }
            if (m_addNoneColOption && !useRowID) {
                m_chooser.setSelectedItem(m_noneColSpec);
            } else if (m_addRowIDOption && useRowID) {
                m_chooser.setSelectedItem(m_rowIDColSpec);
            } else {
                // select last element
                final int size = m_chooser.getItemCount();
                if (size > 0) {
                    m_chooser.setSelectedIndex(size - 1);
                }
            }
        }
    }
    if ((m_chooser.getItemCount() == 0 || containsOnlyTheInvalidColumn(addSelColIfInvalid, selectMe)) && m_isRequired) {
        throw new NotConfigurableException(m_columnFilter.allFilteredMsg());
    }
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec)

Example 50 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class DataColumnSpecListCellRenderer method createInvalidSpec.

/**
 * @param originalSpec the original {@link DataColumnSpec} to be marked as invalid
 * @return the given {@link DataColumnSpec} with the added invalid flag
 * @since 2.8
 * @see #INVALID_PROPERTY_NAME
 */
public static final DataColumnSpec createInvalidSpec(final DataColumnSpec originalSpec) {
    DataColumnSpecCreator creator = new DataColumnSpecCreator(originalSpec);
    final DataColumnProperties origProps = originalSpec.getProperties();
    final Map<String, String> map = creaeteInvalidPropertiesMap();
    final DataColumnProperties props;
    if (origProps != null) {
        props = origProps.cloneAndOverwrite(map);
    } else {
        props = new DataColumnProperties(map);
    }
    creator.setProperties(props);
    final DataColumnSpec invalidSpec = creator.createSpec();
    return invalidSpec;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnProperties(org.knime.core.data.DataColumnProperties)

Aggregations

DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)267 DataColumnSpec (org.knime.core.data.DataColumnSpec)210 DataTableSpec (org.knime.core.data.DataTableSpec)132 DataCell (org.knime.core.data.DataCell)92 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)77 DataType (org.knime.core.data.DataType)74 DataRow (org.knime.core.data.DataRow)73 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)57 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)51 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)48 ArrayList (java.util.ArrayList)46 DoubleCell (org.knime.core.data.def.DoubleCell)45 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)44 StringCell (org.knime.core.data.def.StringCell)29 BufferedDataTable (org.knime.core.node.BufferedDataTable)23 DoubleValue (org.knime.core.data.DoubleValue)22 HashSet (java.util.HashSet)19 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)17 DataColumnDomain (org.knime.core.data.DataColumnDomain)16 DefaultRow (org.knime.core.data.def.DefaultRow)16