Velocity
  1. Velocity
  2. VELOCITY-814

Allow for overriding the query used by DataSourceResourceLoader

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.7.x
    • Fix Version/s: 2.x
    • Component/s: Engine
    • Labels:

      Description

      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 is attached.

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

      http://old.nabble.com/Customizing-the-query-used-by-DataSourceResourceLoader-to32957497.html

        Activity

        Hide
        Darren Cruse added a comment -

        File with suggested changes.

        Show
        Darren Cruse added a comment - File with suggested changes.
        Hide
        Nathan Bubna added a comment -

        Sorry it took so long. Holidays and all...

        I applied the patch to 2.x, as that is the trunk now and likely next version.

        Show
        Nathan Bubna added a comment - Sorry it took so long. Holidays and all... I applied the patch to 2.x, as that is the trunk now and likely next version.

          People

          • Assignee:
            Unassigned
            Reporter:
            Darren Cruse
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - 1h
              1h
              Remaining:
              Remaining Estimate - 1h
              1h
              Logged:
              Time Spent - Not Specified
              Not Specified

                Development