dgraham     2003/12/07 09:25:01

  Modified:    dbutils/src/java/org/apache/commons/dbutils
                        BasicRowProcessor.java BasicColumnProcessor.java
                        ColumnProcessor.java
  Log:
  Moved BasicRowProcessor.mapColumnsToProperties()
  to the ColumnProcessor interface to allow pluggable
  implementations.  PR# 25229
  
  Revision  Changes    Path
  1.7       +28 -51    
jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/BasicRowProcessor.java
  
  Index: BasicRowProcessor.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/BasicRowProcessor.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- BasicRowProcessor.java    28 Nov 2003 19:32:10 -0000      1.6
  +++ BasicRowProcessor.java    7 Dec 2003 17:25:01 -0000       1.7
  @@ -78,9 +78,13 @@
   
   /**
    * Basic implementation of the <code>RowProcessor</code> interface.
  - * This class is a thread-safe Singleton.
  + * 
  + * <p>
  + * This class is thread-safe.
  + * </p>
    * 
    * @see RowProcessor
  + * @see ColumnProcessor
    * 
    * @author Henri Yandell
    * @author Juozas Baliuka
  @@ -108,12 +112,6 @@
       }
   
       /**
  -     * Special array index that indicates there is no bean property that
  -     * matches a column from a ResultSet.
  -     */
  -    private static final int PROPERTY_NOT_FOUND = -1;
  -
  -    /**
        * The Singleton instance of this class.
        */
       private static final BasicRowProcessor instance = new BasicRowProcessor();
  @@ -135,10 +133,23 @@
        */
       private static final ColumnProcessor defaultProcessor =
           new ColumnProcessor() {
  +
  +        private final ColumnProcessor p = new BasicColumnProcessor();
  +
           public Object process(ResultSet rs, int index, Class propType)
               throws SQLException {
               return rs.getObject(index);
           }
  +
  +        public int[] mapColumnsToProperties(
  +            ResultSetMetaData rsmd,
  +            PropertyDescriptor[] props)
  +            throws SQLException {
  +
  +            // TODO Compose the default behavior from BasicColumnProcessor.  
  +            // Maybe the default should just be a BasicColumnProcessor?             
   
  +            return p.mapColumnsToProperties(rsmd, props);
  +        }
       };
   
       /**
  @@ -218,7 +229,8 @@
   
           ResultSetMetaData rsmd = rs.getMetaData();
   
  -        int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
  +        int[] columnToProperty =
  +            this.convert.mapColumnsToProperties(rsmd, props);
   
           int cols = rsmd.getColumnCount();
   
  @@ -262,7 +274,10 @@
   
           PropertyDescriptor[] props = this.propertyDescriptors(type);
           ResultSetMetaData rsmd = rs.getMetaData();
  -        int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
  +        
  +        int[] columnToProperty =
  +            this.convert.mapColumnsToProperties(rsmd, props);
  +            
           int cols = rsmd.getColumnCount();
   
           do {
  @@ -296,7 +311,7 @@
   
           for (int i = 1; i <= cols; i++) {
   
  -            if (columnToProperty[i] == PROPERTY_NOT_FOUND) {
  +            if (columnToProperty[i] == ColumnProcessor.PROPERTY_NOT_FOUND) {
                   continue;
               }
               
  @@ -313,44 +328,6 @@
           }
   
           return bean;
  -    }
  -
  -    /**
  -     * The positions in the returned array represent column numbers.  The values
  -     * stored at each position represent the index in the PropertyDescriptor[] 
  -     * for the bean property that matches the column name.  If no bean property
  -     * was found for a column, the position is set to PROPERTY_NOT_FOUND.
  -     * 
  -     * @param rsmd The result set meta data containing column information
  -     * @param props The bean property descriptors
  -     * @return An int[] with column index to property index mappings.  The 0th 
  -     * element is meaningless as column indexing starts at 1.
  -     * 
  -     * @throws SQLException If a database error occurs
  -     */
  -    private int[] mapColumnsToProperties(
  -        ResultSetMetaData rsmd,
  -        PropertyDescriptor[] props)
  -        throws SQLException {
  -
  -        int cols = rsmd.getColumnCount();
  -        int columnToProperty[] = new int[cols + 1];
  -
  -        for (int col = 1; col <= cols; col++) {
  -            String columnName = rsmd.getColumnName(col);
  -            for (int i = 0; i < props.length; i++) {
  -
  -                if (columnName.equalsIgnoreCase(props[i].getName())) {
  -                    columnToProperty[col] = i;
  -                    break;
  -
  -                } else {
  -                    columnToProperty[col] = PROPERTY_NOT_FOUND;
  -                }
  -            }
  -        }
  -
  -        return columnToProperty;
       }
   
       /**
  
  
  
  1.2       +45 -2     
jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/BasicColumnProcessor.java
  
  Index: BasicColumnProcessor.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/BasicColumnProcessor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BasicColumnProcessor.java 28 Nov 2003 21:11:33 -0000      1.1
  +++ BasicColumnProcessor.java 7 Dec 2003 17:25:01 -0000       1.2
  @@ -61,7 +61,9 @@
   
   package org.apache.commons.dbutils;
   
  +import java.beans.PropertyDescriptor;
   import java.sql.ResultSet;
  +import java.sql.ResultSetMetaData;
   import java.sql.SQLException;
   
   /**
  @@ -70,10 +72,15 @@
    * the <code>ResultSet</code> such as getDouble(), getInt(), etc. depending on 
    * the supplied JavaBean property type.
    * 
  + * <p>
  + * This class is thread-safe.
  + * </p>
  + * 
    * @author Corby Page
    * @author David Graham
    * 
    * @see ColumnProcessor
  + * @see BasicRowProcessor
    * 
    * @since DbUtils 1.1
    */
  @@ -85,8 +92,44 @@
       public BasicColumnProcessor() {
           super();
       }
  +    
  +    /**
  +     * Column names are matched to bean property names with a case insensitive
  +     * comparison. 
  +     * 
  +     * @see 
org.apache.commons.dbutils.ColumnProcessor#mapColumnsToProperties(java.sql.ResultSetMetaData,
 java.beans.PropertyDescriptor[])
  +     */
  +    public int[] mapColumnsToProperties(
  +        ResultSetMetaData rsmd,
  +        PropertyDescriptor[] props)
  +        throws SQLException {
  +
  +        int cols = rsmd.getColumnCount();
  +        int columnToProperty[] = new int[cols + 1];
  +
  +        for (int col = 1; col <= cols; col++) {
  +            String columnName = rsmd.getColumnName(col);
  +            for (int i = 0; i < props.length; i++) {
  +
  +                if (columnName.equalsIgnoreCase(props[i].getName())) {
  +                    columnToProperty[col] = i;
  +                    break;
  +
  +                } else {
  +                    columnToProperty[col] = PROPERTY_NOT_FOUND;
  +                }
  +            }
  +        }
  +
  +        return columnToProperty;
  +    }
   
       /**
  +     * Call the appropriate <code>ResultSet</code> getter method for the given
  +     * property type to perform the type conversion.  If the property type
  +     * doesn't match one of the supported <code>ResultSet</code> types, 
  +     * <code>getObject</code> is called.
  +     * 
        * @see org.apache.commons.dbutils.ColumnProcessor#process(java.sql.ResultSet, 
int, java.lang.Class)
        */
       public Object process(ResultSet rs, int index, Class propType)
  
  
  
  1.2       +38 -5     
jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/ColumnProcessor.java
  
  Index: ColumnProcessor.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/ColumnProcessor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ColumnProcessor.java      28 Nov 2003 19:32:10 -0000      1.1
  +++ ColumnProcessor.java      7 Dec 2003 17:25:01 -0000       1.2
  @@ -61,13 +61,17 @@
   
   package org.apache.commons.dbutils;
   
  +import java.beans.PropertyDescriptor;
   import java.sql.ResultSet;
  +import java.sql.ResultSetMetaData;
   import java.sql.SQLException;
   
   /**
  - * <code>ColumnProcessor</code> implementations convert 
  - * <code>ResultSet</code> columns into objects.  The processor is invoked when
  - * creating a JavaBean from a <code>ResultSet</code>.   
  + * <code>ColumnProcessor</code> implementations match 
  + * column names to bean property names and convert 
  + * <code>ResultSet</code> columns into objects for those bean properties.  
  + * The processor is invoked when creating a JavaBean from a 
  + * <code>ResultSet</code>.   
    *
    * @author Corby Page
    * @author David Graham
  @@ -77,6 +81,35 @@
    * @since DbUtils 1.1
    */
   public interface ColumnProcessor {
  +
  +    /**
  +     * Special array index used by <code>mapColumnsToProperties</code> that 
  +     * indicates there is no bean property that matches a column from a 
  +     * <code>ResultSet</code>.
  +     */
  +    public static final int PROPERTY_NOT_FOUND = -1;
  +
  +    /**
  +     * The positions in the returned array represent column numbers.  The 
  +     * values stored at each position represent the index in the 
  +     * <code>PropertyDescriptor[]</code> for the bean property that matches 
  +     * the column name.  If no bean property was found for a column, the 
  +     * position is set to <code>PROPERTY_NOT_FOUND</code>.
  +     * 
  +     * @param rsmd The <code>ResultSetMetaData</code> containing column 
  +     * information.
  +     * 
  +     * @param props The bean property descriptors.
  +     * 
  +     * @return An int[] with column index to property index mappings.  The 0th 
  +     * element is meaningless because JDBC column indexing starts at 1.
  +     * 
  +     * @throws SQLException
  +     */
  +    public int[] mapColumnsToProperties(
  +        ResultSetMetaData rsmd,
  +        PropertyDescriptor[] props)
  +        throws SQLException;
   
       /**
        * Convert a <code>ResultSet</code> column into an object.  Simple 
  
  
  

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

Reply via email to