org.apache.calcite.avatica.util.ArrayImpl Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.avatica.util;
import org.apache.calcite.avatica.ColumnMetaData;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Map;
/** Implementation of JDBC {@link Array}. */
public class ArrayImpl implements Array {
private final ColumnMetaData.AvaticaType elementType;
private final Factory factory;
private final List list;
public ArrayImpl(List list, ColumnMetaData.AvaticaType elementType,
Factory factory) {
this.list = list;
this.elementType = elementType;
this.factory = factory;
}
public String getBaseTypeName() throws SQLException {
return elementType.name;
}
public int getBaseType() throws SQLException {
return elementType.id;
}
public Object getArray() throws SQLException {
return getArray(list);
}
/**
* Converts a list into an array.
*
* If the elements of the list are primitives, converts to an array of
* primitives (e.g. {@code boolean[]}.
*
* @param list List of objects
*
* @return array
* @throws ClassCastException if any element is not of the box type
* @throws NullPointerException if any element is null
*/
@SuppressWarnings("unchecked")
protected Object getArray(List list) throws SQLException {
int i = 0;
switch (elementType.rep) {
case PRIMITIVE_DOUBLE:
final double[] doubles = new double[list.size()];
for (double v : (List) list) {
doubles[i++] = v;
}
return doubles;
case PRIMITIVE_FLOAT:
final float[] floats = new float[list.size()];
for (float v : (List) list) {
floats[i++] = v;
}
return floats;
case PRIMITIVE_INT:
final int[] ints = new int[list.size()];
for (int v : (List) list) {
ints[i++] = v;
}
return ints;
case PRIMITIVE_LONG:
final long[] longs = new long[list.size()];
for (long v : (List) list) {
longs[i++] = v;
}
return longs;
case PRIMITIVE_SHORT:
final short[] shorts = new short[list.size()];
for (short v : (List) list) {
shorts[i++] = v;
}
return shorts;
case PRIMITIVE_BOOLEAN:
final boolean[] booleans = new boolean[list.size()];
for (boolean v : (List) list) {
booleans[i++] = v;
}
return booleans;
case PRIMITIVE_BYTE:
final byte[] bytes = new byte[list.size()];
for (byte v : (List) list) {
bytes[i++] = v;
}
return bytes;
case PRIMITIVE_CHAR:
final char[] chars = new char[list.size()];
for (char v : (List) list) {
chars[i++] = v;
}
return chars;
default:
// fall through
}
final Object[] objects = list.toArray();
switch (elementType.id) {
case Types.ARRAY:
final ColumnMetaData.ArrayType arrayType =
(ColumnMetaData.ArrayType) elementType;
for (i = 0; i < objects.length; i++) {
objects[i] =
new ArrayImpl((List) objects[i], arrayType.component, factory);
}
}
return objects;
}
public Object getArray(Map> map) throws SQLException {
throw new UnsupportedOperationException(); // TODO
}
public Object getArray(long index, int count) throws SQLException {
return getArray(list.subList((int) index, count));
}
public Object getArray(long index, int count, Map> map)
throws SQLException {
throw new UnsupportedOperationException(); // TODO
}
public ResultSet getResultSet() throws SQLException {
return factory.create(elementType, list);
}
public ResultSet getResultSet(Map> map)
throws SQLException {
throw new UnsupportedOperationException(); // TODO
}
public ResultSet getResultSet(long index, int count) throws SQLException {
throw new UnsupportedOperationException(); // TODO
}
public ResultSet getResultSet(long index, int count,
Map> map) throws SQLException {
throw new UnsupportedOperationException(); // TODO
}
public void free() throws SQLException {
// nothing to do
}
/** Factory that can create a result set based on a list of values. */
public interface Factory {
ResultSet create(ColumnMetaData.AvaticaType elementType,
Iterable