[#1140] Add ResultQuery.fetchResultSet() to return the underlying JDBC result set

[#1148] Add Cursor.resultSet() to expose the underlying ResultSet
This commit is contained in:
Lukas Eder 2012-02-15 18:50:13 +00:00
parent 95e04de914
commit fc682afd31
7 changed files with 1069 additions and 10 deletions

View File

@ -46,6 +46,7 @@ import static org.jooq.impl.Factory.val;
import static org.joor.Reflect.on;
import java.sql.Date;
import java.sql.ResultSet;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
@ -834,6 +835,35 @@ extends BaseTest<A, B, S, B2S, BS, L, X, D, T, U, I, IPK, T658, T725, T639, T785
assertEquals(activeCount, Thread.activeCount());
}
@Test
public void testFetchResultSet() throws Exception {
assertEquals(
create().fetch("select * from t_author order by id"),
create().fetch(create().resultQuery("select * from t_author order by id").fetchResultSet()));
ResultSet rs = create().resultQuery("select * from t_author order by id").fetchResultSet();
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals(1, rs.getInt(1));
assertFalse(rs.wasNull());
assertEquals(1, rs.getInt(TAuthor_ID().getName()));
assertEquals((short) 1, rs.getShort(TAuthor_ID().getName()));
assertEquals(1L, rs.getLong(TAuthor_ID().getName()));
assertEquals(AUTHOR_FIRST_NAMES.get(0), rs.getString(2));
assertEquals(AUTHOR_FIRST_NAMES.get(0), rs.getString(TAuthor_FIRST_NAME().getName()));
assertEquals(AUTHOR_LAST_NAMES.get(0), rs.getString(3));
assertEquals(AUTHOR_LAST_NAMES.get(0), rs.getString(TAuthor_LAST_NAME().getName()));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertEquals(2, rs.getInt(1));
assertFalse(rs.wasNull());
assertEquals(2, rs.getInt(TAuthor_ID().getName()));
assertFalse(rs.next());
rs.close();
}
@Test
public void testFetchLazy() throws Exception {
for (int fetchSize : Arrays.asList(0, 1)) {

View File

@ -646,6 +646,11 @@ public abstract class jOOQAbstractTest<
new FunctionTests(this).testSystemFunctions();
}
@Test
public void testFetchResultSet() throws Exception {
new FetchTests(this).testFetchResultSet();
}
@Test
public void testFetchLazy() throws Exception {
new FetchTests(this).testFetchLazy();

View File

@ -209,4 +209,15 @@ public interface Cursor<R extends Record> extends FieldProvider, Iterable<R> {
* last record was fetched.
*/
boolean isClosed();
/**
* Get the <code>Cursor</code>'s underlying {@link ResultSet}
* <p>
* If you modify the underlying <code>ResultSet</code>, the
* <code>Cursor</code> may be affected and in some cases, rendered unusable.
*
* @return The underlying <code>ResultSet</code>. May be <code>null</code>,
* for instance when the <code>Cursor</code> is closed.
*/
ResultSet resultSet();
}

View File

@ -83,6 +83,22 @@ public interface ResultQuery<R extends Record> extends Query {
*/
Result<R> fetch() throws DataAccessException;
/**
* Execute the query and return the generated result as a JDBC
* {@link ResultSet}
* <p>
* This will return the {@link ResultSet} returned by the JDBC driver,
* leaving it untouched. Use this method when you want to use jOOQ for query
* execution, but not for result fetching.
* <p>
* The returned <code>ResultSet</code> can be used with
* {@link FactoryOperations#fetch(ResultSet)}
*
* @return The result.
* @throws DataAccessException if something went wrong executing the query
*/
ResultSet fetchResultSet() throws DataAccessException;
/**
* Execute the query and "lazily" return the generated result
* <p>

View File

@ -35,6 +35,7 @@
*/
package org.jooq.impl;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
@ -48,7 +49,6 @@ import org.jooq.Result;
import org.jooq.ResultQuery;
import org.jooq.Select;
import org.jooq.Table;
import org.jooq.exception.DataAccessException;
/**
* @author Lukas Eder
@ -96,6 +96,11 @@ abstract class AbstractDelegatingSelect<R extends Record>
return getDelegate().fetch();
}
@Override
public final ResultSet fetchResultSet() {
return getDelegate().fetchResultSet();
}
@Override
public final Cursor<R> fetchLazy() {
return getDelegate().fetchLazy();
@ -237,7 +242,7 @@ abstract class AbstractDelegatingSelect<R extends Record>
}
@Override
public final <Z extends Record> Result<Z> fetchInto(Table<Z> table) throws DataAccessException {
public final <Z extends Record> Result<Z> fetchInto(Table<Z> table) {
return getDelegate().fetchInto(table);
}

View File

@ -205,6 +205,11 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return result;
}
@Override
public final ResultSet fetchResultSet() throws DataAccessException {
return fetchLazy().resultSet();
}
@Override
public final Cursor<R> fetchLazy() {
return fetchLazy(0);

File diff suppressed because it is too large Load Diff