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

This commit is contained in:
Lukas Eder 2011-08-25 18:45:16 +00:00
parent 7fe22e497d
commit cdc19b3b73
4 changed files with 111 additions and 0 deletions

View File

@ -1421,6 +1421,8 @@ public abstract class jOOQAbstractTest<
@Test
public void testConversionResult() throws Exception {
// .fetch(..., Class)
// ------------------
assertEquals(
Arrays.asList((byte) 1, (byte) 2),
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetch(0, Byte.class));
@ -1471,6 +1473,61 @@ public abstract class jOOQAbstractTest<
assertEquals(
Arrays.asList(new BigDecimal("1"), new BigDecimal("2")),
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).fetch(TAuthor_ID().getName(), BigDecimal.class));
// .fetchOne(..., Class)
// ---------------------
assertEquals(
(byte) 1,
(byte) create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(0, Byte.class));
assertEquals(
(short) 1,
(short) create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(0, Short.class));
assertEquals(
1,
(int) create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(0, Integer.class));
assertEquals(
1L,
(long) create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(0, Long.class));
assertEquals(
1.0f,
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(0, Float.class));
assertEquals(
1.0,
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(0, Double.class));
assertEquals(
new BigInteger("1"),
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(0, BigInteger.class));
assertEquals(
new BigDecimal("1"),
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(0, BigDecimal.class));
assertEquals(
(byte) 1,
(byte) create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(TAuthor_ID().getName(), Byte.class));
assertEquals(
(short) 1,
(short) create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(TAuthor_ID().getName(), Short.class));
assertEquals(
1,
(int) create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(TAuthor_ID().getName(), Integer.class));
assertEquals(
1L,
(long) create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(TAuthor_ID().getName(), Long.class));
assertEquals(
1.0f,
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(TAuthor_ID().getName(), Float.class));
assertEquals(
1.0,
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(TAuthor_ID().getName(), Double.class));
assertEquals(
new BigInteger("1"),
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(TAuthor_ID().getName(), BigInteger.class));
assertEquals(
new BigDecimal("1"),
create().selectFrom(TAuthor()).orderBy(TAuthor_ID()).limit(1).fetchOne(TAuthor_ID().getName(), BigDecimal.class));
}
@Test

View File

@ -181,6 +181,23 @@ public interface ResultQuery<R extends Record> extends Query {
*/
Object fetchOne(int fieldIndex) throws SQLException;
/**
* Execute the query and return return at most one resulting value for a
* field index from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(int, Class)}
*
* @return The resulting value or <code>null</code> if the query returned no
* records.
* @throws SQLException This exception is thrown
* <ul>
* <li>if something went wrong executing the query</li> <li>if
* the query returned more than one value</li>
* </ul>
*/
<T> T fetchOne(int fieldIndex, Class<? extends T> type) throws SQLException;
/**
* Execute the query and return return at most one resulting value for a
* field name from the generated result.
@ -198,6 +215,23 @@ public interface ResultQuery<R extends Record> extends Query {
*/
Object fetchOne(String fieldName) throws SQLException;
/**
* Execute the query and return return at most one resulting value for a
* field name from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(String, Class)}
*
* @return The resulting value or <code>null</code> if the query returned no
* records.
* @throws SQLException This exception is thrown
* <ul>
* <li>if something went wrong executing the query</li> <li>if
* the query returned more than one value</li>
* </ul>
*/
<T> T fetchOne(String fieldName, Class<? extends T> type) throws SQLException;
/**
* Execute the query and return at most one resulting record.
*

View File

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

View File

@ -182,12 +182,22 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return record == null ? null : record.getValue(fieldIndex);
}
@Override
public final <T> T fetchOne(int fieldIndex, Class<? extends T> type) throws SQLException {
return TypeUtils.convert(fetchOne(fieldIndex), type);
}
@Override
public final Object fetchOne(String fieldName) throws SQLException {
R record = fetchOne();
return record == null ? null : record.getValue(fieldName);
}
@Override
public final <T> T fetchOne(String fieldName, Class<? extends T> type) throws SQLException {
return TypeUtils.convert(fetchOne(fieldName), type);
}
@Override
public final R fetchOne() throws SQLException {
Result<R> r = fetch();