001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source 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, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.eswrap.java.sql;
030:
031: import com.caucho.es.Call;
032: import com.caucho.es.ESBase;
033: import com.caucho.es.Global;
034: import com.caucho.log.Log;
035: import com.caucho.util.NullIterator;
036:
037: import java.io.InputStream;
038: import java.math.BigDecimal;
039: import java.sql.ResultSet;
040: import java.sql.ResultSetMetaData;
041: import java.sql.SQLException;
042: import java.sql.Types;
043: import java.util.Iterator;
044: import java.util.logging.Logger;
045:
046: public class ResultSetEcmaWrap {
047: private static final Logger log = Log.open(ResultSet.class);
048:
049: public static String getString(ResultSet rs, ESBase col)
050: throws Throwable {
051: ESBase key = col.toPrimitive();
052:
053: if (key.isString())
054: return rs.getString(key.toString());
055: else
056: return rs.getString((int) key.toNum());
057: }
058:
059: public static boolean getBoolean(ResultSet rs, ESBase col)
060: throws Throwable {
061: ESBase key = col.toPrimitive();
062:
063: if (key.isString())
064: return rs.getBoolean(key.toString());
065: else
066: return rs.getBoolean((int) key.toNum());
067: }
068:
069: public static byte getByte(ResultSet rs, ESBase col)
070: throws Throwable {
071: ESBase key = col.toPrimitive();
072:
073: if (key.isString())
074: return rs.getByte(key.toString());
075: else
076: return rs.getByte((int) key.toNum());
077: }
078:
079: public static short getShort(ResultSet rs, ESBase col)
080: throws Throwable {
081: ESBase key = col.toPrimitive();
082:
083: if (key.isString())
084: return rs.getShort(key.toString());
085: else
086: return rs.getShort((int) key.toNum());
087: }
088:
089: public static int getInt(ResultSet rs, ESBase col) throws Throwable {
090: ESBase key = col.toPrimitive();
091:
092: if (key.isString())
093: return rs.getInt(key.toString());
094: else
095: return rs.getInt((int) key.toNum());
096: }
097:
098: public static long getLong(ResultSet rs, ESBase col)
099: throws Throwable {
100: ESBase key = col.toPrimitive();
101:
102: if (key.isString())
103: return rs.getLong(key.toString());
104: else
105: return rs.getLong((int) key.toNum());
106: }
107:
108: public static float getFloat(ResultSet rs, ESBase col)
109: throws Throwable {
110: ESBase key = col.toPrimitive();
111:
112: if (key.isString())
113: return rs.getFloat(key.toString());
114: else
115: return rs.getFloat((int) key.toNum());
116: }
117:
118: public static double getDouble(ResultSet rs, ESBase col)
119: throws Throwable {
120: ESBase key = col.toPrimitive();
121:
122: if (key.isString())
123: return rs.getDouble(key.toString());
124: else
125: return rs.getDouble((int) key.toNum());
126: }
127:
128: public static BigDecimal getBigDecimal(ResultSet rs, ESBase col,
129: int i) throws Throwable {
130: ESBase key = col.toPrimitive();
131:
132: if (key.isString())
133: return rs.getBigDecimal(key.toString(), i);
134: else
135: return rs.getBigDecimal((int) key.toNum(), i);
136: }
137:
138: public static byte[] getBytes(ResultSet rs, ESBase col)
139: throws Throwable {
140: ESBase key = col.toPrimitive();
141:
142: if (key.isString())
143: return rs.getBytes(key.toString());
144: else
145: return rs.getBytes((int) key.toNum());
146: }
147:
148: public static ESBase getDate(ResultSet rs, Call call, int len)
149: throws Throwable {
150: ESBase col = call.getArg(0, len);
151: ESBase key = col.toPrimitive();
152: java.util.Date date;
153:
154: if (key.isString())
155: date = rs.getDate(key.toString());
156: else
157: date = rs.getDate((int) key.toNum());
158:
159: return call.createDate(date.getTime());
160: }
161:
162: public static ESBase getTime(ResultSet rs, Call call, int len)
163: throws Throwable {
164: ESBase col = call.getArg(0, len);
165: ESBase key = col.toPrimitive();
166: java.util.Date date;
167:
168: if (key.isString())
169: date = rs.getTime(key.toString());
170: else
171: date = rs.getTime((int) key.toNum());
172:
173: return call.createDate(date.getTime());
174: }
175:
176: public static ESBase getTimestamp(ResultSet rs, Call call, int len)
177: throws Throwable {
178: ESBase col = call.getArg(0, len);
179: ESBase key = col.toPrimitive();
180: java.util.Date date;
181:
182: if (key.isString())
183: date = rs.getTimestamp(key.toString());
184: else
185: date = rs.getTimestamp((int) key.toNum());
186:
187: return call.createDate(date.getTime());
188: }
189:
190: public static InputStream getAsciiStream(ResultSet rs, ESBase col)
191: throws Throwable {
192: ESBase key = col.toPrimitive();
193:
194: if (key.isString())
195: return rs.getAsciiStream(key.toString());
196: else
197: return rs.getAsciiStream((int) key.toNum());
198: }
199:
200: public static InputStream getUnicodeStream(ResultSet rs, ESBase col)
201: throws Throwable {
202: ESBase key = col.toPrimitive();
203:
204: if (key.isString())
205: return rs.getUnicodeStream(key.toString());
206: else
207: return rs.getUnicodeStream((int) key.toNum());
208: }
209:
210: public static InputStream getBinaryStream(ResultSet rs, ESBase col)
211: throws Throwable {
212: ESBase key = col.toPrimitive();
213:
214: if (key.isString())
215: return rs.getBinaryStream(key.toString());
216: else
217: return rs.getBinaryStream((int) key.toNum());
218: }
219:
220: public static Object getObject(ResultSet rs, ESBase col)
221: throws Throwable {
222: ESBase key = col.toPrimitive();
223:
224: if (key.isString())
225: return rs.getObject(key.toString());
226: else
227: return rs.getObject((int) key.toNum());
228: }
229:
230: public static String getByname(ResultSet rs, String string)
231: throws SQLException {
232: return rs.getString(string);
233: }
234:
235: public static Object get(ResultSet rs, String key) throws Throwable {
236: return get(rs, rs.findColumn(key));
237: }
238:
239: public static Object get(ResultSet rs, int index) throws Throwable {
240: ResultSetMetaData md = rs.getMetaData();
241:
242: switch (md.getColumnType(index)) {
243: case Types.BIT:
244: return new Boolean(rs.getInt(index) == 1);
245:
246: case Types.TINYINT:
247: case Types.SMALLINT:
248: case Types.INTEGER:
249: case Types.FLOAT:
250: case Types.REAL:
251: case Types.DOUBLE:
252: return new Double(rs.getDouble(index));
253:
254: case Types.CHAR:
255: case Types.VARCHAR:
256: return rs.getString(index);
257:
258: case Types.NULL:
259: return null;
260:
261: // XXX: the following are bogus
262: case Types.BIGINT:
263: case Types.NUMERIC:
264: case Types.DECIMAL:
265: return rs.getString(index);
266:
267: case Types.LONGVARCHAR:
268: return rs.getAsciiStream(index);
269:
270: case Types.DATE:
271: return rs.getDate(index);
272:
273: case Types.TIME:
274: return rs.getTime(index);
275:
276: case Types.TIMESTAMP:
277: return rs.getTimestamp(index);
278:
279: case Types.BINARY:
280: case Types.VARBINARY:
281: return rs.getBytes(index);
282:
283: case Types.LONGVARBINARY:
284: return rs.getBinaryStream(index);
285:
286: default:
287: return rs.getString(index);
288: }
289: }
290:
291: public static Object toObject(ResultSet rs, Call call, int length)
292: throws Throwable {
293: ResultSetMetaData md;
294:
295: md = rs.getMetaData();
296:
297: Global global = Global.getGlobalProto();
298: ESBase obj;
299: if (length > 0)
300: obj = call.getArg(0, length);
301: else
302: obj = global.createObject();
303:
304: int nColumns = md.getColumnCount();
305: for (int i = 0; i < nColumns; i++) {
306: String name = md.getColumnName(i + 1);
307: Object value = get(rs, i + 1);
308:
309: obj.setProperty(name, global.wrap(value));
310: }
311:
312: return obj;
313: }
314:
315: public static Iterator keys(ResultSet rs) {
316: try {
317: return new ResultSetIterator(rs);
318: } catch (Exception e) {
319: return NullIterator.create();
320: }
321: }
322:
323: static class ResultSetIterator implements Iterator {
324: ResultSet rs;
325: ResultSetMetaData md;
326: int nColumns;
327: int i;
328:
329: public boolean hasNext() {
330: return i < nColumns;
331: }
332:
333: public Object next() {
334: try {
335: return md.getColumnName(++i);
336: } catch (SQLException e) {
337: return null;
338: }
339: }
340:
341: public void remove() {
342: throw new UnsupportedOperationException();
343: }
344:
345: ResultSetIterator(ResultSet rs) throws SQLException {
346: this .rs = rs;
347:
348: this .md = rs.getMetaData();
349: nColumns = md.getColumnCount();
350: }
351: }
352:
353: private ResultSetEcmaWrap() {
354: }
355: }
|