From c9d12d8850c6242c718a2acfbe81df64fff287d0 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 3 Jan 2013 23:26:58 +0100 Subject: [PATCH] [#915] Add Table> Factory.values(Row[N]...), to create ad-hoc tables from data - Simulate VALUES() using SELECT .. UNION ALL SELECT .. where VALUES() is unavailable --- jOOQ/src/main/java/org/jooq/impl/Values.java | 57 +++++++++++++++++--- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/Values.java b/jOOQ/src/main/java/org/jooq/impl/Values.java index a5e8e42439..329a1d71dc 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Values.java +++ b/jOOQ/src/main/java/org/jooq/impl/Values.java @@ -39,6 +39,7 @@ import org.jooq.BindContext; import org.jooq.Record; import org.jooq.RenderContext; import org.jooq.Row; +import org.jooq.Select; import org.jooq.Support; import org.jooq.Table; import org.jooq.exception.DataAccessException; @@ -82,14 +83,58 @@ class Values extends AbstractTable { @Override public final void toSQL(RenderContext context) { - context.keyword("values"); + switch (context.getDialect()) { - String separator = ""; - for (Row row : rows) { - context.sql(separator) - .sql(row); + // [#915] Simulate VALUES(..) with SELECT .. UNION ALL SELECT .. + // for those dialects that do not support a VALUES() constructor + case FIREBIRD: + case ORACLE: + case SYBASE: { + Select selects = null; - separator = ", "; + for (Row row : rows) { + Select select = create().select(row.getFields()); + + if (selects == null) { + selects = select; + } + else { + selects = selects.unionAll(select); + } + } + + context.formatIndentStart() + .formatNewLine() + .sql(selects) + .formatIndentEnd() + .formatNewLine(); + break; + } + + // [#915] Native support of VALUES(..) + case CUBRID: + case DERBY: + case H2: + case HSQLDB: + case POSTGRES: + case SQLSERVER: + default: { + context.keyword("values") + .formatIndentLockStart(); + + boolean firstRow = true; + for (Row row : rows) { + if (!firstRow) { + context.sql(",").formatSeparator(); + } + + context.sql(row); + firstRow = false; + } + + context.formatIndentEnd(); + break; + } } }