Example usage for java.sql Time toInstant

List of usage examples for java.sql Time toInstant

Introduction

In this page you can find the example usage for java.sql Time toInstant.

Prototype

@Override
public Instant toInstant() 

Source Link

Document

This method always throws an UnsupportedOperationException and should not be used because SQL Time values do not have a date component.

Usage

From source file:com.streamsets.pipeline.lib.jdbc.JdbcUtil.java

public Map<String, String> getMinimumOffsetValues(Connection connection, String schema, String tableName,
        QuoteChar quoteChar, Collection<String> offsetColumnNames) throws SQLException {
    Map<String, String> minOffsetValues = new HashMap<>();
    final String qualifiedName = TableContextUtil.getQuotedQualifiedTableName(schema, tableName,
            quoteChar.getQuoteCharacter());
    for (String offsetColumn : offsetColumnNames) {
        final String minOffsetQuery = String.format(MIN_OFFSET_VALUE_QUERY, offsetColumn, qualifiedName);
        try (Statement st = connection.createStatement(); ResultSet rs = st.executeQuery(minOffsetQuery)) {
            if (rs.next()) {
                String minValue = null;
                final int colType = rs.getMetaData().getColumnType(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
                switch (colType) {
                case Types.DATE:
                    java.sql.Date date = rs.getDate(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
                    if (date != null) {
                        minValue = String.valueOf(date.toInstant().toEpochMilli());
                    }//from  w w  w  . j  a  v a 2  s  . c  o  m
                    break;
                case Types.TIME:
                    java.sql.Time time = rs.getTime(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
                    if (time != null) {
                        minValue = String.valueOf(time.toInstant().toEpochMilli());
                    }
                    break;
                case Types.TIMESTAMP:
                    Timestamp timestamp = rs.getTimestamp(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
                    if (timestamp != null) {
                        final Instant instant = timestamp.toInstant();
                        minValue = String.valueOf(instant.toEpochMilli());
                    }
                    break;
                default:
                    minValue = rs.getString(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
                    break;
                }
                if (minValue != null) {
                    minOffsetValues.put(offsetColumn, minValue);
                }
            } else {
                LOG.warn("Unable to get minimum offset value using query {}; result set had no rows",
                        minOffsetQuery);
            }
        }
    }

    return minOffsetValues;
}