You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/11/28 20:32:10 UTC
cvs commit: jakarta-commons/dbutils/src/test/org/apache/commons/dbutils BasicRowProcessorTest.java
dgraham 2003/11/28 11:32:10
Modified: dbutils/src/java/org/apache/commons/dbutils/handlers
MapHandler.java BeanListHandler.java
BeanHandler.java MapListHandler.java
ArrayListHandler.java ArrayHandler.java
dbutils/src/java/org/apache/commons/dbutils
BasicRowProcessor.java ResultSetIterator.java
dbutils/src/test/org/apache/commons/dbutils
BasicRowProcessorTest.java
Added: dbutils/src/java/org/apache/commons/dbutils
ColumnProcessor.java
Log:
Added ColumnProcessor interface to support pluggable
column to bean property type conversions. Made
BasicRowProcessor constructors publicly available to
support passing in ColumnProcessor implementations. It is
no longer a Singleton. PR# 24997
Revision Changes Path
1.2 +4 -5 jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/MapHandler.java
Index: MapHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/MapHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MapHandler.java 2 Nov 2003 19:15:24 -0000 1.1
+++ MapHandler.java 28 Nov 2003 19:32:10 -0000 1.2
@@ -64,7 +64,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;
-import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;
@@ -83,7 +82,7 @@
* The RowProcessor implementation to use when converting rows
* into Maps.
*/
- private RowProcessor convert = BasicRowProcessor.instance();
+ private RowProcessor convert = ArrayHandler.ROW_PROCESSOR;
/**
* Creates a new instance of MapHandler using a
1.2 +4 -5 jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java
Index: BeanListHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BeanListHandler.java 2 Nov 2003 19:15:24 -0000 1.1
+++ BeanListHandler.java 28 Nov 2003 19:32:10 -0000 1.2
@@ -64,7 +64,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;
-import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;
@@ -89,7 +88,7 @@
* The RowProcessor implementation to use when converting rows
* into beans.
*/
- private RowProcessor convert = BasicRowProcessor.instance();
+ private RowProcessor convert = ArrayHandler.ROW_PROCESSOR;
/**
* Creates a new instance of BeanListHandler.
1.2 +4 -5 jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/BeanHandler.java
Index: BeanHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/BeanHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BeanHandler.java 2 Nov 2003 19:15:24 -0000 1.1
+++ BeanHandler.java 28 Nov 2003 19:32:10 -0000 1.2
@@ -63,7 +63,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;
-import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;
@@ -87,7 +86,7 @@
* The RowProcessor implementation to use when converting rows
* into beans.
*/
- private RowProcessor convert = BasicRowProcessor.instance();
+ private RowProcessor convert = ArrayHandler.ROW_PROCESSOR;
/**
* Creates a new instance of BeanHandler.
1.2 +4 -5 jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/MapListHandler.java
Index: MapListHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/MapListHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MapListHandler.java 2 Nov 2003 19:15:24 -0000 1.1
+++ MapListHandler.java 28 Nov 2003 19:32:10 -0000 1.2
@@ -66,7 +66,6 @@
import java.util.ArrayList;
import java.util.List;
-import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;
@@ -85,7 +84,7 @@
* The RowProcessor implementation to use when converting rows
* into Maps.
*/
- private RowProcessor convert = BasicRowProcessor.instance();
+ private RowProcessor convert = ArrayHandler.ROW_PROCESSOR;
/**
* Creates a new instance of MapListHandler using a
1.2 +4 -5 jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java
Index: ArrayListHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ArrayListHandler.java 2 Nov 2003 19:15:24 -0000 1.1
+++ ArrayListHandler.java 28 Nov 2003 19:32:10 -0000 1.2
@@ -66,7 +66,6 @@
import java.util.ArrayList;
import java.util.List;
-import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;
@@ -86,7 +85,7 @@
* The RowProcessor implementation to use when converting rows
* into Object[]s.
*/
- private RowProcessor convert = BasicRowProcessor.instance();
+ private RowProcessor convert = ArrayHandler.ROW_PROCESSOR;
/**
* Creates a new instance of ArrayListHandler using a
1.2 +11 -4 jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/ArrayHandler.java
Index: ArrayHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/ArrayHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ArrayHandler.java 2 Nov 2003 19:15:24 -0000 1.1
+++ ArrayHandler.java 28 Nov 2003 19:32:10 -0000 1.2
@@ -81,10 +81,17 @@
public class ArrayHandler implements ResultSetHandler {
/**
+ * Singleton processor instance that handlers share to save memory. Notice
+ * the default scoping to allow only classes in this package to use this
+ * instance.
+ */
+ static final RowProcessor ROW_PROCESSOR = new BasicRowProcessor();
+
+ /**
* The RowProcessor implementation to use when converting rows
* into arrays.
*/
- private RowProcessor convert = BasicRowProcessor.instance();
+ private RowProcessor convert = ROW_PROCESSOR;
/**
* Creates a new instance of ArrayHandler using a
1.6 +38 -8 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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- BasicRowProcessor.java 11 Nov 2003 00:53:19 -0000 1.5
+++ BasicRowProcessor.java 28 Nov 2003 19:32:10 -0000 1.6
@@ -122,17 +122,47 @@
* Returns the Singleton instance of this class.
*
* @return The single instance of this class.
+ * @deprecated Create instances with the constructors instead. This will
+ * be removed after DbUtils 1.1.
*/
public static BasicRowProcessor instance() {
return instance;
}
+
+ /**
+ * A basic ColumnProcessor implementation to use when client doesn't pass
+ * one into the constructor.
+ */
+ private static final ColumnProcessor defaultProcessor =
+ new ColumnProcessor() {
+ public Object process(ResultSet rs, int index, Class propType)
+ throws SQLException {
+ return rs.getObject(index);
+ }
+ };
+
+ /**
+ * Use this processor to convert columns to bean properties.
+ */
+ private ColumnProcessor convert = defaultProcessor;
/**
- * Protected constructor for BasicRowProcessor subclasses only.
+ * BasicRowProcessor constructor.
*/
- protected BasicRowProcessor() {
+ public BasicRowProcessor() {
super();
}
+
+ /**
+ * BasicRowProcessor constructor.
+ * @param convert The ColumnProcessor to use when converting columns to
+ * bean properties.
+ * @since DbUtils 1.1
+ */
+ public BasicRowProcessor(ColumnProcessor convert) {
+ super();
+ this.convert = convert;
+ }
/**
* Convert a <code>ResultSet</code> row into an <code>Object[]</code>.
@@ -270,11 +300,11 @@
continue;
}
- Object value = rs.getObject(i);
-
PropertyDescriptor prop = props[columnToProperty[i]];
Class propType = prop.getPropertyType();
+ Object value = this.convert.process(rs, i, propType);
+
if (propType != null && value == null && propType.isPrimitive()) {
value = primitiveDefaults.get(propType);
}
@@ -517,7 +547,7 @@
}
/**
- * @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());
1.2 +4 -4 jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/ResultSetIterator.java
Index: ResultSetIterator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/ResultSetIterator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ResultSetIterator.java 2 Nov 2003 19:15:23 -0000 1.1
+++ ResultSetIterator.java 28 Nov 2003 19:32:10 -0000 1.2
@@ -90,7 +90,7 @@
/**
* The processor to use when converting a row into an Object[].
*/
- private RowProcessor convert = BasicRowProcessor.instance();
+ private RowProcessor convert = new BasicRowProcessor();
/**
* Constructor for ResultSetIterator.
1.1 jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/ColumnProcessor.java
Index: ColumnProcessor.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/ColumnProcessor.java,v 1.1 2003/11/28 19:32:10 dgraham Exp $
* $Revision: 1.1 $
* $Date: 2003/11/28 19:32:10 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.dbutils;
import java.sql.ResultSet;
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>.
*
* @author Corby Page
* @author David Graham
*
* @see BasicRowProcessor
*
* @since DbUtils 1.1
*/
public interface ColumnProcessor {
/**
* Convert a <code>ResultSet</code> column into an object. Simple
* implementations could just call <code>rs.getObject(index)</code> while
* more complex implementations could perform type manipulation to match
* the column's type to the bean property type.
*
* @param rs The <code>ResultSet</code> currently being processed. It is
* positioned on a valid row before being passed into this method.
*
* @param index The current column index being processed.
*
* @param propertyType The bean property type that this column needs to be
* converted into.
*
* @return The object from the <code>ResultSet</code> at the given column
* index after optional type processing or <code>null</code> if the column
* value was SQL NULL.
*
* @throws SQLException
*/
public Object process(ResultSet rs, int index, Class propertyType)
throws SQLException;
}
1.4 +4 -4 jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/BasicRowProcessorTest.java
Index: BasicRowProcessorTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/BasicRowProcessorTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- BasicRowProcessorTest.java 9 Nov 2003 04:50:46 -0000 1.3
+++ BasicRowProcessorTest.java 28 Nov 2003 19:32:10 -0000 1.4
@@ -72,7 +72,7 @@
*/
public class BasicRowProcessorTest extends BaseTestCase {
- private static final RowProcessor processor = BasicRowProcessor.instance();
+ private static final RowProcessor processor = new BasicRowProcessor();
/**
* Constructor for BasicRowProcessorTest.
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org