[#8010] Add Table.where(Condition) and overloads

This commit is contained in:
lukaseder 2018-11-02 16:31:15 +01:00
parent 1276fd03b6
commit 3ca1f45335
2 changed files with 214 additions and 6 deletions

View File

@ -606,6 +606,161 @@ public interface Table<R extends Record> extends TableLike<R>, Named {
Table<R> as(Table<?> otherTable, BiFunction<? super Field<?>, ? super Integer, ? extends Field<?>> aliasFunction);
// -------------------------------------------------------------------------
// XXX: WHERE clauses on tables
// -------------------------------------------------------------------------
/**
* Add a <code>WHERE</code> clause to the table, connecting them with each
* other with {@link Operator#AND}.
* <p>
* The resulting table acts like a derived table that projects all of this
* table's columns and filters by the argument {@link Condition}. If
* syntactically reasonable, the derived table may be inlined to the query
* that selects from the resulting table.
*/
@Support
Table<R> where(Condition condition);
/**
* Add a <code>WHERE</code> clause to the table, connecting them with each
* other with {@link Operator#AND}.
* <p>
* The resulting table acts like a derived table that projects all of this
* table's columns and filters by the argument {@link Condition}. If
* syntactically reasonable, the derived table may be inlined to the query
* that selects from the resulting table.
*/
@Support
Table<R> where(Condition... conditions);
/**
* Add a <code>WHERE</code> clause to the table, connecting them with each
* other with {@link Operator#AND}.
* <p>
* The resulting table acts like a derived table that projects all of this
* table's columns and filters by the argument {@link Condition}. If
* syntactically reasonable, the derived table may be inlined to the query
* that selects from the resulting table.
*/
@Support
Table<R> where(Collection<? extends Condition> conditions);
/**
* Add a <code>WHERE</code> clause to the table.
* <p>
* The resulting table acts like a derived table that projects all of this
* table's columns and filters by the argument {@link Condition}. If
* syntactically reasonable, the derived table may be inlined to the query
* that selects from the resulting table.
*/
@Support
Table<R> where(Field<Boolean> field);
/**
* Add a <code>WHERE</code> clause to the table.
* <p>
* The resulting table acts like a derived table that projects all of this
* table's columns and filters by the argument {@link Condition}. If
* syntactically reasonable, the derived table may be inlined to the query
* that selects from the resulting table.
* <p>
* <b>NOTE</b>: When inserting plain SQL into jOOQ objects, you must
* guarantee syntax integrity. You may also create the possibility of
* malicious SQL injection. Be sure to properly use bind variables and/or
* escape literals when concatenated into SQL clauses!
*
* @see DSL#condition(SQL)
* @see SQL
*/
@Support
@PlainSQL
Table<R> where(SQL sql);
/**
* Add a <code>WHERE</code> clause to the table.
* <p>
* The resulting table acts like a derived table that projects all of this
* table's columns and filters by the argument {@link Condition}. If
* syntactically reasonable, the derived table may be inlined to the query
* that selects from the resulting table.
* <p>
* <b>NOTE</b>: When inserting plain SQL into jOOQ objects, you must
* guarantee syntax integrity. You may also create the possibility of
* malicious SQL injection. Be sure to properly use bind variables and/or
* escape literals when concatenated into SQL clauses!
*
* @see DSL#condition(String)
* @see SQL
*/
@Support
@PlainSQL
Table<R> where(String sql);
/**
* Add a <code>WHERE</code> clause to the table.
* <p>
* The resulting table acts like a derived table that projects all of this
* table's columns and filters by the argument {@link Condition}. If
* syntactically reasonable, the derived table may be inlined to the query
* that selects from the resulting table.
* <p>
* <b>NOTE</b>: When inserting plain SQL into jOOQ objects, you must
* guarantee syntax integrity. You may also create the possibility of
* malicious SQL injection. Be sure to properly use bind variables and/or
* escape literals when concatenated into SQL clauses!
*
* @see DSL#condition(String, Object...)
* @see DSL#sql(String, Object...)
* @see SQL
*/
@Support
@PlainSQL
Table<R> where(String sql, Object... bindings);
/**
* Add a <code>WHERE</code> clause to the table.
* <p>
* The resulting table acts like a derived table that projects all of this
* table's columns and filters by the argument {@link Condition}. If
* syntactically reasonable, the derived table may be inlined to the query
* that selects from the resulting table.
* <p>
* <b>NOTE</b>: When inserting plain SQL into jOOQ objects, you must
* guarantee syntax integrity. You may also create the possibility of
* malicious SQL injection. Be sure to properly use bind variables and/or
* escape literals when concatenated into SQL clauses!
*
* @see DSL#condition(String, QueryPart...)
* @see DSL#sql(String, QueryPart...)
* @see SQL
*/
@Support
@PlainSQL
Table<R> where(String sql, QueryPart... parts);
/**
* Add a <code>WHERE EXISTS</code> clause to the table.
* <p>
* The resulting table acts like a derived table that projects all of this
* table's columns and filters by the argument {@link Condition}. If
* syntactically reasonable, the derived table may be inlined to the query
* that selects from the resulting table.
*/
@Support
Table<R> whereExists(Select<?> select);
/**
* Add a <code>WHERE NOT EXISTS</code> clause to the table.
* <p>
* The resulting table acts like a derived table that projects all of this
* table's columns and filters by the argument {@link Condition}. If
* syntactically reasonable, the derived table may be inlined to the query
* that selects from the resulting table.
*/
@Support
Table<R> whereNotExists(Select<?> select);
// -------------------------------------------------------------------------
// XXX: JOIN clauses on tables
// -------------------------------------------------------------------------

View File

@ -51,7 +51,11 @@ import static org.jooq.JoinType.NATURAL_RIGHT_OUTER_JOIN;
import static org.jooq.JoinType.OUTER_APPLY;
import static org.jooq.JoinType.RIGHT_OUTER_JOIN;
import static org.jooq.JoinType.STRAIGHT_JOIN;
import static org.jooq.impl.DSL.condition;
import static org.jooq.impl.DSL.exists;
// ...
import static org.jooq.impl.DSL.notExists;
import static org.jooq.impl.DSL.selectFrom;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.DSL.val;
import static org.jooq.impl.Tools.EMPTY_FIELD;
@ -90,6 +94,7 @@ import org.jooq.RecordType;
import org.jooq.Row;
import org.jooq.SQL;
import org.jooq.Schema;
import org.jooq.Select;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableLike;
@ -461,9 +466,6 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return Collections.emptyList();
}
/**
* {@inheritDoc}
*/
@Override
public final <O extends Record> List<ForeignKey<O, R>> getReferencesFrom(Table<O> other) {
return other.getReferencesTo(this);
@ -479,9 +481,6 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return Collections.emptyList();
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public final <O extends Record> List<ForeignKey<R, O>> getReferencesTo(Table<O> other) {
@ -1003,6 +1002,60 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return (TableOnStep) join(table, LEFT_ANTI_JOIN);
}
// ------------------------------------------------------------------------
// XXX: WHERE API
// ------------------------------------------------------------------------
@Override
public /* non-final */ Table<R> where(Condition condition) {
return table(selectFrom(this).where(condition)).as(this);
}
@Override
public /* non-final */ Table<R> where(Condition... conditions) {
return table(selectFrom(this).where(conditions)).as(this);
}
@Override
public /* non-final */ Table<R> where(Collection<? extends Condition> conditions) {
return table(selectFrom(this).where(conditions)).as(this);
}
@Override
public /* non-final */ Table<R> where(Field<Boolean> field) {
return where(condition(field));
}
@Override
public /* non-final */ Table<R> where(SQL sql) {
return where(condition(sql));
}
@Override
public /* non-final */ Table<R> where(String sql) {
return where(condition(sql));
}
@Override
public /* non-final */ Table<R> where(String sql, Object... bindings) {
return where(condition(sql, bindings));
}
@Override
public /* non-final */ Table<R> where(String sql, QueryPart... parts) {
return where(condition(sql, parts));
}
@Override
public /* non-final */ Table<R> whereExists(Select<?> select) {
return where(exists(select));
}
@Override
public /* non-final */ Table<R> whereNotExists(Select<?> select) {
return where(notExists(select));
}
// ------------------------------------------------------------------------
// XXX: JOIN API
// ------------------------------------------------------------------------