diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java index baa22b64b7..ce1cc9d0ee 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java @@ -78,6 +78,7 @@ import org.jooq.UDTRecord; import org.jooq.exception.SQLDialectNotSupportedException; import org.jooq.tools.Convert; import org.jooq.tools.JooqLogger; +import org.jooq.tools.jdbc.MockArray; import org.jooq.types.DayToSecond; import org.jooq.types.UByte; import org.jooq.types.UInteger; @@ -373,7 +374,7 @@ class DefaultBindContext extends AbstractBindContext { t = String[].class; } - stmt.setArray(nextIndex(), new DefaultArray(dialect, a, t)); + stmt.setArray(nextIndex(), new MockArray(dialect, a, t)); break; } case H2: { diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultArray.java b/jOOQ/src/main/java/org/jooq/tools/jdbc/MockArray.java similarity index 52% rename from jOOQ/src/main/java/org/jooq/impl/DefaultArray.java rename to jOOQ/src/main/java/org/jooq/tools/jdbc/MockArray.java index 57a57ab505..f1e6383fd6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultArray.java +++ b/jOOQ/src/main/java/org/jooq/tools/jdbc/MockArray.java @@ -38,22 +38,38 @@ * This library is distributed with a LIMITED WARRANTY. See the jOOQ License * and Maintenance Agreement for more details: http://www.jooq.org/licensing */ -package org.jooq.impl; +package org.jooq.tools.jdbc; + +import static java.lang.reflect.Array.newInstance; +import static org.jooq.impl.DSL.fieldByName; import java.sql.Array; import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; import java.util.Map; +import org.jooq.DSLContext; +import org.jooq.Field; +import org.jooq.Record2; +import org.jooq.Result; import org.jooq.SQLDialect; -import org.jooq.exception.SQLDialectNotSupportedException; +import org.jooq.impl.DSL; +import org.jooq.impl.DefaultDataType; -class DefaultArray implements Array { +/** + * A mock {@link Array}. + * + * @author Lukas Eder + * @see MockConnection + */ +public class MockArray implements Array { - private final SQLDialect dialect; - private final Object[] array; - private final Class type; + private final SQLDialect dialect; + private final T[] array; + private final Class type; - public DefaultArray(SQLDialect dialect, Object[] array, Class type) { + public MockArray(SQLDialect dialect, T[] array, Class type) { this.dialect = dialect; this.array = array; this.type = type; @@ -66,47 +82,74 @@ class DefaultArray implements Array { @Override public int getBaseType() { - throw new SQLDialectNotSupportedException("Array.getBaseType()"); + return DefaultDataType.getDataType(dialect, type.getComponentType()).getSQLType(); } @Override - public Object getArray() { + public T[] getArray() { return array; } @Override - public Object getArray(Map> map) { + public T[] getArray(Map> map) { return array; } + @SuppressWarnings("unchecked") @Override - public Object getArray(long index, int count) { - throw new SQLDialectNotSupportedException("Array.getArray(long, int)"); + public T[] getArray(long index, int count) throws SQLException { + if (index - 1 > Integer.MAX_VALUE) + throw new SQLException("Cannot access array indexes beyond Integer.MAX_VALUE"); + + return array == null ? null : Arrays + .asList(array) + .subList(((int) index) - 1, ((int) index) - 1 + count) + .toArray((T[]) newInstance(array.getClass().getComponentType(), count)); } @Override - public Object getArray(long index, int count, Map> map) { - throw new SQLDialectNotSupportedException("Array.getArray(long, int, Map)"); + public T[] getArray(long index, int count, Map> map) throws SQLException { + return getArray(index, count); } @Override public ResultSet getResultSet() { - throw new SQLDialectNotSupportedException("Array.getResultSet()"); + return getResultSet0(array); } @Override public ResultSet getResultSet(Map> map) { - throw new SQLDialectNotSupportedException("Array.getResultSet(Map)"); + return getResultSet(); } @Override - public ResultSet getResultSet(long index, int count) { - throw new SQLDialectNotSupportedException("Array.getResultSet(long, int)"); + public ResultSet getResultSet(long index, int count) throws SQLException { + return getResultSet0(getArray(index, count)); } @Override - public ResultSet getResultSet(long index, int count, Map> map) { - throw new SQLDialectNotSupportedException("Array.getResultSet(long, int, Map)"); + public ResultSet getResultSet(long index, int count, Map> map) throws SQLException { + return getResultSet(index, count); + } + + @SuppressWarnings("unchecked") + private ResultSet getResultSet0(T[] a) { + DSLContext create = DSL.using(dialect); + + Field index = fieldByName(Long.class, "INDEX"); + Field value = (Field) fieldByName(type.getComponentType(), "VALUE"); + Result> result = create.newResult(index, value); + + for (int i = 0; i < a.length; i++) { + Record2 record = create.newRecord(index, value); + + record.setValue(index, i + 1L); + record.setValue(value, a[i]); + + result.add(record); + } + + return new MockResultSet(result); } @Override