diff --git a/jOOQ/src/main/java/org/jooq/impl/Greatest.java b/jOOQ/src/main/java/org/jooq/impl/Greatest.java index ec2e29f852..7ef9c63715 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Greatest.java +++ b/jOOQ/src/main/java/org/jooq/impl/Greatest.java @@ -54,15 +54,15 @@ import org.jooq.impl.QOM.UnmodifiableList; */ final class Greatest extends AbstractField implements QOM.Greatest { - private final QueryPartListView> args; + private final QueryPartListView> args; + @SuppressWarnings({ "unchecked", "rawtypes" }) Greatest(Field... args) { super(N_GREATEST, (DataType) Tools.nullSafeDataType(args[0])); - this.args = QueryPartListView.wrap(args); + this.args = (QueryPartListView) QueryPartListView.wrap(args); } - @SuppressWarnings("unchecked") @Override public final void accept(Context ctx) { @@ -73,9 +73,11 @@ final class Greatest extends AbstractField implements QOM.Greatest { } switch (ctx.family()) { - // This implementation has O(2^n) complexity. Better implementations - // are very welcome - // [#1049] TODO Fix this! + + + + + @@ -84,22 +86,7 @@ final class Greatest extends AbstractField implements QOM.Greatest { case DERBY: { - Field first = (Field) args.get(0); - Field other = (Field) args.get(1); - - if (args.size() > 2) { - Field[] remaining = args.subList(2, args.size()).toArray(Tools.EMPTY_FIELD); - - ctx.visit(DSL - .when(first.gt(other), DSL.greatest(first, remaining)) - .otherwise(DSL.greatest(other, remaining))); - } - else - ctx.visit(DSL - .when(first.gt(other), first) - .otherwise(other)); - - return; + GreatestLeast.acceptCaseEmulation(ctx, args, DSL::greatest, Field::gt); } case FIREBIRD: diff --git a/jOOQ/src/main/java/org/jooq/impl/GreatestLeast.java b/jOOQ/src/main/java/org/jooq/impl/GreatestLeast.java new file mode 100644 index 0000000000..bc6d994f18 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/GreatestLeast.java @@ -0,0 +1,98 @@ +/* + * 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 static org.jooq.impl.DSL.select; +import static org.jooq.impl.DSL.values; + +import java.util.function.BiFunction; +import java.util.function.Function; + +import org.jooq.Condition; +import org.jooq.Context; +import org.jooq.DataType; +import org.jooq.Field; +// ... +import org.jooq.Row1; + +/** + * @author Lukas Eder + */ +final class GreatestLeast { + + + + + + + + + + + + + + + + + + static final void acceptCaseEmulation( + Context ctx, + QueryPartListView> args, + BiFunction, ? super Field[], ? extends Field> greatestLeast, + BiFunction, ? super Field, ? extends Condition> gtLt + ) { + + // This implementation has O(2^n) complexity. Better implementations + // are very welcome + Field first = args.get(0); + Field other = args.get(1); + + if (args.size() > 2) { + Field[] remaining = args.subList(2, args.size()).toArray(Tools.EMPTY_FIELD); + + ctx.visit(DSL + .when(gtLt.apply(first, other), greatestLeast.apply(first, remaining)) + .otherwise(greatestLeast.apply(other, remaining))); + } + else + ctx.visit(DSL + .when(gtLt.apply(first, other), first) + .otherwise(other)); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Least.java b/jOOQ/src/main/java/org/jooq/impl/Least.java index 7e57c2944b..400973dd6b 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Least.java +++ b/jOOQ/src/main/java/org/jooq/impl/Least.java @@ -54,15 +54,15 @@ import org.jooq.impl.QOM.UnmodifiableList; */ final class Least extends AbstractField implements QOM.Least { - private final QueryPartListView> args; + private final QueryPartListView> args; + @SuppressWarnings({ "unchecked", "rawtypes" }) Least(Field... args) { super(N_LEAST, (DataType) Tools.nullSafeDataType(args[0])); - this.args = QueryPartListView.wrap(args); + this.args = (QueryPartListView) QueryPartListView.wrap(args); } - @SuppressWarnings("unchecked") @Override public final void accept(Context ctx) { @@ -73,8 +73,11 @@ final class Least extends AbstractField implements QOM.Least { } switch (ctx.family()) { - // This implementation has O(2^n) complexity. Better implementations - // are very welcome + + + + + @@ -83,21 +86,7 @@ final class Least extends AbstractField implements QOM.Least { case DERBY: { - Field first = (Field) args.get(0); - Field other = (Field) args.get(1); - - if (args.size() > 2) { - Field[] remaining = args.subList(2, args.size()).toArray(Tools.EMPTY_FIELD); - - ctx.visit(DSL - .when(first.lt(other), DSL.least(first, remaining)) - .otherwise(DSL.least(other, remaining))); - } - else - ctx.visit(DSL - .when(first.lt(other), first) - .otherwise(other)); - + GreatestLeast.acceptCaseEmulation(ctx, args, DSL::least, Field::lt); return; }