[jOOQ/jOOQ#10181] Prefer Select.asTable(X) over table(Select).as(X)

This commit is contained in:
Lukas Eder 2020-05-08 22:24:12 +02:00
parent 7950b49fe0
commit 874ddfb25e
8 changed files with 14 additions and 18 deletions

View File

@ -1093,17 +1093,17 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
@Override
public /* non-final */ Table<R> where(Condition condition) {
return table(selectFrom(this).where(condition)).as(this);
return selectFrom(this).where(condition).asTable(this);
}
@Override
public /* non-final */ Table<R> where(Condition... conditions) {
return table(selectFrom(this).where(conditions)).as(this);
return selectFrom(this).where(conditions).asTable(this);
}
@Override
public /* non-final */ Table<R> where(Collection<? extends Condition> conditions) {
return table(selectFrom(this).where(conditions)).as(this);
return selectFrom(this).where(conditions).asTable(this);
}
@Override

View File

@ -138,20 +138,17 @@ final class ArrayTableEmulation extends AbstractTable<Record> {
Field<?> val = DSL.val(element, field.fields[0].getDataType());
Select<Record> subselect = using(configuration).select(val.as("COLUMN_VALUE")).select();
if (select == null) {
if (select == null)
select = subselect;
}
else {
else
select = select.unionAll(subselect);
}
}
// Empty arrays should result in empty tables
if (select == null) {
if (select == null)
select = using(configuration).select(one().as("COLUMN_VALUE")).select().where(falseCondition());
}
table = DSL.table(select).as(alias);
table = select.asTable(alias);
}
return table;

View File

@ -575,7 +575,7 @@ final class CreateTableImpl extends AbstractRowCountQuery implements
ctx.start(CREATE_TABLE_AS);
if (!columnFields.isEmpty() && NO_SUPPORT_CTAS_COLUMN_NAMES.contains(ctx.family()))
ctx.visit(select(asterisk()).from(table(select).as(table(name("t")), columnFields.toArray(EMPTY_FIELD))));
ctx.visit(select(asterisk()).from(select.asTable(table(name("t")), columnFields.toArray(EMPTY_FIELD))));
else
ctx.visit(select);

View File

@ -58,7 +58,6 @@ import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.conf.ParamType.INLINED;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.selectFrom;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.Keywords.K_ALTER;
import static org.jooq.impl.Keywords.K_AS;
import static org.jooq.impl.Keywords.K_CREATE;
@ -285,7 +284,7 @@ final class CreateViewImpl<R extends Record> extends AbstractRowCountQuery imple
.paramType(INLINED)
.visit(
rename && !renameSupported
? selectFrom(table(parsed()).as(name("t"), Tools.fieldNames(f)))
? selectFrom(parsed().asTable(name("t"), Tools.fieldNames(f)))
: select)
.paramType(paramType)
.end(CREATE_VIEW_AS);

View File

@ -44,7 +44,6 @@ import static org.jooq.Clause.INSERT_VALUES;
import static org.jooq.SQLDialect.POSTGRES;
// ...
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.Keywords.K_DEFAULT_VALUES;
import static org.jooq.impl.Keywords.K_VALUES;
import static org.jooq.impl.Tools.BooleanDataKey.DATA_EMULATE_BULK_INSERT_RETURNING;

View File

@ -769,7 +769,7 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
return configuration.dsl()
.insertInto(table())
.columns(insertMaps.fields())
.select(selectFrom(DSL.table(rows).as("t")));
.select(selectFrom(rows.asTable("t")));
}
else {
throw new IllegalStateException("The ON DUPLICATE KEY IGNORE/UPDATE clause cannot be emulated when inserting into tables without any known keys : " + table());
@ -784,7 +784,7 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
// [#6375] INSERT .. VALUES and INSERT .. SELECT distinction also in MERGE
Table<?> t = select == null
? dual()
: DSL.table(select).as("t", fieldNameStrings(insertMaps.fields().toArray(EMPTY_FIELD)));
: select.asTable("t", fieldNameStrings(insertMaps.fields().toArray(EMPTY_FIELD)));
MergeOnConditionStep<R> on = select == null
? configuration.dsl().mergeInto(table())

View File

@ -2063,6 +2063,7 @@ final class ParserImpl implements Parser {
return map;
}
@SuppressWarnings("null")
private static final Merge<?> parseMerge(ParserContext ctx, WithImpl with) {
parseKeyword(ctx, "MERGE");
parseKeywordIf(ctx, "INTO");
@ -2085,7 +2086,7 @@ final class ParserImpl implements Parser {
TableLike<?> usingTable = (table != null ? table : using);
if (parseKeywordIf(ctx, "AS") || !peekKeyword(ctx, "ON"))
usingTable = (table != null ? table : DSL.table(using)).as(parseIdentifier(ctx));
usingTable = usingTable.asTable(parseIdentifier(ctx));
parseKeyword(ctx, "ON");
Condition on = parseCondition(ctx);

View File

@ -872,7 +872,7 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
&& REQUIRES_DERIVED_TABLE_DML.contains(context.family())
&& (dmlTable = (Table<?>) context.data(DATA_DML_TARGET_TABLE)) != null
&& containsTable(dmlTable)) {
context.visit(DSL.select(asterisk()).from(DSL.table(this).as("t")));
context.visit(DSL.select(asterisk()).from(asTable("t")));
}