diff --git a/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.11.txt b/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.11.txt index 4e6cd39601..18555a12f8 100644 --- a/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.11.txt +++ b/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.11.txt @@ -534,12 +534,13 @@ term = | 'BIT_LENGTH' '(' field ')' | case | 'CAST' '(' field 'AS' dataType ')' -| 'CONVERT' '(' dataType ',' field ')' | ( 'CEIL' | 'CEILING' ) '(' sum ')' | 'CHARINDEX' '(' field ',' field ')' | 'CHAR_LENGTH' '(' field ')' +| 'CHOOSE' '(' field { ',' field } ')' | 'COALESCE' '(' fields ')' | 'CONCAT' '(' fields ')' +| 'CONVERT' '(' dataType ',' field ')' | 'COS' '(' sum ')' | 'COSH' '(' sum ')' | 'COT' '(' sum ')' diff --git a/jOOQ/src/main/java/org/jooq/impl/Choose.java b/jOOQ/src/main/java/org/jooq/impl/Choose.java new file mode 100644 index 0000000000..073f8f0d76 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Choose.java @@ -0,0 +1,96 @@ +/* + * 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.choose; +import static org.jooq.impl.DSL.function; +import static org.jooq.impl.DSL.inline; +import static org.jooq.impl.DSL.unquotedName; + +import org.jooq.CaseValueStep; +import org.jooq.CaseWhenStep; +import org.jooq.Context; +import org.jooq.DataType; +import org.jooq.Field; + +/** + * @author Lukas Eder + */ +final class Choose extends AbstractField { + + private static final long serialVersionUID = 4861196545079661438L; + private Field index; + private Field[] values; + + Choose(Field index, Field[] values) { + super(DSL.name("choose"), dataType(values)); + + this.index = index; + this.values = values; + } + + @SuppressWarnings("unchecked") + private static final DataType dataType(Field[] values) { + return values == null || values.length == 0 ? (DataType) SQLDataType.OTHER : values[0].getDataType(); + } + + @Override + public final void accept(Context ctx) { + switch (ctx.family()) { + + + + + + + default: { + CaseValueStep s = choose(index); + CaseWhenStep when = null; + + for (int i = 0; i < values.length; i++) { + when = when == null + ? s.when(inline(i + 1), values[i]) + : when.when(inline(i + 1), values[i]); + } + + ctx.visit(when); + break; + } + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 844ca36029..9d2b1b040e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -12046,6 +12046,28 @@ public class DSL { return decode().value(value); } + @Support + public static Field choose(int index, T... values) { + return choose(val(index), (Field[]) Tools.fields(values).toArray(EMPTY_FIELD)); + } + + @Support + @SafeVarargs + public static Field choose(int index, Field... values) { + return choose(val(index), values); + } + + @Support + public static Field choose(Field index, T... values) { + return choose(index, (Field[]) Tools.fields(values).toArray(EMPTY_FIELD)); + } + + @Support + @SafeVarargs + public static Field choose(Field index, Field... values) { + return new Choose(index, values); + } + /** * Initialise a {@link Case} statement. *

diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index fe6f796800..d19f7a23aa 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -3886,6 +3886,8 @@ final class ParserImpl implements Parser { return field; else if ((field = parseFieldConvertIf(ctx)) != null) return field; + else if ((field = parseFieldChooseIf(ctx)) != null) + return field; break; @@ -5026,7 +5028,20 @@ final class ParserImpl implements Parser { (Field) fields.get(2), (Field[]) (size == 3 ? EMPTY_FIELD : fields.subList(3, size).toArray(EMPTY_FIELD)) ); + } + return null; + } + + private static final Field parseFieldChooseIf(ParserContext ctx) { + if (parseFunctionNameIf(ctx, "CHOOSE")) { + parse(ctx, '('); + Field index = (Field) parseField(ctx, Type.N); + parse(ctx, ','); + List> fields = parseFields(ctx); + parse(ctx, ')'); + + return DSL.choose(index, fields.toArray(EMPTY_FIELD)); } return null;