[jOOQ/jOOQ#13335] Add Table::as and TableLike::asTable overloads

accepting Collection<? extends X> for derived column lists
This commit is contained in:
Lukas Eder 2022-03-24 14:48:21 +01:00
parent 4561d1607c
commit bce4e68e51
5 changed files with 218 additions and 8 deletions

View File

@ -446,6 +446,61 @@ extends
@Support
Table<R> as(String alias, String... fieldAliases);
/**
* Create an alias for this table and its fields.
* <p>
* Note that the case-sensitivity of the returned table and columns depends
* on {@link Settings#getRenderQuotedNames()}. By default, table aliases are
* quoted, and thus case-sensitive in many SQL dialects!
* <p>
* <h5>Derived column lists for table references</h5>
* <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. In other words,
* the following statements are equivalent: <code><pre>
* -- Using derived column lists to rename columns (e.g. Postgres)
* SELECT t.a, t.b
* FROM my_table t(a, b)
*
* -- Nesting table references within derived tables (e.g. SQL Server)
* SELECT t.a, t.b
* FROM (
* SELECT * FROM my_table
* ) t(a, b)
* </pre></code>
* <p>
* <h5>Derived column lists for derived tables</h5>
* <p>
* Other databases may not support derived column lists at all, but they do
* support common table expressions. The following statements are
* equivalent: <code><pre>
* -- Using derived column lists to rename columns (e.g. Postgres)
* SELECT t.a, t.b
* FROM (
* SELECT 1, 2
* ) AS t(a, b)
*
* -- Using UNION ALL to produce column names (e.g. MySQL)
* SELECT t.a, t.b
* FROM (
* SELECT null a, null b FROM DUAL WHERE 1 = 0
* UNION ALL
* SELECT 1, 2 FROM DUAL
* ) t
* </pre></code>
*
* @param alias The alias name
* @param fieldAliases The field aliases. Excess aliases are ignored,
* missing aliases will be substituted by this table's field
* names.
* @return The table alias
*/
@NotNull
@Support
Table<R> as(String alias, Collection<? extends String> fieldAliases);
/**
* Create an alias for this table and its fields.
* <p>
@ -574,6 +629,65 @@ extends
@Support
Table<R> as(Name alias, Name... fieldAliases);
/**
* Create an alias for this table and its fields.
* <p>
* Note that the case-sensitivity of the returned table depends on
* {@link Settings#getRenderQuotedNames()} and the {@link Name}. By default,
* table aliases are quoted, and thus case-sensitive in many SQL dialects -
* use {@link DSL#unquotedName(String...)} for case-insensitive aliases.
* <p>
* If the argument {@link Name#getName()} is qualified, then the
* {@link Name#last()} part will be used.
* <p>
* <h5>Derived column lists for table references</h5>
* <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. In other words,
* the following statements are equivalent: <code><pre>
* -- Using derived column lists to rename columns (e.g. Postgres)
* SELECT t.a, t.b
* FROM my_table t(a, b)
*
* -- Nesting table references within derived tables (e.g. SQL Server)
* SELECT t.a, t.b
* FROM (
* SELECT * FROM my_table
* ) t(a, b)
* </pre></code>
* <p>
* <h5>Derived column lists for derived tables</h5>
* <p>
* Other databases may not support derived column lists at all, but they do
* support common table expressions. The following statements are
* equivalent: <code><pre>
* -- Using derived column lists to rename columns (e.g. Postgres)
* SELECT t.a, t.b
* FROM (
* SELECT 1, 2
* ) AS t(a, b)
*
* -- Using UNION ALL to produce column names (e.g. MySQL)
* SELECT t.a, t.b
* FROM (
* SELECT null a, null b FROM DUAL WHERE 1 = 0
* UNION ALL
* SELECT 1, 2 FROM DUAL
* ) t
* </pre></code>
*
* @param alias The alias name
* @param fieldAliases The field aliases. Excess aliases are ignored,
* missing aliases will be substituted by this table's field
* names.
* @return The table alias
*/
@NotNull
@Support
Table<R> as(Name alias, Collection<? extends Name> fieldAliases);
/**
* Create an alias for this table and its fields.
* <p>
@ -646,6 +760,18 @@ extends
@Support
Table<R> as(Table<?> otherTable, Field<?>... otherFields);
/**
* Create an alias for this table based on another table's name.
*
* @param otherTable The other table whose name this table is aliased with.
* @param otherFields The other fields whose field name this table's fields
* are aliased with.
* @return The table alias.
*/
@NotNull
@Support
Table<R> as(Table<?> otherTable, Collection<? extends Field<?>> otherFields);
/**
* Create an alias for this table and its fields.
* <p>

View File

@ -37,6 +37,7 @@
*/
package org.jooq;
import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.Function;
@ -87,6 +88,15 @@ extends
@Support
Table<R> asTable(String alias, String... fieldAliases);
/**
* The underlying aliased table representation of this object.
*
* @see Table#as(String, Collection)
*/
@NotNull
@Support
Table<R> asTable(String alias, Collection<? extends String> fieldAliases);
/**
* The underlying aliased table representation of this object.
*
@ -99,7 +109,7 @@ extends
/**
* The underlying aliased table representation of this object.
*
* @see Table#as(String, String...)
* @see Table#as(Name, Name...)
*/
@NotNull
@Support
@ -108,7 +118,16 @@ extends
/**
* The underlying aliased table representation of this object.
*
* @see Table#as(Name)
* @see Table#as(Name, Collection)
*/
@NotNull
@Support
Table<R> asTable(Name alias, Collection<? extends Name> fieldAliases);
/**
* The underlying aliased table representation of this object.
*
* @see Table#as(Table)
*/
@NotNull
@Support
@ -117,12 +136,21 @@ extends
/**
* The underlying aliased table representation of this object.
*
* @see Table#as(String, String...)
* @see Table#as(Table, Field...)
*/
@NotNull
@Support
Table<R> asTable(Table<?> alias, Field<?>... fieldAliases);
/**
* The underlying aliased table representation of this object.
*
* @see Table#as(Table, Collection)
*/
@NotNull
@Support
Table<R> asTable(Table<?> alias, Collection<? extends Field<?>> fieldAliases);
/**
* The underlying aliased table representation of this object.
*

View File

@ -61,6 +61,7 @@ import static org.jooq.impl.DSL.sql;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.DSL.val;
import static org.jooq.impl.Tools.EMPTY_FIELD;
import static org.jooq.impl.Tools.EMPTY_NAME;
import static org.jooq.impl.Tools.map;
import static org.jooq.impl.Tools.traverseJoins;
@ -315,6 +316,11 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(String alias, Collection<? extends String> fieldAliases) {
return as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Name alias) {
return as(alias);
@ -325,6 +331,11 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Name alias, Collection<? extends Name> fieldAliases) {
return as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Table<?> alias) {
return as(alias);
@ -335,6 +346,11 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Table<?> alias, Collection<? extends Field<?>> fieldAliases) {
return as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(String alias, Function<? super Field<?>, ? extends String> aliasFunction) {
return as(alias, aliasFunction);
@ -355,6 +371,11 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return as(DSL.name(alias), Tools.names(fieldAliases));
}
@Override
public /* non-final for covariant overriding */ Table<R> as(String alias, Collection<? extends String> fieldAliases) {
return as(DSL.name(alias), Tools.names(fieldAliases));
}
@Override
public final Table<R> as(String alias, Function<? super Field<?>, ? extends String> aliasFunction) {
return as(alias, map(fields(), f -> aliasFunction.apply(f), String[]::new));
@ -375,6 +396,11 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return new TableAlias<>(this, alias, fieldAliases);
}
@Override
public /* non-final for covariant overriding */ Table<R> as(Name alias, Collection<? extends Name> fieldAliases) {
return new TableAlias<>(this, alias, fieldAliases == null ? null : fieldAliases.toArray(EMPTY_NAME));
}
@Override
public final Table<R> as(Name alias, Function<? super Field<?>, ? extends Name> aliasFunction) {
return as(alias, map(fields(), f -> aliasFunction.apply(f), Name[]::new));
@ -960,6 +986,11 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return as(otherTable.getUnqualifiedName(), Tools.map(otherFields, Field::getUnqualifiedName, Name[]::new));
}
@Override
public final Table<R> as(Table<?> otherTable, Collection<? extends Field<?>> otherFields) {
return as(otherTable.getUnqualifiedName(), Tools.map(otherFields, Field::getUnqualifiedName));
}
@Override
public final Table<R> as(Table<?> otherTable, Function<? super Field<?>, ? extends Field<?>> aliasFunction) {
return as(otherTable.getUnqualifiedName(), f -> aliasFunction.apply(f).getUnqualifiedName());

View File

@ -68,13 +68,11 @@ import org.jooq.JoinType;
import org.jooq.Name;
import org.jooq.Operator;
import org.jooq.OrderField;
import org.jooq.Param;
// ...
import org.jooq.QuantifiedSelect;
import org.jooq.QueryPart;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.ResultQuery;
import org.jooq.Results;
import org.jooq.Row;
import org.jooq.SQL;
@ -136,8 +134,6 @@ import org.jooq.WindowDefinition;
import org.jooq.impl.QOM.UnmodifiableList;
import org.jooq.impl.QOM.With;
import org.jetbrains.annotations.NotNull;
/**
* A wrapper for a {@link SelectQuery}
*
@ -2875,6 +2871,11 @@ implements
return getDelegate().asTable(alias, fieldAliases);
}
@Override
public final Table<R> asTable(String alias, Collection<? extends String> fieldAliases) {
return getDelegate().asTable(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Name alias) {
return getDelegate().asTable(alias);
@ -2885,6 +2886,11 @@ implements
return getDelegate().asTable(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Name alias, Collection<? extends Name> fieldAliases) {
return getDelegate().asTable(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Table<?> alias) {
return getDelegate().asTable(alias);
@ -2895,6 +2901,11 @@ implements
return getDelegate().asTable(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Table<?> alias, Collection<? extends Field<?>> fieldAliases) {
return getDelegate().asTable(alias, fieldAliases);
}
@Override
public final Table<R> asTable(String alias, Function<? super Field<?>, ? extends String> aliasFunction) {
return getDelegate().asTable(alias, aliasFunction);

View File

@ -279,7 +279,6 @@ import org.jooq.TableOptionalOnStep;
import org.jooq.TablePartitionByStep;
// ...
// ...
// ...
import org.jooq.WindowDefinition;
import org.jooq.XML;
import org.jooq.exception.DataAccessException;
@ -665,6 +664,11 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
return new DerivedTable<>(this).as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(String alias, Collection<? extends String> fieldAliases) {
return new DerivedTable<>(this).as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Name alias) {
return new DerivedTable<>(this).as(alias);
@ -675,6 +679,11 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
return new DerivedTable<>(this).as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Name alias, Collection<? extends Name> fieldAliases) {
return new DerivedTable<>(this).as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Table<?> alias) {
return new DerivedTable<>(this).as(alias);
@ -685,6 +694,11 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
return new DerivedTable<>(this).as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(Table<?> alias, Collection<? extends Field<?>> fieldAliases) {
return new DerivedTable<>(this).as(alias, fieldAliases);
}
@Override
public final Table<R> asTable(String alias, Function<? super Field<?>, ? extends String> aliasFunction) {
return new DerivedTable<>(this).as(alias, aliasFunction);