From 7883dd3e89a5ebb31f97948fc60e4f12747555e7 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 28 Apr 2021 13:32:00 +0200 Subject: [PATCH] [jOOQ/jOOQ#11656] Added more dialect support for TO_HEX - Rename function to DSL.toHex() - Support H2, Oracle, PostgreSQL, SQLite, SQLServer --- jOOQ/src/main/java/org/jooq/impl/DSL.java | 44 ++--- jOOQ/src/main/java/org/jooq/impl/Names.java | 2 + .../main/java/org/jooq/impl/ParserImpl.java | 4 +- jOOQ/src/main/java/org/jooq/impl/ToHex.java | 132 ++++++++++++++ jOOQ/src/main/java/org/jooq/impl/Uuid.java | 164 ++++++++++++++++++ 5 files changed, 322 insertions(+), 24 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/ToHex.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/Uuid.java diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index ed82f615ff..f38bddb383 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -16980,28 +16980,6 @@ public class DSL { return new Digits(value); } - /** - * The HEX function. - *

- * Format a number to its hex value. - */ - @NotNull - @Support({ MYSQL }) - public static Field hex(Number value) { - return new Hex(Tools.field(value)); - } - - /** - * The HEX function. - *

- * Format a number to its hex value. - */ - @NotNull - @Support({ MYSQL }) - public static Field hex(Field value) { - return new Hex(value); - } - /** * The LEFT function. *

@@ -18406,6 +18384,28 @@ public class DSL { return new ToDate(value, formatMask); } + /** + * The TO_HEX function. + *

+ * Format a number to its hex value. + */ + @NotNull + @Support({ H2, MARIADB, MYSQL, POSTGRES, SQLITE }) + public static Field toHex(Number value) { + return new ToHex(Tools.field(value)); + } + + /** + * The TO_HEX function. + *

+ * Format a number to its hex value. + */ + @NotNull + @Support({ H2, MARIADB, MYSQL, POSTGRES, SQLITE }) + public static Field toHex(Field value) { + return new ToHex(value); + } + /** * The TO_TIMESTAMP function. *

diff --git a/jOOQ/src/main/java/org/jooq/impl/Names.java b/jOOQ/src/main/java/org/jooq/impl/Names.java index 1fb38ec290..9dca96d037 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Names.java +++ b/jOOQ/src/main/java/org/jooq/impl/Names.java @@ -142,6 +142,7 @@ final class Names { static final Name N_EXP = unquotedName("exp"); static final Name N_EXTRACT = unquotedName("extract"); static final Name N_FIELD = unquotedName("field"); + static final Name N_FORMAT = unquotedName("format"); static final Name N_FLASHBACK = unquotedName("flashback"); static final Name N_FLOOR = unquotedName("floor"); static final Name N_FUNCTION = unquotedName("function"); @@ -237,6 +238,7 @@ final class Names { static final Name N_PLPGSQL = unquotedName("plpgsql"); static final Name N_POSITION = unquotedName("position"); static final Name N_POWER = unquotedName("power"); + static final Name N_PRINTF = unquotedName("printf"); static final Name N_PRIOR = unquotedName("prior"); static final Name N_PRODUCT = unquotedName("product"); static final Name N_RADIANS = unquotedName("radians"); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index e65594ef97..b2410f9a87 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -7318,7 +7318,7 @@ final class DefaultParseContext extends AbstractScope implements ParseContext { if (parseFunctionNameIf("HASH_MD5")) return md5((Field) parseFieldParenthesised(S)); else if (parseFunctionNameIf("HEX")) - return hex((Field) parseFieldParenthesised(N)); + return toHex((Field) parseFieldParenthesised(N)); break; @@ -7606,7 +7606,7 @@ final class DefaultParseContext extends AbstractScope implements ParseContext { else if ((field = parseFieldToCharIf()) != null) return field; else if (parseFunctionNameIf("TO_HEX")) - return hex((Field) parseFieldParenthesised(N)); + return toHex((Field) parseFieldParenthesised(N)); if (N.is(type)) if (parseFunctionNameIf("TANH")) diff --git a/jOOQ/src/main/java/org/jooq/impl/ToHex.java b/jOOQ/src/main/java/org/jooq/impl/ToHex.java new file mode 100644 index 0000000000..6eb076595e --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/ToHex.java @@ -0,0 +1,132 @@ +/* + * 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.impl.Tools.DataExtendedKey.*; +import static org.jooq.impl.Tools.DataKey.*; +import static org.jooq.SQLDialect.*; + +import org.jooq.*; +import org.jooq.Record; +import org.jooq.conf.*; +import org.jooq.impl.*; +import org.jooq.tools.*; + +import java.util.*; + + +/** + * The TO HEX statement. + */ +@SuppressWarnings({ "rawtypes", "unused" }) +final class ToHex +extends + AbstractField +{ + + private static final long serialVersionUID = 1L; + + private final Field value; + + ToHex( + Field value + ) { + super( + N_TO_HEX, + allNotNull(VARCHAR, value) + ); + + this.value = nullSafeNotNull(value, INTEGER); + } + + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + @Override + public final void accept(Context ctx) { + switch (ctx.family()) { + + case MARIADB: + case MYSQL: + ctx.visit(function(N_HEX, getDataType(), value)); + break; + + + case H2: + ctx.visit(DSL.trim(toChar(value, inline("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")))); + break; + + case SQLITE: + ctx.visit(function(N_PRINTF, getDataType(), inline("%X"), value)); + break; + + + + + + + + default: + ctx.visit(function(N_TO_HEX, getDataType(), value)); + break; + } + } + + // ------------------------------------------------------------------------- + // The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof ToHex) { + return + StringUtils.equals(value, ((ToHex) that).value) + ; + } + else + return super.equals(that); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Uuid.java b/jOOQ/src/main/java/org/jooq/impl/Uuid.java new file mode 100644 index 0000000000..c93c00ad85 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Uuid.java @@ -0,0 +1,164 @@ +/* + * 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.impl.Tools.DataExtendedKey.*; +import static org.jooq.impl.Tools.DataKey.*; +import static org.jooq.SQLDialect.*; + +import org.jooq.*; +import org.jooq.Record; +import org.jooq.conf.*; +import org.jooq.impl.*; +import org.jooq.tools.*; + +import java.util.*; +import java.util.UUID; + + +/** + * The UUID statement. + */ +@SuppressWarnings({ "unused" }) +final class Uuid +extends + AbstractField +{ + + private static final long serialVersionUID = 1L; + + Uuid() { + super( + N_UUID, + allNotNull(UUID) + ); + } + + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + @Override + public final void accept(Context ctx) { + switch (ctx.family()) { + + + + + + + + + + + + + + + + case POSTGRES: + ctx.visit(function(N_GEN_RANDOM_UUID, getDataType())); + break; + + + + + + + + case FIREBIRD: + ctx.visit(function(N_UUID_TO_CHAR, getDataType(), function(N_GEN_UUID, getDataType()))); + break; + + + + + + + + case H2: + ctx.visit(function(N_RANDOM_UUID, getDataType())); + break; + + case HSQLDB: + case IGNITE: + case MARIADB: + case MYSQL: + ctx.visit(function(N_UUID, getDataType())); + break; + + + + + + + + + + + + + + + + default: + ctx.visit(function(N_UUID, getDataType())); + break; + } + } + + // ------------------------------------------------------------------------- + // The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof Uuid) { + return true; + } + else + return super.equals(that); + } +}