com.vaadin.data.util.sqlcontainer.query.


Class FreeformQuery

java.lang.Object
  com.vaadin.data.util.sqlcontainer.query.FreeformQuery

All Implemented Interfaces:

QueryDelegate, Serializable

public class FreeformQuery
extends Object
implements QueryDelegate

See Also:

Serialized Form

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)

Deprecated. @see FreeformQuery(String, JDBCConnectionPool, String...)

Creates a new freeform query delegate to be used with the SQLContainer.

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.

FreeformQuery

public FreeformQuery(String queryString,
                     JDBCConnectionPool connectionPool,
                     String... primaryKeyColumns)

Creates a new freeform query delegate to be used with the SQLContainer.

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)

Method Detail

getCount

public int getCount()
             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.

Specified by:
getCount in interface QueryDelegate

Returns:
row count
Throws:
SQLException

getResults

public ResultSet getResults(int offset,
                            int pagelength)
                     throws SQLException

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.

Specified by:
getResults in interface QueryDelegate

Parameters:
offset - the first item of the page to load
pagelength - the length of the page to load
Returns:
a ResultSet containing the rows of the page
Throws:
SQLException
See Also:
{@inheritDoc}

implementationRespectsPagingLimits

public boolean implementationRespectsPagingLimits()

Description copied from interface: QueryDelegate

Allows the SQLContainer implementation to check whether the QueryDelegate implementation implements paging in the getResults method.

Specified by:
implementationRespectsPagingLimits in interface QueryDelegate

Returns:
true if the delegate implements paging
See Also:
QueryDelegate.getResults(int, int)

setFilters

public void setFilters(List<Container.Filter> filters)
                throws UnsupportedOperationException

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.

Specified by:
setFilters in interface QueryDelegate

Parameters:
filters - The filters to apply.
Throws:
UnsupportedOperationException - if the implementation doesn't support filtering.

setOrderBy

public void setOrderBy(List<OrderBy> orderBys)
                throws UnsupportedOperationException

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.

Specified by:
setOrderBy in interface QueryDelegate

Parameters:
orderBys - A list of the OrderBy conditions.
Throws:
UnsupportedOperationException - if the implementation doesn't support ordering.

storeRow

public int storeRow(RowItem row)
             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.

Specified by:
storeRow in interface QueryDelegate

Returns:
the number of affected rows in the database table
Throws:
SQLException

removeRow

public boolean removeRow(RowItem row)
                  throws SQLException

Description copied from interface: QueryDelegate

Removes the given RowItem from the database.

Specified by:
removeRow in interface QueryDelegate

Parameters:
row - RowItem to be removed
Returns:
true on success
Throws:
SQLException

beginTransaction

public void beginTransaction()
                      throws UnsupportedOperationException,
                             SQLException

Reserves a connection with auto-commit off if no transaction is in progress.

Specified by:
beginTransaction in interface QueryDelegate

Throws:
SQLException - if a connection could not be obtained or configured
UnsupportedOperationException

commit

public void commit()
            throws UnsupportedOperationException,
                   SQLException

Commits (if not in auto-commit mode) and releases the active connection.

Specified by:
commit in interface QueryDelegate

Throws:
SQLException - if not in a transaction managed by this query
UnsupportedOperationException

rollback

public void rollback()
              throws UnsupportedOperationException,
                     SQLException

Rolls back and releases the active connection.

Specified by:
rollback in interface QueryDelegate

Throws:
SQLException - if not in a transaction managed by this query
UnsupportedOperationException

getPrimaryKeyColumns

public List<String> getPrimaryKeyColumns()

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.

Specified by:
getPrimaryKeyColumns in interface QueryDelegate

Returns:

getQueryString

public String getQueryString()

getDelegate

public FreeformQueryDelegate getDelegate()

setDelegate

public void setDelegate(FreeformQueryDelegate delegate)

containsRowWithKey

public boolean containsRowWithKey(Object... keys)
                           throws SQLException

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.

Specified by:
containsRowWithKey in interface QueryDelegate

Parameters:
keys - the primary keys
Returns:
true if the SQL table contains a row with the provided keys
Throws:
SQLException
See Also:
{@inheritDoc}

ensureTransaction

protected void ensureTransaction()
                          throws SQLException

Check that a transaction is active.

Throws:
SQLException - if no active transaction

releaseConnection

protected void releaseConnection(Connection conn,
                                 Statement statement,
                                 ResultSet rs)
                          throws SQLException

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.

Parameters:
conn - the connection to release
statement - the statement to close, may be null to skip closing
rs - the result set to close, may be null to skip closing
Throws:
SQLException - if closing the result set or the statement fails

getConnection

protected Connection getConnection()
                            throws SQLException

Returns the currently active connection, reserves and returns a new connection if no active connection.

Returns:
previously active or newly reserved connection
Throws:
SQLException

isInTransaction

protected boolean isInTransaction()