org.jadetower.sql.impl
Class VelocitySQLEvaluator

java.lang.Object
  extended byorg.jadetower.sql.impl.VelocitySQLEvaluator
All Implemented Interfaces:
SQLEvaluator

public class VelocitySQLEvaluator
extends java.lang.Object
implements SQLEvaluator

An SQLEvaluator that uses Jakarta Velocity templates for SQL statement creation.


Field Summary
static java.lang.String BIND_PARAMETERS
           
static java.lang.String SQL_KEY
           
 
Constructor Summary
VelocitySQLEvaluator(java.io.Reader reader)
          creates a new SQLEvalutor.
 
Method Summary
 void clearContext()
          clears the Velocity Context and creates a new one.
 void close()
          attempts to close all result sets and prepared statements
 java.sql.ResultSet executeQuery(java.sql.Connection dbCon)
          executes the query using the velocity template and a JDBC PreparedStatement
 java.util.Collection executeQuery(java.sql.Connection dbCon, java.lang.Class beanClazz, java.util.Map map)
          executes query and maps results to a collection of type "beanClazz".
 int executeUpdate(java.sql.Connection dbCon)
          executes INSERT, UPDATE, or DELETE statements
 java.lang.String getSQL()
          Returns the SQL statement by evaluating a Velocity template.
protected  void initVelocity()
          initialized Velocity runtime
 boolean isSQLMutable()
          returns SQL mutability state.
 void set(java.lang.String key, java.lang.Object value)
          sets a value in the Velocity context
 void setContext(java.util.Map map)
          puts all values of the map into the context.
protected  void setReader(java.io.Reader reader)
          sets the template reader.
 void setSQLMutable(boolean mutable)
          sets the mutability for SQL statements.
protected  void setVelocityContext(org.apache.velocity.VelocityContext context)
          sets the Velocity context
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BIND_PARAMETERS

public static final java.lang.String BIND_PARAMETERS
See Also:
Constant Field Values

SQL_KEY

public static final java.lang.String SQL_KEY
See Also:
Constant Field Values
Constructor Detail

VelocitySQLEvaluator

public VelocitySQLEvaluator(java.io.Reader reader)
creates a new SQLEvalutor.

Parameters:
reader - a reader to the Velocity template for the query.
Method Detail

set

public void set(java.lang.String key,
                java.lang.Object value)
sets a value in the Velocity context

Specified by:
set in interface SQLEvaluator
Parameters:
key - context key
value - context value

setContext

public void setContext(java.util.Map map)
puts all values of the map into the context. Map keys should be of type String or they will be ignored. This method overrides any other values set by the set(String key, Object value) method.

Specified by:
setContext in interface SQLEvaluator
Parameters:
map - context map

getSQL

public java.lang.String getSQL()
                        throws java.sql.SQLException
Returns the SQL statement by evaluating a Velocity template.

Specified by:
getSQL in interface SQLEvaluator
Returns:
SQL statement
Throws:
java.sql.SQLException - thrown if error created during template parsing

executeQuery

public java.sql.ResultSet executeQuery(java.sql.Connection dbCon)
                                throws java.sql.SQLException
executes the query using the velocity template and a JDBC PreparedStatement

Specified by:
executeQuery in interface SQLEvaluator
Parameters:
dbCon - the database connection
Returns:
a JDBC ResultSet
Throws:
java.sql.SQLException - thrown if JDBC or Template parsing error occurs

executeQuery

public java.util.Collection executeQuery(java.sql.Connection dbCon,
                                         java.lang.Class beanClazz,
                                         java.util.Map map)
                                  throws java.sql.SQLException
executes query and maps results to a collection of type "beanClazz". Uses Jakarta BeanUtils for mapping.

Specified by:
executeQuery in interface SQLEvaluator
Parameters:
dbCon - the JDBC database connection
beanClazz - a Java Bean class
map - an alternate Column to Bean property mapping
Returns:
a collection of type "beanClazz"
Throws:
java.sql.SQLException - thrown if JDBC, query parsing, or bean mapping error occurs
See Also:
SQLEvaluator

executeUpdate

public int executeUpdate(java.sql.Connection dbCon)
                  throws java.sql.SQLException
executes INSERT, UPDATE, or DELETE statements

Specified by:
executeUpdate in interface SQLEvaluator
Parameters:
dbCon - a JDBC connection
Returns:
number of effected rows
Throws:
java.sql.SQLException - thrown if JDBC or sql parsing error occurs

setReader

protected void setReader(java.io.Reader reader)
sets the template reader. can only be set once

Parameters:
reader - a reader to the template

setVelocityContext

protected void setVelocityContext(org.apache.velocity.VelocityContext context)
sets the Velocity context

Parameters:
context - a VelocityContext object

initVelocity

protected void initVelocity()
                     throws java.lang.Exception
initialized Velocity runtime

Throws:
java.lang.Exception - thrown if error initializing Velocity occurs

setSQLMutable

public void setSQLMutable(boolean mutable)
sets the mutability for SQL statements. Allows for caching of statements.

Specified by:
setSQLMutable in interface SQLEvaluator
Parameters:
mutable - if false, SQL statements will be cached and updates to the context ignored.

isSQLMutable

public boolean isSQLMutable()
returns SQL mutability state. Defaults to 'true'.

Specified by:
isSQLMutable in interface SQLEvaluator
Returns:
mutability
See Also:
SQLEvaluator.setSQLMutable(boolean mutable)

clearContext

public void clearContext()
clears the Velocity Context and creates a new one. If not mutable, this method does nothing.

Specified by:
clearContext in interface SQLEvaluator

close

public void close()
           throws java.sql.SQLException
attempts to close all result sets and prepared statements

Specified by:
close in interface SQLEvaluator
Throws:
java.sql.SQLException - thrown if error during close


Copyright © 2003 JadeTower. All Rights Reserved.