[#1722] ResultQuery.fetchArray(int) and .fetchArray(String) should
return a typed array, even if this cannot be checked by the compiler
This commit is contained in:
parent
0ace81628e
commit
3ba7ef5a9c
@ -267,15 +267,20 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
|
||||
|
||||
// fetch single field
|
||||
// ------------------
|
||||
|
||||
// [#1722] Check the actual returned type of arrays, also
|
||||
String[] array1 = create().selectFrom(TBook()).orderBy(TBook_ID()).fetchArray(TBook_TITLE());
|
||||
assertEquals(create().selectFrom(TBook()).orderBy(TBook_ID()).fetch(TBook_TITLE()),
|
||||
Arrays.asList(create().selectFrom(TBook()).orderBy(TBook_ID()).fetchArray(TBook_TITLE())));
|
||||
Arrays.asList(array1));
|
||||
|
||||
assertEquals(create().selectFrom(TBook()).orderBy(TBook_ID()).fetch(1),
|
||||
Arrays.asList(create().selectFrom(TBook()).orderBy(TBook_ID()).fetchArray(1)));
|
||||
Object[] array2 = create().selectFrom(TBook()).orderBy(TBook_ID()).fetchArray(1);
|
||||
assertEquals(create().selectFrom(TBook()).orderBy(TBook_ID()).fetch(1), Arrays.asList(array2));
|
||||
assertTrue(array2 instanceof Integer[]);
|
||||
|
||||
Object[] array3 = create().selectFrom(TBook()).orderBy(TBook_ID()).fetchArray(TBook_ID().getName());
|
||||
assertEquals(create().selectFrom(TBook()).orderBy(TBook_ID()).fetch(TBook_ID().getName()),
|
||||
Arrays.asList(create().selectFrom(TBook()).orderBy(TBook_ID()).fetchArray(TBook_ID().getName())));
|
||||
|
||||
Arrays.asList(array3));
|
||||
assertTrue(array3 instanceof Integer[]);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@ -366,13 +366,16 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
|
||||
assertEquals("10", fetch4.getValue("p"));
|
||||
assertEquals("10", fetch4.getValue(fetch4.getField("p")));
|
||||
|
||||
// [#1722] Check the actual returned type of arrays, also
|
||||
Object[] fetch5 = q.fetchArray("p");
|
||||
assertEquals(1, fetch5.length);
|
||||
assertEquals("10", fetch5[0]);
|
||||
assertTrue(fetch5 instanceof String[]);
|
||||
|
||||
Object[] fetch6 = q.fetchArray(0);
|
||||
assertEquals(1, fetch6.length);
|
||||
assertEquals("10", fetch6[0]);
|
||||
assertTrue(fetch6 instanceof String[]);
|
||||
|
||||
Long[] fetch7 = q.fetchArray(0, Long.class);
|
||||
assertEquals(1, fetch7.length);
|
||||
|
||||
@ -555,7 +555,9 @@ public interface ResultQuery<R extends Record> extends Query {
|
||||
* You can access data like this
|
||||
* <code><pre>query.fetchArray(fieldIndex)[recordIndex]</pre></code>
|
||||
*
|
||||
* @return The resulting values.
|
||||
* @return The resulting values. This may be an array type more concrete
|
||||
* than <code>Object[]</code>, depending on whether jOOQ has any
|
||||
* knowledge about <code>fieldIndex</code>'s actual type.
|
||||
* @throws DataAccessException if something went wrong executing the query
|
||||
*/
|
||||
Object[] fetchArray(int fieldIndex) throws DataAccessException;
|
||||
@ -589,7 +591,9 @@ public interface ResultQuery<R extends Record> extends Query {
|
||||
* You can access data like this
|
||||
* <code><pre>query.fetchArray(fieldName)[recordIndex]</pre></code>
|
||||
*
|
||||
* @return The resulting values.
|
||||
* @return The resulting values. This may be an array type more concrete
|
||||
* than <code>Object[]</code>, depending on whether jOOQ has any
|
||||
* knowledge about <code>fieldName</code>'s actual type.
|
||||
* @throws DataAccessException if something went wrong executing the query
|
||||
*/
|
||||
Object[] fetchArray(String fieldName) throws DataAccessException;
|
||||
|
||||
@ -444,7 +444,10 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
|
||||
|
||||
@Override
|
||||
public final Object[] fetchArray(int fieldIndex) {
|
||||
return fetch(fieldIndex).toArray();
|
||||
Result<R> fetch = fetch();
|
||||
Class<?> type = fetch.getField(fieldIndex).getType();
|
||||
List<?> list = fetch.getValues(fieldIndex);
|
||||
return list.toArray((Object[]) Array.newInstance(type, list.size()));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -461,7 +464,10 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
|
||||
|
||||
@Override
|
||||
public final Object[] fetchArray(String fieldName) {
|
||||
return fetch(fieldName).toArray();
|
||||
Result<R> fetch = fetch();
|
||||
Class<?> type = fetch.getField(fieldName).getType();
|
||||
List<?> list = fetch.getValues(fieldName);
|
||||
return list.toArray((Object[]) Array.newInstance(type, list.size()));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user