Commons DbUtils
  1. Commons DbUtils
  2. DBUTILS-16

[dbutils] ResultSetRowProcessor abstract handler and some classes rework

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.1
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

      Description

      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;
      +}

      1. ASF.LICENSE.NOT.GRANTED--GenericListHandler_2.patch
        17 kB
        Stefan Fleiter
      2. ASF.LICENSE.NOT.GRANTED--GenericListHandler.patch
        17 kB
        Stefan Fleiter
      3. DBUTILS-16.patch
        10 kB
        Henri Yandell

        Activity

        Hide
        David Graham added a comment -

        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.

        Show
        David Graham added a comment - 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.
        Hide
        Stefan Fleiter added a comment -

        Created an attachment (id=13351)
        Generic Implementaion of List creation and ResultSet traversal for *ListHandler.java

        Show
        Stefan Fleiter added a comment - Created an attachment (id=13351) Generic Implementaion of List creation and ResultSet traversal for *ListHandler.java
        Hide
        Stefan Fleiter added a comment -

        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)?

        Show
        Stefan Fleiter added a comment - 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)?
        Hide
        Stefan Fleiter added a comment -

        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.

        Show
        Stefan Fleiter added a comment - 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.
        Hide
        David Graham added a comment -

        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.

        Show
        David Graham added a comment - 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.
        Hide
        Stefan Fleiter added a comment -

        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.

        Show
        Stefan Fleiter added a comment - 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.
        Hide
        Stefan Fleiter added a comment -

        Created an attachment (id=13359)
        New Patch, this time GenericListHandler has package visibility

        Show
        Stefan Fleiter added a comment - Created an attachment (id=13359) New Patch, this time GenericListHandler has package visibility
        Hide
        Stefan Fleiter added a comment -

        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?

        Show
        Stefan Fleiter added a comment - 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?
        Hide
        Henri Yandell added a comment -

        FIX or WONTFIX for 1.1.

        Show
        Henri Yandell added a comment - FIX or WONTFIX for 1.1.
        Hide
        Henri Yandell added a comment -

        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.

        Show
        Henri Yandell added a comment - 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.
        Hide
        Henri Yandell added a comment -

        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.

        Show
        Henri Yandell added a comment - 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.
        Hide
        Henri Yandell added a comment -

        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.

        Show
        Henri Yandell added a comment - 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.
        Hide
        Henri Yandell added a comment -

        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.

        Show
        Henri Yandell added a comment - 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.

          People

          • Assignee:
            Unassigned
            Reporter:
            Mikhail Krivoshein
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development