[#2984] Add <R extends Record> Table<R> table(Result<R>) to allow to fetch from / join / etc in-memory tables

This commit is contained in:
Lukas Eder 2014-01-29 11:22:16 +01:00
parent e417f40f35
commit c7d125a321
4 changed files with 68 additions and 3 deletions

View File

@ -42,6 +42,7 @@ package org.jooq.test._.testcases;
import static junit.framework.Assert.assertEquals;
import static org.jooq.impl.DSL.row;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.DSL.values;
import java.sql.Date;
@ -53,6 +54,7 @@ import org.jooq.Record3;
import org.jooq.Record6;
import org.jooq.Result;
import org.jooq.RowN;
import org.jooq.Table;
import org.jooq.TableRecord;
import org.jooq.UpdatableRecord;
import org.jooq.test.BaseTest;
@ -117,4 +119,25 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
assertEquals(r1, r2);
}
@Test
public void testResultConstructor() throws Exception {
Result<Record2<Integer, String>> booksResult =
create().select(TBook_ID(), TBook_TITLE())
.from(TBook())
.orderBy(TBook_ID())
.fetch();
Table<Record2<Integer, String>> booksTable = table(booksResult);
assertEquals(
booksResult,
create().selectFrom(booksTable).fetch());
assertEquals(
BOOK_IDS,
create().select(booksTable.field(TBook_ID()))
.from(booksTable)
.fetch(TBook_ID()));
}
}

View File

@ -1891,6 +1891,11 @@ public abstract class jOOQAbstractTest<
new ValuesConstructorTests(this).testValuesConstructor();
}
@Test
public void testResultConstructor() throws Exception {
new ValuesConstructorTests(this).testResultConstructor();
}
@Test
public void testOrderByInSubquery() throws Exception {
new OrderByTests(this).testOrderByInSubquery();

View File

@ -4489,6 +4489,33 @@ public class DSL {
return select.asTable();
}
/**
* Use a previously obtained result as a new {@link Table} that can be used
* in SQL statements through {@link #values(RowN...)}.
*
* @see #values(RowN...)
*/
@Support
@Transition(
name = "TABLE",
args = "Result"
)
public static <R extends Record> Table<R> table(Result<R> result) {
int size = result.size();
RowN[] rows = new RowN[size];
for (int i = 0; i < size; i++)
rows[i] = (RowN) result.get(i).valuesRow();
Field<?>[] fields = result.fields();
String[] columns = new String[fields.length];
for (int i = 0; i < fields.length; i++)
columns[i] = fields[i].getName();
// TODO [#2986] Coerce the record type upon the resulting table.
return (Table<R>) values(rows).as("v", columns);
}
/**
* A synonym for {@link #unnest(List)}.
*
@ -12364,9 +12391,12 @@ public class DSL {
args = "Row+"
)
public static Table<Record> values(RowN... rows) {
String[] columns = new String[rows.length];
Values.assertNotEmpty(rows);
int size = rows[0].size();
for (int i = 0; i < rows.length; i++)
String[] columns = new String[size];
for (int i = 0; i < size; i++)
columns[i] = "c" + i;
return new Values<Record>(rows).as("v", columns);

View File

@ -68,7 +68,14 @@ class Values<R extends Record> extends AbstractTable<R> {
Values(Row[] rows) {
super("values");
this.rows = rows;
this.rows = assertNotEmpty(rows);
}
static Row[] assertNotEmpty(Row[] rows) {
if (rows == null || rows.length == 0)
throw new IllegalArgumentException("Cannot create a VALUES() constructor with an empty set of rows");
return rows;
}
@SuppressWarnings("unchecked")