use of org.knime.core.data.DataType 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;
}
use of org.knime.core.data.DataType 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;
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class DataContainer method addRowToTableWrite.
private void addRowToTableWrite(final DataRow row) {
// let's do every possible sanity check
int numCells = row.getNumCells();
RowKey key = row.getKey();
if (numCells != m_spec.getNumColumns()) {
throw new IllegalArgumentException("Cell count in row \"" + key + "\" is not equal to length of column names " + "array: " + numCells + " vs. " + m_spec.getNumColumns());
}
for (int c = 0; c < numCells; c++) {
DataType columnClass = m_spec.getColumnSpec(c).getType();
DataCell value;
DataType runtimeType;
if (row instanceof BlobSupportDataRow) {
BlobSupportDataRow bsvalue = (BlobSupportDataRow) row;
value = bsvalue.getRawCell(c);
} else {
value = row.getCell(c);
}
if (value instanceof BlobWrapperDataCell) {
BlobWrapperDataCell bw = (BlobWrapperDataCell) value;
runtimeType = bw.getBlobDataType();
} else {
runtimeType = value.getType();
}
if (!columnClass.isASuperTypeOf(runtimeType)) {
String valString = value.toString();
// avoid too long string representations
if (valString.length() > 30) {
valString = valString.substring(0, 30) + "...";
}
throw new IllegalArgumentException("Runtime class of object \"" + valString + "\" (index " + c + ") in " + "row \"" + key + "\" is " + runtimeType.toString() + " and does " + "not comply with its supposed superclass " + columnClass.toString());
}
}
// for all cells
m_domainCreator.updateDomain(row);
addRowKeyForDuplicateCheck(key);
m_buffer.addRow(row, false, m_forceCopyOfBlobs);
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class DefaultTableStoreWriter method writeMetaInfoAfterWrite.
/**
* {@inheritDoc}
*/
@Override
public void writeMetaInfoAfterWrite(final NodeSettingsWO settings) {
settings.addString(DefaultTableStoreFormat.CFG_COMPRESSION, m_compressionFormat.name());
// unreported bug fix: NPE when the table only contains missing values.
if (m_typeShortCuts == null) {
m_typeShortCuts = new HashMap<CellClassInfo, Byte>();
}
CellClassInfo[] shortCutsLookup = new CellClassInfo[m_typeShortCuts.size()];
for (Map.Entry<CellClassInfo, Byte> e : m_typeShortCuts.entrySet()) {
byte shortCut = e.getValue();
CellClassInfo type = e.getKey();
shortCutsLookup[shortCut - BYTE_TYPE_START] = type;
}
m_shortCutsLookup = shortCutsLookup;
NodeSettingsWO typeSubSettings = settings.addNodeSettings(DefaultTableStoreFormat.CFG_CELL_CLASSES);
for (int i = 0; i < shortCutsLookup.length; i++) {
CellClassInfo info = shortCutsLookup[i];
NodeSettingsWO single = typeSubSettings.addNodeSettings("element_" + i);
single.addString(DefaultTableStoreFormat.CFG_CELL_SINGLE_CLASS, info.getCellClass().getName());
DataType elementType = info.getCollectionElementType();
if (elementType != null) {
NodeSettingsWO subTypeConfig = single.addNodeSettings(DefaultTableStoreFormat.CFG_CELL_SINGLE_ELEMENT_TYPE);
elementType.save(subTypeConfig);
}
}
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class JavaToDataCellConverterRegistry method getConverterFactories.
/**
* Get all {@link JavaToDataCellConverterFactory converter factories} which create {@link JavaToDataCellConverter}s
* that convert <code>sourceType</code> into <code>destType</code>. If you do not require more than one converter
* factory, you should consider using {@link #getPreferredConverterFactory(Class, DataType)} instead.
*
* @param sourceType Source type to convert
* @param destType {@link DataType} to convert to
* @return collection of {@link JavaToDataCellConverterFactory converter factories} which create converters which
* convert from <code>sourceType</code> to <code>destType</code>
* @param <S> A JavaToDataCellConverter type (letting java infer the type is highly recommended)
*/
// we only put JavaToDataCellConverter<T> into the map for Class<T>
public <S> Collection<JavaToDataCellConverterFactory<S>> getConverterFactories(final Class<S> sourceType, final DataType destType) {
final LinkedBlockingQueue<Class<?>> classes = new LinkedBlockingQueue<>();
classes.add(sourceType);
Class<?> curClass = null;
final ArrayList<JavaToDataCellConverterFactory<S>> factories = new ArrayList<>();
while ((curClass = classes.poll()) != null) {
final ArrayList<JavaToDataCellConverterFactory<?>> newFactories = m_converterFactories.get(new ConversionKey(curClass, destType));
if (newFactories != null) {
factories.addAll((Collection<? extends JavaToDataCellConverterFactory<S>>) newFactories);
}
/* check if a supertype has a compatible converter factory */
classes.addAll(Arrays.asList(curClass.getInterfaces()));
if (curClass.getSuperclass() != null) {
classes.add(curClass.getSuperclass());
}
}
if (destType.isCollectionType() && sourceType.isArray()) {
final Collection<? extends JavaToDataCellConverterFactory<S>> elementFactories = (Collection<? extends JavaToDataCellConverterFactory<S>>) getConverterFactories(sourceType.getComponentType(), destType.getCollectionElementType());
final List<?> arrayFactories = elementFactories.stream().map((elementFactory) -> getArrayConverterFactory((JavaToDataCellConverterFactory<?>) elementFactory)).collect(Collectors.toList());
factories.addAll((Collection<? extends JavaToDataCellConverterFactory<S>>) arrayFactories);
}
return factories;
}
Aggregations