[jOOQ/jOOQ#11767] ROWNUM transformation should avoid creating derived tables if possible
This commit is contained in:
parent
bd5698d157
commit
6bdfdf22f9
90
jOOQ/src/main/java/org/jooq/impl/Finder.java
Normal file
90
jOOQ/src/main/java/org/jooq/impl/Finder.java
Normal file
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Other licenses:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Commercial licenses for this work are available. These replace the above
|
||||
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
|
||||
* database integrations.
|
||||
*
|
||||
* For more information, please visit: http://www.jooq.org/licenses
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.jooq.BindContext;
|
||||
import org.jooq.Configuration;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.Param;
|
||||
import org.jooq.QueryPart;
|
||||
import org.jooq.QueryPartInternal;
|
||||
|
||||
/**
|
||||
* A stub {@link BindContext} that acts as a collector of {@link Param}
|
||||
* {@link QueryPart}'s
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
final class Finder extends AbstractBindContext {
|
||||
|
||||
private final Predicate<? super QueryPart> find;
|
||||
private final Predicate<? super QueryPart> enter;
|
||||
private boolean found;
|
||||
|
||||
Finder(
|
||||
Configuration configuration,
|
||||
Predicate<? super QueryPart> find,
|
||||
Predicate<? super QueryPart> enter
|
||||
) {
|
||||
super(configuration, null);
|
||||
|
||||
this.find = find;
|
||||
this.enter = enter;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindInternal(QueryPartInternal internal) {
|
||||
if (found |= find.test(internal))
|
||||
return;
|
||||
|
||||
if (enter.test(internal))
|
||||
super.bindInternal(internal);
|
||||
}
|
||||
|
||||
final boolean found() {
|
||||
return found;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final BindContext bindValue0(Object value, Field<?> field) throws SQLException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@ -495,18 +495,38 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
|
||||
|
||||
|
||||
|
||||
private final SelectQueryImpl<R> copyTo(CopyClause clause, SelectQueryImpl<R> result) {
|
||||
return copyBetween(CopyClause.START, clause, result);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private final SelectQueryImpl<R> copyTo(CopyClause clause, boolean scalarSelect, SelectQueryImpl<R> result) {
|
||||
return copyBetween(CopyClause.START, clause, scalarSelect, result);
|
||||
}
|
||||
|
||||
private final SelectQueryImpl<R> copyAfter(CopyClause clause, SelectQueryImpl<R> result) {
|
||||
return copyBetween(clause, CopyClause.END, result);
|
||||
private final SelectQueryImpl<R> copyAfter(CopyClause clause, boolean scalarSelect, SelectQueryImpl<R> result) {
|
||||
return copyBetween(clause, CopyClause.END, scalarSelect, result);
|
||||
}
|
||||
|
||||
private final SelectQueryImpl<R> copyBetween(CopyClause start, CopyClause end, SelectQueryImpl<R> result) {
|
||||
private final SelectQueryImpl<R> copyBetween(CopyClause start, CopyClause end, boolean scalarSelect, SelectQueryImpl<R> result) {
|
||||
if (CopyClause.START.between(start, end)) {
|
||||
result.from.addAll(from);
|
||||
result.condition.setWhere(condition.getWhere());
|
||||
|
||||
if (scalarSelect)
|
||||
result.select.addAll(select);
|
||||
}
|
||||
|
||||
if (CopyClause.WHERE.between(start, end)) {
|
||||
@ -525,7 +545,9 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
|
||||
}
|
||||
|
||||
if (CopyClause.QUALIFY.between(start, end)) {
|
||||
result.select.addAll(select);
|
||||
if (!scalarSelect)
|
||||
result.select.addAll(select);
|
||||
|
||||
result.hint = hint;
|
||||
result.distinct = distinct;
|
||||
result.distinctOn = distinctOn;
|
||||
@ -561,7 +583,7 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
|
||||
}
|
||||
|
||||
private final SelectQueryImpl<R> copy(Function<? super SelectQueryImpl<R>, ? extends SelectQueryImpl<R>> finisher) {
|
||||
return finisher.apply(copyTo(CopyClause.END, new SelectQueryImpl<>(configuration(), with)));
|
||||
return finisher.apply(copyTo(CopyClause.END, false, new SelectQueryImpl<>(configuration(), with)));
|
||||
}
|
||||
|
||||
|
||||
@ -598,6 +620,11 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user