Search in sources :

Example 11 with Struct

use of java.sql.Struct in project calcite-avatica by apache.

the class JdbcResultSet method getValue.

private static Object getValue(ResultSet resultSet, int type, int j, Calendar calendar) throws SQLException {
    switch(type) {
        case Types.BIGINT:
            final long aLong = resultSet.getLong(j + 1);
            return aLong == 0 && resultSet.wasNull() ? null : aLong;
        case Types.INTEGER:
            final int anInt = resultSet.getInt(j + 1);
            return anInt == 0 && resultSet.wasNull() ? null : anInt;
        case Types.SMALLINT:
            final short aShort = resultSet.getShort(j + 1);
            return aShort == 0 && resultSet.wasNull() ? null : aShort;
        case Types.TINYINT:
            final byte aByte = resultSet.getByte(j + 1);
            return aByte == 0 && resultSet.wasNull() ? null : aByte;
        case Types.DOUBLE:
        case Types.FLOAT:
            final double aDouble = resultSet.getDouble(j + 1);
            return aDouble == 0D && resultSet.wasNull() ? null : aDouble;
        case Types.REAL:
            final float aFloat = resultSet.getFloat(j + 1);
            return aFloat == 0D && resultSet.wasNull() ? null : aFloat;
        case Types.DATE:
            final Date aDate = resultSet.getDate(j + 1, calendar);
            return aDate == null ? null : (int) (aDate.getTime() / DateTimeUtils.MILLIS_PER_DAY);
        case Types.TIME:
            final Time aTime = resultSet.getTime(j + 1, calendar);
            return aTime == null ? null : (int) (aTime.getTime() % DateTimeUtils.MILLIS_PER_DAY);
        case Types.TIMESTAMP:
            final Timestamp aTimestamp = resultSet.getTimestamp(j + 1, calendar);
            return aTimestamp == null ? null : aTimestamp.getTime();
        case Types.ARRAY:
            final Array array = resultSet.getArray(j + 1);
            if (null == array) {
                return null;
            }
            try {
                // Recursively extracts an Array using its ResultSet-representation
                return extractUsingResultSet(array, calendar);
            } catch (UnsupportedOperationException | SQLFeatureNotSupportedException e) {
                // assumes a non-nested array (depends on the db if that's a valid assumption)
                return extractUsingArray(array, calendar);
            }
        case Types.STRUCT:
            Struct struct = resultSet.getObject(j + 1, Struct.class);
            Object[] attrs = struct.getAttributes();
            List<Object> list = new ArrayList<>(attrs.length);
            for (Object o : attrs) {
                list.add(o);
            }
            return list;
        default:
            return resultSet.getObject(j + 1);
    }
}
Also used : SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) ArrayList(java.util.ArrayList) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) Struct(java.sql.Struct) Array(java.sql.Array)

Aggregations

Struct (java.sql.Struct)11 Array (java.sql.Array)5 SQLException (java.sql.SQLException)4 ResultSet (java.sql.ResultSet)3 EventCreationException (com.linkedin.databus2.producers.EventCreationException)2 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 List (java.util.List)2 Schema (org.apache.avro.Schema)2 GenericArray (org.apache.avro.generic.GenericArray)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)2 StructType (org.apache.calcite.avatica.ColumnMetaData.StructType)2 Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)2 DBException (org.jkiss.dbeaver.DBException)2 JDBCStructImpl (org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl)2 JDBCCompositeStatic (org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 BaseTest (util.BaseTest)2