Search in sources :

Example 21 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.

the class ExtractTimeWindowNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    int colIndex = -1;
    if (m_columnName.getStringValue() == null) {
        // no value yet -> auto-configure
        int i = 0;
        for (DataColumnSpec cs : inSpecs[0]) {
            if (cs.getType().isCompatible(DateAndTimeValue.class)) {
                colIndex = i;
                // found first date compatible column
                // -> auto-select it
                m_columnName.setStringValue(cs.getName());
                setWarningMessage("Auto-selected date column: " + cs.getName());
                break;
            }
            i++;
        }
        // if we did not found any time compatible column
        if (colIndex == -1) {
            throw new InvalidSettingsException("No column selected.");
        }
        // set the from and to calendars to the minimum and maximum date of
        // the auto-selected column
        DataColumnSpec colSpec = inSpecs[0].getColumnSpec(m_columnName.getStringValue());
        if (colSpec.getType().isCompatible(DateAndTimeValue.class) && colSpec.getDomain().hasBounds()) {
            DataCell lower = colSpec.getDomain().getLowerBound();
            DataCell upper = colSpec.getDomain().getUpperBound();
            if (lower != null && upper != null && lower.getType().isCompatible(DateAndTimeValue.class) && upper.getType().isCompatible(DateAndTimeValue.class)) {
                Calendar c = ((DateAndTimeValue) lower).getUTCCalendarClone();
                m_fromDate.setCalendar(c);
                c = ((DateAndTimeValue) upper).getUTCCalendarClone();
                m_toDate.setCalendar(c);
            }
        }
    } else {
        // configured once -> we have a name selected
        colIndex = inSpecs[0].findColumnIndex(m_columnName.getStringValue());
        if (colIndex < 0) {
            throw new InvalidSettingsException("No such column: " + m_columnName.getStringValue());
        }
        DataColumnSpec colSpec = inSpecs[0].getColumnSpec(colIndex);
        if (!colSpec.getType().isCompatible(DateAndTimeValue.class)) {
            throw new InvalidSettingsException("Column \"" + m_columnName + "\" does not contain string values: " + colSpec.getType().toString());
        }
    }
    validateFromTo(m_fromDate.getCalendar(), m_toDate.getCalendar());
    // (no structural changes)
    return inSpecs.clone();
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) DataCell(org.knime.core.data.DataCell)

Example 22 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.

the class DateMeanOperator method computeInternal.

/**
 * {@inheritDoc}
 */
@Override
protected boolean computeInternal(final DataCell cell) {
    if (cell instanceof DateAndTimeValue) {
        // skip missing cells
        final DateAndTimeValue dateCell = (DateAndTimeValue) cell;
        m_hasDate |= dateCell.hasDate();
        m_hasTime |= dateCell.hasTime();
        m_hasMilis |= dateCell.hasMillis();
        final double d = dateCell.getUTCTimeInMillis();
        m_mean = m_mean * ((double) m_count / (m_count + 1)) + d * (1.0 / (m_count + 1));
        m_count++;
    }
    return false;
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue)

Example 23 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.

the class DatabaseHelper method fillStatement.

/**
 * Set given column value into SQL statement.
 * @param stmt statement used
 * @param dbIdx database index to update/write
 * @param cspec column spec to check type
 * @param cell the data cell to write into the statement
 * @param tz the {@link TimeZone} to use
 * @param columnTypes
 * @throws SQLException if the value can't be set
 */
protected void fillStatement(final PreparedStatement stmt, final int dbIdx, final DataColumnSpec cspec, final DataCell cell, final TimeZone tz, final Map<Integer, Integer> columnTypes) throws SQLException {
    if (cspec.getType().isCompatible(BooleanValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.BOOLEAN);
        } else {
            boolean bool = ((BooleanValue) cell).getBooleanValue();
            stmt.setBoolean(dbIdx, bool);
        }
    } else if (cspec.getType().isCompatible(IntValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.INTEGER);
        } else {
            int integer = ((IntValue) cell).getIntValue();
            stmt.setInt(dbIdx, integer);
        }
    } else if (cspec.getType().isCompatible(LongValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.BIGINT);
        } else {
            long dbl = ((LongValue) cell).getLongValue();
            stmt.setLong(dbIdx, dbl);
        }
    } else if (cspec.getType().isCompatible(DoubleValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.DOUBLE);
        } else {
            double dbl = ((DoubleValue) cell).getDoubleValue();
            if (Double.isNaN(dbl)) {
                stmt.setNull(dbIdx, Types.DOUBLE);
            } else {
                stmt.setDouble(dbIdx, dbl);
            }
        }
    } else if (cspec.getType().isCompatible(DateAndTimeValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.DATE);
        } else {
            final DateAndTimeValue dateCell = (DateAndTimeValue) cell;
            final long corrDate = dateCell.getUTCTimeInMillis() - tz.getOffset(dateCell.getUTCTimeInMillis());
            if (!dateCell.hasTime() && !dateCell.hasMillis()) {
                java.sql.Date date = new java.sql.Date(corrDate);
                stmt.setDate(dbIdx, date);
            } else if (!dateCell.hasDate()) {
                java.sql.Time time = new java.sql.Time(corrDate);
                stmt.setTime(dbIdx, time);
            } else {
                java.sql.Timestamp timestamp = new java.sql.Timestamp(corrDate);
                stmt.setTimestamp(dbIdx, timestamp);
            }
        }
    } else if (cspec.getType().isCompatible(BinaryObjectDataValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.BLOB);
        } else {
            try {
                BinaryObjectDataValue value = (BinaryObjectDataValue) cell;
                InputStream is = value.openInputStream();
                if (is == null) {
                    stmt.setNull(dbIdx, Types.BLOB);
                } else {
                    try {
                        // to be compatible with JDBC 3.0, the length of the stream is restricted to max integer,
                        // which are ~2GB; with JDBC 4.0 longs are supported and the respective method can be called
                        stmt.setBinaryStream(dbIdx, is, (int) value.length());
                    } catch (SQLException ex) {
                        // if no supported (i.e. SQLite) set byte array
                        byte[] bytes = IOUtils.toByteArray(is);
                        stmt.setBytes(dbIdx, bytes);
                    }
                }
            } catch (IOException ioe) {
                stmt.setNull(dbIdx, Types.BLOB);
            }
        }
    } else if (cspec.getType().isCompatible(CollectionDataValue.class)) {
        fillArray(stmt, dbIdx, cell, tz);
    } else if ((columnTypes == null) || cspec.getType().isCompatible(StringValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.VARCHAR);
        } else {
            stmt.setString(dbIdx, cell.toString());
        }
    } else {
        Integer sqlType = columnTypes.get(dbIdx);
        if (sqlType == null) {
            sqlType = Types.VARCHAR;
        }
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, sqlType);
        } else {
            stmt.setObject(dbIdx, cell.toString(), sqlType);
        }
    }
}
Also used : SQLException(java.sql.SQLException) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) InputStream(java.io.InputStream) IOException(java.io.IOException) DoubleValue(org.knime.core.data.DoubleValue) BinaryObjectDataValue(org.knime.core.data.blob.BinaryObjectDataValue) BooleanValue(org.knime.core.data.BooleanValue) LongValue(org.knime.core.data.LongValue) IntValue(org.knime.core.data.IntValue)

Example 24 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.

the class DatabaseWriterConnection method fillStatement.

/**
 * Set given column value into SQL statement.
 * @param stmt statement used
 * @param dbIdx database index to update/write
 * @param cspec column spec to check type
 * @param cell the data cell to write into the statement
 * @param tz the {@link TimeZone} to use
 * @throws SQLException if the value can't be set
 */
private static void fillStatement(final PreparedStatement stmt, final int dbIdx, final DataColumnSpec cspec, final DataCell cell, final TimeZone tz, final Map<Integer, Integer> columnTypes) throws SQLException {
    if (cspec.getType().isCompatible(BooleanValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.BOOLEAN);
        } else {
            boolean bool = ((BooleanValue) cell).getBooleanValue();
            stmt.setBoolean(dbIdx, bool);
        }
    } else if (cspec.getType().isCompatible(IntValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.INTEGER);
        } else {
            int integer = ((IntValue) cell).getIntValue();
            stmt.setInt(dbIdx, integer);
        }
    } else if (cspec.getType().isCompatible(LongValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.BIGINT);
        } else {
            long dbl = ((LongValue) cell).getLongValue();
            stmt.setLong(dbIdx, dbl);
        }
    } else if (cspec.getType().isCompatible(DoubleValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.DOUBLE);
        } else {
            double dbl = ((DoubleValue) cell).getDoubleValue();
            if (Double.isNaN(dbl)) {
                stmt.setNull(dbIdx, Types.DOUBLE);
            } else {
                stmt.setDouble(dbIdx, dbl);
            }
        }
    } else if (cspec.getType().isCompatible(DateAndTimeValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.DATE);
        } else {
            final DateAndTimeValue dateCell = (DateAndTimeValue) cell;
            final long corrDate = dateCell.getUTCTimeInMillis() - tz.getOffset(dateCell.getUTCTimeInMillis());
            if (!dateCell.hasTime() && !dateCell.hasMillis()) {
                java.sql.Date date = new java.sql.Date(corrDate);
                stmt.setDate(dbIdx, date);
            } else if (!dateCell.hasDate()) {
                java.sql.Time time = new java.sql.Time(corrDate);
                stmt.setTime(dbIdx, time);
            } else {
                java.sql.Timestamp timestamp = new java.sql.Timestamp(corrDate);
                stmt.setTimestamp(dbIdx, timestamp);
            }
        }
    } else if (cspec.getType().isCompatible(BinaryObjectDataValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.BLOB);
        } else {
            try {
                BinaryObjectDataValue value = (BinaryObjectDataValue) cell;
                InputStream is = value.openInputStream();
                if (is == null) {
                    stmt.setNull(dbIdx, Types.BLOB);
                } else {
                    try {
                        // to be compatible with JDBC 3.0, the length of the stream is restricted to max integer,
                        // which are ~2GB; with JDBC 4.0 longs are supported and the respective method can be called
                        stmt.setBinaryStream(dbIdx, is, (int) value.length());
                    } catch (SQLException ex) {
                        // if no supported (i.e. SQLite) set byte array
                        byte[] bytes = IOUtils.toByteArray(is);
                        stmt.setBytes(dbIdx, bytes);
                    }
                }
            } catch (IOException ioe) {
                stmt.setNull(dbIdx, Types.BLOB);
            }
        }
    } else if ((columnTypes == null) || cspec.getType().isCompatible(StringValue.class)) {
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, Types.VARCHAR);
        } else {
            stmt.setString(dbIdx, cell.toString());
        }
    } else {
        Integer sqlType = columnTypes.get(dbIdx);
        if (sqlType == null) {
            sqlType = Types.VARCHAR;
        }
        if (cell.isMissing()) {
            stmt.setNull(dbIdx, sqlType);
        } else {
            stmt.setObject(dbIdx, cell.toString(), sqlType);
        }
    }
}
Also used : SQLException(java.sql.SQLException) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) InputStream(java.io.InputStream) IOException(java.io.IOException) DoubleValue(org.knime.core.data.DoubleValue) BinaryObjectDataValue(org.knime.core.data.blob.BinaryObjectDataValue) BooleanValue(org.knime.core.data.BooleanValue) LongValue(org.knime.core.data.LongValue) StringValue(org.knime.core.data.StringValue) IntValue(org.knime.core.data.IntValue)

Aggregations

DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)24 DataCell (org.knime.core.data.DataCell)16 DateAndTimeCell (org.knime.core.data.date.DateAndTimeCell)11 DataRow (org.knime.core.data.DataRow)8 DoubleValue (org.knime.core.data.DoubleValue)8 IntValue (org.knime.core.data.IntValue)8 LongValue (org.knime.core.data.LongValue)8 IntCell (org.knime.core.data.def.IntCell)8 DoubleCell (org.knime.core.data.def.DoubleCell)7 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)7 Calendar (java.util.Calendar)6 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)6 LongCell (org.knime.core.data.def.LongCell)6 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)5 SettingsModelCalendar (org.knime.timeseries.util.SettingsModelCalendar)5 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)3 BufferedDataTable (org.knime.core.node.BufferedDataTable)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2