You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/07/13 07:53:48 UTC

cvs commit: jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc ResultSetRowProcessor.java ResultSetProcessor.java

dgraham     2003/07/12 22:53:47

  Added:       mapper/src/share/org/apache/commons/mapper/jdbc
                        ResultSetRowProcessor.java ResultSetProcessor.java
  Log:
  Replaced ResultObjectFactory with more generic ResultSetProcessor interface
  plus a ResultSetRowProcessor implementation that provides the functionality
  of ResultObjectFactory.  This allows implementations to handle the 
  entire ResultSet at once.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/ResultSetRowProcessor.java
  
  Index: ResultSetRowProcessor.java
  ===================================================================
  /*
   * $Header$
   * $Revision$
   * $Date$
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.mapper.jdbc;
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * This is an implementation of ResultSetProcessor that processes each row of the
   * ResultSet and stores a generated object in a List.  Subclasses must override the
   * <code>processRows(ResultSet)</code> method to generate an object from the
   * current ResultSet row.  A typical subclass will be an anonymous inner class like
   * this:<br/>
   * <pre>
     private static final ResultSetRowProcessor makePerson = new ResultSetRowProcessor() {
          protected Object processRow(ResultSet rs) throws SQLException {
              Person p = new Person()
              p.setName(rs.getString("name"));
              p.setEmail(rs.getString("email"));
              return p;
          }
      };
   * </pre>
   * 
   * @see ResultSetProcessor
   * @author David Graham
   */
  public abstract class ResultSetRowProcessor implements ResultSetProcessor {
  
      /**
       * Constructor for ResultSetRowProcessor.
       */
      public ResultSetRowProcessor() {
          super();
      }
  
      /**
       * Loop through the ResultSet's rows calling the processRow() method on each
       * iteration and storing the result in a List.
       * @param rs The ResultSet to iterate through.
       * @return A List of generated objects from the ResultSet.
       * @throws SQLException
       * @see org.apache.commons.mapper.jdbc.ResultSetProcessor#process(java.sql.ResultSet)
       */
      public List process(ResultSet rs) throws SQLException {
          ArrayList rows = new ArrayList();
          
          while (rs.next()) {
              rows.add(this.processRow(rs));
          }
          
          return rows;
      }
  
      /**
       * Reads data from the ResultSet and stores it in an Object.  This method must
       * <strong>not</strong> alter the ResultSet or read any rows from it other than
       * the row it is currently positioned on.  This method is called once per row.
       * @return An object that holds the row data.  This object is added to the List
       * of results to be returned from the process(ResultSet) method.
       * @throws SQLException
       */
      protected abstract Object processRow(ResultSet rs) throws SQLException;
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/ResultSetProcessor.java
  
  Index: ResultSetProcessor.java
  ===================================================================
  /*
   * $Header$
   * $Revision$
   * $Date$
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
   
  package org.apache.commons.mapper.jdbc;
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.List;
  
  /**
   * ResultSetProcessor processes a ResultSet into a List of result objects.  This is 
   * especially useful when using the BeanUtils <code>RowSetDynaClass</code> to 
   * turn the ResultSet into DynaBeans in very few lines of code.  For example:<br/>
   * <pre>
          ResultSetProcessor processor = new ResultSetProcessor() {
              public List process(ResultSet rs) throws SQLException {
                  RowSetDynaClass rsdc = new RowSetDynaClass(rs);
                  List rows = rsdc.getRows();
                  return rows;
              }
          };
   * </pre>
   * 
   * @see JdbcHelper
   * @author David Graham
   */
  public interface ResultSetProcessor {
  
      /**
       * Process an entire ResultSet at one time.
       * @param rs The ResultSet to process.  The ResultSet is passed to this method
       * immediately after being generated by a PreparedStatement.executeQuery()
       * call.
       * @throws SQLException
       */
      public List process(ResultSet rs) throws SQLException;
      
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org