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);
}
}
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);
}
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);
}
Aggregations