dgraham     2003/10/19 19:10:21

  Modified:    dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  Added wrap(ResultSet) method that allows subclasses to decorate 
  a ResultSet before processing it.  Clients can override this method to
  wrap a StringTrimmedResultSet or SqlNullCheckedResultSet around
  the results.
  
  Revision  Changes    Path
  1.5       +84 -70    
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
  
  Index: QueryRunner.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- QueryRunner.java  19 Oct 2003 19:47:51 -0000      1.4
  +++ QueryRunner.java  20 Oct 2003 02:10:21 -0000      1.5
  @@ -109,7 +109,7 @@
       public boolean execute(
           Connection conn,
           String query,
  -        Object[] vals,
  +        Object[] params,
           ResultSetHandler rsh,
           ResultSetMetaDataHandler rsmdh,
           Object userObject)
  @@ -120,7 +120,7 @@
   
           try {
               stmt = conn.prepareStatement(query);
  -            fillStatement(stmt, vals);
  +            fillStatement(stmt, params);
   
               if (!stmt.execute()) {
                   return false;
  @@ -129,17 +129,19 @@
               do {
                   rs = stmt.getResultSet();
                   if (rs != null) {
  +                    rs = this.wrap(rs);
  +                    
                       if (rsmdh != null) {
                           rsmdh.handle(rs.getMetaData());
                       }
   
  -                    rsh.handle(rs, vals, userObject);
  +                    rsh.handle(rs, params, userObject);
                   }
   
               } while (stmt.getMoreResults());
   
           } catch (SQLException e) {
  -            rethrow(e, query, vals);
  +            rethrow(e, query, params);
   
           } finally {
               DbUtils.closeQuietly(rs);
  @@ -150,6 +152,37 @@
       }
   
       /**
  +     * Fill the <code>PreparedStatement</code> replacement parameters with 
  +     * the given objects.
  +     * @param stmt
  +     * @param params Query replacement parameters; <code>null</code> is a valid
  +     * value to pass in.
  +     * @throws SQLException
  +     */
  +    protected void fillStatement(PreparedStatement stmt, Object[] params)
  +        throws SQLException {
  +
  +        if (params == null) {
  +            return;
  +        }
  +
  +        for (int i = 0; i < params.length; i++) {
  +            if (params[i] != null) {
  +                stmt.setObject(i + 1, params[i]);
  +            } else {
  +                stmt.setNull(i + 1, Types.OTHER);
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Returns the <code>DataSource</code> this runner is using.
  +     */
  +    public DataSource getDataSource() {
  +        return this.ds;
  +    }
  +
  +    /**
        * Creates a PreparedStatement using the String and Object array,
        * executes this using the Connection, and returns the results
        * inside an Iterator.
  @@ -193,7 +226,7 @@
               stmt = conn.prepareStatement(query);
               this.fillStatement(stmt, params);
   
  -            rs = stmt.executeQuery();
  +            rs = this.wrap(stmt.executeQuery());
   
               if (rsmdh != null) {
                   rsmdh.handle(rs.getMetaData());
  @@ -264,6 +297,40 @@
       }
   
       /**
  +     * Throws a new exception with a more informative error message.
  +     * @param cause The original exception that will be chained to the new 
  +     * exception when it's rethrown. 
  +     * @param sql The query that was executing when the exception happened.
  +     * @param params The query replacement paramaters; <code>null</code> is a 
  +     * valid value to pass in.
  +     * @throws SQLException
  +     */
  +    protected void rethrow(SQLException cause, String sql, Object[] params)
  +        throws SQLException {
  +
  +        StringBuffer msg = new StringBuffer(cause.getMessage() + " in query " + 
sql);
  +        if (params != null) {
  +            msg.append(Arrays.asList(params).toString());
  +        }
  +
  +        SQLException newsqle = new SQLException(msg.toString());
  +        newsqle.setNextException(cause);
  +
  +        throw newsqle;
  +    }
  +
  +    /**
  +     * Sets the <code>DataSource</code> this runner will use to get
  +     * database connections from.  This should be called after creating a
  +     * runner with the default constructor if you intend to use the
  +     * execute methods without passing in a <code>Connection</code>.
  +     * @param dataSource The DataSource to use.
  +     */
  +    public void setDataSource(DataSource dataSource) {
  +        this.ds = dataSource;
  +    }
  +
  +    /**
        * Execute an SQL INSERT, UPDATE, or DELETE query without replacement
        * parameters.
        * @param conn The connection to use to run the query.
  @@ -348,70 +415,17 @@
   
           return rows;
       }
  -
  -    /**
  -     * Fill the <code>PreparedStatement</code> replacement parameters with 
  -     * the given objects.
  -     * @param stmt
  -     * @param params Query replacement parameters; <code>null</code> is a valid
  -     * value to pass in.
  -     * @throws SQLException
  -     */
  -    protected void fillStatement(PreparedStatement stmt, Object[] params)
  -        throws SQLException {
  -
  -        if (params == null) {
  -            return;
  -        }
  -
  -        for (int i = 0; i < params.length; i++) {
  -            if (params[i] != null) {
  -                stmt.setObject(i + 1, params[i]);
  -            } else {
  -                stmt.setNull(i + 1, Types.OTHER);
  -            }
  -        }
  -    }
  -
  -    /**
  -     * Returns the <code>DataSource</code> this runner is using.
  -     */
  -    public DataSource getDataSource() {
  -        return this.ds;
  -    }
  -
  -    /**
  -     * Throws a new exception with a more informative error message.
  -     * @param cause The original exception that will be chained to the new 
  -     * exception when it's rethrown. 
  -     * @param sql The query that was executing when the exception happened.
  -     * @param params The query replacement paramaters; <code>null</code> is a 
  -     * valid value to pass in.
  -     * @throws SQLException
  -     */
  -    protected void rethrow(SQLException cause, String sql, Object[] params)
  -        throws SQLException {
  -
  -        StringBuffer msg = new StringBuffer(cause.getMessage() + " in query " + 
sql);
  -        if (params != null) {
  -            msg.append(Arrays.asList(params).toString());
  -        }
  -
  -        SQLException newsqle = new SQLException(msg.toString());
  -        newsqle.setNextException(cause);
  -
  -        throw newsqle;
  -    }
  -
  +    
       /**
  -     * Sets the <code>DataSource</code> this runner will use to get
  -     * database connections from.  This should be called after creating a
  -     * runner with the default constructor if you intend to use the
  -     * execute methods without passing in a <code>Connection</code>.
  -     * @param dataSource The DataSource to use.
  +     * Wrap the <code>ResultSet</code> in a decorator before processing it.
  +     * This implementation returns the <code>ResultSet</code> it is given
  +     * without any decoration.
  +     * @param rs The <code>ResultSet</code> to decorate; never 
  +     * <code>null</code>.
  +     * @return The <code>ResultSet</code> wrapped in some decorator. 
        */
  -    public void setDataSource(DataSource dataSource) {
  -        this.ds = dataSource;
  +    protected ResultSet wrap(ResultSet rs){
  +        return rs;
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to