[jOOQ/jOOQ#11097] Add single argument DSL.function() overload

This commit is contained in:
Lukas Eder 2020-12-04 22:12:27 +01:00
parent 5b1e7eb50f
commit f2036e022e
10 changed files with 124 additions and 20 deletions

View File

@ -40,6 +40,9 @@ package org.jooq.impl;
import static org.jooq.impl.Names.N_ASC;
import static org.jooq.impl.Names.N_ASCII;
import static org.jooq.impl.Names.N_ASCII_VAL;
import static org.jooq.impl.SQLDataType.INTEGER;
import static org.jooq.impl.SQLDataType.VARCHAR;
import static org.jooq.impl.Tools.nullSafeNotNull;
import org.jooq.Context;
import org.jooq.Field;
@ -57,9 +60,9 @@ final class Ascii extends AbstractField<Integer> {
private final Field<?> string;
Ascii(Field<?> string) {
super(N_ASCII, SQLDataType.INTEGER);
super(N_ASCII, INTEGER.nullable(string == null || string.getDataType().nullable()));
this.string = string;
this.string = nullSafeNotNull(string, VARCHAR);
}
@Override
@ -81,9 +84,9 @@ final class Ascii extends AbstractField<Integer> {
case DERBY:
case SQLITE:
default:
ctx.visit(N_ASCII).sql('(').visit(string).sql(')');
break;
}
}
}

View File

@ -94,9 +94,13 @@ import static org.jooq.impl.Internal.imul;
import static org.jooq.impl.Internal.isub;
import static org.jooq.impl.Keywords.K_CUBE;
import static org.jooq.impl.Keywords.K_GROUPING_SETS;
import static org.jooq.impl.Names.N_ABS;
import static org.jooq.impl.Names.N_COS;
import static org.jooq.impl.Names.N_IF;
import static org.jooq.impl.Names.N_IIF;
import static org.jooq.impl.Names.N_SIN;
import static org.jooq.impl.Names.N_SYSTEM_TIME;
import static org.jooq.impl.Names.N_TAN;
import static org.jooq.impl.Names.N_VALUE;
import static org.jooq.impl.PositionalWindowFunction.PositionalFunctionType.FIRST_VALUE;
import static org.jooq.impl.PositionalWindowFunction.PositionalFunctionType.LAG;
@ -13050,6 +13054,20 @@ public class DSL {
return new org.jooq.impl.Function<>(name, type, Tools.nullSafe(arguments));
}
/**
* <code>function()</code> can be used to access native or user-defined
* functions that are not yet or insufficiently supported by jOOQ.
*
* @param name The function name (possibly qualified)
* @param type The function return type
* @param arguments The function arguments
*/
@NotNull
@Support
static <T> Field<T> function(Name name, DataType<T> type, Field<?> argument) {
return new org.jooq.impl.Function1<>(name, type, Tools.nullSafe(argument));
}
/**
* Create a new condition holding plain SQL.
* <p>
@ -15672,7 +15690,7 @@ public class DSL {
@NotNull
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<Integer> ascii(Field<String> field) {
return new Ascii(Tools.nullSafe(field));
return new Ascii(field);
}
/**
@ -19479,7 +19497,7 @@ public class DSL {
@NotNull
@Support
public static <T extends Number> Field<T> abs(Field<T> field) {
return function("abs", Tools.nullSafeDataType(field), field);
return function(N_ABS, Tools.nullSafeDataType(field), field);
}
/**
@ -19981,7 +19999,7 @@ public class DSL {
@NotNull
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<BigDecimal> cos(Field<? extends Number> field) {
return function("cos", SQLDataType.NUMERIC, field);
return function(N_COS, SQLDataType.NUMERIC, field);
}
/**
@ -20004,7 +20022,7 @@ public class DSL {
@NotNull
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<BigDecimal> sin(Field<? extends Number> field) {
return function("sin", SQLDataType.NUMERIC, field);
return function(N_SIN, SQLDataType.NUMERIC, field);
}
/**
@ -20027,7 +20045,7 @@ public class DSL {
@NotNull
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
public static Field<BigDecimal> tan(Field<? extends Number> field) {
return function("tan", SQLDataType.NUMERIC, field);
return function(N_TAN, SQLDataType.NUMERIC, field);
}
/**

View File

@ -79,7 +79,7 @@ final class Exp extends AbstractField<BigDecimal> {
default:
ctx.visit(function("exp", NUMERIC, argument));
ctx.visit(function(N_EXP, NUMERIC, argument));
return;
}
}

View File

@ -37,6 +37,8 @@
*/
package org.jooq.impl;
import static org.jooq.impl.DSL.unquotedName;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
@ -55,7 +57,7 @@ final class Function<T> extends AbstractField<T> {
private final QueryPartList<Field<?>> arguments;
Function(String name, DataType<T> type, Field<?>... arguments) {
this(DSL.unquotedName(name), type, arguments);
this(unquotedName(name), type, arguments);
}
Function(Name name, DataType<T> type, Field<?>... arguments) {

View File

@ -0,0 +1,68 @@
/*
* 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.unquotedName;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.Name;
/**
* @author Lukas Eder
*/
final class Function1<T> extends AbstractField<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = -2034096213592995420L;
private final Field<?> argument;
Function1(Name name, DataType<T> type, Field<?> argument) {
super(name, type);
this.argument = argument;
}
@Override
public final void accept(Context<?> ctx) {
ctx.visit(getQualifiedName()).sql('(').visit(argument).sql(')');
}
}

View File

@ -42,6 +42,8 @@ import static org.jooq.impl.DSL.function;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.Keywords.K_IS_JSON;
import static org.jooq.impl.Keywords.K_IS_NOT_JSON;
import static org.jooq.impl.Names.N_ISJSON;
import static org.jooq.impl.Names.N_JSON_VALID;
import static org.jooq.impl.SQLDataType.BOOLEAN;
import static org.jooq.impl.SQLDataType.INTEGER;
@ -69,7 +71,7 @@ final class IsJSON extends AbstractCondition {
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case MYSQL:
ctx.visit(function("json_valid", BOOLEAN, field));
ctx.visit(function(N_JSON_VALID, BOOLEAN, field));
break;

View File

@ -40,6 +40,8 @@ package org.jooq.impl;
import static org.jooq.impl.DSL.function;
import static org.jooq.impl.Internal.idiv;
import static org.jooq.impl.Names.N_LN;
import static org.jooq.impl.Names.N_LOG;
import static org.jooq.impl.Names.N_LOGN;
import static org.jooq.impl.SQLDataType.NUMERIC;
import java.math.BigDecimal;
@ -65,7 +67,7 @@ final class Ln extends AbstractField<BigDecimal> {
}
Ln(Field<? extends Number> argument, Field<? extends Number> base) {
super(N_LN, SQLDataType.NUMERIC);
super(N_LN, NUMERIC);
this.argument = argument;
this.base = base;
@ -98,7 +100,7 @@ final class Ln extends AbstractField<BigDecimal> {
default:
ctx.visit(function("ln", SQLDataType.NUMERIC, argument));
ctx.visit(function(N_LN, NUMERIC, argument));
return;
}
}
@ -131,7 +133,7 @@ final class Ln extends AbstractField<BigDecimal> {
return;
default:
ctx.visit(function("log", SQLDataType.NUMERIC, base, argument));
ctx.visit(function(N_LOG, NUMERIC, base, argument));
return;
}
}

View File

@ -52,6 +52,7 @@ final class Names {
static final Name N_ARRAY_TABLE = name("array_table");
static final Name N_COLUMN_VALUE = name("COLUMN_VALUE");
static final Name N_ABS = unquotedName("abs");
static final Name N_ACOS = unquotedName("acos");
static final Name N_ADD_DAYS = unquotedName("add_days");
static final Name N_ADD_MONTHS = unquotedName("add_months");
@ -84,6 +85,7 @@ final class Names {
static final Name N_COLLECT = unquotedName("collect");
static final Name N_CONCAT = unquotedName("concat");
static final Name N_CONVERT = unquotedName("convert");
static final Name N_COS = unquotedName("cos");
static final Name N_COSH = unquotedName("cosh");
static final Name N_COT = unquotedName("cot");
static final Name N_COUNT = unquotedName("count");
@ -133,6 +135,7 @@ final class Names {
static final Name N_IIF = unquotedName("iif");
static final Name N_INSERT = unquotedName("insert");
static final Name N_INSTR = unquotedName("instr");
static final Name N_ISJSON = unquotedName("isjson");
static final Name N_ISOWEEK = unquotedName("isoweek");
static final Name N_JOIN = unquotedName("join");
static final Name N_JSON_AGG = unquotedName("json_agg");
@ -147,6 +150,7 @@ final class Names {
static final Name N_JSON_OBJECTAGG = unquotedName("json_objectagg");
static final Name N_JSON_QUOTE = unquotedName("json_quote");
static final Name N_JSON_TABLE = unquotedName("json_table");
static final Name N_JSON_VALID = unquotedName("json_valid");
static final Name N_JSON_VALUE = unquotedName("json_value");
static final Name N_JSONB_AGG = unquotedName("jsonb_agg");
static final Name N_JSONB_BUILD_ARRAY = unquotedName("jsonb_build_array");
@ -165,6 +169,8 @@ final class Names {
static final Name N_LISTAGG = unquotedName("listagg");
static final Name N_LN = unquotedName("ln");
static final Name N_LOCATE = unquotedName("locate");
static final Name N_LOG = unquotedName("log");
static final Name N_LOGN = unquotedName("logn");
static final Name N_LOWER = unquotedName("lower");
static final Name N_LPAD = unquotedName("lpad");
static final Name N_LTRIM = unquotedName("ltrim");
@ -219,6 +225,7 @@ final class Names {
static final Name N_SELECT = unquotedName("select");
static final Name N_SGN = unquotedName("sgn");
static final Name N_SIGN = unquotedName("sign");
static final Name N_SIN = unquotedName("sin");
static final Name N_SINH = unquotedName("sinh");
static final Name N_SPACE = unquotedName("space");
static final Name N_SPLIT_PART = unquotedName("split_part");
@ -245,6 +252,7 @@ final class Names {
static final Name N_SYSTEM_RANGE = unquotedName("system_range");
static final Name N_SYSTEM_TIME = unquotedName("system_time");
static final Name N_T = unquotedName("t");
static final Name N_TAN = unquotedName("tan");
static final Name N_TANH = unquotedName("tanh");
static final Name N_TIMESTAMPADD = unquotedName("timestampadd");
static final Name N_TIMESTAMPDIFF = unquotedName("timestampdiff");

View File

@ -41,6 +41,7 @@ import static org.jooq.impl.DSL.function;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.pi;
import static org.jooq.impl.Names.N_RADIANS;
import static org.jooq.impl.SQLDataType.NUMERIC;
import static org.jooq.impl.Tools.castIfNeeded;
import java.math.BigDecimal;
@ -61,7 +62,7 @@ final class Radians extends AbstractField<BigDecimal> {
private final Field<?> argument;
Radians(Field<?> argument) {
super(N_RADIANS, SQLDataType.NUMERIC);
super(N_RADIANS, NUMERIC);
this.argument = argument;
}
@ -81,7 +82,7 @@ final class Radians extends AbstractField<BigDecimal> {
return;
default:
ctx.visit(function("radians", SQLDataType.NUMERIC, argument));
ctx.visit(function(N_RADIANS, NUMERIC, argument));
return;
}
}

View File

@ -135,17 +135,17 @@ final class Round<T extends Number> extends AbstractField<T> {
// There's no function round(double precision, integer) in Postgres
case POSTGRES:
if (decimals == null)
ctx.visit(function("round", getDataType(), argument));
ctx.visit(function(N_ROUND, getDataType(), argument));
else
ctx.visit(function("round", getDataType(), castIfNeeded(argument, BigDecimal.class), decimals));
ctx.visit(function(N_ROUND, getDataType(), castIfNeeded(argument, BigDecimal.class), decimals));
return;
default:
if (decimals == null)
ctx.visit(function("round", getDataType(), argument));
ctx.visit(function(N_ROUND, getDataType(), argument));
else
ctx.visit(function("round", getDataType(), argument, decimals));
ctx.visit(function(N_ROUND, getDataType(), argument, decimals));
return;
}