use of java.sql.Array in project calcite-avatica by apache.
the class TypedValue method jdbcToSerial.
/** Converts a value from JDBC format to a type that can be serialized as
* JSON. */
private static Object jdbcToSerial(ColumnMetaData.Rep rep, Object value, Calendar calendar, SqlType componentType) {
if (null == value) {
return null;
}
switch(rep) {
case BYTE_STRING:
return new ByteString((byte[]) value).toBase64String();
case JAVA_UTIL_DATE:
case JAVA_SQL_TIMESTAMP:
case JAVA_SQL_DATE:
case JAVA_SQL_TIME:
long t = ((Date) value).getTime();
if (calendar != null) {
t += calendar.getTimeZone().getOffset(t);
}
switch(rep) {
case JAVA_SQL_DATE:
return (int) DateTimeUtils.floorDiv(t, DateTimeUtils.MILLIS_PER_DAY);
case JAVA_SQL_TIME:
return (int) DateTimeUtils.floorMod(t, DateTimeUtils.MILLIS_PER_DAY);
default:
return t;
}
case ARRAY:
Array array = (Array) value;
Objects.requireNonNull(componentType, "Component Type must not be null for ARRAYs");
try {
switch(componentType) {
case BINARY:
case VARBINARY:
case LONGVARBINARY:
Object[] byteStrings = (Object[]) array.getArray();
List<String> convertedStrings = new ArrayList<>(byteStrings.length);
for (Object byteString : byteStrings) {
convertedStrings.add((String) jdbcToSerial(Rep.BYTE_STRING, byteString, calendar, null));
}
return convertedStrings;
case DATE:
case TIME:
Object[] dates = (Object[]) array.getArray();
List<Integer> serializedDates = new ArrayList<>(dates.length);
for (Object obj : dates) {
Date date = (Date) obj;
if (null == obj) {
serializedDates.add(null);
} else if (componentType == SqlType.DATE) {
serializedDates.add((int) jdbcToSerial(Rep.JAVA_SQL_DATE, date, calendar, null));
} else if (componentType == SqlType.TIME) {
serializedDates.add((int) jdbcToSerial(Rep.JAVA_SQL_TIME, date, calendar, null));
} else {
throw new RuntimeException("Unexpected type: " + componentType);
}
}
return serializedDates;
case TIMESTAMP:
Object[] timestamps = (Object[]) array.getArray();
List<Long> serializedTimestamps = new ArrayList<>(timestamps.length);
for (Object obj : timestamps) {
Timestamp timestamp = (Timestamp) obj;
if (null == obj) {
serializedTimestamps.add(null);
} else {
serializedTimestamps.add((long) jdbcToSerial(Rep.JAVA_SQL_TIMESTAMP, timestamp, calendar, null));
}
}
return serializedTimestamps;
default:
// Either a primitive array or Object[], converted into List<Object>
return AvaticaUtils.primitiveList(array.getArray());
}
} catch (SQLException e) {
throw new RuntimeException("Could not obtain base array object", e);
}
default:
return value;
}
}
use of java.sql.Array in project calcite-avatica by apache.
the class ArrayTypeTest method testCreateArrayOf.
@Test
public void testCreateArrayOf() throws Exception {
try (Connection conn = DriverManager.getConnection(url)) {
final String componentName = SqlType.INTEGER.name();
Array a1 = conn.createArrayOf(componentName, new Object[] { 1, 2, 3, 4, 5 });
Array a2 = conn.createArrayOf(componentName, new Object[] { 2, 3, 4, 5, 6 });
Array a3 = conn.createArrayOf(componentName, new Object[] { 3, 4, 5, 6, 7 });
AvaticaType arrayType = ColumnMetaData.array(ColumnMetaData.scalar(Types.INTEGER, componentName, Rep.INTEGER), "NUMBERS", Rep.ARRAY);
writeAndReadArrays(conn, "CREATE_ARRAY_OF_INTEGERS", componentName, arrayType, Arrays.asList(a1, a2, a3), PRIMITIVE_LIST_VALIDATOR);
}
}
use of java.sql.Array in project calcite-avatica by apache.
the class ArrayTypeTest method dateArrays.
@Test
public void dateArrays() throws Exception {
try (Connection conn = DriverManager.getConnection(url)) {
final long now = System.currentTimeMillis();
ScalarType component = ColumnMetaData.scalar(Types.DATE, "DATE", Rep.JAVA_SQL_DATE);
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 5; i++) {
List<Date> elements = new ArrayList<>();
for (int j = 0; j < 5; j++) {
elements.add(new Date(now + i + j));
}
arrays.add(createArray("DATE", component, elements));
}
writeAndReadArrays(conn, "date_arrays", "DATE", component, arrays, new Validator<Array>() {
@Override
public void validate(Array expected, Array actual) throws SQLException {
Object[] expectedDates = (Object[]) expected.getArray();
Object[] actualDates = (Object[]) actual.getArray();
assertEquals(expectedDates.length, actualDates.length);
final Calendar cal = Unsafe.localCalendar();
for (int i = 0; i < expectedDates.length; i++) {
cal.setTime((Date) expectedDates[i]);
int expectedDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int expectedMonth = cal.get(Calendar.MONTH);
int expectedYear = cal.get(Calendar.YEAR);
cal.setTime((Date) actualDates[i]);
assertEquals(expectedDayOfMonth, cal.get(Calendar.DAY_OF_MONTH));
assertEquals(expectedMonth, cal.get(Calendar.MONTH));
assertEquals(expectedYear, cal.get(Calendar.YEAR));
}
}
});
// Ensure an array with a null element can be written/read
Array arrayWithNull = createArray("DATE", component, Arrays.asList((Time) null));
writeAndReadArrays(conn, "date_array_with_null", "DATE", component, Collections.singletonList(arrayWithNull), new Validator<Array>() {
@Override
public void validate(Array expected, Array actual) throws Exception {
Object[] expectedArray = (Object[]) expected.getArray();
Object[] actualArray = (Object[]) actual.getArray();
assertEquals(1, expectedArray.length);
assertEquals(expectedArray.length, actualArray.length);
assertEquals(expectedArray[0], actualArray[0]);
}
});
}
}
use of java.sql.Array in project calcite-avatica by apache.
the class ArrayTypeTest method shortArrays.
@Test
public void shortArrays() throws Exception {
final Random r = new Random();
try (Connection conn = DriverManager.getConnection(url)) {
ScalarType component = ColumnMetaData.scalar(Types.SMALLINT, "SMALLINT", Rep.SHORT);
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 5; i++) {
List<Short> elements = new ArrayList<>();
for (int j = 0; j < 5; j++) {
short value = (short) r.nextInt(Short.MAX_VALUE);
// 50% of the time, negate the value
if (0 == r.nextInt(2)) {
value *= -1;
}
elements.add(Short.valueOf(value));
}
arrays.add(createArray("SMALLINT", component, elements));
}
// Verify read/write
writeAndReadArrays(conn, "short_arrays", "SMALLINT", component, arrays, PRIMITIVE_LIST_VALIDATOR);
}
}
use of java.sql.Array in project calcite-avatica by apache.
the class ArrayTypeTest method stringArrays.
@Test
public void stringArrays() throws Exception {
try (Connection conn = DriverManager.getConnection(url)) {
ScalarType component = ColumnMetaData.scalar(Types.VARCHAR, "VARCHAR", Rep.STRING);
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 5; i++) {
List<String> elements = new ArrayList<>();
for (int j = 0; j < 5; j++) {
elements.add(i + "_" + j);
}
arrays.add(createArray("VARCHAR", component, elements));
}
// Verify read/write
writeAndReadArrays(conn, "string_arrays", "VARCHAR", component, arrays, PRIMITIVE_LIST_VALIDATOR);
}
}
Aggregations