- Pull up OracleDSL.toChar() to DSL
- Support and emulate Oracle's TO_CHAR() function
This commit is contained in:
Lukas Eder 2020-09-17 14:13:26 +02:00
parent b7fc8fc900
commit d15c72149e
3 changed files with 151 additions and 2 deletions

View File

@ -18296,6 +18296,60 @@ public class DSL {
}
/**
* The Oracle-specific <code>TO_CHAR</code> function.
*/
@NotNull
@Support
public static Field<String> toChar(Object value) {
return toChar(val(value));
}
/**
* The Oracle-specific <code>TO_CHAR</code> function.
*/
@NotNull
@Support
public static Field<String> toChar(Field<?> value) {
return new ToChar(value, null);
}
/**
* The Oracle-specific <code>TO_CHAR</code> function.
*/
@NotNull
@Support({ H2, POSTGRES })
public static Field<String> toChar(Object value, String formatMask) {
return toChar(val(value), val(formatMask));
}
/**
* The Oracle-specific <code>TO_CHAR</code> function.
*/
@NotNull
@Support({ H2, POSTGRES })
public static Field<String> toChar(Object value, Field<String> formatMask) {
return toChar(val(value), formatMask);
}
/**
* The Oracle-specific <code>TO_CHAR</code> function.
*/
@NotNull
@Support({ H2, POSTGRES })
public static Field<String> toChar(Field<?> value, String formatMask) {
return toChar(value, val(formatMask));
}
/**
* The Oracle-specific <code>TO_CHAR</code> function.
*/
@NotNull
@Support({ H2, POSTGRES })
public static Field<String> toChar(Field<?> value, Field<String> formatMask) {
return new ToChar(value, formatMask);
}
/**
* Parse a value to a <code>DATE</code>.
*

View File

@ -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<String> f1 = (Field) parseField(ctx);
Field<?> f1 = parseField(ctx);
Field<String> f2 = (Field) (parseIf(ctx, ',') ? parseField(ctx) : null);
parse(ctx, ')');
return cast(f1, SQLDataType.VARCHAR);
return f2 == null ? toChar(f1) : toChar(f1, f2);
}
return null;

View File

@ -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<String> {
/**
* Generated UID
*/
private static final long serialVersionUID = 2484479701190490450L;
private static final Set<SQLDialect> NO_SUPPORT_NATIVE = SQLDialect.supportedBy(MARIADB, MYSQL);
private final Field<?> field;
private final Field<String> format;
ToChar(Field<?> field, Field<String> 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));
}
}