[jOOQ/jOOQ#10160] Add support for MySQL's IF() function

This commit is contained in:
Lukas Eder 2020-05-06 09:24:09 +02:00
parent ed91af6681
commit b96fbd9b67
4 changed files with 52 additions and 15 deletions

View File

@ -85,6 +85,8 @@ import static org.jooq.SQLDialect.SQLITE;
// ...
// ...
// ...
import static org.jooq.impl.Names.N_IF;
import static org.jooq.impl.Names.N_IIF;
import static org.jooq.impl.Names.N_SYSTEM_TIME;
import static org.jooq.impl.PositionalWindowFunction.PositionalFunctionType.FIRST_VALUE;
import static org.jooq.impl.PositionalWindowFunction.PositionalFunctionType.LAG;
@ -12702,6 +12704,38 @@ public class DSL {
return select.<T>asField();
}
/**
* Create a MySQL style <code>IF(condition, ifTrue, ifFalse)</code> function.
*/
@Support
public static <T> Field<T> if_(Condition condition, T ifTrue, T ifFalse) {
return iif0(N_IF, condition, Tools.field(ifTrue), Tools.field(ifFalse));
}
/**
* Create a MySQL style <code>IF(condition, ifTrue, ifFalse)</code> function.
*/
@Support
public static <T> Field<T> if_(Condition condition, T ifTrue, Field<T> ifFalse) {
return iif0(N_IF, condition, Tools.field(ifTrue), nullSafe(ifFalse));
}
/**
* Create a MySQL style <code>IF(condition, ifTrue, ifFalse)</code> function.
*/
@Support
public static <T> Field<T> if_(Condition condition, Field<T> ifTrue, T ifFalse) {
return iif0(N_IF, condition, nullSafe(ifTrue), Tools.field(ifFalse));
}
/**
* Create a MySQL style <code>IF(condition, ifTrue, ifFalse)</code> function.
*/
@Support
public static <T> Field<T> if_(Condition condition, Field<T> ifTrue, Field<T> ifFalse) {
return iif0(N_IF, condition, nullSafe(ifTrue), nullSafe(ifFalse));
}
/**
* Initialise a {@link Case} statement.
* <p>
@ -13538,7 +13572,7 @@ public class DSL {
*/
@Support
public static <T> Field<T> iif(Condition condition, T ifTrue, T ifFalse) {
return iif0(condition, Tools.field(ifTrue), Tools.field(ifFalse));
return iif0(N_IIF, condition, Tools.field(ifTrue), Tools.field(ifFalse));
}
/**
@ -13548,7 +13582,7 @@ public class DSL {
*/
@Support
public static <T> Field<T> iif(Condition condition, T ifTrue, Field<T> ifFalse) {
return iif0(condition, Tools.field(ifTrue, ifFalse), nullSafe(ifFalse));
return iif0(N_IIF, condition, Tools.field(ifTrue, ifFalse), nullSafe(ifFalse));
}
/**
@ -13558,7 +13592,7 @@ public class DSL {
*/
@Support
public static <T> Field<T> iif(Condition condition, Field<T> ifTrue, T ifFalse) {
return iif0(condition, nullSafe(ifTrue), Tools.field(ifFalse, ifTrue));
return iif0(N_IIF, condition, nullSafe(ifTrue), Tools.field(ifFalse, ifTrue));
}
/**
@ -13566,13 +13600,13 @@ public class DSL {
*/
@Support
public static <T> Field<T> iif(Condition condition, Field<T> ifTrue, Field<T> ifFalse) {
return iif0(condition, ifTrue, ifFalse);
return iif0(N_IIF, condition, ifTrue, ifFalse);
}
// Java 8 is stricter than Java 7 with respect to generics and overload
// resolution (http://stackoverflow.com/q/5361513/521799)
static <T> Field<T> iif0(Condition condition, Field<T> ifTrue, Field<T> ifFalse) {
return new Iif<>(condition, nullSafe(ifTrue), nullSafe(ifFalse));
static <T> Field<T> iif0(Name name, Condition condition, Field<T> ifTrue, Field<T> ifFalse) {
return new Iif<>(name, condition, nullSafe(ifTrue), nullSafe(ifFalse));
}
// -------------------------------------------------------------------------

View File

@ -37,11 +37,10 @@
*/
package org.jooq.impl;
import static org.jooq.impl.Names.N_IIF;
import org.jooq.Condition;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.Name;
/**
* @author Lukas Eder
@ -57,15 +56,14 @@ final class Iif<T> extends AbstractField<T> {
private final Field<T> ifTrue;
private final Field<T> ifFalse;
Iif(Condition condition, Field<T> ifTrue, Field<T> ifFalse) {
super(N_IIF, ifTrue.getDataType());
Iif(Name name, Condition condition, Field<T> ifTrue, Field<T> ifFalse) {
super(name, ifTrue.getDataType());
this.condition = condition;
this.ifTrue = ifTrue;
this.ifFalse = ifFalse;
}
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
@ -75,9 +73,13 @@ final class Iif<T> extends AbstractField<T> {
case MARIADB:
case MYSQL:
ctx.visit(getUnqualifiedName()).sql('(').visit(condition).sql(", ").visit(ifTrue).sql(", ").visit(ifFalse).sql(')');
break;
default:
ctx.visit(DSL.choose().when(condition, ifTrue).otherwise(ifFalse));
ctx.visit(DSL.when(condition, ifTrue).otherwise(ifFalse));
break;
}
}

View File

@ -110,6 +110,7 @@ final class Names {
static final Name N_GROUP_CONCAT = unquotedName("group_concat");
static final Name N_HASHBYTES = unquotedName("hashbytes");
static final Name N_HEX = unquotedName("hex");
static final Name N_IF = unquotedName("if");
static final Name N_IFNULL = unquotedName("ifnull");
static final Name N_IIF = unquotedName("iif");
static final Name N_INSTR = unquotedName("instr");

View File

@ -6330,7 +6330,7 @@ final class ParserImpl implements Parser {
return field;
else if ((field = parseFieldIsnullIf(ctx)) != null)
return field;
else if ((field = parseFieldIifIf(ctx)) != null)
else if ((field = parseFieldIfIf(ctx)) != null)
return field;
else
break;
@ -9008,8 +9008,8 @@ final class ParserImpl implements Parser {
return null;
}
private static final Field<?> parseFieldIifIf(ParserContext ctx) {
if (parseFunctionNameIf(ctx, "IIF")) {
private static final Field<?> parseFieldIfIf(ParserContext ctx) {
if (parseFunctionNameIf(ctx, "IF") || parseFunctionNameIf(ctx, "IIF")) {
parse(ctx, '(');
Condition c = parseCondition(ctx);
parse(ctx, ',');