From 51ebec9fc5c0dec353ec2fc0985f36a393af9129 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 14 Jan 2021 15:57:49 +0100 Subject: [PATCH] [jOOQ/jOOQ#11232] Support parsing MINVALUE() and MAXVALUE() --- jOOQ/src/main/java/org/jooq/impl/DSL.java | 16 +-- .../src/main/java/org/jooq/impl/Greatest.java | 5 +- jOOQ/src/main/java/org/jooq/impl/Least.java | 5 +- jOOQ/src/main/java/org/jooq/impl/Names.java | 2 + .../main/java/org/jooq/impl/ParserImpl.java | 9 +- jOOQ/src/main/java/org/jooq/impl/Square.java | 113 ++++++++++++++++++ 6 files changed, 132 insertions(+), 18 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/Square.java diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index f46ab798ff..56bc8f98b8 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -89,7 +89,6 @@ import static org.jooq.SQLDialect.SQLITE; // ... // ... // ... -import static org.jooq.impl.Internal.imul; import static org.jooq.impl.Keywords.K_CUBE; import static org.jooq.impl.Keywords.K_GROUPING_SETS; import static org.jooq.impl.Names.N_IF; @@ -144,12 +143,7 @@ import java.util.function.Function; import javax.sql.DataSource; import org.jooq.AggregateFunction; -import org.jooq.AlterIndexOnStep; -import org.jooq.AlterIndexStep; -import org.jooq.AlterSequenceStep; import org.jooq.AlterTableStep; -import org.jooq.AlterTypeStep; -import org.jooq.AlterViewStep; import org.jooq.ArrayAggOrderByStep; // ... import org.jooq.Asterisk; @@ -162,7 +156,6 @@ import org.jooq.CharacterSet; import org.jooq.CloseableDSLContext; import org.jooq.Collation; import org.jooq.Comment; -import org.jooq.CommentOnIsStep; import org.jooq.CommonTableExpression; import org.jooq.Condition; import org.jooq.Configuration; @@ -193,7 +186,6 @@ import org.jooq.ConstraintForeignKeyReferencesStep9; import org.jooq.ConstraintForeignKeyReferencesStepN; import org.jooq.ConstraintTypeStep; // ... -import org.jooq.CreateIndexStep; import org.jooq.CreateTableColumnStep; import org.jooq.CreateTypeStep; import org.jooq.CreateViewAsStep; @@ -205,10 +197,7 @@ import org.jooq.Delete; import org.jooq.DeleteUsingStep; import org.jooq.DerivedColumnList; import org.jooq.Domain; -import org.jooq.DropIndexOnStep; -import org.jooq.DropTableStep; import org.jooq.DropTypeStep; -import org.jooq.DropViewFinalStep; // ... import org.jooq.False; import org.jooq.Field; @@ -367,7 +356,6 @@ import org.jooq.Support; import org.jooq.Table; import org.jooq.TableLike; import org.jooq.True; -import org.jooq.TruncateIdentityStep; import org.jooq.UDTRecord; import org.jooq.Update; import org.jooq.UpdateSetFirstStep; @@ -20570,6 +20558,7 @@ public class DSL { */ @NotNull @Support + @SafeVarargs public static Field greatest(T value, T... values) { return greatest(Tools.field(value), Tools.fieldsArray(values)); } @@ -20585,6 +20574,7 @@ public class DSL { */ @NotNull @Support + @SafeVarargs public static Field greatest(Field field, Field... others) { return new Greatest<>(Tools.nullSafeDataType(field), Tools.nullSafe(combine(field, others))); } @@ -20602,6 +20592,7 @@ public class DSL { */ @NotNull @Support + @SafeVarargs public static Field least(T value, T... values) { return least(Tools.field(value), Tools.fieldsArray(values)); } @@ -20617,6 +20608,7 @@ public class DSL { */ @NotNull @Support + @SafeVarargs public static Field least(Field field, Field... others) { return new Least<>(Tools.nullSafeDataType(field), Tools.nullSafe(combine(field, others))); } diff --git a/jOOQ/src/main/java/org/jooq/impl/Greatest.java b/jOOQ/src/main/java/org/jooq/impl/Greatest.java index 0c5f3f6553..8ea2180c53 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Greatest.java +++ b/jOOQ/src/main/java/org/jooq/impl/Greatest.java @@ -39,6 +39,7 @@ package org.jooq.impl; import static org.jooq.impl.DSL.function; import static org.jooq.impl.Names.N_GREATEST; +import static org.jooq.impl.Names.N_MAXVALUE; import org.jooq.Context; import org.jooq.DataType; @@ -106,7 +107,7 @@ final class Greatest extends AbstractField { } case FIREBIRD: - ctx.visit(function("maxvalue", getDataType(), args)); + ctx.visit(function(N_MAXVALUE, getDataType(), args)); return; case SQLITE: @@ -114,7 +115,7 @@ final class Greatest extends AbstractField { return; default: - ctx.visit(function("greatest", getDataType(), args)); + ctx.visit(function(N_GREATEST, getDataType(), args)); return; } } diff --git a/jOOQ/src/main/java/org/jooq/impl/Least.java b/jOOQ/src/main/java/org/jooq/impl/Least.java index 156350e060..b61400f929 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Least.java +++ b/jOOQ/src/main/java/org/jooq/impl/Least.java @@ -39,6 +39,7 @@ package org.jooq.impl; import static org.jooq.impl.DSL.function; import static org.jooq.impl.Names.N_LEAST; +import static org.jooq.impl.Names.N_MINVALUE; import org.jooq.Context; import org.jooq.DataType; @@ -105,7 +106,7 @@ final class Least extends AbstractField { } case FIREBIRD: - ctx.visit(function("minvalue", getDataType(), args)); + ctx.visit(function(N_MINVALUE, getDataType(), args)); return; case SQLITE: @@ -113,7 +114,7 @@ final class Least extends AbstractField { return; default: - ctx.visit(function("least", getDataType(), args)); + ctx.visit(function(N_LEAST, getDataType(), args)); return; } } diff --git a/jOOQ/src/main/java/org/jooq/impl/Names.java b/jOOQ/src/main/java/org/jooq/impl/Names.java index f162a1b896..8df7d6884c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Names.java +++ b/jOOQ/src/main/java/org/jooq/impl/Names.java @@ -186,9 +186,11 @@ final class Names { static final Name N_LOWER = unquotedName("lower"); static final Name N_LPAD = unquotedName("lpad"); static final Name N_LTRIM = unquotedName("ltrim"); + static final Name N_MAXVALUE = unquotedName("maxvalue"); static final Name N_MD5 = unquotedName("md5"); static final Name N_MEDIAN = unquotedName("median"); static final Name N_MID = unquotedName("mid"); + static final Name N_MINVALUE = unquotedName("minvalue"); static final Name N_MOD = unquotedName("mod"); static final Name N_MODE = unquotedName("mode"); static final Name N_NANO100_BETWEEN = unquotedName("nano100_between"); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index 5ac537a56e..42e63999f2 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -6960,6 +6960,11 @@ final class ParserContext { else if (parseFunctionNameIf("MD5")) return md5((Field) parseFieldParenthesised(S)); + if ((field = parseFieldGreatestIf()) != null) + return field; + else if ((field = parseFieldLeastIf()) != null) + return field; + break; case 'N': @@ -8109,7 +8114,7 @@ final class ParserContext { } private final Field parseFieldLeastIf() { - if (parseFunctionNameIf("LEAST")) { + if (parseFunctionNameIf("LEAST", "MINVALUE")) { parse('('); List> fields = parseFields(); parse(')'); @@ -8121,7 +8126,7 @@ final class ParserContext { } private final Field parseFieldGreatestIf() { - if (parseFunctionNameIf("GREATEST")) { + if (parseFunctionNameIf("GREATEST", "MAXVALUE")) { parse('('); List> fields = parseFields(); parse(')'); diff --git a/jOOQ/src/main/java/org/jooq/impl/Square.java b/jOOQ/src/main/java/org/jooq/impl/Square.java new file mode 100644 index 0000000000..8f88176bdc --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Square.java @@ -0,0 +1,113 @@ +/* + * 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.*; +import static org.jooq.impl.Internal.*; +import static org.jooq.impl.Keywords.*; +import static org.jooq.impl.Names.*; +import static org.jooq.impl.SQLDataType.*; +import static org.jooq.impl.Tools.*; +import static org.jooq.impl.Tools.BooleanDataKey.*; +import static org.jooq.SQLDialect.*; + +import org.jooq.*; +import org.jooq.conf.*; +import org.jooq.impl.*; +import org.jooq.tools.*; + +import java.util.*; + + +/** + * The SQUARE statement. + */ +@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) +final class Square +extends + AbstractField +{ + + private static final long serialVersionUID = 1L; + + private final Field value; + + Square( + Field value + ) { + super( + N_SQUARE, + allNotNull((DataType) dataType(INTEGER, value, false), value) + ); + + this.value = nullSafeNotNull(value, INTEGER); + } + + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + + + private static final Set NO_SUPPORT_SQUARE = SQLDialect.supportedBy(CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE); + + @Override + public final void accept(Context ctx) { + if (NO_SUPPORT_SQUARE.contains(ctx.dialect())) + ctx.visit(value.times(value)); + else + ctx.visit(N_SQUARE).sql('(').visit(value).sql(')'); + } + + + + // ------------------------------------------------------------------------- + // The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof Square) { + return + StringUtils.equals(value, ((Square) that).value) + ; + } + else + return super.equals(that); + } +}