001: package net.sourceforge.squirrel_sql.fw.sql;
002:
003: /*
004: * Copyright (C) 2002-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: * Copyright (C) 2002 Johan Compagner
007: * jcompagner@j-com.nl
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: import java.sql.Date;
024: import java.sql.ResultSet;
025: import java.sql.ResultSetMetaData;
026: import java.sql.SQLException;
027: import java.sql.Time;
028: import java.sql.Timestamp;
029: import java.sql.Types;
030:
031: public class ResultSetColumnReader {
032: /** Logger for this class. */
033: // private final static ILogger s_log =
034: // LoggerController.createLogger(ResultSetColumnReader.class);
035: private final static Long LONG_ZERO = Long.valueOf(0);
036: private final static Double DOUBLE_ZERO = Double.valueOf(0);
037:
038: /** The <TT>ResultSet</TT> being read. */
039: private final ResultSet _rs;
040:
041: /** <TT>true</TT> if the last column read had a value of SQL NULL. */
042: private boolean _wasNull;
043:
044: /** Metadata for the <TT>ResultSet</TT>. */
045: private ResultSetMetaData _rsmd;
046:
047: public ResultSetColumnReader(ResultSet rs) throws SQLException {
048: super ();
049: if (rs == null) {
050: throw new IllegalArgumentException("ResultSet == null");
051: }
052:
053: _rs = rs;
054: _rsmd = rs.getMetaData();
055: }
056:
057: /**
058: * Position <TT>ResultSet</TT> to the next row.
059: *
060: * @return <TT>true</TT> if there is a "next row" to read.
061: *
062: * @throws SQLException SQL error occured.
063: */
064: public boolean next() throws SQLException {
065: return _rs.next();
066: }
067:
068: /**
069: * Retrieve the specifed column as a <TT>Boolean</TT>.
070: *
071: * @param columnIdx Column index (starts at 1) of the column to be read.
072: *
073: * @return Boolean value of the specified column.
074: *
075: * @throws SQLException SQL error occured.
076: */
077: public Boolean getBoolean(int columnIdx) throws SQLException {
078: Object obj = _rs.getObject(columnIdx);
079: Boolean results = Boolean.FALSE;
080: _wasNull = true;
081:
082: if (obj != null) {
083: final int columnType = _rsmd.getColumnType(columnIdx);
084: if (columnType != Types.NULL) {
085: _wasNull = false;
086: switch (columnType) {
087: // TODO: When JDK1.4 is the earliest JDK supported
088: // by Squirrel then remove the hardcoding of the
089: // boolean data type.
090: case Types.BIT:
091: case Types.BOOLEAN:
092: if (obj instanceof Boolean) {
093: results = (Boolean) obj;
094: } else {
095: if (obj instanceof Number) {
096: if (((Number) obj).intValue() == 0) {
097: results = Boolean.FALSE;
098: } else {
099: results = Boolean.TRUE;
100: }
101: } else {
102: results = Boolean.valueOf(obj.toString());
103: }
104: }
105: break;
106:
107: default:
108: results = Boolean.valueOf(obj.toString());
109: break;
110: }
111: }
112: }
113:
114: return results;
115: }
116:
117: /**
118: * Retrieve the specifed column as a <TT>Date</TT>.
119: *
120: * @param columnIdx Column index (starts at 1) of the column to be read.
121: *
122: * @return Time value of the specified column.
123: *
124: * @throws SQLException SQL error occured.
125: */
126: public Date getDate(int columnIdx) throws SQLException {
127: final Date results = _rs.getDate(columnIdx);
128: _wasNull = results == null;
129: return results;
130: }
131:
132: /**
133: * Retrieve the specifed column as a <TT>Double</TT>.
134: *
135: * @param columnIdx Column index (starts at 1) of the column to be read.
136: *
137: * @return Double value of the specified column.
138: *
139: * @throws SQLException SQL error occured.
140: */
141: public Double getDouble(int columnIdx) throws SQLException {
142: Object obj = _rs.getObject(columnIdx);
143: Double results = DOUBLE_ZERO;
144: _wasNull = true;
145:
146: if (obj != null) {
147: final int columnType = _rsmd.getColumnType(columnIdx);
148: if (columnType != Types.NULL) {
149: _wasNull = false;
150: switch (columnType) {
151: case Types.DOUBLE:
152: case Types.FLOAT:
153: case Types.REAL:
154: if (obj instanceof Number) {
155: results = ((Number) obj).doubleValue();
156: } else {
157: results = new Double(obj.toString());
158: }
159: break;
160: default:
161: results = new Double(obj.toString());
162: break;
163: }
164: }
165: }
166:
167: return results;
168: }
169:
170: /**
171: * Retrieve the specifed column as a <TT>Long</TT>.
172: *
173: * @param columnIdx Column index (starts at 1) of the column to be read.
174: *
175: * @return long value of the specified column.
176: *
177: * @throws SQLException SQL error occured.
178: */
179: public Long getLong(int columnIdx) throws SQLException {
180: Object obj = _rs.getObject(columnIdx);
181: Long results = LONG_ZERO;
182: _wasNull = true;
183:
184: if (obj != null) {
185: final int columnType = _rsmd.getColumnType(columnIdx);
186: if (columnType != Types.NULL) {
187: _wasNull = false;
188: switch (columnType) {
189: case Types.SMALLINT:
190: case Types.TINYINT:
191: case Types.INTEGER:
192: case Types.BIGINT:
193: if (obj instanceof Number) {
194: results = ((Number) obj).longValue();
195: } else {
196: results = new Long(obj.toString());
197: }
198: break;
199: case Types.BIT:
200: if ("true".equalsIgnoreCase(obj.toString())) {
201: results = Long.valueOf(1);
202: } else {
203: results = Long.valueOf(0);
204: }
205: break;
206: default:
207: results = new Long(obj.toString());
208: break;
209: }
210: }
211: }
212:
213: return results;
214: }
215:
216: /**
217: * Retrieve the specifed column as an object.
218: *
219: * @param columnIdx Column index (starts at 1) of the column to be read.
220: *
221: * @return Object value of the specified column.
222: *
223: * @throws SQLException SQL error occured.
224: */
225: public Object getObject(int columnIdx) throws SQLException {
226: final Object results = _rs.getObject(columnIdx);
227: _wasNull = results == null;
228: return results;
229: }
230:
231: /**
232: * Retrieve the specifed column as a string.
233: *
234: * @param columnIdx Column index (starts at 1) of the column to be read.
235: *
236: * @return String value of the specified column.
237: *
238: * @throws SQLException SQL error occured.
239: */
240: public String getString(int columnIdx) throws SQLException {
241: final String results = _rs.getString(columnIdx);
242: _wasNull = results == null;
243: return results;
244: }
245:
246: /**
247: * Retrieve the specifed column as a <TT>Time</TT>.
248: *
249: * @param columnIdx Column index (starts at 1) of the column to be read.
250: *
251: * @return Time value of the specified column.
252: *
253: * @throws SQLException SQL error occured.
254: */
255: public Time getTime(int columnIdx) throws SQLException {
256: final Time results = _rs.getTime(columnIdx);
257: _wasNull = results == null;
258: return results;
259: }
260:
261: /**
262: * Retrieve the specifed column as a <TT>TimeStamp</TT>.
263: *
264: * @param columnIdx Column index (starts at 1) of the column to be read.
265: *
266: * @return TimeStamp value of the specified column.
267: *
268: * @throws SQLException SQL error occured.
269: */
270: public Timestamp getTimeStamp(int columnIdx) throws SQLException {
271: final Timestamp results = _rs.getTimestamp(columnIdx);
272: _wasNull = results == null;
273: return results;
274: }
275:
276: /**
277: * Reports whether the last column read had a value of SQL NULL.
278: *
279: * @return <TT>true</TT> if last column read was <TT>null</TT>.
280: */
281: public boolean wasNull() {
282: return _wasNull;
283: }
284: }
|