Allow for overriding the query used by DataSourceResourceLoader
---------------------------------------------------------------
Key: VELOCITY-814
URL: https://issues.apache.org/jira/browse/VELOCITY-814
Project: Velocity
Issue Type: Improvement
Components: Engine
Affects Versions: 1.7.x
Reporter: Darren Cruse
Priority: Minor
Fix For: 1.7.x
In future versions of Velocity 1.7, allow users of DataSourceResourceLoader to
customize the database query used by extending the class and overriding the
getStatement() method.
This would be helpful for users in cases where the table being accessed is
pre-existing and has a structure that does not match the default query. e.g.
The default query does not work if the table has a compound key rather than a
single column key.
This feature is also helpful if the template name passed by velocity differs
from what is actually stored in their table's keys - by overriding
getStatement() they can parse the template name passed prior to the query being
run to deal with such differences.
Without this change such users will likely copy and modify the entire
DataSourceResourceLoader.java file, but with this change it's far easier to
simply override the getStatement() function alone.
The necessary changes are simply to:
a. make the getStatement() function protected not private, and
b. pass in tableName and keyColumn so the function doesn't need access those
as private members.
i.e. the getStatement() function becomes as follows:
/**
* Creates the following PreparedStatement query :
* <br>
* SELECT columnNames FROM tableName WHERE keyColumn
* = 'templateName'
* <br>
* where keyColumn is a class member set in init()
*
* @param conn connection to datasource
* @param columnNames columns to fetch from datasource
* @param tableName table to fetch from
* @param keyColumn column whose value should match templateName
* @param templateName name of template to fetch
* @return PreparedStatement
*/
protected PreparedStatement getStatement(final Connection conn,
final String columnNames,
final String tableName,
final String keyColumn,
final String templateName) throws SQLException
{
PreparedStatement ps = conn.prepareStatement("SELECT " + columnNames +
" FROM "+ tableName + " WHERE " + keyColumn + " = ?");
ps.setString(1, templateName);
return ps;
}
And then the two calls to getStatement() are changed to pass the tableName and
keyColumn name as parameters:
1. The call to getStatement() at line 232 changes from:
ps = getStatement(conn, templateColumn, name);
to:
ps = getStatement(conn, templateColumn, tableName, keyColumn, name);
2. The call to getStatement() at line 308 changes from:
ps = getStatement(conn, timestampColumn, name);
to:
ps = getStatement(conn, timestampColumn, tableName, keyColumn,
name);
The changed and tested Velocity 1.7 DataSourceResourceLoader.java file is also
available as an attachment to the forum message at:
http://old.nabble.com/Customizing-the-query-used-by-DataSourceResourceLoader-to32957497.html
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]