[#1391] Implemented emulation for other databases

This commit is contained in:
lukaseder 2014-12-02 20:20:48 +01:00
parent dc0cbb09c8
commit a73bbbc6b0
3 changed files with 105 additions and 36 deletions

View File

@ -9803,17 +9803,17 @@ public class DSL {
/**
* Get the every value over a field: every(field).
*/
@Support(POSTGRES)
@Support
public static AggregateFunction<Boolean> every(Field<Boolean> field) {
return new Function<Boolean>("every", SQLDataType.BOOLEAN, nullSafe(field));
return every(condition(nullSafe(field)));
}
/**
* Get the every value over a condition: every(condition).
*/
@Support(POSTGRES)
@Support
public static AggregateFunction<Boolean> every(Condition condition) {
return every(field(condition));
return new Every(condition);
}
/**

View File

@ -0,0 +1,90 @@
/**
* Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* 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.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq.impl;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.one;
import static org.jooq.impl.DSL.zero;
import org.jooq.Condition;
import org.jooq.Context;
import org.jooq.Field;
/**
* @author Lukas Eder
*/
class Every extends Function<Boolean> {
/**
* Generated UID
*/
private static final long serialVersionUID = 7292087943334025737L;
private final Condition condition;
Every(Condition condition) {
super("every", SQLDataType.BOOLEAN, DSL.field(condition));
this.condition = condition;
}
@SuppressWarnings("serial")
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case POSTGRES:
super.accept(ctx);
break;
default:
final Field<Integer> sum = DSL.field("{0}", Integer.class, new CustomQueryPart() {
@Override
public void accept(Context<?> c) {
c.visit(DSL.sum(DSL.decode().when(condition, zero()).otherwise(one())));
toSQLOverClause(c);
}
});
ctx.visit(DSL.decode().when(sum.eq(zero()), inline(true)).otherwise(inline(false)));
break;
}
}
}

View File

@ -169,7 +169,7 @@ class Function<T> extends AbstractField<T> implements
this.withinGroupOrderBy = new SortFieldList();
}
private static String last(String... strings) {
final static String last(String... strings) {
if (strings != null && strings.length > 0) {
return strings[strings.length - 1];
}
@ -180,30 +180,9 @@ class Function<T> extends AbstractField<T> implements
// -------------------------------------------------------------------------
// XXX QueryPart API
// -------------------------------------------------------------------------
//
// @Override
// public final void bind(BindContext ctx) {
// if (term == LIST_AGG && asList(CUBRID, H2, HSQLDB, MARIADB, MYSQL).contains(ctx.configuration().dialect())) {
// ctx.visit(arguments.get(0));
// ctx.visit(withinGroupOrderBy);
//
// if (arguments.size() > 1) {
// ctx.visit(arguments.get(1));
// }
// }
// else {
// ctx.visit(arguments)
// .visit(keepDenseRankOrderBy)
// .visit(withinGroupOrderBy);
//
// QueryPart window = window(ctx);
// if (window != null)
// ctx.visit(window);
// }
// }
@Override
public final void accept(Context<?> ctx) {
public /* final */ void accept(Context<?> ctx) {
if (term == LIST_AGG && asList(CUBRID, H2, HSQLDB, MARIADB, MYSQL).contains(ctx.configuration().dialect())) {
toSQLGroupConcat(ctx);
}
@ -228,7 +207,7 @@ class Function<T> extends AbstractField<T> implements
xxx
x xxxxxxx xxxxxxxxxxxxxxxxxxxxx xxxxxxxxxx xxx xxx
xx
xxxxxxx xxxx xxxxxxxxxxxxxxxxxxxxxx xxxx x
xxxxx xxxx xxxxxxxxxxxxxxxxxxxxxx xxxx x
xx xxxx xx x xxxxxxxx xxxx xx xxxx xxx xxxxx xxx xxxx xxxxxx
xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xx xxxxxxx xxxxx xx xxx xx xxxxxxxxxxxxxxx xx
@ -276,7 +255,7 @@ class Function<T> extends AbstractField<T> implements
/**
* [#1275] <code>LIST_AGG</code> simulation for Postgres, Sybase
*/
private void toSQLStringAgg(Context<?> ctx) {
final void toSQLStringAgg(Context<?> ctx) {
toSQLFunctionName(ctx);
ctx.sql("(");
@ -306,7 +285,7 @@ class Function<T> extends AbstractField<T> implements
/**
* [#1273] <code>LIST_AGG</code> simulation for MySQL and CUBRID
*/
private final void toSQLGroupConcat(Context<?> ctx) {
final void toSQLGroupConcat(Context<?> ctx) {
toSQLFunctionName(ctx);
ctx.sql("(");
@ -329,7 +308,7 @@ class Function<T> extends AbstractField<T> implements
ctx.sql(")");
}
private final void toSQLOverClause(Context<?> ctx) {
final void toSQLOverClause(Context<?> ctx) {
QueryPart window = window(ctx);
// Render this clause only if needed
@ -349,7 +328,7 @@ class Function<T> extends AbstractField<T> implements
}
@SuppressWarnings("unchecked")
private final QueryPart window(Context<?> ctx) {
final QueryPart window(Context<?> ctx) {
if (windowSpecification != null)
return windowSpecification;
@ -386,7 +365,7 @@ class Function<T> extends AbstractField<T> implements
/**
* Render <code>KEEP (DENSE_RANK [FIRST | LAST] ORDER BY {...})</code> clause
*/
private void toSQLKeepDenseRankOrderByClause(Context<?> ctx) {
final void toSQLKeepDenseRankOrderByClause(Context<?> ctx) {
if (!keepDenseRankOrderBy.isEmpty()) {
ctx.sql(" ").keyword("keep")
.sql(" (").keyword("dense_rank")
@ -400,7 +379,7 @@ class Function<T> extends AbstractField<T> implements
/**
* Render <code>WITHIN GROUP (ORDER BY ..)</code> clause
*/
private final void toSQLWithinGroupClause(Context<?> ctx) {
final void toSQLWithinGroupClause(Context<?> ctx) {
if (!withinGroupOrderBy.isEmpty()) {
ctx.sql(" ").keyword("within group")
.sql(" (").keyword("order by")
@ -412,7 +391,7 @@ class Function<T> extends AbstractField<T> implements
/**
* Render function arguments and argument modifiers
*/
private final void toSQLArguments(Context<?> ctx) {
final void toSQLArguments(Context<?> ctx) {
toSQLFunctionName(ctx);
ctx.sql("(");
@ -464,7 +443,7 @@ class Function<T> extends AbstractField<T> implements
ctx.sql(")");
}
private final void toSQLFunctionName(Context<?> ctx) {
final void toSQLFunctionName(Context<?> ctx) {
if (name != null) {
ctx.visit(name);
}