in src/main/java/com/amazon/redshift/jdbc/RedshiftArray.java [547:810]
private Object buildArray(RsArrayList input, int index, int count) throws SQLException {
if (count < 0) {
count = input.size();
}
// array to be returned
Object ret = null;
// how many dimensions
int dims = input.dimensionsCount;
// dimensions length array (to be used with java.lang.reflect.Array.newInstance(Class<?>,
// int[]))
int[] dimsLength = dims > 1 ? new int[dims] : null;
if (dims > 1) {
for (int i = 0; i < dims; i++) {
dimsLength[i] = (i == 0 ? count : 0);
}
}
// array elements counter
int length = 0;
// array elements type
final int type =
connection.getTypeInfo().getSQLType(connection.getTypeInfo().getRSArrayElement(oid));
if (type == Types.BIT) {
boolean[] pa = null; // primitive array
Object[] oa = null; // objects array
if (dims > 1 || useObjects) {
ret = oa = (dims > 1
? (Object[]) java.lang.reflect.Array
.newInstance(useObjects ? Boolean.class : boolean.class, dimsLength)
: new Boolean[count]);
} else {
ret = pa = new boolean[count];
}
// add elements
for (; count > 0; count--) {
Object o = input.get(index++);
if (dims > 1 || useObjects) {
oa[length++] = o == null ? null
: (dims > 1 ? buildArray((RsArrayList) o, 0, -1) : BooleanTypeUtil.castToBoolean((String) o));
} else {
pa[length++] = o == null ? false : BooleanTypeUtil.castToBoolean((String) o);
}
}
} else if (type == Types.SMALLINT) {
short[] pa = null;
Object[] oa = null;
if (dims > 1 || useObjects) {
ret =
oa = (dims > 1
? (Object[]) java.lang.reflect.Array
.newInstance(useObjects ? Short.class : short.class, dimsLength)
: new Short[count]);
} else {
ret = pa = new short[count];
}
for (; count > 0; count--) {
Object o = input.get(index++);
if (dims > 1 || useObjects) {
oa[length++] = o == null ? null
: (dims > 1 ? buildArray((RsArrayList) o, 0, -1) : RedshiftResultSet.toShort((String) o));
} else {
pa[length++] = o == null ? 0 : RedshiftResultSet.toShort((String) o);
}
}
} else if (type == Types.INTEGER) {
int[] pa = null;
Object[] oa = null;
if (dims > 1 || useObjects) {
ret =
oa = (dims > 1
? (Object[]) java.lang.reflect.Array
.newInstance(useObjects ? Integer.class : int.class, dimsLength)
: new Integer[count]);
} else {
ret = pa = new int[count];
}
for (; count > 0; count--) {
Object o = input.get(index++);
if (dims > 1 || useObjects) {
oa[length++] = o == null ? null
: (dims > 1 ? buildArray((RsArrayList) o, 0, -1) : RedshiftResultSet.toInt((String) o));
} else {
pa[length++] = o == null ? 0 : RedshiftResultSet.toInt((String) o);
}
}
} else if (type == Types.BIGINT) {
long[] pa = null;
Object[] oa = null;
if (dims > 1 || useObjects) {
ret =
oa = (dims > 1
? (Object[]) java.lang.reflect.Array
.newInstance(useObjects ? Long.class : long.class, dimsLength)
: new Long[count]);
} else {
ret = pa = new long[count];
}
for (; count > 0; count--) {
Object o = input.get(index++);
if (dims > 1 || useObjects) {
oa[length++] = o == null ? null
: (dims > 1 ? buildArray((RsArrayList) o, 0, -1) : RedshiftResultSet.toLong((String) o));
} else {
pa[length++] = o == null ? 0L : RedshiftResultSet.toLong((String) o);
}
}
} else if (type == Types.NUMERIC) {
Object[] oa = null;
ret = oa =
(dims > 1 ? (Object[]) java.lang.reflect.Array.newInstance(BigDecimal.class, dimsLength)
: new BigDecimal[count]);
for (; count > 0; count--) {
Object v = input.get(index++);
oa[length++] = dims > 1 && v != null ? buildArray((RsArrayList) v, 0, -1)
: (v == null ? null : RedshiftResultSet.toBigDecimal((String) v));
}
} else if (type == Types.REAL) {
float[] pa = null;
Object[] oa = null;
if (dims > 1 || useObjects) {
ret =
oa = (dims > 1
? (Object[]) java.lang.reflect.Array
.newInstance(useObjects ? Float.class : float.class, dimsLength)
: new Float[count]);
} else {
ret = pa = new float[count];
}
for (; count > 0; count--) {
Object o = input.get(index++);
if (dims > 1 || useObjects) {
oa[length++] = o == null ? null
: (dims > 1 ? buildArray((RsArrayList) o, 0, -1) : RedshiftResultSet.toFloat((String) o));
} else {
pa[length++] = o == null ? 0f : RedshiftResultSet.toFloat((String) o);
}
}
} else if (type == Types.DOUBLE) {
double[] pa = null;
Object[] oa = null;
if (dims > 1 || useObjects) {
ret = oa = (dims > 1
? (Object[]) java.lang.reflect.Array
.newInstance(useObjects ? Double.class : double.class, dimsLength)
: new Double[count]);
} else {
ret = pa = new double[count];
}
for (; count > 0; count--) {
Object o = input.get(index++);
if (dims > 1 || useObjects) {
oa[length++] = o == null ? null
: (dims > 1 ? buildArray((RsArrayList) o, 0, -1) : RedshiftResultSet.toDouble((String) o));
} else {
pa[length++] = o == null ? 0d : RedshiftResultSet.toDouble((String) o);
}
}
} else if (type == Types.CHAR || type == Types.VARCHAR || oid == Oid.JSONB_ARRAY) {
Object[] oa = null;
ret =
oa = (dims > 1 ? (Object[]) java.lang.reflect.Array.newInstance(String.class, dimsLength)
: new String[count]);
for (; count > 0; count--) {
Object v = input.get(index++);
oa[length++] = dims > 1 && v != null ? buildArray((RsArrayList) v, 0, -1) : v;
}
} else if (type == Types.DATE) {
Object[] oa = null;
ret = oa = (dims > 1
? (Object[]) java.lang.reflect.Array.newInstance(java.sql.Date.class, dimsLength)
: new java.sql.Date[count]);
for (; count > 0; count--) {
Object v = input.get(index++);
oa[length++] = dims > 1 && v != null ? buildArray((RsArrayList) v, 0, -1)
: (v == null ? null : connection.getTimestampUtils().toDate(null, (String) v));
}
} else if (type == Types.TIME) {
Object[] oa = null;
ret = oa = (dims > 1
? (Object[]) java.lang.reflect.Array.newInstance(java.sql.Time.class, dimsLength)
: new java.sql.Time[count]);
for (; count > 0; count--) {
Object v = input.get(index++);
oa[length++] = dims > 1 && v != null ? buildArray((RsArrayList) v, 0, -1)
: (v == null ? null : connection.getTimestampUtils().toTime(null, (String) v));
}
} else if (type == Types.TIMESTAMP) {
Object[] oa = null;
ret = oa = (dims > 1
? (Object[]) java.lang.reflect.Array.newInstance(java.sql.Timestamp.class, dimsLength)
: new java.sql.Timestamp[count]);
for (; count > 0; count--) {
Object v = input.get(index++);
oa[length++] = dims > 1 && v != null ? buildArray((RsArrayList) v, 0, -1)
: (v == null ? null : connection.getTimestampUtils().toTimestamp(null, (String) v));
}
} else if (ArrayAssistantRegistry.getAssistant(oid) != null) {
ArrayAssistant arrAssistant = ArrayAssistantRegistry.getAssistant(oid);
Object[] oa = null;
ret = oa = (dims > 1)
? (Object[]) java.lang.reflect.Array.newInstance(arrAssistant.baseType(), dimsLength)
: (Object[]) java.lang.reflect.Array.newInstance(arrAssistant.baseType(), count);
for (; count > 0; count--) {
Object v = input.get(index++);
oa[length++] = (dims > 1 && v != null) ? buildArray((RsArrayList) v, 0, -1)
: (v == null ? null : arrAssistant.buildElement((String) v));
}
} else if (dims == 1) {
Object[] oa = new Object[count];
String typeName = getBaseTypeName();
for (; count > 0; count--) {
Object v = input.get(index++);
if (v instanceof String) {
oa[length++] = connection.getObject(typeName, (String) v, null);
} else if (v instanceof byte[]) {
oa[length++] = connection.getObject(typeName, null, (byte[]) v);
} else if (v == null) {
oa[length++] = null;
} else {
throw com.amazon.redshift.Driver.notImplemented(this.getClass(), "getArrayImpl(long,int,Map)");
}
}
ret = oa;
} else {
// other datatypes not currently supported
if (RedshiftLogger.isEnable())
connection.getLogger().log(LogLevel.DEBUG, "getArrayImpl(long,int,Map) with {0}", getBaseTypeName());
throw com.amazon.redshift.Driver.notImplemented(this.getClass(), "getArrayImpl(long,int,Map)");
}
return ret;
}