thinkmap.datasource.database
Class QueryDataSource

java.lang.Object
  extended by thinkmap.datasource.database.QueryDataSource
Direct Known Subclasses:
ProcedureCallDataSource

public class QueryDataSource
extends Object

A query DataSource handles a query, working in concert with others to make a database DataSource work. You can write custom queries using XML and the query tag which this class represents. Or, you can extend this class and override whichever method you need to for your particular custom query (This is in effect a more convenient way that allows a Java Developer familiar with JDBC to write their own sql statements and create plugins that will be used in the server.tas file under the database datasource).

Version:
$Revision: 1.25 $

Field Summary
protected  String bindableEntityType
           
protected  Binding[] bindings
           
protected  DatabaseDataSource database
           
protected  boolean debug
           
protected  EntityResultMapping[] entityMappings
           
protected  int maxResults
           
protected  String query
           
protected  Binding rejectionBinding
           
protected  int requestType
           
 
Constructor Summary
QueryDataSource()
          Creates a new QueryDataSource object.
 
Method Summary
protected  Object[] bindParameters(Message message, SourceEntity entity, EntityId id)
          Get's the binding parameters which will be set on the prepared statement that is generated from the prepareQuery method.
protected  boolean canBind(Message message, SourceEntity entity, EntityId id)
          Check if I can bind to this request and the indicated entity.
protected  void executeQuery(Message message, SourceEntity entity, EntityId id, IEntityHandler callback, Connection c)
          The main method that is called to execute the entire query.
 Binding[] getArguments()
          Get the ArgumentBinding array this QueryDataSource uses.
 String getBindableEntityType()
          Get the entity type this QueryDataSource responds to.
 Binding getRejectionBinding()
          Get a binding that will prevent this query from executing.
 int getRequestType()
          Get the request type this QueryDataSource responds to.
 EntityResultMapping[] getResults()
          Get the EntityResultMapping array this uses when sending back results.
 String getSQL()
          Get the query string this QueryDataSource uses.
protected  void outputResults(ResultSet rs, Message message, SourceEntity entity, EntityId id, IEntityHandler callback)
          Outputs the results using a resultset which represents the results from the prepared statement that was generated using the prepareQuery method.
protected  PreparedStatement prepareQuery(Connection c, Message message, SourceEntity entity, EntityId id, IEntityHandler callback)
          Prepares the query that will be executed.
 void processRequest(Message message, IEntityHandler callback, Connection connection)
           
 void setArguments(Binding[] bindings)
          Set the array of argument bindings that this query DataSource uses.
 void setBindableEntityType(String bindableEntityType)
          Set the entity type this query DataSource responds to.
 void setDebug(boolean b)
          Set the true to enable debug output.
 void setMaxResults(int maxResults)
          Set the maximum number of results to be returned by this query, or -1 for no limit.
 void setRejectionBinding(Binding rejectionBinding)
          Set a binding that will prevent this query from executing.
 void setRequestType(int requestType)
          Set the request type this query DataSource responds to.
 void setResults(EntityResultMapping[] entityMappings)
          Set the EntityResultMapping array this uses when sending back results.
 void setSQL(String sql)
          Set the query string used by this query DataSource.
 void setType(String type)
          Set the type of this query.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

requestType

protected int requestType

bindings

protected Binding[] bindings

bindableEntityType

protected String bindableEntityType

query

protected String query

entityMappings

protected EntityResultMapping[] entityMappings

maxResults

protected int maxResults

debug

protected boolean debug

database

protected DatabaseDataSource database

rejectionBinding

protected Binding rejectionBinding
Constructor Detail

QueryDataSource

public QueryDataSource()
Creates a new QueryDataSource object.

Method Detail

setSQL

public void setSQL(String sql)
Set the query string used by this query DataSource.

Parameters:
sql - new query string

getSQL

public String getSQL()
Get the query string this QueryDataSource uses.

Returns:
query string

setArguments

public void setArguments(Binding[] bindings)
Set the array of argument bindings that this query DataSource uses.

Parameters:
bindings - AgumentBinding array

getArguments

public Binding[] getArguments()
Get the ArgumentBinding array this QueryDataSource uses.

Returns:
ArgumentBinding array

setBindableEntityType

public void setBindableEntityType(String bindableEntityType)
Set the entity type this query DataSource responds to. If no entity type is set, this will bind to any entity type.

Parameters:
bindableEntityType - entity type name

getBindableEntityType

public String getBindableEntityType()
Get the entity type this QueryDataSource responds to.

Returns:
entity type name

setType

public void setType(String type)
Set the type of this query. Can be 'related', 'search', 'entity', 'list', 'index', 'update', 'add', 'insert', 'remove', or 'delete'.

Parameters:
type -

setMaxResults

public void setMaxResults(int maxResults)
Set the maximum number of results to be returned by this query, or -1 for no limit.

Parameters:
maxResults - max results

setRequestType

public void setRequestType(int requestType)
Set the request type this query DataSource responds to.

Parameters:
requestType - request type

getRequestType

public int getRequestType()
Get the request type this QueryDataSource responds to.

Returns:
request type

setResults

public void setResults(EntityResultMapping[] entityMappings)
Set the EntityResultMapping array this uses when sending back results.

Parameters:
entityMappings - EntityResultMapping array

getResults

public EntityResultMapping[] getResults()
Get the EntityResultMapping array this uses when sending back results.

Returns:
EntityResultMapping array

setDebug

public void setDebug(boolean b)
Set the true to enable debug output.

Parameters:
b - true to enable debug output

setRejectionBinding

public void setRejectionBinding(Binding rejectionBinding)
Set a binding that will prevent this query from executing. If this binding returns false, or the binding throws a BindingException, the query cannot execute.

Parameters:
rejectionBinding -

getRejectionBinding

public Binding getRejectionBinding()
Get a binding that will prevent this query from executing.


processRequest

public void processRequest(Message message,
                           IEntityHandler callback,
                           Connection connection)

executeQuery

protected void executeQuery(Message message,
                            SourceEntity entity,
                            EntityId id,
                            IEntityHandler callback,
                            Connection c)
The main method that is called to execute the entire query. This method retrieves the bind parameters, prepares the sql statement, binds the parameters to the statement, executes the prepared statement, and outputs the result set. If you wish to change just one of these operations, then override the corresponding method for each operation rather than this one. If you wish to change how this works completely, then override this method.

Parameters:
message - the incoming message from the client
entity - the entity that was sent to the server from the client during this request (Related Request for instance). Can be null.
id - id the id that was sent to the server from the client during this request. Can be null.
callback - the EntityHandler that is used to transcribe messages back to the client
c - The JDBC Connection

prepareQuery

protected PreparedStatement prepareQuery(Connection c,
                                         Message message,
                                         SourceEntity entity,
                                         EntityId id,
                                         IEntityHandler callback)
                                  throws SQLException
Prepares the query that will be executed. This method should return a PreparedStatement that HAS NOT BEEN Executed. Override this method if you wish to write your own custom sql, use your own parameters, pull attributes from the message etc. If this method returns null, then the prepared statement will not be excecuted and no results will be written to the client.

Parameters:
c - The JDBC Connection which is used to generate the prepared statement
message - the incoming message from the client
entity - the entity that was sent to the server from the client during this request (Related Request for instance). Can be null.
id - id the id that was sent to the server from the client during this request. Can be null.
callback - the EntityHandler that is used to transcribe messages back to the client
Returns:
Throws:
SQLException

bindParameters

protected Object[] bindParameters(Message message,
                                  SourceEntity entity,
                                  EntityId id)
                           throws BindingException
Get's the binding parameters which will be set on the prepared statement that is generated from the prepareQuery method.

Parameters:
message - the incoming message from the client
entity - the entity that was sent to the server from the client during this request (Related Request for instance). Can be null.
id - id the id that was sent to the server from the client during this request. Can be null.
Returns:
Throws:
BindingException

outputResults

protected void outputResults(ResultSet rs,
                             Message message,
                             SourceEntity entity,
                             EntityId id,
                             IEntityHandler callback)
                      throws SQLException,
                             BindingException
Outputs the results using a resultset which represents the results from the prepared statement that was generated using the prepareQuery method. Override this method if you wish to modify how the query outputs results back to the client (i.e. send additional attributes back to the client which were not available earlier).

Parameters:
rs - the SQL ResultSet
message - the original message the came from the client
entity - the entity that was sent to the server from the client during this request (Related Request for instance). Can be null.
id - the id that was sent to the server from the client during this request. Can be null.
callback - the EntityHandler that is used to transcribe messages back to the client
Throws:
SQLException
BindingException

canBind

protected boolean canBind(Message message,
                          SourceEntity entity,
                          EntityId id)
Check if I can bind to this request and the indicated entity. I look at the EntityType and see if it's our bindableEntityType.

Parameters:
message - incoming message
entity - entity to check (can be null even if id is present)
id - id of entity to check (can be null)
Returns:
true if this can bind


Copyright © 2013 Thinkmap, Inc. All Rights Reserved.