Hi guys new to the forum hope I'm posting this to the right place - apologies
if not please let me know what/where else I should ask this if so.  I notice
there's references to Jira here if this should be one of those please let me
know (I'm dumb about Jira not sure if I'm even allowed?)

Basically I'm needing to use DataSourceResourceLoader against a pre-existing
table that I have no control over, and the table doesn't quite match what
the DataSourceResourceLoader's query requires.

e.g. My table uses a compound key rather than a single column (it's actually
a template name in one column and a template version number in the other and
I have to pick the most recent version).

Plus what's in the template name column doesn't match what Velocity passes
through as the templateName used in the DataSourceResourceLoader's query.

i.e. Velocity will give me a template name something like
"templates/template.vm" and what I have in my db table is just the
"template" part.

So anyway from what I can see the DataSourceResourceLoader will not work
with my table as is.  Please correct me if I'm wrong.  Unfortunately I don't
really have control to change the table...

I had the thought that I could extend DataSourceResourceLoader and simply
override the part where the query is constructed, but unfortunately
DataSourceResourceLoader has a private method getStatement(), so that would
need a little tweaking to be overriden in a class extending
DataSourceResourceLoader.

If everything above seems correct what I was hoping to ask/suggest is a tiny
modification to the class that would allow myself and others to customize
the query.  

And that is simply:  
a. to make that function protected not private, and 
b.  to pass in tableName and keyColumn so the function doesn't need to
access those private members as it does now.

i.e. the function becomes as follows, and the two calls to the originally
private getStatement() in the are changed to pass the private members as
arguments:

    /**
     * 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;
    }

Does this seem like a reasonable enhancement?  (has nobody requested
something like this before?)

Can anybody see any harm in it?

At the moment I've made my own copy of DataSourceResourceLoader that I've
modified as described above, I just hate that on a Velocity upgrade things
could break.

Attaching my modified DataSourceResourceLoader.java file with these changes
in case the change really could be considered for the next version of
Velocity.  This is from the velocity-1.7 source btw.

Thanks and hope this doesn't seem pushy I do think it would be nice to have
in the official code base if others also think it's worthwhile.

Darren

http://old.nabble.com/file/p32957497/DataSourceResourceLoader.java
DataSourceResourceLoader.java 
   
-- 
View this message in context: 
http://old.nabble.com/Customizing-the-query-used-by-DataSourceResourceLoader-tp32957497p32957497.html
Sent from the Velocity - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to