|
|
|
Dan Fabulich-2
|
Author: dfabulich
Date: Tue Nov 3 19:53:33 2009 New Revision: 832529 URL: http://svn.apache.org/viewvc?rev=832529&view=rev Log: [DBUTILS-61] Backwards binary compatibility broken in KeyedHandler Added: commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/AbstractKeyedHandler.java Modified: commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/KeyedHandler.java Added: commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/AbstractKeyedHandler.java URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/AbstractKeyedHandler.java?rev=832529&view=auto ============================================================================== --- commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/AbstractKeyedHandler.java (added) +++ commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/AbstractKeyedHandler.java Tue Nov 3 19:53:33 2009 @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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; + +/** + * <p> + * <code>ResultSetHandler</code> implementation that returns a Map. + * <code>ResultSet</code> rows are converted into objects (Vs) which are then stored + * in a Map under the given keys (Ks). + * </p> + * + * @see org.apache.commons.dbutils.ResultSetHandler + * @since DbUtils 1.3 + */ +public abstract class AbstractKeyedHandler<K,V> implements ResultSetHandler<Map<K,V>> { + + + /** + * 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>, never <code>null</code>. + * @throws SQLException if a database access error occurs + * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) + */ + public Map<K,V> handle(ResultSet rs) throws SQLException { + Map<K,V> 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. + * + * @return Map to store records in + */ + protected Map<K,V> createMap() { + return new HashMap<K,V>(); + } + + /** + * This factory method is called by <code>handle()</code> to retrieve the + * key value from the current <code>ResultSet</code> row. + * @param rs ResultSet to create a key from + * @return K from the configured key column name/index + * @throws SQLException if a database access error occurs + */ + protected abstract K createKey(ResultSet rs) throws SQLException; + + /** + * This factory method is called by <code>handle()</code> to store the + * current <code>ResultSet</code> row in some object. + * @param rs ResultSet to create a row from + * @return V object created from the current row + * @throws SQLException if a database access error occurs + */ + protected abstract V createRow(ResultSet rs) throws SQLException; + +} Modified: commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/KeyedHandler.java URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/KeyedHandler.java?rev=832529&r1=832528&r2=832529&view=diff ============================================================================== --- commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/KeyedHandler.java (original) +++ commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/KeyedHandler.java Tue Nov 3 19:53:33 2009 @@ -18,19 +18,15 @@ 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. + * in a Map under the given key. * </p> * <p> * If you had a Person table with a primary key column called ID, you could @@ -47,17 +43,12 @@ * 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 org.apache.commons.dbutils.ResultSetHandler * @since DbUtils 1.1 */ -public class KeyedHandler implements ResultSetHandler<Map<Object,Map<String,Object>>> { +public class KeyedHandler extends AbstractKeyedHandler<Object, Map<String,Object>> { /** * The RowProcessor implementation to use when converting rows @@ -123,34 +114,6 @@ this.columnIndex = columnIndex; 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 if a database access error occurs - * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) - */ - public Map<Object,Map<String,Object>> handle(ResultSet rs) throws SQLException { - Map<Object,Map<String,Object>> 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. - * - * @return Map to store records in - */ - protected Map<Object,Map<String,Object>> createMap() { - return new HashMap<Object,Map<String,Object>>(); - } - /** * This factory method is called by <code>handle()</code> to retrieve the * key value from the current <code>ResultSet</code> row. This @@ -160,6 +123,7 @@ * @return Object from the configured key column name/index * @throws SQLException if a database access error occurs */ + @Override protected Object createKey(ResultSet rs) throws SQLException { return (columnName == null) ? rs.getObject(columnIndex) : rs .getObject(columnName); @@ -175,6 +139,7 @@ * @return Object typed Map containing column names to values * @throws SQLException if a database access error occurs */ + @Override protected Map<String,Object> createRow(ResultSet rs) throws SQLException { return this.convert.toMap(rs); } |
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |