T
- The class that the code will be operating on.ID
- The class of the ID column associated with the class. The T class does not require an ID field. The class
needs an ID parameter however so you can use Void or Object to satisfy the compiler.public class QueryBuilder<T,ID> extends StatementBuilder<T,ID>
Here is a good tutorial of SQL commands.
Modifier and Type | Class and Description |
---|---|
static class |
QueryBuilder.InternalQueryBuilderWrapper
Internal class used to expose methods to internal classes but through a wrapper instead of a builder.
|
static class |
QueryBuilder.JoinType
Type of the JOIN that we are adding.
|
static class |
QueryBuilder.JoinWhereOperation
When we are combining WHERE statements from the two joined query-builders, this determines the operator to use to
do so.
|
StatementBuilder.StatementInfo, StatementBuilder.StatementType, StatementBuilder.WhereOperation
addTableName, dao, databaseType, tableInfo, tableName, type, where
Constructor and Description |
---|
QueryBuilder(DatabaseType databaseType,
TableInfo<T,ID> tableInfo,
Dao<T,ID> dao) |
Modifier and Type | Method and Description |
---|---|
protected void |
appendStatementEnd(StringBuilder sb,
List<ArgumentHolder> argList)
Append the end of our statement string to the StringBuilder.
|
protected void |
appendStatementStart(StringBuilder sb,
List<ArgumentHolder> argList)
Append the start of our statement string to the StringBuilder.
|
protected void |
appendTableQualifier(StringBuilder sb) |
protected boolean |
appendWhereStatement(StringBuilder sb,
List<ArgumentHolder> argList,
StatementBuilder.WhereOperation operation)
Append the WHERE part of the statement to the StringBuilder.
|
long |
countOf()
Returns the count of the number of rows in the table.
|
long |
countOf(String countOfQuery)
Returns the count of the number of rows that match a field so you can do
qb.countOf("DISTINCT(fieldname)") . |
QueryBuilder<T,ID> |
distinct()
Add "DISTINCT" clause to the SQL query statement.
|
protected FieldType[] |
getResultFieldTypes()
Get the result array from our statement after the
StatementBuilder.appendStatementStart(StringBuilder, List) was called. |
protected String |
getTableName() |
QueryBuilder<T,ID> |
groupBy(String columnName)
Add "GROUP BY" clause to the SQL query statement.
|
QueryBuilder<T,ID> |
groupByRaw(String rawSql)
Add a raw SQL "GROUP BY" clause to the SQL query statement.
|
QueryBuilder<T,ID> |
having(String having)
Add raw SQL "HAVING" clause to the SQL query statement.
|
CloseableIterator<T> |
iterator()
A short cut to
Dao.iterator(PreparedQuery) . |
QueryBuilder<T,ID> |
join(QueryBuilder<?,?> joinedQueryBuilder)
Join with another query builder.
|
QueryBuilder<T,ID> |
join(QueryBuilder<?,?> joinedQueryBuilder,
QueryBuilder.JoinType type,
QueryBuilder.JoinWhereOperation operation)
Like
join(QueryBuilder) but allows you to specify the join type and the operation used to combine the
WHERE statements. |
QueryBuilder<T,ID> |
join(String localColumnName,
String joinedColumnName,
QueryBuilder<?,?> joinedQueryBuilder)
Similar to
join(QueryBuilder) but this allows you to link two tables that share a field of the same
type. |
QueryBuilder<T,ID> |
join(String localColumnName,
String joinedColumnName,
QueryBuilder<?,?> joinedQueryBuilder,
QueryBuilder.JoinType type,
QueryBuilder.JoinWhereOperation operation)
Similar to
join(QueryBuilder, JoinType, JoinWhereOperation) but this allows you to link two tables that
share a field of the same type. |
QueryBuilder<T,ID> |
joinOr(QueryBuilder<?,?> joinedQueryBuilder)
Like
join(QueryBuilder) but this combines the WHERE statements of two query builders with a SQL "OR". |
QueryBuilder<T,ID> |
leftJoin(QueryBuilder<?,?> joinedQueryBuilder)
Similar to
join(QueryBuilder) but it will use "LEFT JOIN" instead. |
QueryBuilder<T,ID> |
leftJoinOr(QueryBuilder<?,?> joinedQueryBuilder)
Like
leftJoin(QueryBuilder) but this combines the WHERE statements of two query builders with a SQL
"OR". |
QueryBuilder<T,ID> |
limit(Long maxRows)
Limit the output to maxRows maximum number of rows.
|
QueryBuilder<T,ID> |
offset(Long startRow)
Start the output at this row number.
|
QueryBuilder<T,ID> |
orderBy(String columnName,
boolean ascending)
Add "ORDER BY" clause to the SQL query statement.
|
QueryBuilder<T,ID> |
orderByNullsFirst(String columnName,
boolean ascending)
Add "ORDER BY" clause to the SQL query statement with a column-name and ascending (if true) with a NULLS FIRST
qualifier.
|
QueryBuilder<T,ID> |
orderByNullsLast(String columnName,
boolean ascending)
Add "ORDER BY" clause to the SQL query statement with a column-name and ascending (if true) with a NULLS LAST
qualifier.
|
QueryBuilder<T,ID> |
orderByRaw(String rawSql)
Add raw SQL "ORDER BY" clause to the SQL query statement.
|
QueryBuilder<T,ID> |
orderByRaw(String rawSql,
ArgumentHolder... args)
Add raw SQL "ORDER BY" clause to the SQL query statement.
|
PreparedQuery<T> |
prepare()
Build and return a prepared query that can be used by
Dao.query(PreparedQuery) or
Dao.iterator(PreparedQuery) methods. |
List<T> |
query()
A short cut to
Dao.query(PreparedQuery) . |
T |
queryForFirst()
A short cut to
Dao.queryForFirst(PreparedQuery) . |
GenericRawResults<String[]> |
queryRaw()
A short cut to
Dao.queryRaw(String, String...) . |
String[] |
queryRawFirst()
A short cut to
Dao.queryRaw(String, String...) and GenericRawResults.getFirstResult() . |
void |
reset()
Clear out all of the statement settings so we can reuse the builder.
|
QueryBuilder<T,ID> |
selectColumns(Iterable<String> columns)
Same as
selectColumns(String...) except the columns are specified as an iterable -- probably will be a
Collection . |
QueryBuilder<T,ID> |
selectColumns(String... columns)
Add columns to be returned by the SELECT query.
|
QueryBuilder<T,ID> |
selectRaw(String... columns)
Add raw columns or aggregate functions (COUNT, MAX, ...) to the query.
|
QueryBuilder<T,ID> |
setAlias(String alias)
Add an alias for this table.
|
QueryBuilder<T,ID> |
setCountOf(boolean countOf)
Set whether or not we should only return the count of the results.
|
QueryBuilder<T,ID> |
setCountOf(String countOfQuery)
Set the field that we are counting from the database.
|
protected boolean |
shouldPrependTableNameToColumns()
Return true if we need to prepend table-name to columns.
|
appendStatementString, buildStatementString, prepareStatement, prepareStatementInfo, prepareStatementString, setWhere, verifyColumnName, where
public PreparedQuery<T> prepare() throws SQLException
Dao.query(PreparedQuery)
or
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 statement for execution.SQLException
public QueryBuilder<T,ID> selectColumns(String... columns)
WARNING: If you specify any columns to return, then any foreign-collection fields will be returned as null
unless their ForeignCollectionField.columnName()
is also in the list.
public QueryBuilder<T,ID> selectColumns(Iterable<String> columns)
selectColumns(String...)
except the columns are specified as an iterable -- probably will be a
Collection
. This can be called multiple times to add more columns to select.public QueryBuilder<T,ID> selectRaw(String... columns)
Dao.queryRaw(String, String...)
type of statement. This can be called multiple
times to add more columns to select.public QueryBuilder<T,ID> groupBy(String columnName)
NOTE: Use of this means that the resulting objects may not have a valid ID column value so cannot be deleted or updated.
public QueryBuilder<T,ID> groupByRaw(String rawSql)
public QueryBuilder<T,ID> orderBy(String columnName, boolean ascending)
public QueryBuilder<T,ID> orderByNullsFirst(String columnName, boolean ascending)
public QueryBuilder<T,ID> orderByNullsLast(String columnName, boolean ascending)
public QueryBuilder<T,ID> orderByRaw(String rawSql)
rawSql
- The raw SQL order by clause. This should not include the "ORDER BY".public QueryBuilder<T,ID> orderByRaw(String rawSql, ArgumentHolder... args)
rawSql
- The raw SQL order by clause. This should not include the "ORDER BY".args
- Optional arguments that correspond to any ? specified in the rawSql. Each of the arguments must have
the sql-type set.public QueryBuilder<T,ID> distinct()
NOTE: Use of this means that the resulting objects may not have a valid ID column value so cannot be deleted or updated.
public QueryBuilder<T,ID> limit(Long maxRows)
public QueryBuilder<T,ID> offset(Long startRow) throws SQLException
Dao.iterator()
method instead which handles paging with a database cursor.
Otherwise, if you are paging you probably want to specify a orderBy(String, boolean)
.
NOTE: This is not supported for all databases. Also, for some databases, the limit _must_ also be specified since the offset is an argument of the limit.
SQLException
public QueryBuilder<T,ID> setCountOf(boolean countOf)
Dao.countOf(PreparedQuery)
.
To get the count-of directly, use countOf()
.public QueryBuilder<T,ID> setCountOf(String countOfQuery)
qb.setCountOf("DISTINCT(fieldname)")
. This query can then be used by Dao.countOf(PreparedQuery)
.
To get the count-of directly, use countOf(String)
.public QueryBuilder<T,ID> having(String having)
public QueryBuilder<T,ID> join(QueryBuilder<?,?> joinedQueryBuilder) throws SQLException
NOTE: This will do combine the WHERE statement of the two query builders with a SQL "AND". See
joinOr(QueryBuilder)
.
SQLException
public QueryBuilder<T,ID> join(QueryBuilder<?,?> joinedQueryBuilder, QueryBuilder.JoinType type, QueryBuilder.JoinWhereOperation operation) throws SQLException
join(QueryBuilder)
but allows you to specify the join type and the operation used to combine the
WHERE statements.SQLException
public QueryBuilder<T,ID> joinOr(QueryBuilder<?,?> joinedQueryBuilder) throws SQLException
join(QueryBuilder)
but this combines the WHERE statements of two query builders with a SQL "OR".SQLException
public QueryBuilder<T,ID> leftJoin(QueryBuilder<?,?> joinedQueryBuilder) throws SQLException
join(QueryBuilder)
but it will use "LEFT JOIN" instead.
See: LEFT JOIN SQL docs
NOTE: RIGHT and FULL JOIN SQL commands are not supported because we are only returning objects from the "left" table.
NOTE: This will do combine the WHERE statement of the two query builders with a SQL "AND". See
leftJoinOr(QueryBuilder)
.
SQLException
public QueryBuilder<T,ID> leftJoinOr(QueryBuilder<?,?> joinedQueryBuilder) throws SQLException
leftJoin(QueryBuilder)
but this combines the WHERE statements of two query builders with a SQL
"OR".SQLException
public QueryBuilder<T,ID> join(String localColumnName, String joinedColumnName, QueryBuilder<?,?> joinedQueryBuilder) throws SQLException
join(QueryBuilder)
but this allows you to link two tables that share a field of the same
type. So even if there is _not_ a foreign-object relationship between the tables, you can JOIN them. This will
add into the SQL something close to " INNER JOIN other-table ...".SQLException
public QueryBuilder<T,ID> join(String localColumnName, String joinedColumnName, QueryBuilder<?,?> joinedQueryBuilder, QueryBuilder.JoinType type, QueryBuilder.JoinWhereOperation operation) throws SQLException
join(QueryBuilder, JoinType, JoinWhereOperation)
but this allows you to link two tables that
share a field of the same type.SQLException
public List<T> query() throws SQLException
Dao.query(PreparedQuery)
.SQLException
public GenericRawResults<String[]> queryRaw() throws SQLException
Dao.queryRaw(String, String...)
.SQLException
public T queryForFirst() throws SQLException
Dao.queryForFirst(PreparedQuery)
.SQLException
public String[] queryRawFirst() throws SQLException
Dao.queryRaw(String, String...)
and GenericRawResults.getFirstResult()
.SQLException
public CloseableIterator<T> iterator() throws SQLException
Dao.iterator(PreparedQuery)
.SQLException
public long countOf() throws SQLException
setCountOf(boolean)
to true and then
calls Dao.countOf(PreparedQuery)
. It restores the previous count-of value before returning.SQLException
public long countOf(String countOfQuery) throws SQLException
qb.countOf("DISTINCT(fieldname)")
. This uses setCountOf(String)
and then calls
Dao.countOf(PreparedQuery)
. It restores the previous count-of value before returning.SQLException
public QueryBuilder<T,ID> setAlias(String alias)
public void reset()
StatementBuilder
reset
in class StatementBuilder<T,ID>
protected void appendStatementStart(StringBuilder sb, List<ArgumentHolder> argList)
StatementBuilder
appendStatementStart
in class StatementBuilder<T,ID>
protected FieldType[] getResultFieldTypes()
StatementBuilder
StatementBuilder.appendStatementStart(StringBuilder, List)
was called.
This will be null except for the QueryBuilder.getResultFieldTypes
in class StatementBuilder<T,ID>
protected boolean appendWhereStatement(StringBuilder sb, List<ArgumentHolder> argList, StatementBuilder.WhereOperation operation) throws SQLException
StatementBuilder
appendWhereStatement
in class StatementBuilder<T,ID>
SQLException
protected void appendStatementEnd(StringBuilder sb, List<ArgumentHolder> argList) throws SQLException
StatementBuilder
appendStatementEnd
in class StatementBuilder<T,ID>
SQLException
protected boolean shouldPrependTableNameToColumns()
StatementBuilder
shouldPrependTableNameToColumns
in class StatementBuilder<T,ID>
protected void appendTableQualifier(StringBuilder sb)
protected String getTableName()
getTableName
in class StatementBuilder<T,ID>
This documentation is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.