public class Where<T,ID> extends Object
Manages the various clauses that make up the WHERE part of a SQL statement. You get one of these when you call
StatementBuilder.where
or you can set the where clause by calling StatementBuilder.setWhere(com.j256.ormlite.stmt.Where<T, ID>)
.
Here's a page with a good tutorial of SQL commands.
To create a query which looks up an account by name and password you would do the following:
QueryBuilder<Account, String> qb = accountDao.queryBuilder(); Where where = qb.where(); // the name field must be equal to "foo" where.eq(Account.NAME_FIELD_NAME, "foo"); // and where.and(); // the password field must be equal to "_secret" where.eq(Account.PASSWORD_FIELD_NAME, "_secret"); PreparedQuery<Account, String> preparedQuery = qb.prepareQuery();
In this example, the SQL query that will be generated will be approximately:
SELECT * FROM account WHERE (name = 'foo' AND passwd = '_secret')
If you'd rather chain the methods onto one line (like StringBuilder), this can also be written as:
queryBuilder.where().eq(Account.NAME_FIELD_NAME, "foo").and().eq(Account.PASSWORD_FIELD_NAME, "_secret");
If you'd rather use parens and the like then you can call:
Where where = queryBuilder.where(); where.and(where.eq(Account.NAME_FIELD_NAME, "foo"), where.eq(Account.PASSWORD_FIELD_NAME, "_secret"));
All three of the above call formats produce the same SQL. For complex queries that mix ANDs and ORs, the last format will be necessary to get the grouping correct. For example, here's a complex query:
Where where = queryBuilder.where(); where.or(where.and(where.eq(Account.NAME_FIELD_NAME, "foo"), where.eq(Account.PASSWORD_FIELD_NAME, "_secret")), where.and(where.eq(Account.NAME_FIELD_NAME, "bar"), where.eq(Account.PASSWORD_FIELD_NAME, "qwerty")));
This produces the following approximate SQL:
SELECT * FROM account WHERE ((name = 'foo' AND passwd = '_secret') OR (name = 'bar' AND passwd = 'qwerty'))
Modifier | Constructor and Description |
---|---|
protected |
Where(TableInfo<T,ID> tableInfo,
StatementBuilder<T,ID> statementBuilder,
DatabaseType databaseType) |
Modifier and Type | Method and Description |
---|---|
Where<T,ID> |
and()
AND operation which takes the previous clause and the next clause and AND's them together.
|
Where<T,ID> |
and(int numClauses)
This method needs to be used carefully.
|
Where<T,ID> |
and(Where<T,ID> first,
Where<T,ID> second)
AND operation which takes 2 arguments and AND's them together.
|
Where<T,ID> |
and(Where<T,ID> first,
Where<T,ID> second,
Where<T,ID>... others)
AND operation which takes 2 (or more) arguments and AND's them together.
|
Where<T,ID> |
between(String columnName,
Object low,
Object high)
Add a BETWEEN clause so the column must be between the low and high parameters.
|
long |
countOf()
A short-cut for calling
QueryBuilder.countOf() . |
Where<T,ID> |
eq(String columnName,
Object value)
Add a '=' clause so the column must be equal to the value.
|
Where<T,ID> |
exists(QueryBuilder<?,?> subQueryBuilder)
Add a EXISTS clause with a sub-query inside of parenthesis.
|
Where<T,ID> |
ge(String columnName,
Object value)
Add a '>=' clause so the column must be greater-than or equals-to the value.
|
String |
getStatement()
Returns the associated SQL WHERE statement.
|
Where<T,ID> |
gt(String columnName,
Object value)
Add a '>' clause so the column must be greater-than the value.
|
<OD> Where<T,ID> |
idEq(Dao<OD,?> dataDao,
OD data)
Add a clause where the ID is from an existing object.
|
Where<T,ID> |
idEq(ID id)
Add a clause where the ID is equal to the argument.
|
Where<T,ID> |
in(String columnName,
Iterable<?> objects)
Add a IN clause so the column must be equal-to one of the objects from the list passed in.
|
Where<T,ID> |
in(String columnName,
Object... objects)
Add a IN clause so the column must be equal-to one of the objects passed in.
|
Where<T,ID> |
in(String columnName,
QueryBuilder<?,?> subQueryBuilder)
Add a IN clause which makes sure the column is in one of the columns returned from a sub-query inside of
parenthesis.
|
Where<T,ID> |
isNotNull(String columnName)
Add a 'IS NOT NULL' clause so the column must not be null.
|
Where<T,ID> |
isNull(String columnName)
Add a 'IS NULL' clause so the column must be null.
|
CloseableIterator<T> |
iterator()
A short-cut for calling
QueryBuilder.iterator() . |
Where<T,ID> |
le(String columnName,
Object value)
Add a '<=' clause so the column must be less-than or equals-to the value.
|
Where<T,ID> |
like(String columnName,
Object value)
Add a LIKE clause so the column must mach the value using '%' patterns.
|
Where<T,ID> |
lt(String columnName,
Object value)
Add a '<' clause so the column must be less-than the value.
|
Where<T,ID> |
ne(String columnName,
Object value)
Add a '<>' clause so the column must be not-equal-to the value.
|
Where<T,ID> |
not()
Used to NOT the next clause specified.
|
Where<T,ID> |
not(Where<T,ID> comparison)
Used to NOT the argument clause specified.
|
Where<T,ID> |
notIn(String columnName,
Iterable<?> objects)
Same as
in(String, Iterable) except with a NOT IN clause. |
Where<T,ID> |
notIn(String columnName,
Object... objects)
Same as
in(String, Object...) except with a NOT IN clause. |
Where<T,ID> |
notIn(String columnName,
QueryBuilder<?,?> subQueryBuilder)
Same as
in(String, QueryBuilder) except with a NOT IN clause. |
Where<T,ID> |
or()
OR operation which takes the previous clause and the next clause and OR's them together.
|
Where<T,ID> |
or(int numClauses)
This method needs to be used carefully.
|
Where<T,ID> |
or(Where<T,ID> left,
Where<T,ID> right)
OR operation which takes 2 arguments and OR's them together.
|
Where<T,ID> |
or(Where<T,ID> left,
Where<T,ID> right,
Where<T,ID>... others)
OR operation which takes 2 (or more) arguments and OR's them together.
|
PreparedQuery<T> |
prepare()
A short-cut for calling
QueryBuilder.prepare() . |
List<T> |
query()
A short-cut for calling
QueryBuilder.query() . |
QueryBuilder<T,ID> |
queryBuilder()
Returns the
QueryBuilder used to create this Where, if present |
T |
queryForFirst()
A short-cut for calling
QueryBuilder.queryForFirst() . |
GenericRawResults<String[]> |
queryRaw()
A short-cut for calling
QueryBuilder.queryRaw() . |
String[] |
queryRawFirst()
A short-cut for calling
QueryBuilder.queryRawFirst() . |
Where<T,ID> |
raw(String rawStatement,
ArgumentHolder... args)
Add a raw statement as part of the where that can be anything that the database supports.
|
Where<T,ID> |
rawComparison(String columnName,
String rawOperator,
Object value)
Make a comparison where the operator is specified by the caller.
|
Where<T,ID> |
reset()
Reset the Where object so it can be re-used.
|
String |
toString() |
protected Where(TableInfo<T,ID> tableInfo, StatementBuilder<T,ID> statementBuilder, DatabaseType databaseType)
public Where<T,ID> and()
public Where<T,ID> and(Where<T,ID> first, Where<T,ID> second)
NOTE: There is no guarantee of the order of the clauses that are generated in the final query.
NOTE: I can't remove the generics code warning that can be associated with this method. You can instead
use the and(int)
method.
public Where<T,ID> and(Where<T,ID> first, Where<T,ID> second, Where<T,ID>... others)
NOTE: There is no guarantee of the order of the clauses that are generated in the final query.
NOTE: I can't remove the generics code warning that can be associated with this method. You can instead
use the and(int)
method.
public Where<T,ID> and(int numClauses)
eq(String, Object)
or other methods and will string them together with AND's. There is no
way to verify the number of previous clauses so the programmer has to count precisely.
NOTE: There is no guarantee of the order of the clauses that are generated in the final query.
NOTE: This will throw an exception if numClauses is 0 but will work with 1 or more.
public Where<T,ID> between(String columnName, Object low, Object high) throws SQLException
SQLException
public Where<T,ID> eq(String columnName, Object value) throws SQLException
SQLException
public Where<T,ID> ge(String columnName, Object value) throws SQLException
SQLException
public Where<T,ID> gt(String columnName, Object value) throws SQLException
SQLException
public Where<T,ID> in(String columnName, Iterable<?> objects) throws SQLException
SQLException
public Where<T,ID> notIn(String columnName, Iterable<?> objects) throws SQLException
in(String, Iterable)
except with a NOT IN clause.SQLException
public Where<T,ID> in(String columnName, Object... objects) throws SQLException
SQLException
public Where<T,ID> notIn(String columnName, Object... objects) throws SQLException
in(String, Object...)
except with a NOT IN clause.SQLException
public Where<T,ID> in(String columnName, QueryBuilder<?,?> subQueryBuilder) throws SQLException
QueryBuilder.selectColumns(String...)
method calls. That 1 argument must match the SQL type of the
column-name passed to this method.
NOTE: The sub-query will be prepared at the same time that the outside query is.
SQLException
public Where<T,ID> notIn(String columnName, QueryBuilder<?,?> subQueryBuilder) throws SQLException
in(String, QueryBuilder)
except with a NOT IN clause.SQLException
public Where<T,ID> exists(QueryBuilder<?,?> subQueryBuilder)
NOTE: The sub-query will be prepared at the same time that the outside query is.
public Where<T,ID> isNull(String columnName) throws SQLException
SQLException
public Where<T,ID> isNotNull(String columnName) throws SQLException
SQLException
public Where<T,ID> le(String columnName, Object value) throws SQLException
SQLException
public Where<T,ID> lt(String columnName, Object value) throws SQLException
SQLException
public Where<T,ID> like(String columnName, Object value) throws SQLException
SQLException
public Where<T,ID> ne(String columnName, Object value) throws SQLException
SQLException
public Where<T,ID> or()
public Where<T,ID> or(Where<T,ID> left, Where<T,ID> right)
NOTE: There is no guarantee of the order of the clauses that are generated in the final query.
NOTE: I can't remove the generics code warning that can be associated with this method. You can instead
use the or(int)
method.
public Where<T,ID> or(Where<T,ID> left, Where<T,ID> right, Where<T,ID>... others)
NOTE: There is no guarantee of the order of the clauses that are generated in the final query.
NOTE: I can't remove the generics code warning that can be associated with this method. You can instead
use the or(int)
method.
public Where<T,ID> or(int numClauses)
eq(String, Object)
or other methods and will string them together with OR's. There is no
way to verify the number of previous clauses so the programmer has to count precisely.
NOTE: There is no guarantee of the order of the clauses that are generated in the final query.
NOTE: This will throw an exception if numClauses is 0 but will work with 1 or more.
public Where<T,ID> idEq(ID id) throws SQLException
SQLException
public <OD> Where<T,ID> idEq(Dao<OD,?> dataDao, OD data) throws SQLException
SQLException
public Where<T,ID> raw(String rawStatement, ArgumentHolder... args)
rawStatement
- The statement that we should insert into the WHERE.args
- Optional arguments that correspond to any ? specified in the rawStatement. Each of the arguments must
have either the corresponding columnName or the sql-type set. WARNING, you cannot use the
SelectArg("columnName")
constructor since that sets the _value_, not the name. Use
new SelectArg("column-name", null);
.public Where<T,ID> rawComparison(String columnName, String rawOperator, Object value) throws SQLException
SQLException
public PreparedQuery<T> prepare() throws SQLException
QueryBuilder.prepare()
.SQLException
public List<T> query() throws SQLException
QueryBuilder.query()
.SQLException
public GenericRawResults<String[]> queryRaw() throws SQLException
QueryBuilder.queryRaw()
.SQLException
public T queryForFirst() throws SQLException
QueryBuilder.queryForFirst()
.SQLException
public String[] queryRawFirst() throws SQLException
QueryBuilder.queryRawFirst()
.SQLException
public long countOf() throws SQLException
QueryBuilder.countOf()
.SQLException
public CloseableIterator<T> iterator() throws SQLException
QueryBuilder.iterator()
.SQLException
public String getStatement() throws SQLException
SQLException
public QueryBuilder<T,ID> queryBuilder() throws SQLException
QueryBuilder
used to create this Where, if presentSQLException
This documentation is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.