Class Query
java.lang.Object
org.tentackle.dbms.Query
A database query.
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 result-set (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.
- Author:
- harald
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(int index, CharSequence sql, Object... data) Appends an SQL-part and corresponding parameters to this query.voidadd(int index, CharSequence sql, List<Object> data) Appends an SQL-part and corresponding parameters to this query.
Same asadd(int, CharSequence, Object...)but with aListof parameters instead of an array or varargs.voidadd(int index, SqlSupplier sqlSupplier) Appends a backend-specific SQL-part to this query.voidadd(CharSequence sql, Object... data) Appends an SQL-part and corresponding parameters to this query.voidadd(CharSequence sql, List<Object> data) Appends an SQL-part and corresponding parameters to this query.
Same asadd(CharSequence, Object...)but with aListof parameters instead of an array or varargs.voidadd(SqlSupplier sqlSupplier) Appends a backend-specific SQL-part to this query.voidapply(Db db, PreparedStatementWrapper st) Applies the query parameters to the statement.
Optionally applies limit, offset as well.intapplyParameters(PreparedStatementWrapper st, int ndx) Apply the query parameters only.buildInnerSql(Backend backend) Builds the inner sql query string.Creates the SQL-code of this query.
Optionally modifies the query according to limit and offset.createStatementKey(String sql) Creates the statement key.Executes the query with ResultSet.TYPE_FORWARD_ONLY and ResultSet.CONCUR_READ_ONLY.Executes the query with ResultSet.CONCUR_READ_ONLY.Executes the query.intGets the fetchsize.intgetLimit()Gets the maximum number of rows for this query.intGets the maximum row count for this cursorintGets the offset of this query.intgetRowCount(Db db) Gets the number of objects returned by this query.booleanReturns whether statement caching is enabled.voidsetFetchSize(int fetchSize) Sets the optional fetchsize. 0 = drivers default.voidsetLimit(int limit) Sets the maximum number of rows to retrieve for this query. evoidsetMaxRows(int maxRows) Sets the optional maximum row count for this cursor.voidsetOffset(int offset) Sets an offset, i.e. number of rows to skip in a query.voidsetStatementCached(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.
-
Constructor Details
-
Query
public Query()Creates a query.
-
-
Method Details
-
setStatementCached
public 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. However, if a query uses a fixed SQL string and is executed a significant number of times, the prepared statement can be prepared only once and re-used afterward. Please notice, that once prepared with caching, the statement will remain open until it is closed explicitly.- Parameters:
statementCached- true if prepare only once and reuse cached statement, false if one-shot
-
isStatementCached
public boolean isStatementCached()Returns whether statement caching is enabled.- Returns:
- true if prepare only once and reuse cached statement, false if one-shot (default)
-
setOffset
public void setOffset(int offset) Sets an offset, i.e. number of rows to skip in a query. By default, the offset is 0.- Parameters:
offset- the offset, 0 to disable
-
getOffset
public int getOffset()Gets the offset of this query.- Returns:
- the number of rows to skip, 0 if no offset
-
setLimit
public void setLimit(int limit) Sets the maximum number of rows to retrieve for this query. e- Parameters:
limit- the maximum number of rows, 0 if unlimited (default)
-
getLimit
public int getLimit()Gets the maximum number of rows for this query.- Returns:
- the maximum number of rows, 0 if unlimited (default)
-
add
Appends an SQL-part and corresponding parameters to this query.Example: add(" AND CN_ID=?", object.getId()); add(" AND CN_MONEY=?", object.getAmount());- Parameters:
sql- is the SQL-string, null or empty if just datadata- is an array of parameters
-
add
Appends an SQL-part and corresponding parameters to this query.
Same asadd(CharSequence, Object...)but with aListof parameters instead of an array or varargs.- Parameters:
sql- is the SQL-string, null or empty if just datadata- theListof parameters
-
add
Appends a backend-specific SQL-part to this query.- Parameters:
sqlSupplier- supplier for backend specific SQL code
-
add
Appends an SQL-part and corresponding parameters to this query.Example: add(" AND CN_ID=?", object.getId()); add(" AND CN_MONEY=?", object.getAmount());- Parameters:
index- index at which the specified SQL is to be insertedsql- is the SQL-string, null or empty if just datadata- is an array of parameters
-
add
Appends an SQL-part and corresponding parameters to this query.
Same asadd(int, CharSequence, Object...)but with aListof parameters instead of an array or varargs.- Parameters:
index- index at which the specified SQL is to be insertedsql- is the SQL-string, null or empty if just datadata- theListof parameters
-
add
Appends a backend-specific SQL-part to this query.- Parameters:
index- index at which the specified SQL is to be insertedsqlSupplier- supplier for backend specific SQL code
-
setFetchSize
public void setFetchSize(int fetchSize) Sets the optional fetchsize. 0 = drivers default.- Parameters:
fetchSize- the fetchsize- See Also:
-
getFetchSize
public int getFetchSize()Gets the fetchsize.- Returns:
- the fetchsize
- See Also:
-
setMaxRows
public void setMaxRows(int maxRows) Sets the optional maximum row count for this cursor.- Parameters:
maxRows- the max rows, 0 = no limit
-
getMaxRows
public int getMaxRows()Gets the maximum row count for this cursor- Returns:
- the max rows, 0 = no limit
-
execute
Executes the query.- Parameters:
db- is the sessionresultSetType- is one of ResultSet.TYPE_...resultSetConcurrency- is one of ResultSet.CONCUR_..- Returns:
- the result set
-
execute
Executes the query with ResultSet.CONCUR_READ_ONLY.- Parameters:
db- is the sessionresultSetType- is one of ResultSet.TYPE_...- Returns:
- the result set
-
execute
Executes the query with ResultSet.TYPE_FORWARD_ONLY and ResultSet.CONCUR_READ_ONLY.- Parameters:
db- is the session- Returns:
- the result set
-
getRowCount
Gets the number of objects returned by this query.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).- Parameters:
db- the session- Returns:
- the number of rows for this query
-
apply
Applies the query parameters to the statement.
Optionally applies limit, offset as well.- Parameters:
db- the sessionst- the prepared statement
-
applyParameters
Apply the query parameters only.- Parameters:
st- the statementndx- the starting index- Returns:
- the next parameter index
-
createSql
Creates the SQL-code of this query.
Optionally modifies the query according to limit and offset.- Parameters:
backend- the backend- Returns:
- the SQL code
-
createStatementKey
Creates the statement key.- Parameters:
sql- the SQL string sent to the backend- Returns:
- the key
-
buildInnerSql
Builds the inner sql query string.- Parameters:
backend- the backend- Returns:
- the sql string
-