1 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 ; 38 import java.math.BigDecimal ; 39 import java.sql.ResultSet ; 40 import java.sql.ResultSetMetaData ; 41 import java.sql.SQLException ; 42 import java.sql.Types ; 43 import java.util.Iterator ; 44 import java.util.logging.Logger ; 45 46 public class ResultSetEcmaWrap { 47 private static final Logger log = Log.open(ResultSet .class); 48 49 public static String getString(ResultSet rs, ESBase col) 50 throws Throwable 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 rs, ESBase col) 61 throws Throwable 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 rs, ESBase col) 72 throws Throwable 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 rs, ESBase col) 83 throws Throwable 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 rs, ESBase col) 94 throws Throwable 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 rs, ESBase col) 105 throws Throwable 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 rs, ESBase col) 116 throws Throwable 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 rs, ESBase col) 127 throws Throwable 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 getBigDecimal(ResultSet rs, ESBase col, int i) 138 throws Throwable 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 rs, ESBase col) 149 throws Throwable 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 rs, Call call, int len) 160 throws Throwable 161 { 162 ESBase col = call.getArg(0, len); 163 ESBase key = col.toPrimitive(); 164 java.util.Date 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 rs, Call call, int len) 175 throws Throwable 176 { 177 ESBase col = call.getArg(0, len); 178 ESBase key = col.toPrimitive(); 179 java.util.Date 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 rs, Call call, int len) 190 throws Throwable 191 { 192 ESBase col = call.getArg(0, len); 193 ESBase key = col.toPrimitive(); 194 java.util.Date 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 getAsciiStream(ResultSet rs, ESBase col) 205 throws Throwable 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 getUnicodeStream(ResultSet rs, ESBase col) 216 throws Throwable 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 getBinaryStream(ResultSet rs, ESBase col) 227 throws Throwable 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 getObject(ResultSet rs, ESBase col) 238 throws Throwable 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 getByname(ResultSet rs, String string) 249 throws SQLException 250 { 251 return rs.getString(string); 252 } 253 254 public static Object get(ResultSet rs, String key) 255 throws Throwable 256 { 257 return get(rs, rs.findColumn(key)); 258 } 259 260 public static Object get(ResultSet rs, int index) 261 throws Throwable 262 { 263 ResultSetMetaData md = rs.getMetaData(); 264 265 switch (md.getColumnType(index)) { 266 case Types.BIT: 267 return new Boolean (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 (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 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 toObject(ResultSet rs, Call call, int length) 315 throws Throwable 316 { 317 ResultSetMetaData 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 name = md.getColumnName(i + 1); 331 Object 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 rs) 340 { 341 try { 342 return new ResultSetIterator(rs); 343 } catch (Exception e) { 344 return NullIterator.create(); 345 } 346 } 347 348 static class ResultSetIterator implements Iterator { 349 ResultSet rs; 350 ResultSetMetaData md; 351 int nColumns; 352 int i; 353 354 public boolean hasNext() 355 { 356 return i < nColumns; 357 } 358 359 public Object next() 360 { 361 try { 362 return md.getColumnName(++i); 363 } catch (SQLException e) { 364 return null; 365 } 366 } 367 368 public void remove() { throw new UnsupportedOperationException (); } 369 370 ResultSetIterator(ResultSet rs) 371 throws SQLException 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 |