Keel 2.1RC2

org.keel.services.query.jdbc
Class DefaultQuery

java.lang.Object
  extended byorg.keel.core.container.AbstractKeelServiceable
      extended byorg.keel.services.query.jdbc.DefaultQuery
All Implemented Interfaces:
Configurable, Disposable, Initializable, LogEnabled, Poolable, Query, Recyclable, Serviceable

public class DefaultQuery
extends AbstractKeelServiceable
implements Query, Poolable, Recyclable, LogEnabled, Configurable, Initializable, Disposable

Default JDBC-based implementation of Query, using simple JDBC/SQL access to a database to get query results.

Author:
Michael Nash, Eliot Clingman, Ryan Thoma

Field Summary
private  Configuration[] criteria
           
private  java.util.Map criteriaInput
           
private  DataSourceComponent dataSource
          Data source service itself.
private  java.lang.String dataSourceName
          The name of the data source we will access for this query.
private static java.lang.String headerCriteriaName
          If the criteria specified contains the special criteria name "headers", we include a row header automatically.
private static Logger log
           
protected  java.lang.String myName
          Name of this Query
private  Configuration[] resultFields
           
private  java.lang.String sql
           
protected  ServiceConfig svcConfig
           
 
Fields inherited from class org.keel.core.container.AbstractKeelServiceable
 
Fields inherited from interface org.keel.services.query.Query
ROLE
 
Constructor Summary
DefaultQuery()
           
 
Method Summary
private  SuperString bindParameters(int myQueryId)
          Bind parameters to actual values in the sql statement
private  int cacheObjectKeys(java.util.SortedSet objectKeys, int myQueryId, java.sql.Connection myConnection)
           
 void configure(Configuration configuration)
          Pass the Configuration to the Configurable class.
private  void deCacheObjectKeys(int myQueryId, java.sql.Connection myConnection)
           
 void dispose()
          The dispose operation is called at the end of a components lifecycle.
 void enableLogging(Logger newLog)
          Provide component with a logger.
private  java.util.List executeQuery(java.sql.Connection myConnection, SuperString myStatement)
          Run sql query, returning the query result as a set of Maps, where each map represents one row of the result.
 java.util.List getQueryResult(ObjectKey objectKey)
          Creates the tabular report rows, where there is exactly one object of interest.
 java.util.List getQueryResults()
          Creates the tabular report rows, in the case where there are no object keys (because the criteria service wasn't used).
 java.util.List getQueryResults(java.util.SortedSet objectKeys)
          Creates the tabular report rows.
 void initialize()
          Initialize the component.
 void recycle()
          This method should be implemented to remove all costly resources in object.
private  java.util.List runQuery(java.sql.Connection myConnection, int myQueryId)
          Bind parameters to actual values in the sql statement, run the sql statement, and create the queryResults as a List of Maps.
 void setCriteria(java.lang.String criteriaCode, java.lang.Object criteriaValue)
          Provide named "criteria" to control the operation of the query
 
Methods inherited from class org.keel.core.container.AbstractKeelServiceable
getService, getService, getService, getService, getServiceManager, getServiceSelector, releaseService, releaseServices, service
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static Logger log

svcConfig

protected ServiceConfig svcConfig

headerCriteriaName

private static final java.lang.String headerCriteriaName
If the criteria specified contains the special criteria name "headers", we include a row header automatically.

See Also:
Constant Field Values

criteriaInput

private java.util.Map criteriaInput

criteria

private Configuration[] criteria

resultFields

private Configuration[] resultFields

sql

private java.lang.String sql

dataSourceName

private java.lang.String dataSourceName
The name of the data source we will access for this query. Defined with the configuration element


dataSource

private DataSourceComponent dataSource
Data source service itself. Used for all connections from this query


myName

protected java.lang.String myName
Name of this Query

Constructor Detail

DefaultQuery

public DefaultQuery()
Method Detail

setCriteria

public void setCriteria(java.lang.String criteriaCode,
                        java.lang.Object criteriaValue)
Description copied from interface: Query
Provide named "criteria" to control the operation of the query

Specified by:
setCriteria in interface Query
Parameters:
criteriaCode - Name of the criteria being specified
criteriaValue - Value of the criteria
See Also:
org.keel.services.query.Query#setCriteria(java.lang.String, java. lang. Object)

getQueryResults

public java.util.List getQueryResults(java.util.SortedSet objectKeys)
                               throws QueryException
Creates the tabular report rows. In those cases where Criteria component has provided a collection of ObjectKeys of interest, joins to table queryJoin which caches objectKeys. Otherwise executes a generic SQL statement with simple parameter substitution Returns results as a List of Maps, not as a java.sql.ResultSet

Specified by:
getQueryResults in interface Query
Parameters:
objectKeys - The unique Keys of those objects for which tabular data is to be returned.
Returns:
the tabular data from sql data source as a List of Maps (each row is in a single map)
Throws:
QueryException - Thrown in the case of system failure.
See Also:
Query.getQueryResults(java.util.SortedSet)

getQueryResults

public java.util.List getQueryResults()
                               throws QueryException
Creates the tabular report rows, in the case where there are no object keys (because the criteria service wasn't used). Executes a generic SQL statement with simple parameter substitution Returns results as a List of Maps, not as a java.sql.ResultSet

Specified by:
getQueryResults in interface Query
Returns:
the tabular data from sql data source as a List of Maps (each row is in a single map)
Throws:
QueryException - Thrown in the case of system failure.
See Also:
Query.getQueryResults()

getQueryResult

public java.util.List getQueryResult(ObjectKey objectKey)
                              throws QueryException
Creates the tabular report rows, where there is exactly one object of interest. This delegates implementation to getQueryResult(SortedSet objectKeys)

Specified by:
getQueryResult in interface Query
Parameters:
objectKey - The unique Key of an object for which tabular data is to be returned.
Returns:
the tabular data from sql data source as a List of Maps (each row is in a single map)
Throws:
QueryException - Thrown in the case of system failure.
See Also:
org.keel.services.query.Query#getQueryResults(ObjectKey)

cacheObjectKeys

private int cacheObjectKeys(java.util.SortedSet objectKeys,
                            int myQueryId,
                            java.sql.Connection myConnection)
                     throws QueryException
Throws:
QueryException

deCacheObjectKeys

private void deCacheObjectKeys(int myQueryId,
                               java.sql.Connection myConnection)
                        throws QueryException
Throws:
QueryException

runQuery

private java.util.List runQuery(java.sql.Connection myConnection,
                                int myQueryId)
                         throws QueryException
Bind parameters to actual values in the sql statement, run the sql statement, and create the queryResults as a List of Maps.

Parameters:
myConnection - The database connection
myQueryId - The identifier of the cache of objectKeys
Returns:
the query results
Throws:
QueryException - if the service failed

bindParameters

private SuperString bindParameters(int myQueryId)
                            throws QueryException
Bind parameters to actual values in the sql statement

Parameters:
myQueryId - The identifier of the cache of objectKeys
Returns:
The SQL statement with the actual values in lieu of parameters
Throws:
QueryException - Thrown if the Query service fails

executeQuery

private java.util.List executeQuery(java.sql.Connection myConnection,
                                    SuperString myStatement)
                             throws QueryException
Run sql query, returning the query result as a set of Maps, where each map represents one row of the result.

Parameters:
myStatement - The sql statement to be run
Returns:
Set queryResultRows containing Maps, each representing a row.
Throws:
QueryException - Thrown in the case of system failure.

enableLogging

public void enableLogging(Logger newLog)
Description copied from interface: LogEnabled
Provide component with a logger.

Specified by:
enableLogging in interface LogEnabled
Parameters:
newLog - the logger. Must not be null.

initialize

public void initialize()
                throws QueryException
Description copied from interface: Initializable
Initialize the component. Initialization includes allocating any resources required throughout the component's lifecycle.

Specified by:
initialize in interface Initializable
Throws:
QueryException

configure

public void configure(Configuration configuration)
               throws ConfigurationException
Description copied from interface: Configurable
Pass the Configuration to the Configurable class.

Specified by:
configure in interface Configurable
Parameters:
configuration - the class configurations. Must not be null.
Throws:
ConfigurationException - if an error occurs

recycle

public void recycle()
Description copied from interface: Recyclable
This method should be implemented to remove all costly resources in object. These resources can be object references, database connections, threads, etc. What is categorised as "costly" resources is determined on a case by case analysis.

Specified by:
recycle in interface Recyclable

dispose

public void dispose()
Description copied from interface: Disposable
The dispose operation is called at the end of a components lifecycle. This method will be called after Startable.stop() method (if implemented by component). Components use this method to release and destroy any resources that the Component owns.

Specified by:
dispose in interface Disposable

Keel 2.1RC2

Please see keelframework.org for information about new Keel releases.