Uploaded image for project: 'Velocity'
  1. Velocity
  2. VELOCITY-814

Allow for overriding the query used by DataSourceResourceLoader



    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.7.x
    • 2.0
    • Engine


      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);
      ps = getStatement(conn, templateColumn, tableName, keyColumn, name);

      2. The call to getStatement() at line 308 changes from:
      ps = getStatement(conn, timestampColumn, name);

      ps = getStatement(conn, timestampColumn, tableName, keyColumn, name);

      The changed and tested Velocity 1.7 DataSourceResourceLoader.java is attached.

      This JIRA was originally discussed in the Velocity Old Nabble forum message at:



        1. DataSourceResourceLoader.java
          17 kB
          Darren Cruse



            Unassigned Unassigned
            darrencruse Darren Cruse
            0 Vote for this issue
            0 Start watching this issue



              Time Tracking

                Original Estimate - 1h
                Remaining Estimate - 1h
                Time Spent - Not Specified
                Not Specified