KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > eswrap > java > sql > ResultSetEcmaWrap


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.eswrap.java.sql;
30
31 import com.caucho.es.Call;
32 import com.caucho.es.ESBase;
33 import com.caucho.es.Global;
34 import com.caucho.log.Log;
35 import com.caucho.util.NullIterator;
36
37 import java.io.InputStream JavaDoc;
38 import java.math.BigDecimal JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.ResultSetMetaData JavaDoc;
41 import java.sql.SQLException JavaDoc;
42 import java.sql.Types JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 public class ResultSetEcmaWrap {
47   private static final Logger JavaDoc log = Log.open(ResultSet JavaDoc.class);
48
49   public static String JavaDoc getString(ResultSet JavaDoc rs, ESBase col)
50     throws Throwable JavaDoc
51   {
52     ESBase key = col.toPrimitive();
53
54     if (key.isString())
55       return rs.getString(key.toString());
56     else
57       return rs.getString((int) key.toNum());
58   }
59
60   public static boolean getBoolean(ResultSet JavaDoc rs, ESBase col)
61     throws Throwable JavaDoc
62   {
63     ESBase key = col.toPrimitive();
64
65     if (key.isString())
66       return rs.getBoolean(key.toString());
67     else
68       return rs.getBoolean((int) key.toNum());
69   }
70
71   public static byte getByte(ResultSet JavaDoc rs, ESBase col)
72     throws Throwable JavaDoc
73   {
74     ESBase key = col.toPrimitive();
75
76     if (key.isString())
77       return rs.getByte(key.toString());
78     else
79       return rs.getByte((int) key.toNum());
80   }
81
82   public static short getShort(ResultSet JavaDoc rs, ESBase col)
83     throws Throwable JavaDoc
84   {
85     ESBase key = col.toPrimitive();
86
87     if (key.isString())
88       return rs.getShort(key.toString());
89     else
90       return rs.getShort((int) key.toNum());
91   }
92
93   public static int getInt(ResultSet JavaDoc rs, ESBase col)
94     throws Throwable JavaDoc
95   {
96     ESBase key = col.toPrimitive();
97
98     if (key.isString())
99       return rs.getInt(key.toString());
100     else
101       return rs.getInt((int) key.toNum());
102   }
103
104   public static long getLong(ResultSet JavaDoc rs, ESBase col)
105     throws Throwable JavaDoc
106   {
107     ESBase key = col.toPrimitive();
108
109     if (key.isString())
110       return rs.getLong(key.toString());
111     else
112       return rs.getLong((int) key.toNum());
113   }
114
115   public static float getFloat(ResultSet JavaDoc rs, ESBase col)
116     throws Throwable JavaDoc
117   {
118     ESBase key = col.toPrimitive();
119
120     if (key.isString())
121       return rs.getFloat(key.toString());
122     else
123       return rs.getFloat((int) key.toNum());
124   }
125
126   public static double getDouble(ResultSet JavaDoc rs, ESBase col)
127     throws Throwable JavaDoc
128   {
129     ESBase key = col.toPrimitive();
130
131     if (key.isString())
132       return rs.getDouble(key.toString());
133     else
134       return rs.getDouble((int) key.toNum());
135   }
136
137   public static BigDecimal JavaDoc getBigDecimal(ResultSet JavaDoc rs, ESBase col, int i)
138     throws Throwable JavaDoc
139   {
140     ESBase key = col.toPrimitive();
141
142     if (key.isString())
143       return rs.getBigDecimal(key.toString(), i);
144     else
145       return rs.getBigDecimal((int) key.toNum(), i);
146   }
147
148   public static byte[] getBytes(ResultSet JavaDoc rs, ESBase col)
149     throws Throwable JavaDoc
150   {
151     ESBase key = col.toPrimitive();
152
153     if (key.isString())
154       return rs.getBytes(key.toString());
155     else
156       return rs.getBytes((int) key.toNum());
157   }
158
159   public static ESBase getDate(ResultSet JavaDoc rs, Call call, int len)
160     throws Throwable JavaDoc
161   {
162     ESBase col = call.getArg(0, len);
163     ESBase key = col.toPrimitive();
164     java.util.Date JavaDoc date;
165
166     if (key.isString())
167       date = rs.getDate(key.toString());
168     else
169       date = rs.getDate((int) key.toNum());
170
171     return call.createDate(date.getTime());
172   }
173
174   public static ESBase getTime(ResultSet JavaDoc rs, Call call, int len)
175     throws Throwable JavaDoc
176   {
177     ESBase col = call.getArg(0, len);
178     ESBase key = col.toPrimitive();
179     java.util.Date JavaDoc date;
180
181     if (key.isString())
182       date = rs.getTime(key.toString());
183     else
184       date = rs.getTime((int) key.toNum());
185
186     return call.createDate(date.getTime());
187   }
188
189   public static ESBase getTimestamp(ResultSet JavaDoc rs, Call call, int len)
190     throws Throwable JavaDoc
191   {
192     ESBase col = call.getArg(0, len);
193     ESBase key = col.toPrimitive();
194     java.util.Date JavaDoc date;
195
196     if (key.isString())
197       date = rs.getTimestamp(key.toString());
198     else
199       date = rs.getTimestamp((int) key.toNum());
200
201     return call.createDate(date.getTime());
202   }
203
204   public static InputStream JavaDoc getAsciiStream(ResultSet JavaDoc rs, ESBase col)
205     throws Throwable JavaDoc
206   {
207     ESBase key = col.toPrimitive();
208
209     if (key.isString())
210       return rs.getAsciiStream(key.toString());
211     else
212       return rs.getAsciiStream((int) key.toNum());
213   }
214
215   public static InputStream JavaDoc getUnicodeStream(ResultSet JavaDoc rs, ESBase col)
216     throws Throwable JavaDoc
217   {
218     ESBase key = col.toPrimitive();
219
220     if (key.isString())
221       return rs.getUnicodeStream(key.toString());
222     else
223       return rs.getUnicodeStream((int) key.toNum());
224   }
225
226   public static InputStream JavaDoc getBinaryStream(ResultSet JavaDoc rs, ESBase col)
227     throws Throwable JavaDoc
228   {
229     ESBase key = col.toPrimitive();
230
231     if (key.isString())
232       return rs.getBinaryStream(key.toString());
233     else
234       return rs.getBinaryStream((int) key.toNum());
235   }
236
237   public static Object JavaDoc getObject(ResultSet JavaDoc rs, ESBase col)
238     throws Throwable JavaDoc
239   {
240     ESBase key = col.toPrimitive();
241
242     if (key.isString())
243       return rs.getObject(key.toString());
244     else
245       return rs.getObject((int) key.toNum());
246   }
247
248   public static String JavaDoc getByname(ResultSet JavaDoc rs, String JavaDoc string)
249     throws SQLException JavaDoc
250   {
251     return rs.getString(string);
252   }
253
254   public static Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc key)
255     throws Throwable JavaDoc
256   {
257     return get(rs, rs.findColumn(key));
258   }
259
260   public static Object JavaDoc get(ResultSet JavaDoc rs, int index)
261     throws Throwable JavaDoc
262   {
263     ResultSetMetaData JavaDoc md = rs.getMetaData();
264     
265     switch (md.getColumnType(index)) {
266     case Types.BIT:
267       return new Boolean JavaDoc(rs.getInt(index) == 1);
268
269     case Types.TINYINT:
270     case Types.SMALLINT:
271     case Types.INTEGER:
272     case Types.FLOAT:
273     case Types.REAL:
274     case Types.DOUBLE:
275       return new Double JavaDoc(rs.getDouble(index));
276
277     case Types.CHAR:
278     case Types.VARCHAR:
279       return rs.getString(index);
280
281     case Types.NULL:
282       return null;
283
284       // XXX: the following are bogus
285
case Types.BIGINT:
286     case Types.NUMERIC:
287     case Types.DECIMAL:
288       return rs.getString(index);
289
290     case Types.LONGVARCHAR:
291       return rs.getAsciiStream(index);
292
293     case Types.DATE:
294       return rs.getDate(index);
295
296     case Types.TIME:
297       return rs.getTime(index);
298
299     case Types.TIMESTAMP:
300       return rs.getTimestamp(index);
301
302     case Types.BINARY:
303     case Types.VARBINARY:
304       return rs.getBytes(index);
305
306     case Types.LONGVARBINARY:
307       return rs.getBinaryStream(index);
308
309     default:
310       return rs.getString(index);
311     }
312   }
313
314   public static Object JavaDoc toObject(ResultSet JavaDoc rs, Call call, int length)
315     throws Throwable JavaDoc
316   {
317     ResultSetMetaData JavaDoc md;
318
319     md = rs.getMetaData();
320
321     Global global = Global.getGlobalProto();
322     ESBase obj;
323     if (length > 0)
324       obj = call.getArg(0, length);
325     else
326       obj = global.createObject();
327
328     int nColumns = md.getColumnCount();
329     for (int i = 0; i < nColumns; i++) {
330       String JavaDoc name = md.getColumnName(i + 1);
331       Object JavaDoc value = get(rs, i + 1);
332
333       obj.setProperty(name, global.wrap(value));
334     }
335
336     return obj;
337   }
338
339   public static Iterator keys(ResultSet JavaDoc rs)
340   {
341     try {
342       return new ResultSetIterator(rs);
343     } catch (Exception JavaDoc e) {
344       return NullIterator.create();
345     }
346   }
347
348   static class ResultSetIterator implements Iterator {
349     ResultSet JavaDoc rs;
350     ResultSetMetaData JavaDoc md;
351     int nColumns;
352     int i;
353
354     public boolean hasNext()
355     {
356       return i < nColumns;
357     }
358
359     public Object JavaDoc next()
360     {
361       try {
362     return md.getColumnName(++i);
363       } catch (SQLException JavaDoc e) {
364     return null;
365       }
366     }
367
368     public void remove() { throw new UnsupportedOperationException JavaDoc(); }
369
370     ResultSetIterator(ResultSet JavaDoc rs)
371       throws SQLException JavaDoc
372     {
373       this.rs = rs;
374
375       this.md = rs.getMetaData();
376       nColumns = md.getColumnCount();
377     }
378   }
379
380   private ResultSetEcmaWrap()
381   {
382   }
383 }
384
Popular Tags