Search in sources :

Example 1 with ListIteratorCursor

use of org.apache.calcite.avatica.util.ListIteratorCursor 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);
        assertTrue("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)

Aggregations

ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 List (java.util.List)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 ArrayImpl (org.apache.calcite.avatica.util.ArrayImpl)1 Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)1 ListIteratorCursor (org.apache.calcite.avatica.util.ListIteratorCursor)1