001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.dbutils;
018:
019: import java.sql.ResultSet;
020: import java.sql.ResultSetMetaData;
021: import java.sql.SQLException;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: /**
028: * Basic implementation of the <code>RowProcessor</code> interface.
029: *
030: * <p>
031: * This class is thread-safe.
032: * </p>
033: *
034: * @see RowProcessor
035: */
036: public class BasicRowProcessor implements RowProcessor {
037:
038: /**
039: * The default BeanProcessor instance to use if not supplied in the
040: * constructor.
041: */
042: private static final BeanProcessor defaultConvert = new BeanProcessor();
043:
044: /**
045: * The Singleton instance of this class.
046: */
047: private static final BasicRowProcessor instance = new BasicRowProcessor();
048:
049: /**
050: * Returns the Singleton instance of this class.
051: *
052: * @return The single instance of this class.
053: * @deprecated Create instances with the constructors instead. This will
054: * be removed after DbUtils 1.1.
055: */
056: public static BasicRowProcessor instance() {
057: return instance;
058: }
059:
060: /**
061: * Use this to process beans.
062: */
063: private BeanProcessor convert = null;
064:
065: /**
066: * BasicRowProcessor constructor. Bean processing defaults to a
067: * BeanProcessor instance.
068: */
069: public BasicRowProcessor() {
070: this (defaultConvert);
071: }
072:
073: /**
074: * BasicRowProcessor constructor.
075: * @param convert The BeanProcessor to use when converting columns to
076: * bean properties.
077: * @since DbUtils 1.1
078: */
079: public BasicRowProcessor(BeanProcessor convert) {
080: super ();
081: this .convert = convert;
082: }
083:
084: /**
085: * Convert a <code>ResultSet</code> row into an <code>Object[]</code>.
086: * This implementation copies column values into the array in the same
087: * order they're returned from the <code>ResultSet</code>. Array elements
088: * will be set to <code>null</code> if the column was SQL NULL.
089: *
090: * @see org.apache.commons.dbutils.RowProcessor#toArray(java.sql.ResultSet)
091: */
092: public Object[] toArray(ResultSet rs) throws SQLException {
093: ResultSetMetaData meta = rs.getMetaData();
094: int cols = meta.getColumnCount();
095: Object[] result = new Object[cols];
096:
097: for (int i = 0; i < cols; i++) {
098: result[i] = rs.getObject(i + 1);
099: }
100:
101: return result;
102: }
103:
104: /**
105: * Convert a <code>ResultSet</code> row into a JavaBean. This
106: * implementation delegates to a BeanProcessor instance.
107: * @see org.apache.commons.dbutils.RowProcessor#toBean(java.sql.ResultSet, java.lang.Class)
108: * @see org.apache.commons.dbutils.BeanProcessor#toBean(java.sql.ResultSet, java.lang.Class)
109: */
110: public Object toBean(ResultSet rs, Class type) throws SQLException {
111: return this .convert.toBean(rs, type);
112: }
113:
114: /**
115: * Convert a <code>ResultSet</code> into a <code>List</code> of JavaBeans.
116: * This implementation delegates to a BeanProcessor instance.
117: * @see org.apache.commons.dbutils.RowProcessor#toBeanList(java.sql.ResultSet, java.lang.Class)
118: * @see org.apache.commons.dbutils.BeanProcessor#toBeanList(java.sql.ResultSet, java.lang.Class)
119: */
120: public List toBeanList(ResultSet rs, Class type)
121: throws SQLException {
122: return this .convert.toBeanList(rs, type);
123: }
124:
125: /**
126: * Convert a <code>ResultSet</code> row into a <code>Map</code>. This
127: * implementation returns a <code>Map</code> with case insensitive column
128: * names as keys. Calls to <code>map.get("COL")</code> and
129: * <code>map.get("col")</code> return the same value.
130: * @see org.apache.commons.dbutils.RowProcessor#toMap(java.sql.ResultSet)
131: */
132: public Map toMap(ResultSet rs) throws SQLException {
133: Map result = new CaseInsensitiveHashMap();
134: ResultSetMetaData rsmd = rs.getMetaData();
135: int cols = rsmd.getColumnCount();
136:
137: for (int i = 1; i <= cols; i++) {
138: result.put(rsmd.getColumnName(i), rs.getObject(i));
139: }
140:
141: return result;
142: }
143:
144: /**
145: * A Map that converts all keys to lowercase Strings for case insensitive
146: * lookups. This is needed for the toMap() implementation because
147: * databases don't consistenly handle the casing of column names.
148: */
149: private static class CaseInsensitiveHashMap extends HashMap {
150:
151: /**
152: * @see java.util.Map#containsKey(java.lang.Object)
153: */
154: public boolean containsKey(Object key) {
155: return super .containsKey(key.toString().toLowerCase());
156: }
157:
158: /**
159: * @see java.util.Map#get(java.lang.Object)
160: */
161: public Object get(Object key) {
162: return super .get(key.toString().toLowerCase());
163: }
164:
165: /**
166: * @see java.util.Map#put(java.lang.Object, java.lang.Object)
167: */
168: public Object put(Object key, Object value) {
169: return super .put(key.toString().toLowerCase(), value);
170: }
171:
172: /**
173: * @see java.util.Map#putAll(java.util.Map)
174: */
175: public void putAll(Map m) {
176: Iterator iter = m.keySet().iterator();
177: while (iter.hasNext()) {
178: Object key = iter.next();
179: Object value = m.get(key);
180: this .put(key, value);
181: }
182: }
183:
184: /**
185: * @see java.util.Map#remove(java.lang.Object)
186: */
187: public Object remove(Object key) {
188: return super.remove(key.toString().toLowerCase());
189: }
190: }
191:
192: }
|