diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/BasicRowProcessor.java src/java/org/apache/commons/dbutils/BasicRowProcessor.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/BasicRowProcessor.java 2003-11-11 18:27:16.000000000 -0600 +++ src/java/org/apache/commons/dbutils/BasicRowProcessor.java 2003-11-25 16:38:07.910033400 -0600 @@ -2,7 +2,7 @@ * $Header: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/BasicRowProcessor.java,v 1.5 2003/11/11 00:53:19 dgraham Exp $ * $Revision: 1.5 $ * $Date: 2003/11/11 00:53:19 $ - * + * * ==================================================================== * * The Apache Software License, Version 1.1 @@ -79,19 +79,20 @@ /** * Basic implementation of the RowProcessor interface. * This class is a thread-safe Singleton. - * + * * @see RowProcessor - * + * * @author Henri Yandell * @author Juozas Baliuka * @author David Graham * @author Yoav Shapira + * @author Corby Page */ public class BasicRowProcessor implements RowProcessor { /** - * Set a bean's primitive properties to these defaults when SQL NULL - * is returned. These are the same as the defaults that ResultSet get* + * Set a bean's primitive properties to these defaults when SQL NULL + * is returned. These are the same as the defaults that ResultSet get* * methods return in the event of a NULL column. */ private static final Map primitiveDefaults = new HashMap(); @@ -136,7 +137,7 @@ /** * Convert a ResultSet row into an Object[]. - * This implementation copies column values into the array in the same + * This implementation copies column values into the array in the same * order they're returned from the ResultSet. Array elements * will be set to null if the column was SQL NULL. * @@ -155,9 +156,9 @@ } /** - * Convert a ResultSet row into a JavaBean. This - * implementation uses reflection and BeanInfo classes to - * match column names to bean property names. Properties are matched to + * Convert a ResultSet row into a JavaBean. This + * implementation uses reflection and BeanInfo classes to + * match column names to bean property names. Properties are matched to * columns based on several factors: *
*
    @@ -165,24 +166,24 @@ * The class has a writable property with the same name as a column. * The name comparison is case insensitive. * - * + * *
  1. - * The property's set method parameter type matches the column + * The property's set method parameter type matches the column * type. If the data types do not match, the setter will not be called. *
  2. *
- * + * *

* Primitive bean properties are set to their defaults when SQL NULL is * returned from the ResultSet. Numeric fields are set to 0 - * and booleans are set to false. Object bean properties are set to + * and booleans are set to false. Object bean properties are set to * null when SQL NULL is returned. This is the same behavior * as the ResultSet get* methods. *

- * - * @see org.apache.commons.dbutils.RowProcessor#toBean(java.sql.ResultSet, java.lang.Class) + * + * @see org.apache.commons.dbutils.RowProcessor#toBean(java.sql.ResultSet, java.lang.Class, org.apache.commons.dbutils.ColumnAdaptor) */ - public Object toBean(ResultSet rs, Class type) throws SQLException { + public Object toBean(ResultSet rs, Class type, ColumnAdaptor adaptor) throws SQLException { PropertyDescriptor[] props = this.propertyDescriptors(type); @@ -192,13 +193,13 @@ int cols = rsmd.getColumnCount(); - return this.createBean(rs, type, props, columnToProperty, cols); + return this.createBean(rs, type, props, columnToProperty, cols, adaptor); } /** - * Convert a ResultSet into a List of JavaBeans. - * This implementation uses reflection and BeanInfo classes to - * match column names to bean property names. Properties are matched to + * Convert a ResultSet into a List of JavaBeans. + * This implementation uses reflection and BeanInfo classes to + * match column names to bean property names. Properties are matched to * columns based on several factors: *
*
    @@ -206,24 +207,24 @@ * The class has a writable property with the same name as a column. * The name comparison is case insensitive. * - * + * *
  1. - * The property's set method parameter type matches the column + * The property's set method parameter type matches the column * type. If the data types do not match, the setter will not be called. *
  2. *
- * + * *

* Primitive bean properties are set to their defaults when SQL NULL is * returned from the ResultSet. Numeric fields are set to 0 - * and booleans are set to false. Object bean properties are set to + * and booleans are set to false. Object bean properties are set to * null when SQL NULL is returned. This is the same behavior * as the ResultSet get* methods. *

- * - * @see org.apache.commons.dbutils.RowProcessor#toBeanList(java.sql.ResultSet, java.lang.Class) + * + * @see org.apache.commons.dbutils.RowProcessor#toBeanList(java.sql.ResultSet, java.lang.Class, org.apache.commons.dbutils.ColumnAdaptor) */ - public List toBeanList(ResultSet rs, Class type) throws SQLException { + public List toBeanList(ResultSet rs, Class type, ColumnAdaptor adaptor) throws SQLException { List results = new ArrayList(); if (!rs.next()) { @@ -236,7 +237,7 @@ int cols = rsmd.getColumnCount(); do { - results.add(this.createBean(rs, type, props, columnToProperty, cols)); + results.add(this.createBean(rs, type, props, columnToProperty, cols, adaptor)); } while (rs.next()); @@ -259,7 +260,8 @@ Class type, PropertyDescriptor[] props, int[] columnToProperty, - int cols) + int cols, + ColumnAdaptor adaptor ) throws SQLException { Object bean = this.newInstance(type); @@ -269,12 +271,12 @@ if (columnToProperty[i] == PROPERTY_NOT_FOUND) { continue; } - - Object value = rs.getObject(i); PropertyDescriptor prop = props[columnToProperty[i]]; Class propType = prop.getPropertyType(); + Object value = adaptor.getValue( rs, i, propType ); + if (propType != null && value == null && propType.isPrimitive()) { value = primitiveDefaults.get(propType); } @@ -287,15 +289,15 @@ /** * The positions in the returned array represent column numbers. The values - * stored at each position represent the index in the PropertyDescriptor[] + * 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 + * @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( @@ -324,9 +326,9 @@ } /** - * Convert a ResultSet row into a Map. This + * Convert a ResultSet row into a Map. This * implementation returns a Map with case insensitive column - * names as keys. Calls to map.get("COL") and + * names as keys. Calls to map.get("COL") and * map.get("col") return the same value. * @see org.apache.commons.dbutils.RowProcessor#toMap(java.sql.ResultSet) */ @@ -364,7 +366,7 @@ Class[] params = setter.getParameterTypes(); try { - // Don't call setter if the value object isn't the right type + // Don't call setter if the value object isn't the right type if (this.isCompatibleType(value, params[0])) { setter.invoke(target, new Object[] { value }); } @@ -389,7 +391,7 @@ * This method returns true if the value can be successfully passed into * the setter method. Remember, Method.invoke() handles the unwrapping * of Integer into an int. - * + * * @param value The value to be passed into the setter method. * @param type The setter's parameter type. * @return boolean True if the value is compatible. @@ -478,8 +480,8 @@ /** * A Map that converts all keys to lowercase Strings for case insensitive - * lookups. This is needed for the toMap() implementation because - * databases don't consistenly handle the casing of column names. + * lookups. This is needed for the toMap() implementation because + * databases don't consistenly handle the casing of column names. */ private static class CaseInsensitiveHashMap extends HashMap { @@ -517,11 +519,11 @@ } /** - * @see java.util.Map#remove(java.lang.ObjecT) + * @see java.util.Map#remove(java.lang.Object) */ public Object remove(Object key) { return super.remove(key.toString().toLowerCase()); } } - + } diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/ColumnAdaptor.java src/java/org/apache/commons/dbutils/ColumnAdaptor.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/ColumnAdaptor.java 1969-12-31 18:00:00.000000000 -0600 +++ src/java/org/apache/commons/dbutils/ColumnAdaptor.java 2003-11-25 16:36:43.392546400 -0600 @@ -0,0 +1,31 @@ +package org.apache.commons.dbutils; + +import java.sql.SQLException; +import java.sql.ResultSet; + +/** + * Implementations of this interface adapt result-set columns to a bean property + * + * @author Corby Page + */ +public interface ColumnAdaptor +{ + /** + * Adapts the ResultSet column to a type that is compatible + * with the JavaBean property that the columns will be mapped to. + * + * @param rs the supplied ResultSet + * + * @param index the current column index of the ResultSet + * + * @param propType the datatype of the JavaBean property that we will be adapting + * the ResultSet column to. + * + * @return The resultSet column, adapted to a datatype that is intended + * to be compatible with the supplied property type. + * + * @throws java.sql.SQLException + */ + public Object getValue( ResultSet rs, int index, Class propType ) throws SQLException; + +} diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/RowProcessor.java src/java/org/apache/commons/dbutils/RowProcessor.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/RowProcessor.java 2003-11-11 18:27:16.000000000 -0600 +++ src/java/org/apache/commons/dbutils/RowProcessor.java 2003-11-25 16:36:43.408174200 -0600 @@ -92,7 +92,7 @@ * passing it to this method. Implementations of this method must not * alter the row position of the ResultSet. */ - public Object toBean(ResultSet rs, Class type) throws SQLException; + public Object toBean(ResultSet rs, Class type, ColumnAdaptor adaptor) throws SQLException; /** * Create a List of JavaBeans from the column values in all @@ -102,7 +102,7 @@ * @return A List of beans with the given type in the order * they were returned by the ResultSet. */ - public List toBeanList(ResultSet rs, Class type) throws SQLException; + public List toBeanList(ResultSet rs, Class type, ColumnAdaptor adaptor) throws SQLException; /** * Create a Map from the column values in one diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/adaptors/DriverAdaptor.java src/java/org/apache/commons/dbutils/adaptors/DriverAdaptor.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/adaptors/DriverAdaptor.java 1969-12-31 18:00:00.000000000 -0600 +++ src/java/org/apache/commons/dbutils/adaptors/DriverAdaptor.java 2003-11-25 16:36:43.408174200 -0600 @@ -0,0 +1,63 @@ +package org.apache.commons.dbutils.adaptors; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.apache.commons.dbutils.ColumnAdaptor; + +/** + * ColumnAdaptor implementation that delegates to the JDBC + * driver to perform the adaption. Will call type-specific method on the driver + * (such as getDouble, getInt, etc.) depending on the supplied datatype for the + * JavaBean property + * + * @see org.apache.commons.dbutils.ColumnAdaptor + * + * @author Corby Page + */ +public class DriverAdaptor implements ColumnAdaptor { + + /** + * The Singleton instance of this class. + */ + private static final DriverAdaptor instance = new DriverAdaptor(); + + /** + * Returns the Singleton instance of this class. + * + * @return The single instance of this class. + */ + public static DriverAdaptor instance() { + return instance; + } + + public Object getValue( ResultSet rs, int index, Class propType ) throws SQLException { + Object value; + if ( propType.equals( Double.TYPE ) || propType.equals( Double.class ) ) { + value = new Double( rs.getDouble( index ) ); + } + else if ( propType.equals( Float.TYPE ) || propType.equals( Float.class ) ) { + value = new Float( rs.getFloat( index ) ); + } + else if ( propType.equals( Integer.TYPE ) || propType.equals( Integer.class ) ) { + value = new Integer( rs.getInt( index ) ); + } + else if ( propType.equals( Long.TYPE ) || propType.equals( Long.class ) ) { + value = new Long( rs.getLong( index ) ); + } + else if ( propType.equals( Short.TYPE ) || propType.equals( Short.class ) ) { + value = new Short( rs.getShort( index ) ); + } + else if ( propType.equals( Boolean.TYPE ) || propType.equals( Boolean.class ) ) { + value = new Boolean( rs.getBoolean( index ) ); + } + else if ( propType.equals( Byte.TYPE ) || propType.equals( Byte.class ) ) { + value = new Byte( rs.getByte( index ) ); + } + else { + value = rs.getObject( index ); + } + + return value; + } +} diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/adaptors/SimpleAdaptor.java src/java/org/apache/commons/dbutils/adaptors/SimpleAdaptor.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/adaptors/SimpleAdaptor.java 1969-12-31 18:00:00.000000000 -0600 +++ src/java/org/apache/commons/dbutils/adaptors/SimpleAdaptor.java 2003-11-25 16:36:43.392546400 -0600 @@ -0,0 +1,36 @@ +package org.apache.commons.dbutils.adaptors; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.apache.commons.dbutils.ColumnAdaptor; + +/** + * ColumnAdaptor implementation that does no real work, + * but performs quite well. Simply returns the result of a getObject() call on + * the ResultSet + * + * @see org.apache.commons.dbutils.ColumnAdaptor + * + * @author Corby Page + */ +public class SimpleAdaptor implements ColumnAdaptor +{ + /** + * The Singleton instance of this class. + */ + private static final SimpleAdaptor instance = new SimpleAdaptor(); + + /** + * Returns the Singleton instance of this class. + * + * @return The single instance of this class. + */ + public static SimpleAdaptor instance() { + return instance; + } + + public Object getValue( ResultSet rs, int index, Class propType ) throws SQLException { + return rs.getObject( index ); + } +} diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/handlers/BeanHandler.java src/java/org/apache/commons/dbutils/handlers/BeanHandler.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/handlers/BeanHandler.java 2003-11-11 18:27:16.000000000 -0600 +++ src/java/org/apache/commons/dbutils/handlers/BeanHandler.java 2003-11-25 17:17:43.099507200 -0600 @@ -66,6 +66,8 @@ import org.apache.commons.dbutils.BasicRowProcessor; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.RowProcessor; +import org.apache.commons.dbutils.ColumnAdaptor; +import org.apache.commons.dbutils.adaptors.SimpleAdaptor; /** * ResultSetHandler implementation that converts the first @@ -75,6 +77,7 @@ * * @author Juozas Baliuka * @author David Graham + * @author Corby Page */ public class BeanHandler implements ResultSetHandler { @@ -89,7 +92,13 @@ */ private RowProcessor convert = BasicRowProcessor.instance(); - /** + /** + * The ColumnAdaptor implementation to use when adapting + * columns to bean properties. + */ + private ColumnAdaptor adaptor = SimpleAdaptor.instance(); + + /** * Creates a new instance of BeanHandler. * * @param type The Class that objects returned from handle() @@ -113,6 +122,22 @@ } /** + * Creates a new instance of BeanHandler. + * + * @param type The Class that objects returned from handle() + * are created from. + * @param convert The RowProcessor implementation + * to use when converting rows into beans. + * @param adaptor The ColumnAdaptor implementation to use when adapting + * columns to bean properties. + */ + public BeanHandler(Class type, RowProcessor convert, ColumnAdaptor adaptor ) { + this.type = type; + this.convert = convert; + this.adaptor = adaptor; + } + + /** * Convert the first row of the ResultSet into a bean with the * Class given in the constructor. * @@ -123,7 +148,7 @@ * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) */ public Object handle(ResultSet rs) throws SQLException { - return rs.next() ? this.convert.toBean(rs, this.type) : null; + return rs.next() ? this.convert.toBean(rs, this.type, adaptor ) : null; } } diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java 2003-11-11 18:27:16.000000000 -0600 +++ src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java 2003-11-25 16:41:37.701537600 -0600 @@ -67,6 +67,8 @@ import org.apache.commons.dbutils.BasicRowProcessor; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.RowProcessor; +import org.apache.commons.dbutils.ColumnAdaptor; +import org.apache.commons.dbutils.adaptors.SimpleAdaptor; /** * ResultSetHandler implementation that converts a @@ -91,7 +93,13 @@ */ private RowProcessor convert = BasicRowProcessor.instance(); - /** + /** + * The ColumnAdaptor implementation to use when adapting + * columns to bean properties. + */ + private ColumnAdaptor adaptor = SimpleAdaptor.instance(); + + /** * Creates a new instance of BeanListHandler. * * @param type The Class that objects returned from handle() @@ -115,6 +123,22 @@ } /** + * Creates a new instance of BeanListHandler. + * + * @param type The Class that objects returned from handle() + * are created from. + * @param convert The RowProcessor implementation + * to use when converting rows into beans. + * @param adaptor The ColumnAdaptor implementation to use when adapting + * columns to bean properties. + */ + public BeanListHandler(Class type, RowProcessor convert, ColumnAdaptor adaptor ) { + this.type = type; + this.convert = convert; + this.adaptor = adaptor; + } + + /** * Convert the ResultSet rows into a List of * beans with the Class given in the constructor. * @@ -125,7 +149,7 @@ * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) */ public Object handle(ResultSet rs) throws SQLException { - return this.convert.toBeanList(rs, type); + return this.convert.toBeanList(rs, type, adaptor ); } } diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/test/org/apache/commons/dbutils/BaseTestCase.java src/test/org/apache/commons/dbutils/BaseTestCase.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/test/org/apache/commons/dbutils/BaseTestCase.java 2003-11-11 18:27:16.000000000 -0600 +++ src/test/org/apache/commons/dbutils/BaseTestCase.java 2003-11-25 17:23:16.079183700 -0600 @@ -61,24 +61,20 @@ package org.apache.commons.dbutils; +import java.math.BigDecimal; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.Date; +import org.apache.commons.dbutils.adaptors.DriverAdaptorTest; +import org.apache.commons.dbutils.handlers.*; +import org.apache.commons.dbutils.wrappers.SqlNullCheckedResultSetTest; +import org.apache.commons.dbutils.wrappers.StringTrimmedResultSetTest; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; -import org.apache.commons.dbutils.handlers.ArrayHandlerTest; -import org.apache.commons.dbutils.handlers.ArrayListHandlerTest; -import org.apache.commons.dbutils.handlers.BeanHandlerTest; -import org.apache.commons.dbutils.handlers.BeanListHandlerTest; -import org.apache.commons.dbutils.handlers.MapHandlerTest; -import org.apache.commons.dbutils.handlers.MapListHandlerTest; -import org.apache.commons.dbutils.handlers.ScalarHandlerTest; -import org.apache.commons.dbutils.wrappers.SqlNullCheckedResultSetTest; -import org.apache.commons.dbutils.wrappers.StringTrimmedResultSetTest; - /** * BaseTestCase is the base class for all test cases as well as the "all tests" * runner. @@ -97,7 +93,8 @@ "integerTest", "nullObjectTest", "nullPrimitiveTest", - "notDate" }; + "notDate", + "doubleTest"}; /** * The number of columns in the MockResultSet. @@ -117,7 +114,8 @@ new Integer(2), null, null, - new Date()}; + new Date(), + new BigDecimal( 5 )}; private static final Object[] row2 = new Object[] { @@ -129,7 +127,8 @@ new Integer(4), null, null, - new Date()}; + new Date(), + new BigDecimal( 6 )}; private static final Object[][] rows = new Object[][] { row1, row2 }; @@ -190,6 +189,7 @@ suite.addTestSuite(ArrayListHandlerTest.class); suite.addTestSuite(BeanHandlerTest.class); suite.addTestSuite(BeanListHandlerTest.class); + suite.addTestSuite(DriverAdaptorTest.class); suite.addTestSuite(MapHandlerTest.class); suite.addTestSuite(MapListHandlerTest.class); suite.addTestSuite(ScalarHandlerTest.class); diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/test/org/apache/commons/dbutils/BasicRowProcessorTest.java src/test/org/apache/commons/dbutils/BasicRowProcessorTest.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/test/org/apache/commons/dbutils/BasicRowProcessorTest.java 2003-11-11 18:27:16.000000000 -0600 +++ src/test/org/apache/commons/dbutils/BasicRowProcessorTest.java 2003-11-25 16:43:24.115499200 -0600 @@ -65,6 +65,8 @@ import java.util.List; import java.util.Map; +import org.apache.commons.dbutils.adaptors.SimpleAdaptor; + /** * Test the BasicRowProcessor class. * @@ -103,7 +105,7 @@ int rowCount = 0; TestBean b = null; while (this.rs.next()) { - b = (TestBean) processor.toBean(this.rs, TestBean.class); + b = (TestBean) processor.toBean(this.rs, TestBean.class, SimpleAdaptor.instance()); assertNotNull(b); rowCount++; } @@ -122,7 +124,7 @@ public void testToBeanList() throws SQLException { - List list = processor.toBeanList(this.rs, TestBean.class); + List list = processor.toBeanList(this.rs, TestBean.class, SimpleAdaptor.instance()); assertNotNull(list); assertEquals(ROWS, list.size()); diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/test/org/apache/commons/dbutils/MockResultSet.java src/test/org/apache/commons/dbutils/MockResultSet.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/test/org/apache/commons/dbutils/MockResultSet.java 2003-11-11 18:27:16.000000000 -0600 +++ src/test/org/apache/commons/dbutils/MockResultSet.java 2003-11-25 17:55:29.812287000 -0600 @@ -69,6 +69,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Iterator; +import java.math.BigDecimal; /** * MockResultSet dynamically implements the ResultSet interface. @@ -152,7 +153,27 @@ return this.getString((String) args[0]); } - } else if (methodName.equals("wasNull")) { + } else if (methodName.equals("getInt")) { + Integer result = null; + if ( args[0] instanceof Integer ) { + int col = ((Integer) args[0]).intValue(); + result = this.getInt(col); + + } else if (args[0] instanceof String) { + result = this.getInt((String) args[0]); + } + return (result == null) ? new Integer( 0 ) : result; + } else if (methodName.equals("getDouble")) { + Double result = null; + if ( args[0] instanceof Integer ) { + int col = ((Integer) args[0]).intValue(); + result = this.getDouble(col); + + } else if (args[0] instanceof String) { + result = this.getDouble((String) args[0]); + } + return (result == null) ? new Double( 0 ) : result; + } else if (methodName.equals("wasNull")) { return this.wasNull(); } else if (methodName.equals("isLast")) { @@ -215,6 +236,66 @@ return (obj == null) ? null : obj.toString(); } + /** + * Gets the Double at the given column index. + * @param columnIndex A 1 based index. + * @throws SQLException + */ + protected Integer getInt(int columnIndex) throws SQLException { + Object obj = this.getObject(columnIndex); + if ( obj == null ) { + return null; + } + if ( obj instanceof Integer ) { + return (Integer)obj; + } + throw new SQLException( "Column could not be mapped to integer type" ); + } + + protected Integer getInt(String columnName) throws SQLException { + Object obj = this.getObject(this.findColumnIndex(columnName)); + if ( obj == null ) { + return null; + } + if ( obj instanceof Integer ) { + return (Integer)obj; + } + throw new SQLException( "Column could not be mapped to integer type" ); + } + + /** + * Gets the Double at the given column index. + * @param columnIndex A 1 based index. + * @throws SQLException + */ + protected Double getDouble(int columnIndex) throws SQLException { + Object obj = this.getObject(columnIndex); + if ( obj == null ) { + return null; + } + if ( obj instanceof Double ) { + return (Double)obj; + } + if ( obj instanceof BigDecimal ) { + return new Double(((BigDecimal)obj).doubleValue()); + } + throw new SQLException( "Column could not be mapped to double type" ); + } + + protected Double getDouble(String columnName) throws SQLException { + Object obj = this.getObject(this.findColumnIndex(columnName)); + if ( obj == null ) { + return null; + } + if ( obj instanceof Double ) { + return (Double)obj; + } + if ( obj instanceof BigDecimal ) { + return new Double(((BigDecimal)obj).doubleValue()); + } + throw new SQLException( "Column could not be mapped to double type" ); + } + protected Boolean next() throws SQLException { if (!this.iter.hasNext()) { return Boolean.FALSE; diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/test/org/apache/commons/dbutils/TestBean.java src/test/org/apache/commons/dbutils/TestBean.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/test/org/apache/commons/dbutils/TestBean.java 2003-11-11 18:27:16.000000000 -0600 +++ src/test/org/apache/commons/dbutils/TestBean.java 2003-11-25 17:21:30.664699200 -0600 @@ -80,6 +80,8 @@ private String doNotSet = "not set"; + private double doubleTest = 0; + /** * toBean() should set primitive fields to their defaults (ie. 0) when * null is returned from the ResultSet. @@ -178,4 +180,11 @@ notDate = string; } + public double getDoubleTest() { + return doubleTest; + } + + public void setDoubleTest( double d ) { + doubleTest = d; + } } diff -ruN /cygdrive/c/Java/commons-dbutils-1.0/src/test/org/apache/commons/dbutils/adaptors/DriverAdaptorTest.java src/test/org/apache/commons/dbutils/adaptors/DriverAdaptorTest.java --- /cygdrive/c/Java/commons-dbutils-1.0/src/test/org/apache/commons/dbutils/adaptors/DriverAdaptorTest.java 1969-12-31 18:00:00.000000000 -0600 +++ src/test/org/apache/commons/dbutils/adaptors/DriverAdaptorTest.java 2003-11-25 18:38:53.128370300 -0600 @@ -0,0 +1,37 @@ +package org.apache.commons.dbutils.adaptors; + +import java.sql.SQLException; + +import org.apache.commons.dbutils.BaseTestCase; +import org.apache.commons.dbutils.BasicRowProcessor; +import org.apache.commons.dbutils.ResultSetHandler; +import org.apache.commons.dbutils.TestBean; +import org.apache.commons.dbutils.handlers.BeanHandler; + +/** + * BeanHandlerTest + * + * @author Corby Page + */ +public class DriverAdaptorTest extends BaseTestCase +{ + /** + * Constructor for DriverAdaptorTest. + */ + public DriverAdaptorTest( String name ) { + super( name ); + } + + public void testDriverAdaptor() throws SQLException { + ResultSetHandler h = new BeanHandler(TestBean.class, BasicRowProcessor.instance(), + DriverAdaptor.instance()); + TestBean results = (TestBean) h.handle(this.rs); + + assertNotNull(results); + assertEquals("1", results.getOne()); + assertEquals("2", results.getTwo()); + assertEquals("3", results.getThree()); + assertEquals("not set", results.getDoNotSet()); + assertEquals( 5, results.getDoubleTest(), 0 ); + } +}