- Rename LEVEL to ROWNUM when users request this
- Generate_Series should alias its CTE emulation according to its alias
This commit is contained in:
Lukas Eder 2021-04-15 10:24:18 +02:00
parent 26fff069c8
commit 566d9739f5
2 changed files with 31 additions and 7 deletions

View File

@ -85,28 +85,44 @@ final class GenerateSeries extends AbstractTable<Record1<Integer>> implements Au
private final Field<Integer> from;
private final Field<Integer> to;
private final Field<Integer> step;
private final Name name;
GenerateSeries(Field<Integer> from, Field<Integer> to) {
this(from, to, null);
}
GenerateSeries(Field<Integer> from, Field<Integer> to, Field<Integer> step) {
super(TableOptions.expression(), N_GENERATE_SERIES);
this(from, to, step, N_GENERATE_SERIES);
}
GenerateSeries(Field<Integer> from, Field<Integer> to, Field<Integer> step, Name name) {
super(TableOptions.expression(), name);
this.from = from;
this.to = to;
this.step = step;
this.name = name;
}
@Override
public Table<Record1<Integer>> as(Name alias) {
return new TableAlias<>(new GenerateSeries(from, to, step, alias), alias);
}
@Override
public Table<Record1<Integer>> as(Name alias, Name... fieldAliases) {
return new TableAlias<>(new GenerateSeries(from, to, step, alias), alias, fieldAliases);
}
@Override
public final void accept(Context<?> ctx) {
if (EMULATE_WITH_RECURSIVE.contains(ctx.dialect())) {
Field<Integer> f = DSL.field(N_GENERATE_SERIES, INTEGER);
Field<Integer> f = DSL.field(name, INTEGER);
visitSubquery(
ctx,
withRecursive(N_GENERATE_SERIES, N_GENERATE_SERIES)
.as(select(from).unionAll(select(iadd(f, step == null ? inline(1) : step)).from(N_GENERATE_SERIES).where(f.lt(to))))
.select(f).from(N_GENERATE_SERIES)
withRecursive(name, name)
.as(select(from).unionAll(select(iadd(f, step == null ? inline(1) : step)).from(name).where(f.lt(to))))
.select(f).from(name)
);
}
else if (EMULATE_SYSTEM_RANGE.contains(ctx.dialect())) {
@ -163,9 +179,9 @@ final class GenerateSeries extends AbstractTable<Record1<Integer>> implements Au
@Override
public final Table<Record1<Integer>> autoAlias(Context<?> ctx) {
if (EMULATE_WITH_RECURSIVE.contains(ctx.dialect()))
return as(N_GENERATE_SERIES);
return as(name);
else if (EMULATE_SYSTEM_RANGE.contains(ctx.dialect()))
return as(N_GENERATE_SERIES, N_GENERATE_SERIES);
return as(name, name);
else
return null;
}

View File

@ -169,6 +169,7 @@ import static org.jooq.impl.Keywords.K_WITH_CHECK_OPTION;
import static org.jooq.impl.Keywords.K_WITH_READ_ONLY;
import static org.jooq.impl.Names.N_DUAL;
import static org.jooq.impl.Names.N_LEVEL;
import static org.jooq.impl.Names.N_ROWNUM;
import static org.jooq.impl.QueryPartCollectionView.wrap;
import static org.jooq.impl.SQLDataType.JSON;
import static org.jooq.impl.SQLDataType.JSONB;
@ -1485,6 +1486,13 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp