Search in sources :

Example 1 with ArrayImpl

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);
    }
}
Also used : Calendar(java.util.Calendar) ArrayImpl(org.apache.calcite.avatica.util.ArrayImpl) ArrayAccessor(org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) ArrayAccessor(org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor) ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) ListIteratorCursor(org.apache.calcite.avatica.util.ListIteratorCursor) ArrayList(java.util.ArrayList) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 2 with ArrayImpl

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();
    }
}
Also used : Array(java.sql.Array) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) PreparedStatement(java.sql.PreparedStatement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Statement(java.sql.Statement) ArrayImpl(org.apache.calcite.avatica.util.ArrayImpl) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Aggregations

ArrayImpl (org.apache.calcite.avatica.util.ArrayImpl)2 Array (java.sql.Array)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 List (java.util.List)1 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)1 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)1 ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)1 ArrayType (org.apache.calcite.avatica.ColumnMetaData.ArrayType)1 ArrayAccessor (org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor)1 Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)1 ListIteratorCursor (org.apache.calcite.avatica.util.ListIteratorCursor)1 Test (org.junit.Test)1