diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 376966f74b..4474ac326e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -18296,6 +18296,60 @@ public class DSL { } + /** + * The Oracle-specific TO_CHAR function. + */ + @NotNull + @Support + public static Field toChar(Object value) { + return toChar(val(value)); + } + + /** + * The Oracle-specific TO_CHAR function. + */ + @NotNull + @Support + public static Field toChar(Field value) { + return new ToChar(value, null); + } + + /** + * The Oracle-specific TO_CHAR function. + */ + @NotNull + @Support({ H2, POSTGRES }) + public static Field toChar(Object value, String formatMask) { + return toChar(val(value), val(formatMask)); + } + + /** + * The Oracle-specific TO_CHAR function. + */ + @NotNull + @Support({ H2, POSTGRES }) + public static Field toChar(Object value, Field formatMask) { + return toChar(val(value), formatMask); + } + + /** + * The Oracle-specific TO_CHAR function. + */ + @NotNull + @Support({ H2, POSTGRES }) + public static Field toChar(Field value, String formatMask) { + return toChar(value, val(formatMask)); + } + + /** + * The Oracle-specific TO_CHAR function. + */ + @NotNull + @Support({ H2, POSTGRES }) + public static Field toChar(Field value, Field formatMask) { + return new ToChar(value, formatMask); + } + /** * Parse a value to a DATE. * diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index 210c16f85f..bbba330ab5 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -286,6 +286,7 @@ import static org.jooq.impl.DSL.timestamp; import static org.jooq.impl.DSL.timezone; import static org.jooq.impl.DSL.timezoneHour; import static org.jooq.impl.DSL.timezoneMinute; +import static org.jooq.impl.DSL.toChar; import static org.jooq.impl.DSL.toDate; import static org.jooq.impl.DSL.toTimestamp; import static org.jooq.impl.DSL.translate; @@ -8990,9 +8991,11 @@ final class ParserImpl implements Parser { private static final Field parseFieldToCharIf(ParserContext ctx) { if (parseFunctionNameIf(ctx, "TO_CHAR")) { parse(ctx, '('); - Field f1 = (Field) parseField(ctx); + Field f1 = parseField(ctx); + Field f2 = (Field) (parseIf(ctx, ',') ? parseField(ctx) : null); parse(ctx, ')'); - return cast(f1, SQLDataType.VARCHAR); + + return f2 == null ? toChar(f1) : toChar(f1, f2); } return null; diff --git a/jOOQ/src/main/java/org/jooq/impl/ToChar.java b/jOOQ/src/main/java/org/jooq/impl/ToChar.java new file mode 100644 index 0000000000..41b66d7550 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/ToChar.java @@ -0,0 +1,92 @@ +/* + * 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.SQLDialect.MARIADB; +// ... +import static org.jooq.SQLDialect.MYSQL; +import static org.jooq.impl.Names.N_TO_CHAR; +import static org.jooq.impl.SQLDataType.VARCHAR; + +import java.util.Set; + +import org.jooq.Context; +import org.jooq.Field; +import org.jooq.SQLDialect; + +/** + * @author Lukas Eder + */ +final class ToChar extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = 2484479701190490450L; + private static final Set NO_SUPPORT_NATIVE = SQLDialect.supportedBy(MARIADB, MYSQL); + private final Field field; + private final Field format; + + ToChar(Field field, Field format) { + super(N_TO_CHAR, VARCHAR); + + this.field = field; + this.format = format; + } + + @Override + public final void accept(Context ctx) { + if (format == null && NO_SUPPORT_NATIVE.contains(ctx.dialect())) + acceptCast(ctx); + else + acceptNative(ctx); + } + + private final void acceptNative(Context ctx) { + ctx.visit(N_TO_CHAR).sql('(').visit(field); + + if (format != null) + ctx.sql(", ").visit(format); + + ctx.sql(')'); + } + + private final void acceptCast(Context ctx) { + ctx.visit(DSL.cast(field, VARCHAR)); + } +}