Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 69988 invoked from network); 2 Mar 2004 14:35:03 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 2 Mar 2004 14:35:03 -0000 Received: (qmail 69496 invoked by uid 500); 2 Mar 2004 14:34:41 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 69441 invoked by uid 500); 2 Mar 2004 14:34:40 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 69378 invoked from network); 2 Mar 2004 14:34:40 -0000 Received: from unknown (HELO web60805.mail.yahoo.com) (216.155.196.68) by daedalus.apache.org with SMTP; 2 Mar 2004 14:34:40 -0000 Message-ID: <20040302143441.39143.qmail@web60805.mail.yahoo.com> Received: from [24.9.52.44] by web60805.mail.yahoo.com via HTTP; Tue, 02 Mar 2004 06:34:41 PST Date: Tue, 2 Mar 2004 06:34:41 -0800 (PST) From: David Graham Subject: Re: [DbUtils] ScalarListHandler To: Jakarta Commons Developers List In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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éter 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; > > /** > * ResultSetHandler implementation that converts one > * ResultSet column into a List of > * Objects. 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 handle(). > */ > public ScalarListHandler() { > super(); > } > > /** > * Creates a new instance of ScalarListHandler. > * > * @param columnIndex The index of the column to retrieve from the > * ResultSet. > */ > public ScalarListHandler(int columnIndex) { > this.columnIndex = columnIndex; > } > > /** > * Creates a new instance of ScalarListHandler. > * > * @param columnName The name of the column to retrieve from the > * ResultSet. > */ > public ScalarListHandler(String columnName) { > this.columnName = columnName; > } > > /** > * Returns one ResultSet column as a List > of > * Objects. The elements are added to the > List via > * the ResultSet.getObject() method. > * > * @return A List of Objects, never > * null. > * > * @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