001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2007 Nigel Westbury <westbury@users.sourceforge.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.oda.driver;
024:
025: import java.math.BigDecimal;
026: import java.sql.Date;
027: import java.sql.Time;
028: import java.sql.Timestamp;
029:
030: import net.sf.jmoney.oda.Messages;
031:
032: import org.eclipse.datatools.connectivity.oda.IBlob;
033: import org.eclipse.datatools.connectivity.oda.IClob;
034: import org.eclipse.datatools.connectivity.oda.IResultSet;
035: import org.eclipse.datatools.connectivity.oda.IResultSetMetaData;
036: import org.eclipse.datatools.connectivity.oda.OdaException;
037:
038: /**
039: * Class to hold JMoney result sets. These result sets provide an implementation
040: * of the ODA IResultSet interface.
041: */
042:
043: public class ResultSet implements IResultSet {
044: private IFetcher fetcher;
045:
046: private ResultSetMetaData resultSetMetaData = null;
047:
048: private int maxRows = 0;
049:
050: /*
051: * The zero based row index. Initialize to -1 so the first increment puts it
052: * on the first row.
053: */
054: private int rowIndex = -1;
055:
056: /**
057: * Flag to indicate if the last value fetched was null.
058: * This is needed because not all values can be returned
059: * as null values. For example, integers are returned
060: * as zero values if null.
061: */
062: private boolean wasNull = false;
063:
064: /**
065: * Constructor
066: * @param fetcher a two-dimension array which holds the data extracted from a
067: * flat file.
068: * @param rsmd the metadata of sData
069: */
070: ResultSet(IFetcher fetcher, ResultSetMetaData rsmd) {
071: this .fetcher = fetcher;
072: this .resultSetMetaData = rsmd;
073: }
074:
075: /*
076: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getMetaData()
077: */
078: public IResultSetMetaData getMetaData() throws OdaException {
079: return this .resultSetMetaData;
080: }
081:
082: /*
083: * @see org.eclipse.datatools.connectivity.oda.IResultSet#close()
084: */
085: public void close() throws OdaException {
086: // Nothing to do on a close.
087: }
088:
089: /*
090: * @see org.eclipse.datatools.connectivity.oda.IResultSet#setMaxRows(int)
091: */
092: public void setMaxRows(int maxRows) throws OdaException {
093: this .maxRows = maxRows;
094: }
095:
096: /*
097: * @see org.eclipse.datatools.connectivity.oda.IResultSet#next()
098: */
099: public boolean next() throws OdaException {
100: if (maxRows > 0 && rowIndex >= maxRows - 1) {
101: rowIndex = -1;
102: return false;
103: }
104:
105: if (!fetcher.next()) {
106: rowIndex = -1;
107: return false;
108: }
109:
110: rowIndex++;
111: return true;
112: }
113:
114: /*
115: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getRow()
116: */
117: public int getRow() throws OdaException {
118: validateCursorState();
119: return this .rowIndex;
120: }
121:
122: /*
123: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getString(int)
124: */
125: public String getString(int columnNumber) throws OdaException {
126: validateCursorState();
127: Object result = resultSetMetaData.selectedProperties.get(
128: columnNumber - 1).getValue();
129: if (result == null) {
130: wasNull = true;
131: return null;
132: } else {
133: wasNull = false;
134: return result.toString();
135: }
136: }
137:
138: /*
139: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getString(java.lang.String)
140: */
141: public String getString(String columnName) throws OdaException {
142: validateCursorState();
143: int columnNumber = findColumn(columnName);
144: return getString(columnNumber);
145: }
146:
147: /*
148: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getInt(int)
149: */
150: public int getInt(int columnNumber) throws OdaException {
151: validateCursorState();
152: Object result = resultSetMetaData.selectedProperties.get(
153: columnNumber - 1).getValue();
154: if (result == null) {
155: wasNull = true;
156: return 0;
157: } else {
158: wasNull = false;
159: if (result instanceof Number) {
160: return ((Number) result).intValue();
161: } else {
162: return 0;
163: }
164: }
165: }
166:
167: /*
168: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getInt(java.lang.String)
169: */
170: public int getInt(String columnName) throws OdaException {
171: validateCursorState();
172: int columnNumber = findColumn(columnName);
173: return getInt(columnNumber);
174: }
175:
176: /*
177: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDouble(int)
178: */
179: public double getDouble(int columnNumber) throws OdaException {
180: validateCursorState();
181: Object result = resultSetMetaData.selectedProperties.get(
182: columnNumber - 1).getValue();
183: if (result == null) {
184: wasNull = true;
185: return 0;
186: } else {
187: wasNull = false;
188:
189: if (result instanceof Number) {
190: return ((Number) result).doubleValue();
191: } else {
192: return 0;
193: }
194: }
195: }
196:
197: /*
198: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDouble(java.lang.String)
199: */
200: public double getDouble(String columnName) throws OdaException {
201: validateCursorState();
202: int columnNumber = findColumn(columnName);
203: return getDouble(columnNumber);
204: }
205:
206: /*
207: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBigDecimal(int)
208: */
209: public BigDecimal getBigDecimal(int columnNumber)
210: throws OdaException {
211: validateCursorState();
212: Object result = resultSetMetaData.selectedProperties.get(
213: columnNumber - 1).getValue();
214: if (result == null) {
215: wasNull = true;
216: return null;
217: } else {
218: wasNull = false;
219: try {
220: return new BigDecimal(result.toString());
221: } catch (NumberFormatException e) {
222: this .wasNull = true;
223: return null;
224: }
225: }
226: }
227:
228: /*
229: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBigDecimal(java.lang.String)
230: */
231: public BigDecimal getBigDecimal(String columnName)
232: throws OdaException {
233: validateCursorState();
234: int columnNumber = findColumn(columnName);
235: return getBigDecimal(columnNumber);
236: }
237:
238: /*
239: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDate(int)
240: */
241: public Date getDate(int columnNumber) throws OdaException {
242: validateCursorState();
243: Object result = resultSetMetaData.selectedProperties.get(
244: columnNumber - 1).getValue();
245: if (result == null) {
246: wasNull = true;
247: return null;
248: } else {
249: wasNull = false;
250: if (result instanceof Date) {
251: return (Date) result;
252: } else {
253: throw new OdaException("Property is not a date");
254: }
255: }
256: }
257:
258: /*
259: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDate(java.lang.String)
260: */
261: public Date getDate(String columnName) throws OdaException {
262: validateCursorState();
263: int columnNumber = findColumn(columnName);
264: return getDate(columnNumber);
265: }
266:
267: /*
268: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTime(int)
269: */
270: public Time getTime(int columnNumber) throws OdaException {
271: throw new UnsupportedOperationException();
272: }
273:
274: /*
275: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTime(java.lang.String)
276: */
277: public Time getTime(String columnName) throws OdaException {
278: throw new UnsupportedOperationException();
279: }
280:
281: /*
282: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTimestamp(int)
283: */
284: public Timestamp getTimestamp(int columnNumber) throws OdaException {
285: throw new UnsupportedOperationException();
286: }
287:
288: /*
289: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTimestamp(java.lang.String)
290: */
291: public Timestamp getTimestamp(String columnName)
292: throws OdaException {
293: throw new UnsupportedOperationException();
294: }
295:
296: /*
297: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBlob(int)
298: */
299: public IBlob getBlob(int columnNumber) throws OdaException {
300: throw new UnsupportedOperationException();
301: }
302:
303: /*
304: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBlob(java.lang.String)
305: */
306: public IBlob getBlob(String columnName) throws OdaException {
307: throw new UnsupportedOperationException();
308: }
309:
310: /*
311: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getClob(int)
312: */
313: public IClob getClob(int columnNumber) throws OdaException {
314: throw new UnsupportedOperationException();
315: }
316:
317: /*
318: * @see org.eclipse.datatools.connectivity.oda.IResultSet#getClob(java.lang.String)
319: */
320: public IClob getClob(String columnName) throws OdaException {
321: throw new UnsupportedOperationException();
322: }
323:
324: public boolean getBoolean(int columnNumber) throws OdaException {
325: validateCursorState();
326: Object result = resultSetMetaData.selectedProperties.get(
327: columnNumber - 1).getValue();
328: if (result == null) {
329: wasNull = true;
330: return false;
331: } else {
332: wasNull = false;
333: if (result instanceof Boolean) {
334: return (Boolean) result;
335: } else if (result instanceof Number) {
336: return ((Number) result).intValue() != 0;
337: } else {
338: return false;
339: }
340: }
341: }
342:
343: public boolean getBoolean(String columnName) throws OdaException {
344: validateCursorState();
345: int columnNumber = findColumn(columnName);
346: return getBoolean(columnNumber);
347: }
348:
349: /*
350: * @see org.eclipse.datatools.connectivity.oda.IResultSet#wasNull()
351: */
352: public boolean wasNull() throws OdaException {
353: return this .wasNull;
354: }
355:
356: /*
357: * @see org.eclipse.datatools.connectivity.oda.IResultSet#findColumn(java.lang.String)
358: */
359: public int findColumn(String columnName) throws OdaException {
360: String nameWithDots = columnName.replace('_', '.');
361:
362: Integer columnIndex = resultSetMetaData.columnNameToIndexMap
363: .get(nameWithDots);
364: if (columnIndex == null) {
365: throw new OdaException(Messages
366: .getString("resultSet.columnNotFound") + columnName); //$NON-NLS-1$
367: }
368:
369: return columnIndex + 1;
370: }
371:
372: /**
373: * Validate whether the cursor has been initialized and at a valid row.
374: * @throws OdaException if the cursor is not initialized
375: */
376: private void validateCursorState() throws OdaException {
377: if (rowIndex < 0) {
378: throw new OdaException(Messages
379: .getString("resultSet.cursorNotInitialized")); //$NON-NLS-1$
380: }
381: }
382: }
|