001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of JPersist.
006: *
007: * JPersist is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * JPersist is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with JPersist; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jpersist.utils;
020:
021: import java.sql.ResultSet;
022: import java.sql.ResultSetMetaData;
023: import java.sql.SQLException;
024: import java.util.Collection;
025: import java.util.Map;
026: import jpersist.*;
027:
028: /**
029: * This class provides several static utility methods for retrieving data from a result set.
030: */
031:
032: @SuppressWarnings("unchecked")
033: // working to complete a Java 1.5 version
034: public class ResultSetUtils {
035: /**
036: * Returns an array of strings representing the column names in the result set.
037: *
038: * @param resultSet the result set
039: *
040: * @return an array of strings
041: */
042:
043: public static String[] getColumnNames(ResultSet resultSet)
044: throws JPersistException, SQLException {
045: String names[] = null;
046:
047: if (resultSet != null) {
048: names = new String[resultSet.getMetaData().getColumnCount()];
049:
050: for (int i = 0; i < names.length; i++)
051: names[i] = resultSet.getMetaData().getColumnName(i + 1);
052: }
053:
054: return names;
055: }
056:
057: /**
058: * Returns a map of column name and value pairs from the current row of the result set.
059: *
060: * @param resultSet a valid result set
061: * @param map the map to load with column name and value pairs
062: *
063: * @return the map that was passed in
064: */
065:
066: public static Map getRowDataMap(ResultSet resultSet, Map map)
067: throws JPersistException, SQLException {
068: if (resultSet != null)
069: for (int i = 0; i < resultSet.getMetaData()
070: .getColumnCount(); i++)
071: map.put(resultSet.getMetaData().getColumnName(i + 1),
072: resultSet.getObject(i + 1));
073:
074: return map;
075: }
076:
077: /**
078: * Returns a collection of the current rows data.
079: *
080: * @param resultSet a valid result set
081: * @param c the collection to load with the current rows data
082: *
083: * @return the collection that was passed in
084: */
085:
086: public static Collection getRowDataCollection(ResultSet resultSet,
087: Collection c) throws JPersistException, SQLException {
088: if (resultSet != null)
089: for (int i = 0; i < resultSet.getMetaData()
090: .getColumnCount(); i++)
091: c.add(resultSet.getObject(i + 1));
092:
093: return c;
094: }
095:
096: /**
097: * Returns an array of objects obtained from the current rows data.
098: *
099: * @param resultSet a valid result set
100: *
101: * @return an array of objects
102: */
103:
104: public static Object[] getRowDataObjects(ResultSet resultSet)
105: throws JPersistException, SQLException {
106: Object objects[] = null;
107:
108: if (resultSet != null) {
109: objects = new Object[resultSet.getMetaData()
110: .getColumnCount()];
111:
112: for (int i = 0; i < objects.length; i++)
113: objects[i] = resultSet.getObject(i + 1);
114: }
115:
116: return objects;
117: }
118:
119: /**
120: * Loads a map with key and value pairs from the first and second column of each row in the result set.
121: *
122: * @param resultSet a valid result set
123: * @param map the map to load with key and value pairs
124: *
125: * @return the map that was passed in
126: */
127:
128: public static Map loadMap(ResultSet resultSet, Map map)
129: throws JPersistException, SQLException {
130: return loadMap(resultSet, map, 1, 2);
131: }
132:
133: /**
134: * Loads a map with key and value pairs from keyColumn and valueColumn of each row in the result set.
135: *
136: * @param resultSet a valid result set
137: * @param map the map to load with keyColumn and valueColumn
138: * @param keyColumn the key column
139: * @param valueColumn the value column
140: *
141: * @return the map that was passed in
142: */
143:
144: public static Map loadMap(ResultSet resultSet, Map map,
145: int keyColumn, int valueColumn) throws JPersistException,
146: SQLException {
147: if (resultSet != null)
148: while (resultSet.next())
149: map.put(resultSet.getObject(keyColumn), resultSet
150: .getObject(valueColumn));
151:
152: return map;
153: }
154:
155: /**
156: * Loads a collection with row data from the result set. If the rows have more than one value,
157: * then a collection of collections (of the same type) is created, otherwise a collection of an
158: * object is created.
159: *
160: * @param resultSet a valid result set
161: * @param c the collection to load with the result set data
162: *
163: * @return the collection that was passed in
164: */
165:
166: public static Collection loadCollection(ResultSet resultSet,
167: Collection c) throws JPersistException, SQLException,
168: InstantiationException, IllegalAccessException {
169: return loadCollection(resultSet, c, true);
170: }
171:
172: /**
173: * Loads a collection with row data from the result set. If the rows have more than one value,
174: * then a collection of collections (of the same type) is created, otherwise depending on the
175: * value of singleObject, a collection of a given object is created.
176: *
177: * @param resultSet a valid result set
178: * @param c the collection to load with the result set data
179: * @param singleObject if true and a row has a single column then an object will be added to the collection.
180: * If false then a collection of the row data is added to the collection passed in.
181: *
182: * @return the collection that was passed in
183: */
184:
185: public static Collection loadCollection(ResultSet resultSet,
186: Collection c, boolean singleObject)
187: throws JPersistException, SQLException,
188: InstantiationException, IllegalAccessException {
189: if (resultSet != null) {
190: ResultSetMetaData metaData = resultSet.getMetaData();
191: int columnCount = metaData.getColumnCount();
192:
193: while (resultSet.next()) {
194: if (columnCount == 1 && singleObject)
195: c.add(resultSet.getObject(1));
196: else {
197: Collection r = (Collection) c.getClass()
198: .newInstance();
199:
200: for (int i = 0; i < columnCount; i++)
201: r.add(resultSet.getObject(i + 1));
202:
203: c.add(r);
204: }
205: }
206: }
207:
208: return c;
209: }
210: }
|