com.j256.ormlite.stmt
Class Where

java.lang.Object
  extended by com.j256.ormlite.stmt.Where

public class Where
extends Object

Manages the various clauses that make up the WHERE part of a SQL statement. You get one of these when you call QueryBuilder.where or you can set the where clause by calling QueryBuilder.setWhere(com.j256.ormlite.stmt.Where).

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'))
 

Author:
graywatson

Method Summary
 Where and()
          AND operation which takes the previous clause and the next clause and AND's them together.
 Where and(Where left, Where right)
          AND operation which takes 2 arguments and AND's them together.
 Where between(String columnName, Object low, Object high)
          Add a BETWEEN clause so the column must be between the low and high parameters.
 Where eq(String columnName, Object value)
          Add a '=' clause so the column must be equal to the value.
 Where ge(String columnName, Object value)
          Add a '>=' clause so the column must be greater-than or equals-to the value.
 Where gt(String columnName, Object value)
          Add a '>' clause so the column must be greater-than the value.
 Where 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 in(String columnName, Object... objects)
          Add a IN clause so the column must be equal-to one of the objects passed in.
 Where isNotNull(String columnName)
          Add a 'IS NOT NULL' clause so the column must not be null.
 Where isNull(String columnName)
          Add a 'IS NULL' clause so the column must be null.
 Where le(String columnName, Object value)
          Add a '<=' clause so the column must be less-than or equals-to the value.
 Where like(String columnName, Object value)
          Add a LIKE clause so the column must be like the value (where you can specify '%' patterns.
 Where lt(String columnName, Object value)
          Add a '<' clause so the column must be less-than the value.
 Where ne(String columnName, Object value)
          Add a '<>' clause so the column must be not-equal-to the value.
 Where not()
          Used to NOT the next clause specified.
 Where not(Where comparison)
          Used to NOT the argument clause specified.
 Where or()
          OR operation which takes the previous clause and the next clause and OR's them together.
 Where or(Where left, Where right)
          OR operation which takes 2 arguments and OR's them together.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

and

public Where and()
AND operation which takes the previous clause and the next clause and AND's them together.


and

public Where and(Where left,
                 Where right)
AND operation which takes 2 arguments and AND's them together.


between

public Where between(String columnName,
                     Object low,
                     Object high)
              throws SQLException
Add a BETWEEN clause so the column must be between the low and high parameters.

Throws:
SQLException

eq

public Where eq(String columnName,
                Object value)
         throws SQLException
Add a '=' clause so the column must be equal to the value.

Throws:
SQLException

ge

public Where ge(String columnName,
                Object value)
         throws SQLException
Add a '>=' clause so the column must be greater-than or equals-to the value.

Throws:
SQLException

gt

public Where gt(String columnName,
                Object value)
         throws SQLException
Add a '>' clause so the column must be greater-than the value.

Throws:
SQLException

in

public Where in(String columnName,
                Iterable<?> objects)
         throws SQLException
Add a IN clause so the column must be equal-to one of the objects from the list passed in.

Throws:
SQLException

in

public Where in(String columnName,
                Object... objects)
         throws SQLException
Add a IN clause so the column must be equal-to one of the objects passed in.

Throws:
SQLException

isNull

public Where isNull(String columnName)
             throws SQLException
Add a 'IS NULL' clause so the column must be null. '=' NULL does not work.

Throws:
SQLException

isNotNull

public Where isNotNull(String columnName)
                throws SQLException
Add a 'IS NOT NULL' clause so the column must not be null. '<>' NULL does not work.

Throws:
SQLException

le

public Where le(String columnName,
                Object value)
         throws SQLException
Add a '<=' clause so the column must be less-than or equals-to the value.

Throws:
SQLException

lt

public Where lt(String columnName,
                Object value)
         throws SQLException
Add a '<' clause so the column must be less-than the value.

Throws:
SQLException

like

public Where like(String columnName,
                  Object value)
           throws SQLException
Add a LIKE clause so the column must be like the value (where you can specify '%' patterns.

Throws:
SQLException

ne

public Where ne(String columnName,
                Object value)
         throws SQLException
Add a '<>' clause so the column must be not-equal-to the value.

Throws:
SQLException

not

public Where not()
Used to NOT the next clause specified.


not

public Where not(Where comparison)
Used to NOT the argument clause specified.


or

public Where or()
OR operation which takes the previous clause and the next clause and OR's them together.


or

public Where or(Where left,
                Where right)
OR operation which takes 2 arguments and OR's them together.


toString

public String toString()
Overrides:
toString in class Object


Copyright © 2010. All Rights Reserved.