com.vaadin.data.util.sqlcontainer.query.
Class TableQuery
java.lang.Object
com.vaadin.data.util.sqlcontainer.query.TableQuery
All Implemented Interfaces:
QueryDelegate, QueryDelegate.RowIdChangeNotifier, Serializable
- extends Object
- implements QueryDelegate, QueryDelegate.RowIdChangeNotifier
public class TableQuery
See Also:
Nested Class Summary | |
---|---|
class |
TableQuery.RowIdChangeEvent
Simple RowIdChangeEvent implementation. |
Nested classes/interfaces inherited from interface com.vaadin.data.util.sqlcontainer.query.QueryDelegate |
---|
QueryDelegate.RowIdChangeListener, QueryDelegate.RowIdChangeNotifier |
Constructor Summary | |
---|---|
TableQuery(String tableName,
JDBCConnectionPool connectionPool)
Creates a new TableQuery using the given connection pool and table name to fetch the data from. |
|
TableQuery(String tableName,
JDBCConnectionPool connectionPool,
SQLGenerator sqlGenerator)
Creates a new TableQuery using the given connection pool, SQL generator and table name to fetch the data from. |
Method Summary | |
---|---|
void |
addListener(QueryDelegate.RowIdChangeListener listener)
Adds RowIdChangeListener to this query |
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)
Performs a query to find out whether the SQL table contains a row with the given set of primary keys. |
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()
Generates and executes a query to determine the current row count from the DB. |
List<String> |
getPrimaryKeyColumns()
Returns a list of primary key column names. |
ResultSet |
getResults(int offset,
int pagelength)
Executes a paged SQL query and returns the ResultSet. |
SQLGenerator |
getSqlGenerator()
|
String |
getTableName()
|
String |
getVersionColumn()
|
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. |
void |
removeListener(QueryDelegate.RowIdChangeListener listener)
Removes the given RowIdChangeListener from this query |
boolean |
removeRow(RowItem row)
Removes the given RowItem from the database. |
void |
rollback()
Rolls back and releases the active connection. |
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. |
void |
setVersionColumn(String column)
|
int |
storeRow(RowItem row)
Stores a row in the database. |
RowId |
storeRowImmediately(RowItem row)
Inserts the given row in the database table immediately. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
TableQuery
public TableQuery(String tableName,
JDBCConnectionPool connectionPool,
SQLGenerator sqlGenerator)
- Parameters:
tableName
- Name of the database table to connect toconnectionPool
- Connection pool for accessing the databasesqlGenerator
- SQL query generator implementation
Creates a new TableQuery using the given connection pool, SQL generator and table name to fetch the data from. All parameters must be non-null.
TableQuery
public TableQuery(String tableName,
JDBCConnectionPool connectionPool)
- Parameters:
tableName
- Name of the database table to connect toconnectionPool
- Connection pool for accessing the database
Creates a new TableQuery using the given connection pool and table name to fetch the data from. All parameters must be non-null. The default SQL generator will be used for queries.
Method Detail |
---|
getCount
public int getCount()
throws SQLException
- Specified by:
getCount
in interfaceQueryDelegate
- Returns:
- row count
- Throws:
SQLException
Description copied from interface: QueryDelegate
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
- if the database access fails.
Description copied from interface: QueryDelegate
Executes a paged SQL query and returns the ResultSet. The query is defined through implementations of this QueryDelegate interface.
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.
storeRow
public int storeRow(RowItem row)
throws UnsupportedOperationException,
SQLException
- Specified by:
storeRow
in interfaceQueryDelegate
- Returns:
- the number of affected rows in the database table
- Throws:
UnsupportedOperationException
- if the implementation is read only.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.
storeRowImmediately
public RowId storeRowImmediately(RowItem row)
throws SQLException
- Parameters:
row
- RowItem to add to the database- Returns:
- Final RowId of the added row
- Throws:
SQLException
Inserts the given row in the database table immediately. Begins and commits the transaction needed. This method was added specifically to solve the problem of returning the final RowId immediately on the SQLContainer.addItem() call when auto commit mode is enabled in the SQLContainer.
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.
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.
getVersionColumn
public String getVersionColumn()
setVersionColumn
public void setVersionColumn(String column)
getTableName
public String getTableName()
getSqlGenerator
public SQLGenerator getSqlGenerator()
removeRow
public boolean removeRow(RowItem row)
throws UnsupportedOperationException,
SQLException
- Specified by:
removeRow
in interfaceQueryDelegate
- Parameters:
row
- RowItem to be removed- Returns:
- true on success
- Throws:
UnsupportedOperationException
SQLException
Description copied from interface: QueryDelegate
Removes the given RowItem from the database.
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
Description copied from interface: QueryDelegate
Performs a query to find out whether the SQL table contains a row with the given set of primary keys.
addListener
public void addListener(QueryDelegate.RowIdChangeListener listener)
- Specified by:
addListener
in interfaceQueryDelegate.RowIdChangeNotifier
- Parameters:
listener
- listener to be added
Adds RowIdChangeListener to this query
removeListener
public void removeListener(QueryDelegate.RowIdChangeListener listener)
- Specified by:
removeListener
in interfaceQueryDelegate.RowIdChangeNotifier
- Parameters:
listener
- listener to be removed
Removes the given RowIdChangeListener from this query
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()