From dec1e60c29a9d93d4af078b9fbe73b085ae65b13 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 13 Jan 2021 20:27:13 +0100 Subject: [PATCH] [jOOQ/jOOQ#7290] Add DSL.chr(int) --- jOOQ/src/main/java/org/jooq/impl/Chr.java | 133 ++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/DSL.java | 18 +++ jOOQ/src/main/java/org/jooq/impl/Names.java | 3 + .../main/java/org/jooq/impl/ParserImpl.java | 4 +- 4 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/Chr.java diff --git a/jOOQ/src/main/java/org/jooq/impl/Chr.java b/jOOQ/src/main/java/org/jooq/impl/Chr.java new file mode 100644 index 0000000000..d0b6885628 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Chr.java @@ -0,0 +1,133 @@ +/* + * 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 CHR statement. + */ +@SuppressWarnings({ "rawtypes", "unused" }) +final class Chr +extends + AbstractField +{ + + private static final long serialVersionUID = 1L; + + private final Field number; + + Chr( + Field number + ) { + super( + N_CHR, + allNotNull(VARCHAR, number) + ); + + this.number = nullSafeNotNull(number, INTEGER); + } + + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + + + @Override + public final void accept(Context ctx) { + switch (ctx.family()) { + + + + + + + + + + + case HSQLDB: + case MARIADB: + case MYSQL: + case SQLITE: + ctx.visit(N_CHAR).sql('(').visit(number).sql(')'); + break; + + case FIREBIRD: + ctx.visit(N_ASCII_CHAR).sql('(').visit(number).sql(')'); + break; + + default: + ctx.visit(N_CHR).sql('(').visit(number).sql(')'); + break; + } + } + + + + // ------------------------------------------------------------------------- + // The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof Chr) { + return + StringUtils.equals(number, ((Chr) that).number) + ; + } + else + return super.equals(that); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 3bb40a600a..33beb4ab45 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -14906,6 +14906,24 @@ public class DSL { return new CharLength(string); } + /** + * The CHR function. + */ + @NotNull + @Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + public static Field chr(Number number) { + return new Chr(Tools.field(number)); + } + + /** + * The CHR function. + */ + @NotNull + @Support({ FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + public static Field chr(Field number) { + return new Chr(number); + } + /** * The COS function. */ diff --git a/jOOQ/src/main/java/org/jooq/impl/Names.java b/jOOQ/src/main/java/org/jooq/impl/Names.java index 859482cb15..7534dd7d2f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Names.java +++ b/jOOQ/src/main/java/org/jooq/impl/Names.java @@ -64,6 +64,7 @@ final class Names { static final Name N_ARRAY_LENGTH = unquotedName("array_length"); static final Name N_ASC = unquotedName("asc"); static final Name N_ASCII = unquotedName("ascii"); + static final Name N_ASCII_CHAR = unquotedName("ascii_char"); static final Name N_ASCII_VAL = unquotedName("ascii_val"); static final Name N_ASIN = unquotedName("asin"); static final Name N_ATAN = unquotedName("atan"); @@ -81,8 +82,10 @@ final class Names { static final Name N_CAST = unquotedName("cast"); static final Name N_CEIL = unquotedName("ceil"); static final Name N_CEILING = unquotedName("ceiling"); + static final Name N_CHAR = unquotedName("char"); static final Name N_CHARINDEX = unquotedName("charindex"); static final Name N_CHAR_LENGTH = unquotedName("char_length"); + static final Name N_CHR = unquotedName("chr"); static final Name N_CHOOSE = unquotedName("choose"); static final Name N_CLNG = unquotedName("clng"); static final Name N_COALESCE = unquotedName("coalesce"); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index 4922571422..1af46a147c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -88,6 +88,7 @@ import static org.jooq.impl.DSL.charLength; import static org.jooq.impl.DSL.characterSet; import static org.jooq.impl.DSL.check; import static org.jooq.impl.DSL.choose; +import static org.jooq.impl.DSL.chr; import static org.jooq.impl.DSL.coalesce; import static org.jooq.impl.DSL.coerce; import static org.jooq.impl.DSL.collation; @@ -361,7 +362,6 @@ import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -6685,6 +6685,8 @@ final class ParserContext { return currentSchema(); else if ((parseKeywordIf("CURRENT_USER") || parseKeywordIf("CURRENT USER")) && (parseIf('(') && parse(')') || true)) return currentUser(); + else if (parseFunctionNameIf("CHR") || parseFunctionNameIf("CHAR")) + return chr((Field) parseFieldParenthesised(N)); if (N.is(type)) if ((field = parseFieldCharIndexIf()) != null)