From 233d52f888aea9342a89757aa1b8373ba4df4c44 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 1 May 2020 16:37:47 +0200 Subject: [PATCH] [jOOQ/jOOQ#1571] Add support for REGEXP_REPLACE() --- jOOQ/src/main/java/org/jooq/impl/DSL.java | 32 +++++ jOOQ/src/main/java/org/jooq/impl/Names.java | 2 + .../main/java/org/jooq/impl/ParserImpl.java | 69 ++++++++++ .../java/org/jooq/impl/RegexpReplace.java | 122 ++++++++++++++++++ pom.xml | 4 +- 5 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/RegexpReplace.java diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index cf971cad4a..18e4d8af6b 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -14001,6 +14001,38 @@ public class DSL { return new Replace(nullSafe(field), nullSafe(search), nullSafe(replace)); } + /** + * Get the REGEXP_REPLACE_ALL function. + */ + @Support({ H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) + public static Field regexpReplaceAll(Field field, String pattern, String replacement) { + return regexpReplaceAll(field, Tools.field(pattern), Tools.field(replacement)); + } + + /** + * Get the REGEXP_REPLACE_ALL function. + */ + @Support({ H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) + public static Field regexpReplaceAll(Field field, Field pattern, Field replacement) { + return new RegexpReplace(field, nullSafe(pattern), nullSafe(replacement), true); + } + + /** + * Get the REGEXP_REPLACE_ALL function. + */ + @Support({ MYSQL, POSTGRES }) + public static Field regexpReplaceFirst(Field field, String pattern, String replacement) { + return regexpReplaceFirst(field, Tools.field(pattern), Tools.field(replacement)); + } + + /** + * Get the REGEXP_REPLACE_ALL function. + */ + @Support({ MYSQL, POSTGRES }) + public static Field regexpReplaceFirst(Field field, Field pattern, Field replacement) { + return new RegexpReplace(field, nullSafe(pattern), nullSafe(replacement), false); + } + /** * Get the position(in, search) function. * diff --git a/jOOQ/src/main/java/org/jooq/impl/Names.java b/jOOQ/src/main/java/org/jooq/impl/Names.java index 25ae3de5c6..31a80794f9 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Names.java +++ b/jOOQ/src/main/java/org/jooq/impl/Names.java @@ -170,6 +170,8 @@ final class Names { static final Name N_RANDOM = unquotedName("random"); static final Name N_RATIO_TO_REPORT = unquotedName("ratio_to_report"); static final Name N_RAWTOHEX = unquotedName("rawtohex"); + static final Name N_REGEX_REPLACE = unquotedName("regex_replace"); + static final Name N_REGEXP_REPLACE = unquotedName("regexp_replace"); static final Name N_REPEAT = unquotedName("repeat"); static final Name N_REPLACE = unquotedName("replace"); static final Name N_REPLICATE = unquotedName("replicate"); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index af51ea23a9..979689bd80 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -224,6 +224,8 @@ import static org.jooq.impl.DSL.rangeUnboundedFollowing; import static org.jooq.impl.DSL.rangeUnboundedPreceding; import static org.jooq.impl.DSL.rank; import static org.jooq.impl.DSL.ratioToReport; +import static org.jooq.impl.DSL.regexpReplaceAll; +import static org.jooq.impl.DSL.regexpReplaceFirst; import static org.jooq.impl.DSL.regrAvgX; import static org.jooq.impl.DSL.regrAvgY; import static org.jooq.impl.DSL.regrCount; @@ -6478,6 +6480,8 @@ final class ParserImpl implements Parser { if (S.is(type)) if ((field = parseFieldReplaceIf(ctx)) != null) return field; + else if ((field = parseFieldRegexpReplaceIf(ctx)) != null) + return field; else if ((field = parseFieldRepeatIf(ctx)) != null) return field; else if ((field = parseFieldReverseIf(ctx)) != null) @@ -8307,6 +8311,71 @@ final class ParserImpl implements Parser { return null; } + private static final Field parseFieldRegexpReplaceIf(ParserContext ctx) { + boolean all = parseFunctionNameIf(ctx, "REGEXP_REPLACE_ALL"); + boolean first = !all && parseFunctionNameIf(ctx, "REGEXP_REPLACE_FIRST"); + boolean ifx = !all && !first && parseFunctionNameIf(ctx, "REGEX_REPLACE"); + + if (all || first || ifx || parseFunctionNameIf(ctx, "REGEXP_REPLACE")) { + parse(ctx, '('); + Field field = parseField(ctx, S); + parse(ctx, ','); + Field pattern = parseField(ctx, S); + Field replacement = parseIf(ctx, ',') ? parseField(ctx, S) : null; + Long i1; + Long i2; + + if (replacement == null) { + replacement = inline(""); + } + else if (ifx) { + if (parseIf(ctx, ',')) + if (1L == parseUnsignedInteger(ctx)) + first = true; + else + throw ctx.expected("Only a limit of 1 is currently supported"); + } + else if (!all && !first) { + if (parseIf(ctx, ',')) { + String s = parseStringLiteralIf(ctx); + + if (s != null) { + if (s.contains("g")) + all = true; + } + else { + i1 = parseUnsignedInteger(ctx); + parse(ctx, ','); + i2 = parseUnsignedInteger(ctx); + + if (Long.valueOf(1L).equals(i1) && Long.valueOf(1L).equals(i2)) + all = true; + else + throw ctx.expected("Only start and occurence values of 1 are currently supported"); + } + } + + if (!all) switch (ctx.family()) { + + + + + + case POSTGRES: + first = true; + break; + } + } + + parse(ctx, ')'); + return first + ? regexpReplaceFirst(field, pattern, replacement) + : regexpReplaceAll(field, pattern, replacement); + } + + return null; + } + private static final Field parseFieldReverseIf(ParserContext ctx) { if (parseFunctionNameIf(ctx, "REVERSE")) { parse(ctx, '('); diff --git a/jOOQ/src/main/java/org/jooq/impl/RegexpReplace.java b/jOOQ/src/main/java/org/jooq/impl/RegexpReplace.java new file mode 100644 index 0000000000..190b64daf6 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/RegexpReplace.java @@ -0,0 +1,122 @@ +/* + * 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.Names.N_REGEXP_REPLACE; +import static org.jooq.impl.Names.N_REGEX_REPLACE; + +import org.jooq.Context; +import org.jooq.Field; + +/** + * @author Lukas Eder + */ +final class RegexpReplace extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = -1659917024582732223L; + + private final Field field; + private final Field pattern; + private final Field replacement; + private final boolean all; + + RegexpReplace(Field field, Field pattern, Field replacement, boolean all) { + super(N_REGEXP_REPLACE, field.getDataType()); + + this.field = field; + this.pattern = pattern; + this.replacement = replacement; + this.all = all; + } + + @Override + public final void accept(Context ctx) { + switch (ctx.family()) { + + + + + + case POSTGRES: + ctx.visit(N_REGEXP_REPLACE).sql('(').visit(field).sql(", ").visit(pattern).sql(", ").visit(replacement); + + if (all) + ctx.sql(", 'g')"); + else + ctx.sql(')'); + + break; + + + + + + + + + + + + + + + + + + + case MYSQL: + + // [#10151] TODO: Emulate REGEXP_REPLACE_FIRST for these three + case H2: + case HSQLDB: + case MARIADB: + default: + ctx.visit(N_REGEXP_REPLACE).sql('(').visit(field).sql(", ").visit(pattern).sql(", ").visit(replacement); + + if (all) + ctx.sql(')'); + else + ctx.sql(", 1, 1)"); + + break; + } + } +} diff --git a/pom.xml b/pom.xml index 9c4ca7d3be..7e3e1ae752 100644 --- a/pom.xml +++ b/pom.xml @@ -38,7 +38,7 @@ 2.2 - + 5.4.10.Final 3.8.8 @@ -456,7 +456,7 @@ spring-boot-maven-plugin ${spring.boot.version} - + net.alchim31.maven scala-maven-plugin