com.j256.ormlite.stmt
Class QueryBuilder<T,ID>

java.lang.Object
  extended by com.j256.ormlite.stmt.QueryBuilder<T,ID>
Direct Known Subclasses:
QueryBuilder.InternalQueryBuilder

public class QueryBuilder<T,ID>
extends Object

Assists in building SQL query (select) statements for a particular table in a particular database. Uses the DatabaseType to get per-database SQL statements. By default the resulting queries will return objects with all columns -- doing the equivalent of 'select * from table'. See columns(Iterable) or columns(String...) to return partial column lists.

Here is a good tutorial of SQL commands.

Author:
graywatson

Nested Class Summary
static class QueryBuilder.InternalQueryBuilder<T,ID>
           
 
Constructor Summary
QueryBuilder(DatabaseType databaseType, TableInfo<T> tableInfo)
          Provides statements for various SQL operations.
 
Method Summary
 QueryBuilder<T,ID> columns(Iterable<String> columns)
          Add columns to be returned by the query.
 QueryBuilder<T,ID> columns(String... columns)
          Add columns to be returned by the query.
 QueryBuilder<T,ID> distinct()
          Add "DISTINCT" clause to the SQL query statement.
 QueryBuilder<T,ID> groupBy(String columnName)
          Add "GROUP BY" clauses to the SQL query statement.
 QueryBuilder<T,ID> limit(Integer maxRows)
          Limit the output to maxRows maximum number of rows.
 QueryBuilder<T,ID> orderBy(String columnName, boolean ascending)
          Add "ORDER BY" clauses to the SQL query statement.
 PreparedQuery<T> prepareQuery()
          Build and return a PreparedQuery object which then can be used by Dao.query(PreparedQuery) and Dao.iterator(PreparedQuery) methods.
 String prepareQueryString()
          Build and return a string version of the query.
 void setWhere(Where where)
          Set the Where object on the query.
 Where where()
          Returns a Where object that should be used to add SQL where clauses to the statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QueryBuilder

public QueryBuilder(DatabaseType databaseType,
                    TableInfo<T> tableInfo)
Provides statements for various SQL operations.

Parameters:
databaseType - Database type.
tableInfo - Information about the table/class that is being handled.
Method Detail

columns

public QueryBuilder<T,ID> columns(String... columns)
Add columns to be returned by the query. If no column...() method called then all columns are returned by default.


columns

public QueryBuilder<T,ID> columns(Iterable<String> columns)
Add columns to be returned by the query. If no column...() method called then all columns are returned by default.


groupBy

public QueryBuilder<T,ID> groupBy(String columnName)
Add "GROUP BY" clauses to the SQL query statement. NOTE: Use of this means that the resulting objects may not have a valid ID column value so cannot be deleted or updated.


orderBy

public QueryBuilder<T,ID> orderBy(String columnName,
                                  boolean ascending)
Add "ORDER BY" clauses to the SQL query statement.


distinct

public QueryBuilder<T,ID> distinct()
Add "DISTINCT" clause to the SQL query statement. NOTE: Use of this means that the resulting objects may not have a valid ID column value so cannot be deleted or updated.


limit

public QueryBuilder<T,ID> limit(Integer maxRows)
Limit the output to maxRows maximum number of rows. Set to null for no limit (the default). This is implemented at the database level either through a LIMIT SQL query addition or a JDBC setMaxRows method call.


where

public Where where()
Returns a Where object that should be used to add SQL where clauses to the statement. This will also reset the where object so you can use the same query builder with a different where statement.


setWhere

public void setWhere(Where where)
Set the Where object on the query. This allows someone to use the same Where object on multiple queries.


prepareQuery

public PreparedQuery<T> prepareQuery()
Build and return a PreparedQuery object which then can be used by Dao.query(PreparedQuery) and Dao.iterator(PreparedQuery) methods. If you change the where or make other calls you will need to re-call this method to re-prepare the query for execution.


prepareQueryString

public String prepareQueryString()
Build and return a string version of the query. If you change the where or make other calls you will need to re-call this method to re-prepare the query for execution.

This is mostly used for debugging or logging cases. The dao classes us the prepareQuery() method.



Copyright © 2010. All Rights Reserved.