sunlabs.brazil.sql
Class SqlTemplate

java.lang.Object
  |
  +--sunlabs.brazil.template.Template
        |
        +--sunlabs.brazil.sql.SqlTemplate
All Implemented Interfaces:
TemplateInterface

public class SqlTemplate
extends Template

Sample Template class for running SQL queries via jdbc and placing the results into the request properties for further processing.

Foreach session, a connection is made to an sql database via jdbc. Session reconnection is attempted if the server connection breaks. An SQL query is issued, with the results populating the request properties. The following server properties are used:

driver
The name of the jdbc driver class for the desired database. Currently, only one driver may be specified. (e.g. prefix.driver=org.gjt.mm.mysql.Driver).
url
The jdbc url used to establish a connection with the database. (e.g. prefix.url=jdbc:mysql://host/db?user=xxx&password=yyy).
sqlPrefix
The properties prefix for any additional parameters that are required for this connection. For example:
		 prefix.sqlPrefix=params
		 params.user=my_name
		 params.password=xxx
		
All of the parameters are supplied to the jdbc connection at connection time.
The driver and url parameters are required. All of the code between <sql>...</sql> is taken to be an SQL query, and sent to the appropriate database for execution. The result of the query is placed into the request properties for use by other templates, such as the BSLTemplate or PropsTemplate.

For a discussion of how the results map to properties, see below.


Field Summary
 
Fields inherited from class sunlabs.brazil.template.Template
debug
 
Constructor Summary
SqlTemplate()
           
 
Method Summary
 boolean init(RewriteContext hr)
          Called before this template processes any tags.
 Connection setupSql(String url, Properties props)
          Setup a connection to an SQL server.
 void tag_slash_sql(RewriteContext hr)
           
 void tag_sql(RewriteContext hr)
          Replace the SQL query with the appropriate request properties.
 
Methods inherited from class sunlabs.brazil.template.Template
done
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlTemplate

public SqlTemplate()
Method Detail

init

public boolean init(RewriteContext hr)
Description copied from class: Template
Called before this template processes any tags.

Specified by:
init in interface TemplateInterface
Overrides:
init in class Template

tag_sql

public void tag_sql(RewriteContext hr)
Replace the SQL query with the appropriate request properties. Look for the following parameters: (NOTE - This interface is preliminary, and subject to change).
debug
Include diagnostics in html comments
prefix
prefix to prepend to all results. Defaults to template prefix
max
The max # of rows returned (default=100)
na
Value to reurn for NULL. Defaults to "n/a"
eval
If present, do ${...} to entire query. (see getProperty).
zeroIndex
if true, row counts start at 0, not 1
index
If present, use column 1 as part of the name. Otherwise, an index name is invented.
For all queries, the following properties (with the prefix prepended) are set:
columns
The number of columns returned
rowcount
The number of rows returned
Foreach entry in the resultant table, its property is: ${prefix}.${table_name}.${columname}.${key}. If the index parameter is set, the key is the value of the first column returned. Otherwise the key is the row number, and the additional property ${prefix}.rows contains a list of all the row numbers returned.


tag_slash_sql

public void tag_slash_sql(RewriteContext hr)

setupSql

public Connection setupSql(String url,
                           Properties props)
Setup a connection to an SQL server. Assume driver is already registered.


Version 2.1, Generated 12/30/04
Copyright (c) 2001-2004, Sun Microsystems.