Skip to content

Commit

Permalink
[#6817] Add DSL.noCondition()
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Nov 15, 2017
1 parent 18acc1d commit 282264a
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 31 deletions.
Expand Up @@ -194,7 +194,7 @@ private static Route rentalsPerCategory() {
*/
private static Condition storeIdCondition(Request req) {
return storeIdAll(req)
? trueCondition()
? noCondition()
: STORE.STORE_ID.eq(val(req.queryParams("storeId"), int.class));
}

Expand All @@ -210,7 +210,7 @@ private static boolean storeIdAll(Request req) {
*/
private static Condition countryIdCondition(Request req) {
return countryIdAll(req)
? trueCondition()
? noCondition()
: COUNTRY.COUNTRY_ID.eq(val(req.queryParams("countryId"), int.class));
}

Expand All @@ -219,7 +219,7 @@ private static Condition countryIdCondition(Request req) {
*/
private static Condition countryIdSemiJoinCondition(Request req) {
return countryIdAll(req)
? trueCondition()
? noCondition()
: RENTAL.CUSTOMER_ID.in(
select(CUSTOMER.CUSTOMER_ID)
.from(CUSTOMER)
Expand Down
Expand Up @@ -38,8 +38,8 @@
import static org.jooq.impl.DSL.decode;
import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.noCondition;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.trueCondition;
import static org.jooq.util.firebird.rdb.Tables.RDB$GENERATORS;
import static org.jooq.util.firebird.rdb.Tables.RDB$INDEX_SEGMENTS;
import static org.jooq.util.firebird.rdb.Tables.RDB$PROCEDURES;
Expand Down Expand Up @@ -246,7 +246,7 @@ protected List<TableDefinition> getTables0() throws SQLException {
// "selectable" procedures
.where(RDB$PROCEDURES.RDB$PROCEDURE_TYPE.eq((short) 1))
.and(tableValuedFunctions()
? trueCondition()
? noCondition()
: falseCondition())
)
.orderBy(1)) {
Expand Down
Expand Up @@ -44,13 +44,13 @@
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.max;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.noCondition;
import static org.jooq.impl.DSL.not;
import static org.jooq.impl.DSL.one;
import static org.jooq.impl.DSL.partitionBy;
import static org.jooq.impl.DSL.row;
import static org.jooq.impl.DSL.rowNumber;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.trueCondition;
import static org.jooq.impl.DSL.when;
import static org.jooq.util.postgres.PostgresDSL.array;
import static org.jooq.util.postgres.PostgresDSL.arrayAppend;
Expand Down Expand Up @@ -764,7 +764,7 @@ protected List<RoutineDefinition> getRoutines0() throws SQLException {
.where(r1.ROUTINE_SCHEMA.in(getInputSchemata()))
.and(tableValuedFunctions()
? condition(not(PG_PROC.PRORETSET))
: trueCondition())
: noCondition())
.orderBy(
r1.ROUTINE_SCHEMA.asc(),
r1.ROUTINE_NAME.asc(),
Expand Down
49 changes: 40 additions & 9 deletions jOOQ/src/main/java/org/jooq/impl/CombinedCondition.java
Expand Up @@ -40,6 +40,7 @@
import static org.jooq.Clause.CONDITION_OR;
import static org.jooq.Operator.AND;
import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.noCondition;
import static org.jooq.impl.DSL.trueCondition;
import static org.jooq.impl.Keywords.K_AND;
import static org.jooq.impl.Keywords.K_OR;
Expand All @@ -66,6 +67,41 @@ final class CombinedCondition extends AbstractCondition {
private final Operator operator;
private final List<Condition> conditions;

static Condition of(Operator operator, Condition left, Condition right) {
if (left instanceof NoCondition)
return right;
else if (right instanceof NoCondition)
return left;
else
return new CombinedCondition(operator, left, right);
}

static Condition of(Operator operator, Collection<? extends Condition> conditions) {
if (conditions.isEmpty())
return noCondition();

CombinedCondition result = null;
Condition first = null;

for (Condition condition : conditions)
if (!(condition instanceof NoCondition))
if (first == null)
first = condition;
else if (result == null)
(result = new CombinedCondition(operator, conditions.size()))
.add(operator, first)
.add(operator, condition);
else
result.add(operator, condition);

if (result != null)
return result;
else if (first != null)
return first;
else
return noCondition();
}

private CombinedCondition(Operator operator, int size) {
if (operator == null)
throw new IllegalArgumentException("The argument 'operator' must not be null");
Expand All @@ -74,21 +110,14 @@ private CombinedCondition(Operator operator, int size) {
this.conditions = new ArrayList<Condition>(size);
}

CombinedCondition(Operator operator, Condition left, Condition right) {
private CombinedCondition(Operator operator, Condition left, Condition right) {
this(operator, 2);

add(operator, left);
add(operator, right);
}

CombinedCondition(Operator operator, Collection<? extends Condition> conditions) {
this(operator, conditions.size());

for (Condition condition : conditions)
add(operator, condition);
}

private final void add(Operator op, Condition condition) {
private final CombinedCondition add(Operator op, Condition condition) {
if (condition instanceof CombinedCondition) {
CombinedCondition combinedCondition = (CombinedCondition) condition;

Expand All @@ -101,6 +130,8 @@ else if (condition == null)
throw new IllegalArgumentException("The argument 'conditions' must not contain null");
else
this.conditions.add(condition);

return this;
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions jOOQ/src/main/java/org/jooq/impl/ConditionProviderImpl.java
Expand Up @@ -35,7 +35,7 @@

package org.jooq.impl;

import static org.jooq.impl.DSL.trueCondition;
import static org.jooq.impl.DSL.noCondition;

import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -64,11 +64,11 @@ final class ConditionProviderImpl extends AbstractQueryPart implements Condition
}

final Condition getWhere() {
return hasWhere() ? condition : trueCondition();
return hasWhere() ? condition : noCondition();
}

final boolean hasWhere() {
return condition != null;
return condition != null && !(condition instanceof NoCondition);
}

// -------------------------------------------------------------------------
Expand Down
35 changes: 33 additions & 2 deletions jOOQ/src/main/java/org/jooq/impl/DSL.java
Expand Up @@ -10333,6 +10333,37 @@ public static Condition condition(Record record) {
// XXX Global Condition factory
// -------------------------------------------------------------------------

/**
* Return a <code>Condition</code> that behaves like no condition being
* present.
* <p>
* This is useful as an "identity" condition for reduction operations, for
* both <code>AND</code> and <code>OR</code> reductions, e.g.
* <p>
* <code><pre>
* Condition combined =
* Stream.of(cond1, cond2, cond3)
* .reduce(noCondition(), Condition::and);
* </pre></code>
* <p>
* When this condition is passed to SQL clauses, such as the
* <code>WHERE</code> clause, the entire clause is omitted:
* <p>
* <code><pre>
* selectFrom(T).where(noCondition())
* </pre></code>
* <p>
* ... will produce
* <p>
* <code><pre>
* SELECT * FROM t
* </pre></code>
*/
@Support
public static Condition noCondition() {
return NoCondition.INSTANCE;
}

/**
* Return a <code>Condition</code> that will always evaluate to true.
*/
Expand Down Expand Up @@ -10409,7 +10440,7 @@ public static Condition or(Collection<? extends Condition> conditions) {
*/
@Support
public static Condition condition(Operator operator, Condition left, Condition right) {
return new CombinedCondition(operator, left, right);
return CombinedCondition.of(operator, left, right);
}

/**
Expand All @@ -10427,7 +10458,7 @@ public static Condition condition(Operator operator, Condition... conditions) {
*/
@Support
public static Condition condition(Operator operator, Collection<? extends Condition> conditions) {
return new CombinedCondition(operator, conditions);
return CombinedCondition.of(operator, conditions);
}

/**
Expand Down
11 changes: 5 additions & 6 deletions jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java
Expand Up @@ -47,7 +47,6 @@
import static org.jooq.impl.DSL.sequence;
import static org.jooq.impl.DSL.sql;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.DSL.trueCondition;
import static org.jooq.impl.DSL.zero;
import static org.jooq.impl.Tools.EMPTY_QUERY;
import static org.jooq.impl.Tools.EMPTY_TABLE_RECORD;
Expand Down Expand Up @@ -3993,7 +3992,7 @@ public int fetchCount(Select<?> query) {

@Override
public int fetchCount(Table<?> table) {
return fetchCount(table, trueCondition());
return selectCount().from(table).fetchOne(0, int.class);
}

@Override
Expand All @@ -4008,7 +4007,7 @@ public boolean fetchExists(Select<?> query) throws DataAccessException {

@Override
public boolean fetchExists(Table<?> table) throws DataAccessException {
return fetchExists(table, trueCondition());
return fetchExists(selectOne().from(table));
}

@Override
Expand All @@ -4035,7 +4034,7 @@ public int execute(Query query) {

@Override
public <R extends Record> Result<R> fetch(Table<R> table) {
return fetch(table, trueCondition());
return selectFrom(table).fetch();
}

@Override
Expand Down Expand Up @@ -4087,7 +4086,7 @@ public <R extends Record> R fetchAny(Table<R> table, Condition condition) {

@Override
public <R extends Record> Cursor<R> fetchLazy(Table<R> table) {
return fetchLazy(table, trueCondition());
return selectFrom(table).fetchLazy();
}

@Override
Expand Down Expand Up @@ -4119,7 +4118,7 @@ public <R extends Record> CompletionStage<Result<R>> fetchAsync(Executor executo

@Override
public <R extends Record> Stream<R> fetchStream(Table<R> table) {
return fetchStream(table, trueCondition());
return selectFrom(table).stream();
}

@Override
Expand Down
64 changes: 64 additions & 0 deletions jOOQ/src/main/java/org/jooq/impl/NoCondition.java
@@ -0,0 +1,64 @@
/*
* 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.Clause.CONDITION;
import static org.jooq.Clause.CONDITION_COMPARISON;

import org.jooq.Clause;
import org.jooq.Context;

/**
* @author Lukas Eder
*/
final class NoCondition extends AbstractCondition {

private static final long serialVersionUID = 775364624704563687L;
private static final Clause[] CLAUSES = { CONDITION, CONDITION_COMPARISON };
static final NoCondition INSTANCE = new NoCondition();

@Override
public final void accept(Context<?> ctx) {
ctx.sql("1 = 1");
}

@Override
public final Clause[] clauses(Context<?> ctx) {
return CLAUSES;
}

private NoCondition() {}
}
7 changes: 3 additions & 4 deletions jOOQ/src/main/java/org/jooq/impl/Pivot.java
Expand Up @@ -35,7 +35,7 @@
package org.jooq.impl;

import static org.jooq.conf.ParamType.INLINED;
import static org.jooq.impl.DSL.trueCondition;
import static org.jooq.impl.DSL.noCondition;
import static org.jooq.impl.DSL.using;
import static org.jooq.impl.Keywords.K_FOR;
import static org.jooq.impl.Keywords.K_IN;
Expand Down Expand Up @@ -177,11 +177,10 @@ private Table<Record> select(Configuration configuration) {
List<Field<?>> aggregationSelects = new ArrayList<Field<?>>();
for (Field<?> inField : in) {
for (Field<?> aggregateFunction : aggregateFunctions) {
Condition join = trueCondition();
Condition join = noCondition();

for (Field<?> field : groupingFields) {
for (Field<?> field : groupingFields)
join = join.and(condition(pivot, field));
}

@SuppressWarnings("unchecked")
Select<?> aggregateSelect = using(configuration)
Expand Down

0 comments on commit 282264a

Please sign in to comment.