[#2291] Add DSLContext.fetchAny(Table, Condition) method and others

This commit is contained in:
Lukas Eder 2013-05-03 15:28:16 +02:00
parent aa74f02875
commit 392c2e158c
4 changed files with 49 additions and 12 deletions

View File

@ -393,6 +393,18 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
assertEquals("Coelho", record.getValue(TAuthor_LAST_NAME()));
}
@Test
public void testFetchAny() throws Exception {
A a1 = create().fetchAny(TAuthor());
assertTrue(asList(1, 2).contains(a1.getValue(TAuthor_ID())));
A a2 = create().fetchAny(TAuthor(), TAuthor_ID().eq(1));
assertEquals(1, (int) a2.getValue(TAuthor_ID()));
A a3 = create().fetchAny(TAuthor(), TAuthor_ID().eq(3));
assertNull(a3);
}
@Test
public void testFetchMany() throws Exception {
switch (dialect()) {

View File

@ -1298,6 +1298,11 @@ public abstract class jOOQAbstractTest<
new FetchTests(this).testFetch();
}
@Test
public void testFetchAny() throws Exception {
new FetchTests(this).testFetchAny();
}
@Test
public void testFetchMany() throws Exception {
new FetchTests(this).testFetchMany();

View File

@ -4501,7 +4501,7 @@ public interface DSLContext {
/**
* Execute and return all records for
* <code><pre>SELECT * FROM [table]</pre></code>
* <code><pre>SELECT * FROM [table]</pre></code>.
* <p>
* The result and its contained records are attached to this
* {@link Configuration} by default. Use {@link Settings#isAttachRecords()}
@ -4514,7 +4514,7 @@ public interface DSLContext {
/**
* Execute and return all records for
* <code><pre>SELECT * FROM [table] WHERE [condition] </pre></code>
* <code><pre>SELECT * FROM [table] WHERE [condition] </pre></code>.
* <p>
* The result and its contained records are attached to this
* {@link Configuration} by default. Use {@link Settings#isAttachRecords()}
@ -4527,7 +4527,7 @@ public interface DSLContext {
/**
* Execute and return zero or one record for
* <code><pre>SELECT * FROM [table]</pre></code>
* <code><pre>SELECT * FROM [table]</pre></code>.
* <p>
* The resulting record is attached to this {@link Configuration} by
* default. Use {@link Settings#isAttachRecords()} to override this
@ -4542,7 +4542,7 @@ public interface DSLContext {
/**
* Execute and return zero or one record for
* <code><pre>SELECT * FROM [table] WHERE [condition] </pre></code>
* <code><pre>SELECT * FROM [table] WHERE [condition] </pre></code>.
* <p>
* The resulting record is attached to this {@link Configuration} by
* default. Use {@link Settings#isAttachRecords()} to override this
@ -4558,7 +4558,7 @@ public interface DSLContext {
/**
* Execute and return zero or one record for
* <code><pre>SELECT * FROM [table] LIMIT 1</pre></code>
* <code><pre>SELECT * FROM [table] LIMIT 1</pre></code>.
* <p>
* The resulting record is attached to this {@link Configuration} by
* default. Use {@link Settings#isAttachRecords()} to override this
@ -4570,9 +4570,23 @@ public interface DSLContext {
@Support
<R extends Record> R fetchAny(Table<R> table) throws DataAccessException;
/**
* Execute and return zero or one record for
* <code><pre>SELECT * FROM [table] WHERE [condition] LIMIT 1</pre></code>.
* <p>
* The resulting record is attached to this {@link Configuration} by
* default. Use {@link Settings#isAttachRecords()} to override this
* behaviour.
*
* @return The record or <code>null</code> if no record was returned
* @throws DataAccessException if something went wrong executing the query
*/
@Support
<R extends Record> R fetchAny(Table<R> table, Condition condition) throws DataAccessException;
/**
* Execute and return all records lazily for
* <code><pre>SELECT * FROM [table]</pre></code>
* <code><pre>SELECT * FROM [table]</pre></code>.
* <p>
* The result and its contained records are attached to this
* {@link Configuration} by default. Use {@link Settings#isAttachRecords()}
@ -4585,7 +4599,7 @@ public interface DSLContext {
/**
* Execute and return all records lazily for
* <code><pre>SELECT * FROM [table] WHERE [condition] </pre></code>
* <code><pre>SELECT * FROM [table] WHERE [condition] </pre></code>.
* <p>
* The result and its contained records are attached to this
* {@link Configuration} by default. Use {@link Settings#isAttachRecords()}
@ -4597,7 +4611,7 @@ public interface DSLContext {
<R extends Record> Cursor<R> fetchLazy(Table<R> table, Condition condition) throws DataAccessException;
/**
* Insert one record
* Insert one record.
* <p>
* This executes something like the following statement:
* <code><pre>INSERT INTO [table] ... VALUES [record] </pre></code>
@ -4614,7 +4628,7 @@ public interface DSLContext {
<R extends TableRecord<R>> int executeInsert(R record) throws DataAccessException;
/**
* Update a table
* Update a table.
* <code><pre>UPDATE [table] SET [modified values in record] WHERE [record is supplied record] </pre></code>
*
* @return The number of updated records
@ -4624,7 +4638,7 @@ public interface DSLContext {
<R extends UpdatableRecord<R>> int executeUpdate(R record) throws DataAccessException;
/**
* Update a table
* Update a table.
* <code><pre>UPDATE [table] SET [modified values in record] WHERE [condition]</pre></code>
*
* @return The number of updated records
@ -4634,7 +4648,7 @@ public interface DSLContext {
<R extends TableRecord<R>, T> int executeUpdate(R record, Condition condition) throws DataAccessException;
/**
* Delete a record from a table
* Delete a record from a table.
* <code><pre>DELETE FROM [table] WHERE [record is supplied record]</pre></code>
*
* @return The number of deleted records
@ -4644,7 +4658,7 @@ public interface DSLContext {
<R extends UpdatableRecord<R>> int executeDelete(R record) throws DataAccessException;
/**
* Delete a record from a table
* Delete a record from a table.
* <code><pre>DELETE FROM [table] WHERE [condition]</pre></code>
*
* @return The number of deleted records

View File

@ -1659,6 +1659,12 @@ class DSLContextImpl implements DSLContext, Serializable {
return Utils.filterOne(selectFrom(table).limit(1).fetch());
}
@Override
@Support
public final <R extends Record> R fetchAny(Table<R> table, Condition condition) throws DataAccessException {
return Utils.filterOne(selectFrom(table).where(condition).limit(1).fetch());
}
@Override
@Support
public final <R extends Record> Cursor<R> fetchLazy(Table<R> table) throws DataAccessException {