[#7171] [#7276] Add support for SQL Server's CHOOSE() function

This commit is contained in:
lukaseder 2018-03-13 12:29:43 +01:00
parent 78f23b24eb
commit ec129e1ec7
4 changed files with 135 additions and 1 deletions

View File

@ -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 ')'

View File

@ -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<T> extends AbstractField<T> {
private static final long serialVersionUID = 4861196545079661438L;
private Field<Integer> index;
private Field<T>[] values;
Choose(Field<Integer> index, Field<T>[] values) {
super(DSL.name("choose"), dataType(values));
this.index = index;
this.values = values;
}
@SuppressWarnings("unchecked")
private static final <T> DataType<T> dataType(Field<T>[] values) {
return values == null || values.length == 0 ? (DataType<T>) SQLDataType.OTHER : values[0].getDataType();
}
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
default: {
CaseValueStep<Integer> s = choose(index);
CaseWhenStep<Integer, T> 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;
}
}
}
}

View File

@ -12046,6 +12046,28 @@ public class DSL {
return decode().value(value);
}
@Support
public static <T> Field<T> choose(int index, T... values) {
return choose(val(index), (Field<T>[]) Tools.fields(values).toArray(EMPTY_FIELD));
}
@Support
@SafeVarargs
public static <T> Field<T> choose(int index, Field<T>... values) {
return choose(val(index), values);
}
@Support
public static <T> Field<T> choose(Field<Integer> index, T... values) {
return choose(index, (Field<T>[]) Tools.fields(values).toArray(EMPTY_FIELD));
}
@Support
@SafeVarargs
public static <T> Field<T> choose(Field<Integer> index, Field<T>... values) {
return new Choose<T>(index, values);
}
/**
* Initialise a {@link Case} statement.
* <p>

View File

@ -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<Object>) fields.get(2),
(Field<Object>[]) (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<Integer> index = (Field<Integer>) parseField(ctx, Type.N);
parse(ctx, ',');
List<Field<?>> fields = parseFields(ctx);
parse(ctx, ')');
return DSL.choose(index, fields.toArray(EMPTY_FIELD));
}
return null;