[#1178] Allow for treating Condition as Field<Boolean>
This commit is contained in:
parent
102f6065f6
commit
43417d5874
@ -40,7 +40,6 @@ import static junit.framework.Assert.assertEquals;
|
||||
import static org.jooq.SQLDialect.ASE;
|
||||
import static org.jooq.SQLDialect.DB2;
|
||||
import static org.jooq.SQLDialect.DERBY;
|
||||
import static org.jooq.SQLDialect.MYSQL;
|
||||
import static org.jooq.conf.StatementType.STATIC_STATEMENT;
|
||||
import static org.jooq.impl.Factory.all;
|
||||
import static org.jooq.impl.Factory.any;
|
||||
@ -48,12 +47,15 @@ import static org.jooq.impl.Factory.castNull;
|
||||
import static org.jooq.impl.Factory.concat;
|
||||
import static org.jooq.impl.Factory.count;
|
||||
import static org.jooq.impl.Factory.escape;
|
||||
import static org.jooq.impl.Factory.field;
|
||||
import static org.jooq.impl.Factory.lower;
|
||||
import static org.jooq.impl.Factory.one;
|
||||
import static org.jooq.impl.Factory.select;
|
||||
import static org.jooq.impl.Factory.selectOne;
|
||||
import static org.jooq.impl.Factory.trueCondition;
|
||||
import static org.jooq.impl.Factory.upper;
|
||||
import static org.jooq.impl.Factory.val;
|
||||
import static org.jooq.impl.Factory.zero;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.ArrayList;
|
||||
@ -63,6 +65,7 @@ import java.util.List;
|
||||
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.Record;
|
||||
import org.jooq.Record1;
|
||||
import org.jooq.Record2;
|
||||
import org.jooq.Record3;
|
||||
@ -459,7 +462,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
|
||||
.fetch(TBook_ID()));
|
||||
|
||||
// [#1073] Some dialects incorrectly handle NULL in NOT IN predicates
|
||||
if (asList(ASE, MYSQL).contains(getDialect())) {
|
||||
if (asList(ASE).contains(getDialect())) {
|
||||
assertEquals(
|
||||
asList(2, 3, 4),
|
||||
create().select(TBook_ID())
|
||||
@ -488,6 +491,14 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
|
||||
.orderBy(TBook_ID()).fetch(TBook_ID()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConditionsAsFields() throws Exception {
|
||||
Record record = create().select(field(one().eq(zero())), field(one().eq(1))).fetchOne();
|
||||
|
||||
assertEquals(false, record.getValue(0));
|
||||
assertEquals(true, record.getValue(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuantifiedPredicates() throws Exception {
|
||||
|
||||
|
||||
@ -1030,6 +1030,11 @@ public abstract class jOOQAbstractTest<
|
||||
new PredicateTests(this).testConditions();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConditionsAsFields() throws Exception {
|
||||
new PredicateTests(this).testConditionsAsFields();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuantifiedPredicates() throws Exception {
|
||||
new PredicateTests(this).testQuantifiedPredicates();
|
||||
|
||||
92
jOOQ/src/main/java/org/jooq/impl/ConditionAsField.java
Normal file
92
jOOQ/src/main/java/org/jooq/impl/ConditionAsField.java
Normal file
@ -0,0 +1,92 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed to you under the Apache License, Version 2.0
|
||||
* (the "License"); You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* . Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* . Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* . Neither the name "jOOQ" nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import static org.jooq.impl.Factory.inline;
|
||||
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.Configuration;
|
||||
import org.jooq.QueryPart;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
class ConditionAsField extends AbstractFunction<Boolean> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -5921673852489483721L;
|
||||
private final Condition condition;
|
||||
|
||||
ConditionAsField(Condition condition) {
|
||||
super(condition.toString(), SQLDataType.BOOLEAN);
|
||||
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
@Override
|
||||
final QueryPart getFunction0(Configuration configuration) {
|
||||
switch (configuration.getDialect()) {
|
||||
|
||||
// Most databases don't accept predicates where column expressions
|
||||
// are expected.
|
||||
case CUBRID:
|
||||
case DB2:
|
||||
case ORACLE:
|
||||
case SQLSERVER:
|
||||
return Factory.decode().when(condition, inline(true)).otherwise(inline(false));
|
||||
|
||||
// These databases can inline predicates in column expression contexts
|
||||
case DERBY:
|
||||
case H2:
|
||||
case HSQLDB:
|
||||
case MYSQL:
|
||||
case POSTGRES:
|
||||
case SQLITE:
|
||||
|
||||
// Unknown (to be evaluated):
|
||||
case ASE:
|
||||
case FIREBIRD:
|
||||
case INGRES:
|
||||
case SYBASE:
|
||||
return condition;
|
||||
}
|
||||
|
||||
// The default, for new dialects
|
||||
return condition;
|
||||
}
|
||||
}
|
||||
@ -3053,6 +3053,14 @@ public class Factory {
|
||||
return condition.not();
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform a condition into a boolean field
|
||||
*/
|
||||
@Support
|
||||
public static Field<Boolean> field(Condition condition) {
|
||||
return new ConditionAsField(condition);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX Global Field and Function factory
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Loading…
Reference in New Issue
Block a user