Search in sources :

Example 1 with ArrayFactoryImpl

use of org.apache.calcite.avatica.util.ArrayFactoryImpl in project calcite-avatica by apache.

the class TypedValue method serialToJdbc.

/**
   * Converts the given value from serial form to JDBC form.
   *
   * @param type The type of the value
   * @param value The value
   * @param calendar A calendar instance
   * @return The JDBC representation of the value.
   */
private static Object serialToJdbc(ColumnMetaData.Rep type, ColumnMetaData.Rep componentRep, Object value, Calendar calendar) {
    switch(type) {
        case BYTE_STRING:
            return ByteString.ofBase64((String) value).getBytes();
        case JAVA_UTIL_DATE:
            return new java.util.Date(adjust((Number) value, calendar));
        case JAVA_SQL_DATE:
            return new java.sql.Date(adjust(((Number) value).longValue() * DateTimeUtils.MILLIS_PER_DAY, calendar));
        case JAVA_SQL_TIME:
            return new java.sql.Time(adjust((Number) value, calendar));
        case JAVA_SQL_TIMESTAMP:
            return new java.sql.Timestamp(adjust((Number) value, calendar));
        case ARRAY:
            if (null == value) {
                return null;
            }
            final List<?> list = (List<?>) value;
            final List<Object> copy = new ArrayList<>(list.size());
            // Copy the list from the serial representation to a JDBC representation
            for (Object o : list) {
                if (null == o) {
                    copy.add(null);
                } else if (o instanceof TypedValue) {
                    // Protobuf can maintain the TypedValue hierarchy to simplify things
                    copy.add(((TypedValue) o).toJdbc(calendar));
                } else {
                    // We can't get the above recursion with the JSON serialization
                    copy.add(serialToJdbc(componentRep, null, o, calendar));
                }
            }
            AvaticaType elementType = new AvaticaType(componentRep.typeId, componentRep.name(), componentRep);
            return new ArrayFactoryImpl(calendar.getTimeZone()).createArray(elementType, copy);
        default:
            return serialToLocal(type, value);
    }
}
Also used : AvaticaType(org.apache.calcite.avatica.ColumnMetaData.AvaticaType) ArrayList(java.util.ArrayList) Time(java.sql.Time) ByteString(org.apache.calcite.avatica.util.ByteString) Timestamp(java.sql.Timestamp) Date(java.util.Date) ArrayFactoryImpl(org.apache.calcite.avatica.util.ArrayFactoryImpl) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ArrayFactoryImpl

use of org.apache.calcite.avatica.util.ArrayFactoryImpl in project calcite-avatica by apache.

the class AvaticaConnection method createArrayOf.

public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
    @SuppressWarnings("unchecked") List<Object> elementList = (List<Object>) AvaticaUtils.primitiveList(elements);
    SqlType type;
    try {
        type = SqlType.valueOf(typeName);
    } catch (IllegalArgumentException e) {
        throw new SQLException("Could not find JDBC type for '" + typeName + "'");
    }
    AvaticaType avaticaType = null;
    switch(type) {
        case ARRAY:
            // TODO: Nested ARRAYs
            throw helper.createException("Cannot create an ARRAY of ARRAY's");
        case STRUCT:
            // TODO: ARRAYs of STRUCTs
            throw helper.createException("Cannot create an ARRAY of STRUCT's");
        default:
            // This is an ARRAY, we need to use Objects, not primitives (nullable).
            avaticaType = ColumnMetaData.scalar(type.id, typeName, Rep.nonPrimitiveRepOf(type));
    }
    ArrayFactoryImpl arrayFactory = new ArrayFactoryImpl(getTimeZone());
    return arrayFactory.createArray(avaticaType, elementList);
}
Also used : ArrayFactoryImpl(org.apache.calcite.avatica.util.ArrayFactoryImpl) SQLException(java.sql.SQLException) AvaticaType(org.apache.calcite.avatica.ColumnMetaData.AvaticaType) List(java.util.List)

Example 3 with ArrayFactoryImpl

use of org.apache.calcite.avatica.util.ArrayFactoryImpl in project calcite-avatica by apache.

the class TypedValueTest method testArrays.

@Test
public void testArrays() {
    List<Object> serialObj = Arrays.<Object>asList(1, 2, 3, 4);
    ArrayImpl.Factory factory = new ArrayFactoryImpl(Unsafe.localCalendar().getTimeZone());
    ScalarType scalarType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.INTEGER);
    Array a1 = factory.createArray(scalarType, serialObj);
    TypedValue tv1 = TypedValue.ofJdbc(Rep.ARRAY, a1, Unsafe.localCalendar());
    Object jdbcObj = tv1.toJdbc(Unsafe.localCalendar());
    assertTrue("The JDBC object is an " + jdbcObj.getClass(), jdbcObj instanceof Array);
    Object localObj = tv1.toLocal();
    assertTrue("The local object is an " + localObj.getClass(), localObj instanceof List);
    Common.TypedValue protoTv1 = tv1.toProto();
    assertEquals(serialObj.size(), protoTv1.getArrayValueCount());
    TypedValue tv1Copy = TypedValue.fromProto(protoTv1);
    Object jdbcObjCopy = tv1Copy.toJdbc(Unsafe.localCalendar());
    assertTrue("The JDBC object is an " + jdbcObjCopy.getClass(), jdbcObjCopy instanceof Array);
    Object localObjCopy = tv1Copy.toLocal();
    assertTrue("The local object is an " + localObjCopy.getClass(), localObjCopy instanceof List);
}
Also used : Array(java.sql.Array) ArrayFactoryImpl(org.apache.calcite.avatica.util.ArrayFactoryImpl) ArrayImpl(org.apache.calcite.avatica.util.ArrayImpl) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) List(java.util.List) Common(org.apache.calcite.avatica.proto.Common) Test(org.junit.Test)

Aggregations

List (java.util.List)3 ArrayFactoryImpl (org.apache.calcite.avatica.util.ArrayFactoryImpl)3 AvaticaType (org.apache.calcite.avatica.ColumnMetaData.AvaticaType)2 Array (java.sql.Array)1 SQLException (java.sql.SQLException)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)1 Common (org.apache.calcite.avatica.proto.Common)1 ArrayImpl (org.apache.calcite.avatica.util.ArrayImpl)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 Test (org.junit.Test)1