public class Query extends Object
Combines the generation of an sql-string and parameter set for
the ResultSetWrapper. Useful for one-time queries entered by the user.
Notice that by default the underlying prepared statement is closed
when the resultset (or cursor) is closed by the application.
You can change that behaviour, however.
Notice further, that the columns returned by the select must be provided by the application via add() _before_ execution. The SELECT, however, and LIMIT/OFFSET clauses are provided by the query automatically, because they are backend-specific.
| Constructor and Description |
|---|
Query()
Creates a query.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(String sql,
Object... data)
Appends an sql-part and corresponding parameters to this query.
|
void |
apply(Db db,
PreparedStatementWrapper st)
Applies the query parameters to the statement.
Optionally applies limit, offset as well. |
int |
applyParameters(PreparedStatementWrapper st,
int ndx)
Apply the query parameters only.
|
StringBuilder |
buildInnerSql()
Builds the inner sql query string.
|
String |
createSql(Db db)
Creates the SQL-String of this query.
Optionally modifies the sql query according to limit and offset. |
StatementKey |
createStatementKey(String sql)
Creates the statement key.
|
ResultSetWrapper |
execute(Db db)
Executes the query with ResultSet.TYPE_FORWARD_ONLY and ResultSet.CONCUR_READ_ONLY.
|
ResultSetWrapper |
execute(Db db,
int resultSetType)
Executes the query with ResultSet.CONCUR_READ_ONLY.
|
ResultSetWrapper |
execute(Db db,
int resultSetType,
int resultSetConcurrency)
Executes the query.
|
int |
getFetchSize()
Gets the fetchsize.
|
int |
getLimit()
Gets the maximum number of rows for this query.
|
int |
getMaxRows()
Gets the maximum row count for this cursor
|
int |
getOffset()
Gets the offset of this query.
|
int |
getRowCount(Db db)
Gets the number of objects returned by this query.
|
boolean |
isStatementCached()
Returns whether statement caching is enabled.
|
void |
setFetchSize(int fetchSize)
Sets the optional fetchsize.
|
void |
setLimit(int limit)
Sets the maximum number of rows to retrieve for this query.
|
void |
setMaxRows(int maxRows)
Sets the optional maximum row count for this cursor.
|
void |
setOffset(int offset)
Sets an offset, i.e.
|
void |
setStatementCached(boolean statementCached)
Enables caching of the prepared statement.
By default, queries are one-time prepared statements that will be closed when the resultset is closed. |
public void setStatementCached(boolean statementCached)
statementCached - true if prepare only once and reuse cached statement, false if one-shotpublic boolean isStatementCached()
public void setOffset(int offset)
offset - the offset, 0 to disablepublic int getOffset()
public void setLimit(int limit)
limit - the maximum number of rows, 0 if ulimited (default)public int getLimit()
public void add(String sql, Object... data)
Example: add(" AND CN_ID=?", object.getId());
add(" AND CN_MONEY=?", object.getAmount());
sql - is the SQL-stringdata - is an array of parameterspublic void setFetchSize(int fetchSize)
fetchSize - the fetchsizeResultSetWrapper.setFetchSize(int)public int getFetchSize()
ResultSetWrapper.getFetchSize()public void setMaxRows(int maxRows)
maxRows - the max rows, 0 = no limitpublic int getMaxRows()
public ResultSetWrapper execute(Db db, int resultSetType, int resultSetConcurrency)
db - is the database connectionresultSetType - is one of ResultSet.TYPE_...resultSetConcurrency - is one of ResultSet.CONCUR_..public ResultSetWrapper execute(Db db, int resultSetType)
db - is the database connectionresultSetType - is one of ResultSet.TYPE_...public ResultSetWrapper execute(Db db)
db - is the database connectionpublic int getRowCount(Db db)
This is not done by retrieving all rows but by a "SELECT COUNT(1)".
Applications may use this in conjunction with limit and offset for pagination
(in web pages, for example).
db - the db connectionpublic void apply(Db db, PreparedStatementWrapper st)
db - the database connectionst - the prepared statementpublic int applyParameters(PreparedStatementWrapper st, int ndx)
st - the statementndx - the starting indexpublic String createSql(Db db)
db - the db connectionpublic StatementKey createStatementKey(String sql)
sql - the SQL string sent to the backendpublic StringBuilder buildInnerSql()
Copyright © 2016 Krake Softwaretechnik. All rights reserved.