|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.skife.sql.QueryBuilder
QueryBuilder provides a convenient way to build queries programmatically. It supports adding query components in any order and will dynamically construct the resultant SQL query string from all of the components added.
Within a single type of component the ordering is maintained, but it doesn't matter if you specify the where criteria before the tables being queried, etc.
As the ordering can be flexible a mechanism for supporting prepared statements
is also included. Tokens of the form {foo}
will have ?
substituted for them in the generated query string. To then bind the
values to the query there are two available techniques: a map with the
tokens as keys
and get back a List
of the objects to bind in order via the
getBindList(Map map)
method; or the more convenient form,
bind(PreparedStatement stmt, Map bindings)
method which takes the
prepared statement and map of binding values as arguments and binds the values
in the correct order for you.
Field Summary | |
static java.lang.String |
TOKEN_PATTERN
If in doubt as to whether a token is okay or not, here is the regex used to match tokens: "\\{\\s*?(\\w+?)\\s*?\\}" |
Method Summary | |
org.skife.sql.QueryBuilder |
all()
Return all columns |
java.sql.PreparedStatement |
bind(java.sql.PreparedStatement stmt,
java.util.Map bindings)
Binds the bindings to the stmt assuming the statement was built with this querybuilder |
org.skife.sql.QueryBuilder |
column(java.lang.String name)
Specify a column to include in the result set |
org.skife.sql.QueryBuilder |
distinct()
Specify to return distinct results, ie: "select distinct foo from bar" |
org.skife.sql.QueryBuilder |
from(java.lang.String table)
Specify a table to query against Joins are added to the same component as From tables |
org.skife.sql.QueryBuilder |
fullOuterJoin(java.lang.String left,
java.lang.String right,
java.lang.String criteria)
Specify a full outer innerJoin. |
java.util.ArrayList |
getBindList(java.util.Map props)
Convenience method used to properly order binding values to the constructed query. |
java.lang.String |
getQueryString()
Obtain the query string built thus far. |
java.lang.String |
getQueryStringWithTokens()
Obtain the query string without token substitution. |
java.util.List |
getTokens()
Obtain a list of all the tokens in the order they appear in the query |
org.skife.sql.QueryBuilder |
groupBy(java.lang.String clause)
Specify an "group by" clause |
org.skife.sql.QueryBuilder |
in(java.lang.String column,
java.util.Collection values)
Specify an SQL "IN" clause to include in the where component. |
org.skife.sql.QueryBuilder |
in(java.lang.String column,
java.lang.String[] values)
Specify an SQL "IN" clause to include in the where component. |
org.skife.sql.QueryBuilder |
innerJoin(java.lang.String left,
java.lang.String right,
java.lang.String criteria)
Specify an inner innerJoin. |
org.skife.sql.QueryBuilder |
leftOuterJoin(java.lang.String left,
java.lang.String right,
java.lang.String criteria)
Specify a left outer innerJoin. |
org.skife.sql.QueryBuilder |
notIn(java.lang.String column,
java.util.Collection values)
Specify an SQL "NOT IN" clause to include in the where component. |
org.skife.sql.QueryBuilder |
notIn(java.lang.String column,
java.lang.String[] values)
Specify an SQL "NOT IN" clause to include in the where component. |
org.skife.sql.QueryBuilder |
orderBy(java.lang.String clause)
Specify an "order by" clause |
org.skife.sql.QueryBuilder |
rightOuterJoin(java.lang.String left,
java.lang.String right,
java.lang.String criteria)
Specify a right outer innerJoin. |
static org.skife.sql.QueryBuilder |
select()
Factory method used to generate a "select..." style query |
org.skife.sql.QueryBuilder |
subSelect(java.lang.String column,
org.skife.sql.QueryBuilder query)
Specify a sub select for the query of the form "where ([column] in ([query.getQueryString()])) |
org.skife.sql.QueryBuilder |
subSelect(java.lang.String column,
java.lang.String query)
Specify a sub select for the query of the form "where ([column] in ([query])) |
org.skife.sql.QueryBuilder |
subSelectDisjunction(java.lang.String column,
org.skife.sql.QueryBuilder query)
Specify a sub select disjunction for the query of the form "where ([column] not in ([query])) |
org.skife.sql.QueryBuilder |
subSelectDisjunction(java.lang.String column,
java.lang.String query)
Specify a sub select disjunction for the query of the form "where ([column] not in ([query])) |
java.lang.String |
toString()
|
org.skife.sql.QueryBuilder |
where(java.lang.String clause)
Add to the where clause of the query |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final java.lang.String TOKEN_PATTERN
"\\{\\s*?(\\w+?)\\s*?\\}"
Method Detail |
public static org.skife.sql.QueryBuilder select()
public org.skife.sql.QueryBuilder distinct()
public org.skife.sql.QueryBuilder all()
public org.skife.sql.QueryBuilder column(java.lang.String name)
name
- The column name ("column"), and optionally an alias ("column alias") to include
public org.skife.sql.QueryBuilder from(java.lang.String table)
table
- name of the table to query
public org.skife.sql.QueryBuilder where(java.lang.String clause)
clause
- the criteria to include in the where clause
public org.skife.sql.QueryBuilder in(java.lang.String column, java.lang.String[] values)
values
to the form "where ( column in
" (values[0], values[1], ... , values[n-1], values[n]))
column
- column for the subject of the in clausevalues
- values for the object of the in clause
public org.skife.sql.QueryBuilder notIn(java.lang.String column, java.lang.String[] values)
values
to the form "where ( column not in
" (values[0], values[1], ... , values[n-1], values[n]))
column
- column for the subject of the in clausevalues
- values for the object of the in clause
public org.skife.sql.QueryBuilder in(java.lang.String column, java.util.Collection values)
values
to the form "where ( column in
" (values[0], values[1], ... , values[n-1], values[n]))
column
- column for the subject of the in clausevalues
- values for the object of the in clause
public org.skife.sql.QueryBuilder notIn(java.lang.String column, java.util.Collection values)
values
to the form "where ( column not in
" (values[0], values[1], ... , values[n-1], values[n]))
column
- column for the subject of the in clausevalues
- values for the object of the in clause
public org.skife.sql.QueryBuilder subSelect(java.lang.String column, java.lang.String query)
column
- subject of the subselect clausequery
- object of the subselect clause
public org.skife.sql.QueryBuilder subSelect(java.lang.String column, org.skife.sql.QueryBuilder query)
column
- subject of the subselect clausequery
- object of the subselect clause
public org.skife.sql.QueryBuilder subSelectDisjunction(java.lang.String column, java.lang.String query)
column
- subject of the subselect clausequery
- object of the subselect clause
public org.skife.sql.QueryBuilder subSelectDisjunction(java.lang.String column, org.skife.sql.QueryBuilder query)
column
- subject of the subselect clausequery
- object of the subselect clause
public org.skife.sql.QueryBuilder leftOuterJoin(java.lang.String left, java.lang.String right, java.lang.String criteria)
All joins are specified in the From component
left
- column on the left side of the innerJoinright
- column on the right side of the innerJoincriteria
- innerJoin criteria
public org.skife.sql.QueryBuilder rightOuterJoin(java.lang.String left, java.lang.String right, java.lang.String criteria)
All joins are specified in the From component
left
- column on the left side of the innerJoinright
- column on the right side of the innerJoincriteria
- innerJoin criteria
public org.skife.sql.QueryBuilder fullOuterJoin(java.lang.String left, java.lang.String right, java.lang.String criteria)
All joins are specified in the From component
left
- column on the left side of the innerJoinright
- column on the right side of the innerJoincriteria
- innerJoin criteria
public org.skife.sql.QueryBuilder innerJoin(java.lang.String left, java.lang.String right, java.lang.String criteria)
All joins are specified in the From component
left
- column on the left side of the innerJoinright
- column on the right side of the innerJoincriteria
- innerJoin criteria
public org.skife.sql.QueryBuilder orderBy(java.lang.String clause)
public org.skife.sql.QueryBuilder groupBy(java.lang.String clause)
public java.lang.String getQueryString()
bind(...)
or getBindList(...)
to build the
properly ordered prepared statement bind values
public java.lang.String getQueryStringWithTokens()
public java.util.List getTokens()
public java.util.ArrayList getBindList(java.util.Map props)
The binding process can also be handled by this class via the bind(...)
method, which is probably more convenient than this form most of the time.
To use with commons-dbutils querying functionality use the toArray() functionality on the returned list.
props
- token -> value to bind to that token
java.lang.IllegalArgumentException
- if it finds a token in the query
string without without a matching binding in the props
argumentpublic java.sql.PreparedStatement bind(java.sql.PreparedStatement stmt, java.util.Map bindings) throws java.sql.SQLException
java.lang.IllegalArgumentException
- if there is an unmatched token
java.sql.SQLException
- if there is a problem setting a binding on the PreparedStatementpublic java.lang.String toString()
toString
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |