commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Graham <grahamdavid1...@yahoo.com>
Subject Re: [DbUtils] ScalarListHandler
Date Tue, 02 Mar 2004 14:34:41 GMT
Please create a bugzilla enhancement ticket and attach these files to it
so they don't get lost.  While I think the handler is useful, its name is
non-sensical :-).  It fits with the other naming conventions for handlers
but you can't really have a scalar and a list at the same time.

David

--- Bagyinszki P&#233;ter  <petike1@dpg.hu> wrote:
> Hi, I'd like to suggest a new ResultHandler implementation that converts
> one ResultSet column into a List of Objects.
> 
> (Attached)
> 
> With that it's easy to load a whole column into a List.
> 
> -- 
> petike
> http://petike1.uw.hu> Index: BaseTestCase.java
> ===================================================================
> RCS file:
>
/home/cvspublic/jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/BaseTestCase.java,v
> retrieving revision 1.6
> diff -u -r1.6 BaseTestCase.java
> --- BaseTestCase.java	28 Feb 2004 00:12:22 -0000	1.6
> +++ BaseTestCase.java	2 Mar 2004 10:11:49 -0000
> @@ -31,6 +31,7 @@
>  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.handlers.ScalarListHandlerTest;
>  import org.apache.commons.dbutils.wrappers.SqlNullCheckedResultSetTest;
>  import org.apache.commons.dbutils.wrappers.StringTrimmedResultSetTest;
>  
> @@ -150,6 +151,7 @@
>          suite.addTestSuite(MapHandlerTest.class);
>          suite.addTestSuite(MapListHandlerTest.class);
>          suite.addTestSuite(ScalarHandlerTest.class);
> +        suite.addTestSuite(ScalarListHandlerTest.class);
>  
>          suite.addTestSuite(StringTrimmedResultSetTest.class);
>          suite.addTestSuite(SqlNullCheckedResultSetTest.class);> /*
>  * 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;
> 
> /**
>  * <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 ScalarListHandler implements ResultSetHandler {
> 
>     /**
>      * 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 ScalarListHandler.  The first column
> will
>      * be returned from <code>handle()</code>.
>      */
>     public ScalarListHandler() {
>         super();
>     }
> 
>     /** 
>      * Creates a new instance of ScalarListHandler.
>      * 
>      * @param columnIndex The index of the column to retrieve from the 
>      * <code>ResultSet</code>.
>      */
>     public ScalarListHandler(int columnIndex) {
>         this.columnIndex = columnIndex;
>     }
> 
>     /** 
>      * Creates a new instance of ScalarListHandler.
>      * 
>      * @param columnName The name of the column to retrieve from the 
>      * <code>ResultSet</code>.
>      */
>     public ScalarListHandler(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;
>     }
> }> /*
>  * 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.SQLException;
> import java.util.List;
> 
> import org.apache.commons.dbutils.BaseTestCase;
> import org.apache.commons.dbutils.ResultSetHandler;
> 
> /**
>  * ScalarListHandlerTest
>  */
> public class ScalarListHandlerTest extends BaseTestCase {
> 
> 	/**
> 	 * Constructor for ScalarListHandlerTest.
> 	 */
> 	public ScalarListHandlerTest(String name) {
> 		super(name);
> 	}
> 
> 	public void testHandle() throws SQLException {
>         ResultSetHandler h = new ScalarListHandler();
>         
>         List results = (List) h.handle(this.rs);
> 
>         assertNotNull(results);
>         assertEquals(ROWS, results.size());
> 
>         assertEquals("1", results.get(0));
>         assertEquals("4", results.get(1));
> 	}
> 
> 	public void testColumnIndexHandle() throws SQLException {
> 		ResultSetHandler h = new ScalarListHandler(2);
>         List results = (List) h.handle(this.rs);
> 
>         assertNotNull(results);
>         assertEquals(ROWS, results.size());
> 
>         assertEquals("2", results.get(0));
>         assertEquals("5", results.get(1));
> 	}
> 
> 	public void testColumnNameHandle() throws SQLException {
>         ResultSetHandler h = new ScalarListHandler("Three");
>         List results = (List) h.handle(this.rs);
> 
>         assertNotNull(results);
>         assertEquals(ROWS, results.size());
> 
>         assertEquals("3", results.get(0));
>         assertEquals("6", results.get(1));		
> 	}
> 
> 	public void testEmptyResultSetHandle() throws SQLException {
> 		ResultSetHandler h = new ScalarListHandler();
> 		List results = (List) h.handle(this.emptyResultSet);
> 
> 		assertNotNull(results);
>         assertTrue(results.isEmpty());
> 	}
> 
> }
> > ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org


__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message