[#805] Add <T> T[] fetchArray(int, Class<T>) and fetchArray(String, Class<T>) convenience methods

This commit is contained in:
Lukas Eder 2011-08-25 20:38:24 +00:00
parent cdc19b3b73
commit e35c42e41f
4 changed files with 98 additions and 0 deletions

View File

@ -1475,6 +1475,60 @@ public abstract class jOOQAbstractTest<
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetch(TAuthor_ID().getName(), BigDecimal.class));
// .fetchArray(..., Class)
// ------------------
assertEquals(
Arrays.asList((byte) 1, (byte) 2),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(0, Byte.class)));
assertEquals(
Arrays.asList((short) 1, (short) 2),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(0, Short.class)));
assertEquals(
Arrays.asList(1, 2),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(0, Integer.class)));
assertEquals(
Arrays.asList(1L, 2L),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(0, Long.class)));
assertEquals(
Arrays.asList(1.0f, 2.0f),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(0, Float.class)));
assertEquals(
Arrays.asList(1.0, 2.0),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(0, Double.class)));
assertEquals(
Arrays.asList(new BigInteger("1"), new BigInteger("2")),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(0, BigInteger.class)));
assertEquals(
Arrays.asList(new BigDecimal("1"), new BigDecimal("2")),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(0, BigDecimal.class)));
assertEquals(
Arrays.asList((byte) 1, (byte) 2),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(TAuthor_ID().getName(), Byte.class)));
assertEquals(
Arrays.asList((short) 1, (short) 2),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(TAuthor_ID().getName(), Short.class)));
assertEquals(
Arrays.asList(1, 2),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(TAuthor_ID().getName(), Integer.class)));
assertEquals(
Arrays.asList(1L, 2L),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(TAuthor_ID().getName(), Long.class)));
assertEquals(
Arrays.asList(1.0f, 2.0f),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(TAuthor_ID().getName(), Float.class)));
assertEquals(
Arrays.asList(1.0, 2.0),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(TAuthor_ID().getName(), Double.class)));
assertEquals(
Arrays.asList(new BigInteger("1"), new BigInteger("2")),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(TAuthor_ID().getName(), BigInteger.class)));
assertEquals(
Arrays.asList(new BigDecimal("1"), new BigDecimal("2")),
Arrays.asList(create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetchArray(TAuthor_ID().getName(), BigDecimal.class)));
// .fetchOne(..., Class)
// ---------------------
assertEquals(

View File

@ -344,6 +344,17 @@ public interface ResultQuery<R extends Record> extends Query {
*/
Object[] fetchArray(int fieldIndex) throws SQLException;
/**
* Execute the query and return all values for a field index from the
* generated result.
* <p>
* You can access data like this
* <code><pre>query.fetchArray(fieldIndex)[recordIndex]</pre></code>
*
* @return The resulting values.
*/
<T> T[] fetchArray(int fieldIndex, Class<? extends T> type) throws SQLException;
/**
* Execute the query and return all values for a field name from the
* generated result.
@ -355,6 +366,17 @@ public interface ResultQuery<R extends Record> extends Query {
*/
Object[] fetchArray(String fieldName) throws SQLException;
/**
* Execute the query and return all values for a field name from the
* generated result.
* <p>
* You can access data like this
* <code><pre>query.fetchArray(fieldName)[recordIndex]</pre></code>
*
* @return The resulting values.
*/
<T> T[] fetchArray(String fieldName, Class<? extends T> type) throws SQLException;
/**
* Execute the query and return all values for a field from the generated
* result.

View File

@ -196,11 +196,21 @@ abstract class AbstractDelegatingSelect<R extends Record> extends AbstractQueryP
return query.fetchArray(fieldIndex);
}
@Override
public final <T> T[] fetchArray(int fieldIndex, Class<? extends T> type) throws SQLException {
return query.fetchArray(fieldIndex, type);
}
@Override
public final Object[] fetchArray(String fieldName) throws SQLException {
return query.fetchArray(fieldName);
}
@Override
public final <T> T[] fetchArray(String fieldName, Class<? extends T> type) throws SQLException {
return query.fetchArray(fieldName, type);
}
@Override
public final <T> T[] fetchArray(Field<T> field) throws SQLException {
return query.fetchArray(field);

View File

@ -293,11 +293,23 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return fetch(fieldIndex).toArray();
}
@SuppressWarnings("unchecked")
@Override
public final <T> T[] fetchArray(int fieldIndex, Class<? extends T> type) throws SQLException {
return (T[]) TypeUtils.convertArray(fetchArray(fieldIndex), type);
}
@Override
public final Object[] fetchArray(String fieldName) throws SQLException {
return fetch(fieldName).toArray();
}
@SuppressWarnings("unchecked")
@Override
public final <T> T[] fetchArray(String fieldName, Class<? extends T> type) throws SQLException {
return (T[]) TypeUtils.convertArray(fetchArray(fieldName), type);
}
@SuppressWarnings("unchecked")
@Override
public final <T> T[] fetchArray(Field<T> field) throws SQLException {