DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://issues.apache.org/bugzilla/show_bug.cgi?id=29392>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=29392 ResultSetRowProcessor abstract handler and some classes rework Summary: ResultSetRowProcessor abstract handler and some classes rework Product: Commons Version: unspecified Platform: All OS/Version: All Status: NEW Severity: Enhancement Priority: Other Component: DbUtils AssignedTo: [EMAIL PROTECTED] ReportedBy: [EMAIL PROTECTED] 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 { + 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; +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
