Issue Details (XML | Word | Printable)

Key: DBUTILS-16
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: Mikhail Krivoshein
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Commons DbUtils

[dbutils] ResultSetRowProcessor abstract handler and some classes rework

Created: 04/Jun/04 08:28 PM   Updated: 02/Jan/08 07:29 AM
Component/s: None
Affects Version/s: None
Fix Version/s: 1.1

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works DBUTILS-16.patch 2006-11-02 07:43 AM Henri Yandell 10 kB
Text File GenericListHandler.patch 2004-11-08 06:06 AM Stefan Fleiter 17 kB
Text File GenericListHandler_2.patch 2004-11-09 05:39 AM Stefan Fleiter 17 kB
Environment:
Operating System: All
Platform: All

Bugzilla Id: 29392
Resolution Date: 03/Nov/06 08:20 PM


 Description  « Hide
Index: ArrayListHandler.java
===================================================================
RCS file: /home/cvspublic/jakarta-
commons/dbutils/src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.ja
va,v
retrieving revision 1.4
diff -u -r1.4 ArrayListHandler.java
— ArrayListHandler.java 28 Feb 2004 00:12:22 -0000 1.4
+++ ArrayListHandler.java 4 Jun 2004 13:24:00 -0000
@@ -1,82 +1,68 @@
/*
  • Copyright 2003-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
  • *
    + *
    + * 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.
    + * 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.ArrayList;
-import java.util.List;

-import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;

/**

  • * <code>ResultSetHandler</code> implementation that converts the
  • * <code>ResultSet</code> into a <code>List</code> of <code>Object[]</code>s.
  • * This class is thread safe.
    + * <code>ResultSetHandler</code> implementation that converts the
    <code>ResultSet</code>
    + * into a <code>List</code> of <code>Object[]</code>s. This class is
    + * thread safe.
  • @see ResultSetHandler
    */
    -public class ArrayListHandler implements ResultSetHandler {
    -
  • /**
  • * The RowProcessor implementation to use when converting rows
  • * into Object[]s.
  • */
  • private RowProcessor convert = ArrayHandler.ROW_PROCESSOR;
    -
  • /**
  • * Creates a new instance of ArrayListHandler using a
  • * <code>BasicRowProcessor</code> for conversions.
  • */
  • public ArrayListHandler() { - super(); - }
    -
    - /**
    - * Creates a new instance of ArrayListHandler.
    - *
    - * @param convert The <code>RowProcessor</code> implementation
    - * to use when converting rows into Object[]s.
    - */
    - public ArrayListHandler(RowProcessor convert) { - super(); - this.convert = convert; - }
    -
    - /**
    - * Convert each row's columns into an <code>Object[]</code> and store them
    - * in a <code>List</code> in the same order they are returned from the
    - * <code>ResultSet.next()</code> method.
    - *
    - * @return A <code>List</code> of <code>Object[]</code>s, never
    - * <code>null</code>.
    - *
    - * @throws SQLException
    - * @see org.apache.commons.dbutils.ResultSetHandler#handle
    (java.sql.ResultSet)
    - */
    - public Object handle(ResultSet rs) throws SQLException {
    -
    - List result = new ArrayList();
    -
    - while (rs.next()) { - result.add(this.convert.toArray(rs)); - }
    -
    - return result;
    - }
    +public class ArrayListHandler extends ResultSetRowProcessor {

    + /**
    + * The RowProcessor implementation to use when converting rows into
    + * Object[]s.
    + */
    + private RowProcessor convert = ArrayHandler.ROW_PROCESSOR;
    +
    + /**
    + * Creates a new instance of ArrayListHandler using a
    <code>BasicRowProcessor</code>
    + * for conversions.
    + */
    + public ArrayListHandler() { + super(); + }
    +
    + /**
    + * Creates a new instance of ArrayListHandler.
    + *
    + * @param convert The <code>RowProcessor</code> implementation to use
    when
    + * converting rows into Object[]s.
    + */
    + public ArrayListHandler(RowProcessor convert) { + super(); + this.convert = convert; + }
    +
    + /**
    + * Convert row's columns into an <code>Object[]</code>.
    + *
    + * @return <code>Object[]</code>, never <code>null</code>.
    + *
    + * @throws SQLException
    + * @see org.apache.commons.dbutils.handlers.ResultSetRowProcessor#handle
    (ResultSet)
    + */
    + protected Object handleRow(ResultSet rs) throws SQLException { + return this.convert.toArray(rs); + }
    }
    Index: ColumnListHandler.java
    ===================================================================
    RCS file: /home/cvspublic/jakarta-
    commons/dbutils/src/java/org/apache/commons/dbutils/handlers/ColumnListHandler.j
    ava,v
    retrieving revision 1.1
    diff -u -r1.1 ColumnListHandler.java
    — ColumnListHandler.java 9 Mar 2004 03:05:51 -0000 1.1
    +++ ColumnListHandler.java 4 Jun 2004 13:24:01 -0000
    @@ -1,100 +1,85 @@
    /*
    * 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
    - *
    + *
    + * 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.
    + * 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.ArrayList;
    -import java.util.List;
    -
    -import org.apache.commons.dbutils.ResultSetHandler;

    /**
    - * <code>ResultSetHandler</code> implementation that converts one
    - * <code>ResultSet</code> column into a <code>List</code> of
    - * <code>Object</code>s. This class is thread safe.
    + * <code>ResultSetHandler</code> implementation that converts one
    <code>ResultSet</code>
    + * column into a <code>List</code> of <code>Object</code>s. This class is
    + * thread safe.
    *
    * @see ResultSetHandler
    * @since DbUtils 1.1
    */
    -public class ColumnListHandler implements ResultSetHandler {
    +public class ColumnListHandler extends ResultSetRowProcessor {

    - /**
    - * The column number to retrieve.
    - */
    - private int columnIndex = 1;
    -
    - /**
    - * The column name to retrieve. Either columnName or columnIndex
    - * will be used but never both.
    - */
    - private String columnName = null;
    -
    - /**
    - * Creates a new instance of ColumnListHandler. The first column of each
    - * row will be returned from <code>handle()</code>.
    - */
    - public ColumnListHandler() {- super();- } }
    -
  • /**
  • * Creates a new instance of ColumnListHandler.
  • *
  • * @param columnIndex The index of the column to retrieve from the
  • * <code>ResultSet</code>.
  • */
  • public ColumnListHandler(int columnIndex) { - this.columnIndex = columnIndex; - }
    -
  • /**
  • * Creates a new instance of ColumnListHandler.
  • *
  • * @param columnName The name of the column to retrieve from the
  • * <code>ResultSet</code>.
  • */
  • public ColumnListHandler(String columnName) { - this.columnName = columnName; - }
    -
  • /**
  • * Returns one <code>ResultSet</code> column as a <code>List</code> of
  • * <code>Object</code>s. The elements are added to the <code>List</code>
    via
  • * the <code>ResultSet.getObject()</code> method.
  • *
  • * @return A <code>List</code> of <code>Object</code>s, never
  • * <code>null</code>.
  • *
  • * @throws SQLException
  • *
  • * @see org.apache.commons.dbutils.ResultSetHandler#handle
    (java.sql.ResultSet)
  • */
  • public Object handle(ResultSet rs) throws SQLException {
    -
  • List result = new ArrayList();
    -
  • while (rs.next()) {
  • if (this.columnName == null) { - result.add(rs.getObject(this.columnIndex)); - } else { - result.add(rs.getObject(this.columnName)); - }
  • }
  • return result;
  • }
    + /**
    + * The column number to retrieve.
    + */
    + private int columnIndex = 1;
    +
    + /**
    + * The column name to retrieve. Either columnName or columnIndex will
    be used
    + * but never both.
    + */
    + private String columnName = null;
    +
    + /**
    + * Creates a new instance of ColumnListHandler. The first column of
    each row
    + * will be returned from <code>handle()</code>.
    + */
    + public ColumnListHandler() { + super(); + }
    +
    + /**
    + * Creates a new instance of ColumnListHandler.
    + *
    + * @param columnIndex The index of the column to retrieve from the
    <code>ResultSet</code>.
    + */
    + public ColumnListHandler(int columnIndex) { + this.columnIndex = columnIndex; + }
    +
    + /**
    + * Creates a new instance of ColumnListHandler.
    + *
    + * @param columnName The name of the column to retrieve from the
    <code>ResultSet</code>.
    + */
    + public ColumnListHandler(String columnName) { + this.columnName = columnName; + }
    +
    + /**
    + * Returns one <code>ResultSet</code> column value as
    <code>Object</code>.
    + *
    + * @return <code>Object</code>, never <code>null</code>.
    + *
    + * @throws SQLException
    + *
    + * @see org.apache.commons.dbutils.handlers.ResultSetRowProcessor#handle
    (ResultSet)
    + */
    + protected Object handleRow(ResultSet rs) throws SQLException
    Unknown macro: {+ if (this.columnName == null) { + return rs.getObject(this.columnIndex); + } else { + return rs.getObject(this.columnName); + }+ }

    }
    Index: MapListHandler.java
    ===================================================================
    RCS file: /home/cvspublic/jakarta-
    commons/dbutils/src/java/org/apache/commons/dbutils/handlers/MapListHandler.java
    ,v
    retrieving revision 1.4
    diff -u -r1.4 MapListHandler.java
      • MapListHandler.java 28 Feb 2004 00:12:22 -0000 1.4
        +++ MapListHandler.java 4 Jun 2004 13:24:01 -0000
        @@ -17,10 +17,7 @@

import java.sql.ResultSet;
import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.List;

-import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;

/**
@@ -30,7 +27,7 @@

  • @see ResultSetHandler
    */
    -public class MapListHandler implements ResultSetHandler {
    +public class MapListHandler extends ResultSetRowProcessor { /** * The RowProcessor implementation to use when converting rows @@ -58,24 +55,16 @@ }

/**

  • * Converts the <code>ResultSet</code> rows into a <code>List</code> of
  • * <code>Map</code> objects.
    + * Converts the <code>ResultSet</code> row into a <code>Map</code> object.
  • * @return A <code>List</code> of <code>Map</code>s, never null.
    + * @return A <code>Map</code>, never null.
  • @throws SQLException
  • * @see org.apache.commons.dbutils.ResultSetHandler#handle
    (java.sql.ResultSet)
    + * @see org.apache.commons.dbutils.handlers.ResultSetRowProcessor#handle
    (ResultSet)
    */
  • public Object handle(ResultSet rs) throws SQLException {
    -
  • List results = new ArrayList();
    -
  • while (rs.next()) { - results.add(this.convert.toMap(rs)); - }
    -
  • return results;
  • }
    + protected Object handleRow(ResultSet rs) throws SQLException { + return this.convert.toMap(rs); + }

}
Index: src/java/org/apache/commons/dbutils/handlers/ResultSetRowProcessor.java
===================================================================
RCS file:
src/java/org/apache/commons/dbutils/handlers/ResultSetRowProcessor.java
diff -N src/java/org/apache/commons/dbutils/handlers/ResultSetRowProcessor.java
— /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/java/org/apache/commons/dbutils/handlers/ResultSetRowProcessor.java
1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2003-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.ArrayList;
+import java.util.List;
+
+import org.apache.commons.dbutils.ResultSetHandler;
+
+/**
+ * Abstract class that simplify development of <code>ResultSetHandler</code>
+ * classes that convert <code>ResultSet</code> into <code>List</code>.
+ */
+public abstract class ResultSetRowProcessor implements ResultSetHandler {
+ /**
+ * Whole <code>ResultSet</code> handler. It produce <code>List</code> as
+ * result. To convert individual rows into Java objects it uses
<code>handleRow(ResultSet)</code>
+ * method.
+ *
+ * @see #handleRow(ResultSet)
+ */
+ public Object handle(ResultSet rs) throws SQLException {
+ List rows = new ArrayList();
+ while (rs.next()) { + rows.add(this.handleRow(rs)); + }
+ return rows;
+ }
+
+ /**
+ * Row handler. Method converts current row into some Java object.
+ *
+ * @param rs <code>ResultSet</code> to process.
+ * @return row processing result
+ * @throws SQLException error occurs
+ */
+ protected abstract Object handleRow(ResultSet rs) throws SQLException;
+}



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
David Graham added a comment - 06/Jun/04 09:53 PM
I appreciate the patch but I won't apply anything that touches every line in a
file including the license. I will apply a patch that just touches the lines
necessary to make the change.

Stefan Fleiter added a comment - 08/Nov/04 06:06 AM
Created an attachment (id=13351)
Generic Implementaion of List creation and ResultSet traversal for *ListHandler.java

Stefan Fleiter added a comment - 08/Nov/04 06:12 AM
Same as above, but
  • Other name
  • implementation for BeanListHandler included
  • BeanProcess.toBeanList and RowProcessor.toBeanList removed
  • only those lines touched that haven been necessary for the change

Is this ok as it is or are further chnages needed?
Would (in another bug) a patch be accepted which would introduce a
dependency to commons-logging (see TODOs in ResultSetIterator.java)?


Stefan Fleiter added a comment - 08/Nov/04 04:41 PM
I think I need to rivise the patch more carefully.
The GenericListHandler class, f.e. should IMHO not have public visibility, it's
an implementation detail.

I will look at the patch once more and come back to you.


David Graham added a comment - 08/Nov/04 10:34 PM
DbUtils is intentionally a one jar distro and commons logging isn't really
needed. Those TODOs need to be removed. We should either swallow the exception
or throw it as some kind of RuntimeException.

Stefan Fleiter added a comment - 09/Nov/04 05:28 AM
I do not think that swallowing the exception would be a good decission, the user
(of DbUtils) should know something went wrong and we should not hide application
bugs and make debugging more difficult.

I just opened COM-1707 for this. Let's move the discussion there.


Stefan Fleiter added a comment - 09/Nov/04 05:39 AM
Created an attachment (id=13359)
New Patch, this time GenericListHandler has package visibility

Stefan Fleiter added a comment - 09/Nov/04 05:50 AM
I've just attached a new patch with the following diffrences to my last one:
  • GenericListHandler has package visibility
  • I think this class is an implementation detail. Maybe some day one would
    like to change the *ListHandler-classes to use composition to get the
    generic handle()-Method because one would like the classes to drive from
    some other class. If GenericListHandler has public visibility one has to
    support this class forever
  • Fixed a typo.

The unittests all passed.
What do you think?


Henri Yandell made changes - 16/May/06 09:41 AM
Field Original Value New Value
issue.field.bugzillaimportkey 29392 12341485
Henri Yandell made changes - 16/May/06 11:29 AM
Component/s DbUtils [ 12311110 ]
Assignee Jakarta Commons Developers Mailing List [ commons-dev@jakarta.apache.org ]
Key COM-1333 DBUTILS-16
Affects Version/s unspecified [ 12311647 ]
Project Commons [ 12310458 ] Commons DbUtils [ 12310470 ]
Henri Yandell added a comment - 19/Jul/06 06:26 AM
FIX or WONTFIX for 1.1.

Henri Yandell made changes - 19/Jul/06 06:26 AM
Fix Version/s 1.1 [ 12311973 ]
Bugzilla Id 29392
Henri Yandell added a comment - 02/Nov/06 07:34 AM
Negative side of this is that it changes the public 1.0 API by removing toBeanList in a few places. So not usable for 1.1 in its entirety.

Henri Yandell added a comment - 02/Nov/06 07:43 AM
Version of the patch that does not remove the toBeanList methods.

I'm unsure if they should be deprecated - it feels that there is no reason to do so.


Henri Yandell made changes - 02/Nov/06 07:43 AM
Attachment DBUTILS-16.patch [ 12344167 ]
Repository Revision Date User Message
ASF #470975 Fri Nov 03 20:20:06 UTC 2006 bayard Applying the code refactoring in #DBUTILS-16 - the new abstract class is currently not public. In a later release we can decide whether to make it public or not (this is recorded in #DBUTILS-33
Files Changed
MODIFY /jakarta/commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java
MODIFY /jakarta/commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java
MODIFY /jakarta/commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/MapListHandler.java
MODIFY /jakarta/commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/ColumnListHandler.java

Henri Yandell added a comment - 03/Nov/06 08:20 PM
svn ci -m "Applying the code refactoring in #DBUTILS-16 - the new abstract class is currently not public. In a later release we can decide whe
ther to make it public or not (this is recorded in #DBUTILS-33"
Sending src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java
Sending src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java
Sending src/java/org/apache/commons/dbutils/handlers/ColumnListHandler.java
Sending src/java/org/apache/commons/dbutils/handlers/MapListHandler.java
Transmitting file data ....
Committed revision 470975.

Henri Yandell made changes - 03/Nov/06 08:20 PM
Resolution Fixed [ 1 ]
Status Open [ 1 ] Resolved [ 5 ]
Repository Revision Date User Message
ASF #470976 Fri Nov 03 20:21:07 UTC 2006 bayard Missed the new class from #DBUTILS-16.
Files Changed
ADD /jakarta/commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/handlers/GenericListHandler.java

Henri Yandell added a comment - 03/Nov/06 08:21 PM
svn ci -m "Missed the new class from #DBUTILS-16. "
Adding src/java/org/apache/commons/dbutils/handlers/GenericListHandler.java
Transmitting file data .
Committed revision 470976.

Henri Yandell made changes - 02/Jan/08 07:29 AM
Status Resolved [ 5 ] Closed [ 6 ]