[#1801] Add Table.as(String, String...) to allow for creating a
table aliases (correlation names) with derived column lists
This commit is contained in:
parent
c9d12d8850
commit
6ea236e217
@ -123,7 +123,7 @@ public interface Table<R extends Record> extends FieldProvider, TableLike<R> {
|
||||
* names.
|
||||
* @return The table alias
|
||||
*/
|
||||
@Support({ CUBRID, DERBY, FIREBIRD, HSQLDB, POSTGRES, SQLSERVER, SYBASE })
|
||||
@Support({ CUBRID, DERBY, FIREBIRD, HSQLDB, ORACLE, POSTGRES, SQLSERVER, SYBASE })
|
||||
Table<R> as(String alias, String... fieldAliases);
|
||||
|
||||
/**
|
||||
|
||||
@ -35,7 +35,14 @@
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
import org.jooq.conf.Settings;
|
||||
import static org.jooq.SQLDialect.CUBRID;
|
||||
import static org.jooq.SQLDialect.DERBY;
|
||||
import static org.jooq.SQLDialect.FIREBIRD;
|
||||
import static org.jooq.SQLDialect.HSQLDB;
|
||||
import static org.jooq.SQLDialect.ORACLE;
|
||||
import static org.jooq.SQLDialect.POSTGRES;
|
||||
import static org.jooq.SQLDialect.SQLSERVER;
|
||||
import static org.jooq.SQLDialect.SYBASE;
|
||||
|
||||
/**
|
||||
* An object that can behave like a table (a table-like object)
|
||||
@ -55,35 +62,16 @@ public interface TableLike<R extends Record> extends QueryPart {
|
||||
|
||||
/**
|
||||
* The underlying aliased table representation of this object
|
||||
* <p>
|
||||
* This method is useful for things like
|
||||
* <code>SELECT * FROM (SELECT * FROM x WHERE x.a = '1') [alias] WHERE ... </code>
|
||||
* <p>
|
||||
* Note that the case-sensitivity of the returned table depends on
|
||||
* {@link Settings#getRenderNameStyle()}. By default, table aliases are
|
||||
* quoted, and thus case-sensitive!
|
||||
*
|
||||
* @see Table#as(String)
|
||||
*/
|
||||
Table<R> asTable(String alias);
|
||||
|
||||
/**
|
||||
* The underlying aliased table representation of this object
|
||||
* <p>
|
||||
* This method is useful for things like
|
||||
* <code><pre>
|
||||
* SELECT alias.fieldAlias1, alias.fieldAlias2
|
||||
* FROM (
|
||||
* SELECT * FROM x WHERE x.a = '1'
|
||||
* ) AS alias(fieldAlias1, fieldAlias2)
|
||||
* WHERE ... </code>
|
||||
* <p>
|
||||
* Note, not all databases support derived column lists for their table
|
||||
* aliases. On the other hand, some databases do support derived column
|
||||
* lists, but only for derived tables. jOOQ will try to turn table
|
||||
* references into derived tables to make this syntax work.
|
||||
* <p>
|
||||
* Note that the case-sensitivity of the returned table depends on
|
||||
* {@link Settings#getRenderNameStyle()}. By default, table aliases are
|
||||
* quoted, and thus case-sensitive!
|
||||
*
|
||||
* @see Table#as(String, String...)
|
||||
*/
|
||||
@Support({ CUBRID, DERBY, FIREBIRD, HSQLDB, ORACLE, POSTGRES, SQLSERVER, SYBASE })
|
||||
Table<R> asTable(String alias, String... fieldAliases);
|
||||
}
|
||||
|
||||
@ -95,14 +95,17 @@ class Alias<Q extends QueryPart> extends AbstractQueryPart {
|
||||
// [#1801] Some databases don't support "derived column names" at
|
||||
// all. They can be simulated using common table expressions
|
||||
if (fieldAliases != null && simulateDerivedColumnList) {
|
||||
context.keyword("(with")
|
||||
.sql(" v");
|
||||
context.sql("(").formatIndentStart().formatNewLine()
|
||||
.keyword("with").sql(" v");
|
||||
toSQLDerivedColumnList(context);
|
||||
context.keyword(" as (select * from ")
|
||||
context.sql(" ").keyword("as").sql(" (").formatIndentStart().formatNewLine()
|
||||
.keyword("select * from").sql(" (")
|
||||
.sql(wrapped)
|
||||
.sql(") ")
|
||||
.sql(")").formatIndentEnd().formatNewLine()
|
||||
.sql(")").formatSeparator()
|
||||
.keyword("select * from")
|
||||
.sql(" v)");
|
||||
.sql(" v").formatIndentEnd().formatNewLine()
|
||||
.sql(")");
|
||||
}
|
||||
|
||||
// [#1801] Some databases don't allow "derived column names" in
|
||||
@ -113,8 +116,10 @@ class Alias<Q extends QueryPart> extends AbstractQueryPart {
|
||||
// http://jira.cubrid.org/browse/ENGINE-96
|
||||
// http://tracker.firebirdsql.org/browse/CORE-4025
|
||||
else if (fieldAliases != null && asList(CUBRID, FIREBIRD, SQLSERVER, SYBASE).contains(dialect) && wrapped instanceof TableImpl) {
|
||||
context.keyword("(select * from ")
|
||||
context.sql("(").formatIndentStart().formatNewLine()
|
||||
.keyword("select * from")
|
||||
.sql(wrapped)
|
||||
.formatIndentEnd().formatNewLine()
|
||||
.sql(")");
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user