use of org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor in project calcite-avatica by apache.
the class ArrayFactoryImpl method createArray.
@Override
public Array createArray(AvaticaType elementType, Iterable<Object> elements) {
final ArrayType array = ColumnMetaData.array(elementType, elementType.name, Rep.ARRAY);
final List<ColumnMetaData> types = Collections.singletonList(ColumnMetaData.dummy(array, true));
// Avoid creating a new List if we already have a List
List<Object> elementList;
if (elements instanceof List) {
elementList = (List<Object>) elements;
} else {
elementList = new ArrayList<>();
for (Object element : elements) {
elementList.add(element);
}
}
try (ListIteratorCursor cursor = new ListIteratorCursor(createRowForArrayData(elementList))) {
List<Accessor> accessor = cursor.createAccessors(types, Unsafe.localCalendar(), this);
assert 1 == accessor.size();
return new ArrayImpl(elementList, (ArrayAccessor) accessor.get(0));
}
}
use of org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor 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);
}
}
Aggregations