commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons/dbutils project.xml
Date Sun, 19 Dec 2004 02:59:49 GMT
dgraham     2004/12/18 18:59:49

  Modified:    dbutils/xdocs changes.xml
               dbutils/src/test/org/apache/commons/dbutils
                        BaseTestCase.java
               dbutils  project.xml
  Added:       dbutils/src/test/org/apache/commons/dbutils/handlers
                        KeyedHandlerTest.java
               dbutils/src/java/org/apache/commons/dbutils/handlers
                        KeyedHandler.java
  Log:
  Added KeyedHandler to create a Map of Maps from a ResultSet.
  PR: 31446
  
  Revision  Changes    Path
  1.1                  jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/handlers/KeyedHandlerTest.java
  
  Index: KeyedHandlerTest.java
  ===================================================================
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.dbutils.handlers;
  
  import java.sql.SQLException;
  import java.util.Iterator;
  import java.util.Map;
  
  import org.apache.commons.dbutils.BaseTestCase;
  import org.apache.commons.dbutils.ResultSetHandler;
  
  public class KeyedHandlerTest extends BaseTestCase {
  
      public KeyedHandlerTest(String name) {
          super(name);
      }
  
      public void testHandle() throws SQLException {
          ResultSetHandler h = new KeyedHandler();
  
          Map results = (Map) h.handle(this.rs);
  
          assertNotNull(results);
          assertEquals(ROWS, results.size());
  
          Iterator iter = results.keySet().iterator();
          Map row = null;
          while (iter.hasNext()) {
              Object key = iter.next();
              assertNotNull(key);
              row = (Map) results.get(key);
              assertNotNull(row);
              assertEquals(COLS, row.keySet().size());
          }
  
          row = (Map) results.get("1");
          assertEquals("1", row.get("one"));
          assertEquals("2", row.get("TWO"));
          assertEquals("3", row.get("Three"));
      }
  
      public void testColumnIndexHandle() throws SQLException {
          ResultSetHandler h = new KeyedHandler(2);
          Map results = (Map) h.handle(this.rs);
  
          assertNotNull(results);
          assertEquals(ROWS, results.size());
  
          Iterator iter = results.keySet().iterator();
          Map row = null;
          while (iter.hasNext()) {
              Object key = iter.next();
              assertNotNull(key);
              row = (Map) results.get(key);
              assertNotNull(row);
              assertEquals(COLS, row.keySet().size());
          }
  
          row = (Map) results.get("5");
          assertEquals("4", row.get("one"));
          assertEquals("5", row.get("TWO"));
          assertEquals("6", row.get("Three"));
      }
  
      public void testColumnNameHandle() throws SQLException {
          ResultSetHandler h = new KeyedHandler("three");
          Map results = (Map) h.handle(this.rs);
  
          assertNotNull(results);
          assertEquals(ROWS, results.size());
  
          Iterator iter = results.keySet().iterator();
          Map row = null;
          while (iter.hasNext()) {
              Object key = iter.next();
              assertNotNull(key);
              row = (Map) results.get(key);
              assertNotNull(row);
              assertEquals(COLS, row.keySet().size());
          }
  
          row = (Map) results.get("6");
          assertEquals("4", row.get("one"));
          assertEquals("5", row.get("TWO"));
          assertEquals("6", row.get("Three"));
      }
  
      public void testEmptyResultSetHandle() throws SQLException {
          ResultSetHandler h = new KeyedHandler();
          Map results = (Map) h.handle(this.emptyResultSet);
          assertNotNull(results);
          assertTrue(results.isEmpty());
      }
  }
  
  
  1.1                  jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/KeyedHandler.java
  
  Index: KeyedHandler.java
  ===================================================================
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.dbutils.handlers;
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.HashMap;
  import java.util.Map;
  
  import org.apache.commons.dbutils.ResultSetHandler;
  import org.apache.commons.dbutils.RowProcessor;
  
  /**
   * <p>
   * <code>ResultSetHandler</code> implementation that returns a Map of Maps.
   * <code>ResultSet</code> rows are converted into Maps which are then stored
   * in a Map under the given key.  Although this implementation uses Maps to 
   * store row contents, subclasses are encouraged to override the 
   * <code>createRow()</code> method to convert the rows into any kind of object.
   * </p>
   * <p>
   * If you had a Person table with a primary key column called ID, you could 
   * retrieve rows from the table like this:
   * <pre>
   * ResultSetHandler h = new KeyedHandler("id");
   * Map found = (Map) queryRunner.query("select id, name, age from person", h);
   * Map jane = (Map) found.get(new Long(1)); // jane's id is 1
   * String janesName = (String) jane.get("name");
   * Integer janesAge = (Integer) jane.get("age");
   * </pre>
   * Note that the "id" passed to KeyedHandler and "name" and "age" passed to the
   * returned Map's get() method can be in any case.  The data types returned for
   * name and age are dependent upon how your JDBC driver converts SQL column 
   * types from the Person table into Java types.  
   * </p>
   * <p>
   * To avoid these type issues you could subclass KeyedHandler and override 
   * <code>createRow()</code> to store rows in Java bean instances (ie. a
   * Person class).
   * </p>
   * <p>This class is thread safe.</p>
   * 
   * @see ResultSetHandler
   * @since DbUtils 1.1
   */
  public class KeyedHandler implements ResultSetHandler {
  
      /**
       * The RowProcessor implementation to use when converting rows
       * into Objects.
       */
      protected RowProcessor convert = ArrayHandler.ROW_PROCESSOR;
  
      /**
       * The column index to retrieve key values from.  Defaults to 1.
       */
      protected int columnIndex = 1;
  
      /**
       * The column name to retrieve key values from.  Either columnName or 
       * columnIndex will be used but never both.
       */
      protected String columnName = null;
  
      /** 
       * Creates a new instance of KeyedHandler.  The value of the first column 
       * of each row will be a key in the Map.
       */
      public KeyedHandler() {
          super();
      }
  
      /**
       * Creates a new instance of KeyedHandler.  The value of the first column 
       * of each row will be a key in the Map.
       *
       * @param convert The <code>RowProcessor</code> implementation
       * to use when converting rows into Maps
       */
      public KeyedHandler(RowProcessor convert) {
          super();
          this.convert = convert;
      }
  
      /** 
       * Creates a new instance of KeyedHandler.
       * 
       * @param columnIndex The values to use as keys in the Map are 
       * retrieved from the column at this index.
       */
      public KeyedHandler(int columnIndex) {
          super();
          this.columnIndex = columnIndex;
      }
  
      /** 
       * Creates a new instance of KeyedHandler.
       * 
       * @param columnName The values to use as keys in the Map are 
       * retrieved from the column with this name.
       */
      public KeyedHandler(String columnName) {
          super();
          this.columnName = columnName;
      }
  
      /**
       * Convert each row's columns into a Map and store then 
       * in a <code>Map</code> under <code>ResultSet.getObject(key)</code>
key.
       * 
       * @return A <code>Map</code> of Maps, never <code>null</code>.

       * @throws SQLException
       * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
       */
      public Object handle(ResultSet rs) throws SQLException {
          Map result = createMap();
          while (rs.next()) {
              result.put(createKey(rs), createRow(rs));
          }
          return result;
      }
  
      /**
       * This factory method is called by <code>handle()</code> to create the
Map
       * to store records in.  This implementation returns a <code>HashMap</code>
       * instance.
       */
      protected Map createMap() {
          return new HashMap();
      }
  
      /**
       * This factory method is called by <code>handle()</code> to retrieve the
       * key value from the current <code>ResultSet</code> row.  This 
       * implementation returns <code>ResultSet.getObject()</code> for the 
       * configured key column name or index. 
       * @throws SQLException
       */
      protected Object createKey(ResultSet rs) throws SQLException {
          return (columnName == null) ? rs.getObject(columnIndex) : rs
                  .getObject(columnName);
      }
  
      /**
       * This factory method is called by <code>handle()</code> to store the
       * current <code>ResultSet</code> row in some object. This 
       * implementation returns a <code>Map</code> with case insensitive column
       * names as keys.  Calls to <code>map.get("COL")</code> and 
       * <code>map.get("col")</code> return the same value.
       * @throws SQLException
       */
      protected Object createRow(ResultSet rs) throws SQLException {
          return this.convert.toMap(rs);
      }
  
  }
  
  
  1.9       +3 -0      jakarta-commons/dbutils/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbutils/xdocs/changes.xml,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- changes.xml	3 Oct 2004 16:43:44 -0000	1.8
  +++ changes.xml	19 Dec 2004 02:59:49 -0000	1.9
  @@ -39,6 +39,9 @@
     <body>
   
       <release version="1.1-dev" date="in CVS">
  +      <action dev="dgraham" type="add">
  +        Added KeyedHandler to create a Map of Maps from a ResultSet. PR: 31446
  +      </action>
         <action dev="dgraham" type="update">
           Use current class' ClassLoader instead of QueryLoader's ClassLoader
           in loadQueries().  PR: 31169
  
  
  
  1.9       +2 -0      jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/BaseTestCase.java
  
  Index: BaseTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/BaseTestCase.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- BaseTestCase.java	14 Mar 2004 23:03:54 -0000	1.8
  +++ BaseTestCase.java	19 Dec 2004 02:59:49 -0000	1.9
  @@ -30,6 +30,7 @@
   import org.apache.commons.dbutils.handlers.BeanHandlerTest;
   import org.apache.commons.dbutils.handlers.BeanListHandlerTest;
   import org.apache.commons.dbutils.handlers.ColumnListHandlerTest;
  +import org.apache.commons.dbutils.handlers.KeyedHandlerTest;
   import org.apache.commons.dbutils.handlers.MapHandlerTest;
   import org.apache.commons.dbutils.handlers.MapListHandlerTest;
   import org.apache.commons.dbutils.handlers.ScalarHandlerTest;
  @@ -153,6 +154,7 @@
           suite.addTestSuite(MapListHandlerTest.class);
           suite.addTestSuite(ScalarHandlerTest.class);
           suite.addTestSuite(ColumnListHandlerTest.class);
  +        suite.addTestSuite(KeyedHandlerTest.class);
   
           suite.addTestSuite(StringTrimmedResultSetTest.class);
           suite.addTestSuite(SqlNullCheckedResultSetTest.class);
  
  
  
  1.19      +18 -0     jakarta-commons/dbutils/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbutils/project.xml,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- project.xml	19 Dec 2004 01:22:55 -0000	1.18
  +++ project.xml	19 Dec 2004 02:59:49 -0000	1.19
  @@ -145,6 +145,15 @@
         </roles>
       </contributor>
       <contributor>
  +      <name>Piotr Lakomy</name>
  +      <id></id>
  +      <email>piotrl@cft-inc.net</email>
  +      <organization></organization>
  +      <roles>
  +        <role>Java Developer</role>
  +      </roles>
  +    </contributor>
  +    <contributor>
         <name>Corby Page</name>
         <id></id>
         <email></email>
  @@ -157,6 +166,15 @@
         <name>Michael Schuerig</name>
         <id></id>
         <email>michael@schuerig.de</email>
  +      <organization></organization>
  +      <roles>
  +        <role>Java Developer</role>
  +      </roles>
  +    </contributor>
  +    <contributor>
  +      <name>Norris Shelton</name>
  +      <id></id>
  +      <email>norrisshelton@yahoo.com</email>
         <organization></organization>
         <roles>
           <role>Java Developer</role>
  
  
  

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


Mime
View raw message