[jOOQ/jOOQ#11009] Add support for the MySQL FIELD() function

This commit is contained in:
Lukas Eder 2020-11-23 13:13:37 +01:00
parent db45db76cd
commit 00e6517dc6
2 changed files with 127 additions and 2 deletions

View File

@ -0,0 +1,118 @@
/*
* 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.inline;
import static org.jooq.impl.DSL.zero;
import static org.jooq.impl.Names.N_FIELD;
import static org.jooq.impl.QueryPartListView.wrap;
import static org.jooq.impl.SQLDataType.INTEGER;
import static org.jooq.impl.Tools.EMPTY_FIELD;
import static org.jooq.impl.Tools.combine;
import java.util.ArrayList;
import java.util.List;
import org.jooq.Context;
import org.jooq.Field;
/**
* @author Lukas Eder
*/
final class FieldFunction<T> extends AbstractField<Integer> {
/**
* Generated UID
*/
private static final long serialVersionUID = 2328293840567996891L;
private final QueryPartListView<Field<?>> arguments;
FieldFunction(Field<T> field, Field<T>[] arguments) {
super(N_FIELD, INTEGER);
this.arguments = wrap(combine(field, arguments));
}
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case MARIADB:
case MYSQL:
if (arguments.size() > 1)
ctx.visit(N_FIELD).sql('(').visit(arguments).sql(')');
else
acceptDefault(ctx);
break;
default:
acceptDefault(ctx);
break;
}
}
private final void acceptDefault(Context<?> ctx) {
int size = arguments.size();
if (size == 1) {
ctx.visit(zero());
}
else {
List<Field<?>> args = new ArrayList<>();
args.add(arguments.get(0));
for (int i = 1; i < size; i++) {
args.add(arguments.get(i));
args.add(inline(i));
}
args.add(inline(0));
ctx.visit(DSL.decode(
args.get(0),
args.get(1),
args.get(2),
(Object[]) args.subList(3, args.size()).toArray(EMPTY_FIELD)
));
}
}
}

View File

@ -127,8 +127,15 @@ final class RowIsNull extends AbstractCondition {
if (row != null && EMULATE_NULL_ROW.contains(ctx.dialect()))
ctx.visit(condition(row.fields()));
else if (select != null && EMULATE_NULL_QUERY.contains(ctx.dialect())) {
Table<?> t = new AliasedSelect<>(select).as("t");
ctx.visit(inline(1).eq(selectCount().from(t).where(condition(t.fields()))));
// [#11011] Avoid the RVE IS NULL emulation for queries of degree 1
if (select.getSelect().size() == 1) {
acceptStandard(ctx);
}
else {
Table<?> t = new AliasedSelect<>(select).as("t");
ctx.visit(inline(1).eq(selectCount().from(t).where(condition(t.fields()))));
}
}
else
acceptStandard(ctx);