You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2014/07/16 18:55:18 UTC

svn commit: r1611088 - in /commons/proper/dbutils/trunk/src: changes/changes.xml main/java/org/apache/commons/dbutils/BasicRowProcessor.java test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java

Author: britter
Date: Wed Jul 16 16:55:17 2014
New Revision: 1611088

URL: http://svn.apache.org/r1611088
Log:
DBUTILS-114: Order of columns not retained in BasicRowProcessor with HashMap. Thanks to Michael Osipov.

Modified:
    commons/proper/dbutils/trunk/src/changes/changes.xml
    commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java
    commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java

Modified: commons/proper/dbutils/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/changes/changes.xml?rev=1611088&r1=1611087&r2=1611088&view=diff
==============================================================================
--- commons/proper/dbutils/trunk/src/changes/changes.xml (original)
+++ commons/proper/dbutils/trunk/src/changes/changes.xml Wed Jul 16 16:55:17 2014
@@ -44,6 +44,9 @@ The <action> type attribute can be add,u
   </properties>
   <body>
     <release version="1.6" date="TBA" description="Bugfixes and addition of insert methods">
+      <action dev="britter" type="fix" issue="DBUTILS-114" due-to="Michael Osipov">
+        Order of columns not retained in BasicRowProcessor with HashMap
+      </action>
       <action dev="britter" type="fix" issue="DBUTILS-118" due-to="Feysal Rujbally, Daniele Cremonini">
         BeanProcessor not returning nanoseconds
       </action>

Modified: commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java?rev=1611088&r1=1611087&r2=1611088&view=diff
==============================================================================
--- commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java (original)
+++ commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java Wed Jul 16 16:55:17 2014
@@ -20,6 +20,7 @@ import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -140,14 +141,19 @@ public class BasicRowProcessor implement
     }
 
     /**
-     * Convert a <code>ResultSet</code> row into a <code>Map</code>.  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.
-     * @see org.apache.commons.dbutils.RowProcessor#toMap(java.sql.ResultSet)
+     * Convert a <code>ResultSet</code> row into a <code>Map</code>.
+     *
+     * <p>
+     * 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. Furthermore this implementation
+     * will return an ordered map, that preserves the ordering of the columns in the ResultSet, so that iterating over
+     * the entry set of the returned map will return the first column of the ResultSet, then the second and so forth.
+     * </p>
+     *
      * @param rs ResultSet that supplies the map data
-     * @throws SQLException if a database access error occurs
      * @return the newly created Map
+     * @throws SQLException if a database access error occurs
+     * @see org.apache.commons.dbutils.RowProcessor#toMap(java.sql.ResultSet)
      */
     @Override
     public Map<String, Object> toMap(ResultSet rs) throws SQLException {
@@ -176,12 +182,12 @@ public class BasicRowProcessor implement
      * achieve the case insensitive lookup.
      *
      * <p>Note: This implementation does not allow <tt>null</tt>
-     * for key, whereas {@link HashMap} does, because of the code:
+     * for key, whereas {@link LinkedHashMap} does, because of the code:
      * <pre>
      * key.toString().toLowerCase()
      * </pre>
      */
-    private static class CaseInsensitiveHashMap extends HashMap<String, Object> {
+    private static class CaseInsensitiveHashMap extends LinkedHashMap<String, Object> {
         /**
          * The internal mapping from lowercase keys to the real keys.
          *

Modified: commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java?rev=1611088&r1=1611087&r2=1611088&view=diff
==============================================================================
--- commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java (original)
+++ commons/proper/dbutils/trunk/src/test/java/org/apache/commons/dbutils/BasicRowProcessorTest.java Wed Jul 16 16:55:17 2014
@@ -20,6 +20,7 @@ import java.sql.SQLException;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -137,4 +138,24 @@ public class BasicRowProcessorTest exten
         assertFalse(this.rs.next());
     }
 
+    public void testToMapOrdering() throws SQLException {
+
+        assertTrue(this.rs.next());
+        Map<String, Object> m = processor.toMap(this.rs);
+
+        Iterator<String> itr = m.keySet().iterator();
+        assertEquals("one", itr.next());
+        assertEquals("two", itr.next());
+        assertEquals("three", itr.next());
+        assertEquals("notInBean", itr.next());
+        assertEquals("intTest", itr.next());
+        assertEquals("integerTest", itr.next());
+        assertEquals("nullObjectTest", itr.next());
+        assertEquals("nullPrimitiveTest", itr.next());
+        assertEquals("notDate", itr.next());
+        assertEquals("columnProcessorDoubleTest", itr.next());
+
+        assertFalse(itr.hasNext());
+    }
+
 }