org.tentackle.db
Class Query

java.lang.Object
  extended by org.tentackle.db.Query
Direct Known Subclasses:
AppDbQuery

public class Query
extends Object

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 resultset (or cursor) is closed by the application. You can change that behaviour, however.

Author:
harald

Constructor Summary
Query()
          Creates a query.
 
Method Summary
 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.
 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 getEstimatedRowCount(Db db)
          Gets the number of objects returned by this 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.
 String getSql(Db db)
          Gets the SQL-String of this query.
Optionally modifies the sql query according to limit and offset.
 boolean isCloseStatementOnClose()
          Returns whether statement is closed when result set is closed.
 void setCloseStatementOnClose(boolean closeStatementOnClose)
          Sets whether to close the statement when resultset is closed.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Query

public Query()
Creates a query.

Method Detail

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.

Parameters:
limit - the maximum number of rows, 0 if ulimited (default)

getLimit

public int getLimit()
Gets the maximum number of rows for this query.

Returns:
the maximum number of rows, 0 if ulimited (default)

add

public void add(String sql,
                Object... data)
Appends an sql-part and corresponding parameters to this query.
 Example: add(" AND FIELD_ID=?", object.getId());
          add(" AND FIELD_MONEY=?", object.getAmount());
 

Parameters:
sql - is the SQL-string
data - is an array of parameters

setCloseStatementOnClose

public void setCloseStatementOnClose(boolean closeStatementOnClose)
Sets whether to close the statement when resultset is closed. The default is to close.

Parameters:
closeStatementOnClose - true if statement is closed, false = left open

isCloseStatementOnClose

public boolean isCloseStatementOnClose()
Returns whether statement is closed when result set is closed.

Returns:
true if statement is closed, false = left open

setFetchSize

public void setFetchSize(int fetchSize)
Sets the optional fetchsize. 0 = drivers default.

Parameters:
fetchSize - the fetchsize
See Also:
ResultSetWrapper.setFetchSize(int)

getFetchSize

public int getFetchSize()
Gets the fetchsize.

Returns:
the fetchsize
See Also:
ResultSetWrapper.getFetchSize()

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

public ResultSetWrapper execute(Db db,
                                int resultSetType,
                                int resultSetConcurrency)
Executes the query.

Parameters:
db - is the database connection
resultSetType - is one of ResultSet.TYPE_...
resultSetConcurrency - is one of ResultSet.CONCUR_..
Returns:
the result set

execute

public ResultSetWrapper execute(Db db,
                                int resultSetType)
Executes the query with ResultSet.CONCUR_READ_ONLY.

Parameters:
db - is the database connection
resultSetType - is one of ResultSet.TYPE_...
Returns:
the result set

execute

public ResultSetWrapper execute(Db db)
Executes the query with ResultSet.TYPE_FORWARD_ONLY and ResultSet.CONCUR_READ_ONLY.

Parameters:
db - is the database connection
Returns:
the result set

getEstimatedRowCount

public int getEstimatedRowCount(Db db)
Gets the number of objects returned by this query.

This is not done by retrieving all rows but by a "SELECT COUNT(*)". Applications may use this in conjunction with limit and offset for pagination (in web pages, for example).

Parameters:
db - the db connection
Returns:
the number of rows for this query

getSql

public String getSql(Db db)
Gets the SQL-String of this query.
Optionally modifies the sql query according to limit and offset.

Parameters:
db - the db connection
Returns:
the sql string

apply

public void apply(Db db,
                  PreparedStatementWrapper st)
Applies the query parameters to the statement.
Optionally applies limit, offset as well.

Parameters:
db - the database connection
st - the prepared statement


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de