commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 32449] New: - [dbutils] Create handler to return a Map of Maps
Date Tue, 30 Nov 2004 15:55:27 GMT
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=32449>.
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=32449

           Summary: [dbutils] Create handler to return a Map of Maps
           Product: Commons
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DbUtils
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: norrisshelton@yahoo.com


Turns a result set into a map of maps.  I needed it to turn a
result set into a map of state abbreviations keys with state
information maps as values.

It has 2 constructors.  The empty constructor assumes that there
is going to be a column named "key" that will serve as the map
key.  There is another constructor that takes a String that is
used to identify the key column.

Here is the code:
package com.appriss.justicexchange.util.dbutils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.MapListHandler;

/**
 * <code>ResultSetHandler</code> implementation that converts a
 * <code>ResultSet</code> into a <code>Map</code> of <code>Map</code>s.
 * This class is thread safe.
 *
 * @see org.apache.commons.dbutils.ResultSetHandler
 *
 * @author David Graham
 * @author Norris Shelton
 */
public class MapMapHandler extends MapListHandler implements ResultSetHandler {

    private String key = "key";

    /**
     * The RowProcessor implementation to use when converting rows
     * into Maps.
     */
    private RowProcessor convert = BasicRowProcessor.instance();

    /**
     * Creates a new instance of MapMapHandler using a
     * <code>BasicRowProcessor</code> for conversion.  Assumes that the key for
     * the map will be "key".
     */
    public MapMapHandler() {
        super();
    }

    /**
     * Creates a new instance of MapMapHandler using a
     * <code>BasicRowProcessor</code> for conversion.  The provided key String
     * will be the key for each map.
     */
    public MapMapHandler(String key) {
        super();
        this.key = key;
    }

    /**
     * Creates a new instance of MapMapHandler.
     *
     * @param convert The <code>RowProcessor</code> implementation
     * to use when converting rows into Maps.  Assumes that the key for
     * the map will be "key".
     */
    public MapMapHandler(RowProcessor convert) {
        super();
        this.convert = convert;
    }

    /**
     * Converts the <code>ResultSet</code> rows into a <code>Map</code>
of
     * <code>Map</code> objects.
     *
     * @return A <code>Map</code> of <code>Map</code>s, never null.
     *
     * @throws java.sql.SQLException
     *
     * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
     */
    public Object handle(ResultSet rs) throws SQLException {

        Map results = new HashMap();

        Map temp = null;
        while (rs.next()) {
            temp = convert.toMap(rs);
            results.put(temp.get(key), temp);
        }

        return results;
    }
}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

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