use of org.apache.calcite.avatica.util.ArrayImpl in project calcite-avatica by apache.
the class ArrayTypeTest method createArray.
/**
* Creates a JDBC {@link Array} from a list of values.
*
* @param typeName the SQL type name of the elements in the array
* @param componentType The Avatica type for the array elements
* @param arrayValues The array elements
* @return An Array instance for the given component and values
*/
@SuppressWarnings("unchecked")
private <T> Array createArray(String typeName, AvaticaType componentType, List<T> arrayValues) {
// Make a "row" with one "column" (which is really a list)
final List<Object> oneRow = Collections.singletonList((Object) arrayValues);
// Make an iterator over this one "row"
final Iterator<List<Object>> rowIterator = Collections.singletonList(oneRow).iterator();
ArrayType array = ColumnMetaData.array(componentType, typeName, Rep.ARRAY);
try (ListIteratorCursor cursor = new ListIteratorCursor(rowIterator)) {
List<ColumnMetaData> types = Collections.singletonList(ColumnMetaData.dummy(array, true));
Calendar calendar = Unsafe.localCalendar();
List<Accessor> accessors = cursor.createAccessors(types, calendar, null);
assertFalse("Expected at least one accessor, found " + accessors.size(), accessors.isEmpty());
ArrayAccessor arrayAccessor = (ArrayAccessor) accessors.get(0);
return new ArrayImpl((List<Object>) arrayValues, arrayAccessor);
}
}
use of org.apache.calcite.avatica.util.ArrayImpl in project calcite-avatica by apache.
the class RemoteMetaTest method testArrays.
@Test
public void testArrays() throws SQLException {
ConnectionSpec.getDatabaseLock().lock();
try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
ResultSet resultSet = stmt.executeQuery("select * from (values ('a', array['b', 'c']));");
assertTrue(resultSet.next());
assertEquals("a", resultSet.getString(1));
Array arr = resultSet.getArray(2);
assertTrue(arr instanceof ArrayImpl);
Object[] values = (Object[]) ((ArrayImpl) arr).getArray();
assertArrayEquals(new String[] { "b", "c" }, values);
} finally {
ConnectionSpec.getDatabaseLock().unlock();
}
}
Aggregations