com.vaadin.data.util.sqlcontainer.query.
Class FreeformQuery
java.lang.Object
com.vaadin.data.util.sqlcontainer.query.FreeformQuery
All Implemented Interfaces:
- extends Object
- implements QueryDelegate
public class FreeformQuery
See Also:
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface com.vaadin.data.util.sqlcontainer.query.QueryDelegate |
---|
QueryDelegate.RowIdChangeEvent, QueryDelegate.RowIdChangeListener, QueryDelegate.RowIdChangeNotifier |
Constructor Summary | |
---|---|
FreeformQuery(String queryString,
JDBCConnectionPool connectionPool,
String... primaryKeyColumns)
Creates a new freeform query delegate to be used with the SQLContainer . |
|
FreeformQuery(String queryString,
List<String> primaryKeyColumns,
JDBCConnectionPool connectionPool)
Deprecated. @see FreeformQuery(String, JDBCConnectionPool, String...) |
Method Summary | |
---|---|
void |
beginTransaction()
Reserves a connection with auto-commit off if no transaction is in progress. |
void |
commit()
Commits (if not in auto-commit mode) and releases the active connection. |
boolean |
containsRowWithKey(Object... keys)
This implementation of the containsRowWithKey method rewrites existing WHERE clauses in the query string. |
protected void |
ensureTransaction()
Check that a transaction is active. |
protected Connection |
getConnection()
Returns the currently active connection, reserves and returns a new connection if no active connection. |
int |
getCount()
This implementation of getCount() actually fetches all records from the database, which might be a performance issue. |
FreeformQueryDelegate |
getDelegate()
|
List<String> |
getPrimaryKeyColumns()
Returns a list of primary key column names. |
String |
getQueryString()
|
ResultSet |
getResults(int offset,
int pagelength)
Fetches the results for the query. |
boolean |
implementationRespectsPagingLimits()
Allows the SQLContainer implementation to check whether the QueryDelegate implementation implements paging in the getResults method. |
protected boolean |
isInTransaction()
|
protected void |
releaseConnection(Connection conn,
Statement statement,
ResultSet rs)
Closes a statement and a resultset, then releases the connection if it is not part of an active transaction. |
boolean |
removeRow(RowItem row)
Removes the given RowItem from the database. |
void |
rollback()
Rolls back and releases the active connection. |
void |
setDelegate(FreeformQueryDelegate delegate)
|
void |
setFilters(List<Container.Filter> filters)
Sets the filters to apply when performing the SQL query. |
void |
setOrderBy(List<OrderBy> orderBys)
Sets the order in which to retrieve rows from the database. |
int |
storeRow(RowItem row)
Stores a row in the database. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
FreeformQuery
@Deprecated
public FreeformQuery(String queryString,
List<String> primaryKeyColumns,
JDBCConnectionPool connectionPool)
- Parameters:
queryString
- The actual query to perform.primaryKeyColumns
- The primary key columns. Read-only mode is forced if this parameter is null or empty.connectionPool
- the JDBCConnectionPool to use to open connections to the SQL database.
Deprecated. @see
FreeformQuery(String, JDBCConnectionPool, String...)
Creates a new freeform query delegate to be used with the
SQLContainer
.
FreeformQuery
public FreeformQuery(String queryString,
JDBCConnectionPool connectionPool,
String... primaryKeyColumns)
- Parameters:
queryString
- The actual query to perform.connectionPool
- the JDBCConnectionPool to use to open connections to the SQL database.primaryKeyColumns
- The primary key columns. Read-only mode is forced if none are provided. (optional)
Creates a new freeform query delegate to be used with the
SQLContainer
.
Method Detail |
---|
getCount
public int getCount()
throws SQLException
- Specified by:
getCount
in interfaceQueryDelegate
- Returns:
- row count
- Throws:
SQLException
This implementation of getCount() actually fetches all records from the database, which might be a performance issue. Override this method with a SELECT COUNT(*) ... query if this is too slow for your needs. Generates and executes a query to determine the current row count from the DB. Row count will be fetched using filters that are currently set to the QueryDelegate.
getResults
public ResultSet getResults(int offset,
int pagelength)
throws SQLException
- Specified by:
getResults
in interfaceQueryDelegate
- Parameters:
offset
- the first item of the page to loadpagelength
- the length of the page to load- Returns:
- a ResultSet containing the rows of the page
- Throws:
SQLException
- See Also:
{@inheritDoc}
Fetches the results for the query. This implementation always fetches the entire record set, ignoring the offset and pagelength parameters. In order to support lazy loading of records, you must supply a FreeformQueryDelegate that implements the FreeformQueryDelegate.getQueryString(int,int) method.
implementationRespectsPagingLimits
public boolean implementationRespectsPagingLimits()
- Specified by:
implementationRespectsPagingLimits
in interfaceQueryDelegate
- Returns:
- true if the delegate implements paging
- See Also:
QueryDelegate.getResults(int, int)
Description copied from interface: QueryDelegate
Allows the SQLContainer implementation to check whether the QueryDelegate implementation implements paging in the getResults method.
setFilters
public void setFilters(List<Container.Filter> filters)
throws UnsupportedOperationException
- Specified by:
setFilters
in interfaceQueryDelegate
- Parameters:
filters
- The filters to apply.- Throws:
UnsupportedOperationException
- if the implementation doesn't support filtering.
Description copied from interface: QueryDelegate
Sets the filters to apply when performing the SQL query. These are translated into a WHERE clause. Default filtering mode will be used.
setOrderBy
public void setOrderBy(List<OrderBy> orderBys)
throws UnsupportedOperationException
- Specified by:
setOrderBy
in interfaceQueryDelegate
- Parameters:
orderBys
- A list of the OrderBy conditions.- Throws:
UnsupportedOperationException
- if the implementation doesn't support ordering.
Description copied from interface: QueryDelegate
Sets the order in which to retrieve rows from the database. The result can be ordered by zero or more columns and each column can be in ascending or descending order. These are translated into an ORDER BY clause in the SQL query.
storeRow
public int storeRow(RowItem row)
throws SQLException
- Specified by:
storeRow
in interfaceQueryDelegate
- Returns:
- the number of affected rows in the database table
- Throws:
SQLException
Description copied from interface: QueryDelegate
Stores a row in the database. The implementation of this interface decides how to identify whether to store a new row or update an existing one.
removeRow
public boolean removeRow(RowItem row)
throws SQLException
- Specified by:
removeRow
in interfaceQueryDelegate
- Parameters:
row
- RowItem to be removed- Returns:
- true on success
- Throws:
SQLException
Description copied from interface: QueryDelegate
Removes the given RowItem from the database.
beginTransaction
public void beginTransaction()
throws UnsupportedOperationException,
SQLException
- Specified by:
beginTransaction
in interfaceQueryDelegate
- Throws:
SQLException
- if a connection could not be obtained or configuredUnsupportedOperationException
Reserves a connection with auto-commit off if no transaction is in progress.
commit
public void commit()
throws UnsupportedOperationException,
SQLException
- Specified by:
commit
in interfaceQueryDelegate
- Throws:
SQLException
- if not in a transaction managed by this queryUnsupportedOperationException
Commits (if not in auto-commit mode) and releases the active connection.
rollback
public void rollback()
throws UnsupportedOperationException,
SQLException
- Specified by:
rollback
in interfaceQueryDelegate
- Throws:
SQLException
- if not in a transaction managed by this queryUnsupportedOperationException
Rolls back and releases the active connection.
getPrimaryKeyColumns
public List<String> getPrimaryKeyColumns()
- Specified by:
getPrimaryKeyColumns
in interfaceQueryDelegate
- Returns:
Description copied from interface: QueryDelegate
Returns a list of primary key column names. The list is either fetched from the database (TableQuery) or given as an argument depending on implementation.
getQueryString
public String getQueryString()
getDelegate
public FreeformQueryDelegate getDelegate()
setDelegate
public void setDelegate(FreeformQueryDelegate delegate)
containsRowWithKey
public boolean containsRowWithKey(Object... keys)
throws SQLException
- Specified by:
containsRowWithKey
in interfaceQueryDelegate
- Parameters:
keys
- the primary keys- Returns:
- true if the SQL table contains a row with the provided keys
- Throws:
SQLException
- See Also:
{@inheritDoc}
This implementation of the containsRowWithKey method rewrites existing WHERE clauses in the query string. The logic is, however, not very complex and some times can do the Wrong ThingTM. For the situations where this logic is not enough, you can implement the getContainsRowQueryString method in FreeformQueryDelegate and this will be used instead of the logic.
ensureTransaction
protected void ensureTransaction()
throws SQLException
- Throws:
SQLException
- if no active transaction
Check that a transaction is active.
releaseConnection
protected void releaseConnection(Connection conn,
Statement statement,
ResultSet rs)
throws SQLException
- Parameters:
conn
- the connection to releasestatement
- the statement to close, may be null to skip closingrs
- the result set to close, may be null to skip closing- Throws:
SQLException
- if closing the result set or the statement fails
Closes a statement and a resultset, then releases the connection if it is
not part of an active transaction. A failure in closing one of the
parameters does not prevent closing the rest.
If the statement is a PreparedStatement
, its parameters are
cleared prior to closing the statement.
Although JDBC specification does state that closing a statement closes
its result set and closing a connection closes statements and result
sets, this method does try to close the result set and statement
explicitly whenever not null. This can guard against bugs in certain JDBC
drivers and reduce leaks in case e.g. closing the result set succeeds but
closing the statement or connection fails.
getConnection
protected Connection getConnection()
throws SQLException
- Returns:
- previously active or newly reserved connection
- Throws:
SQLException
Returns the currently active connection, reserves and returns a new connection if no active connection.
isInTransaction
protected boolean isInTransaction()