1 9 package com.ziclix.python.sql; 10 11 import org.python.core.Py; 12 import org.python.core.PyFile; 13 import org.python.core.PyLong; 14 import org.python.core.PyObject; 15 import org.python.core.PyList; 16 import org.python.core.PyString; 17 18 import java.io.BufferedInputStream ; 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.Reader ; 23 import java.io.StringReader ; 24 import java.lang.reflect.Constructor ; 25 import java.math.BigDecimal ; 26 import java.sql.CallableStatement ; 27 import java.sql.Date ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Statement ; 32 import java.sql.Time ; 33 import java.sql.Timestamp ; 34 import java.sql.Types ; 35 36 51 public class DataHandler { 52 53 private static final int INITIAL_SIZE = 1024 * 4; 55 56 private static final String [] SYSTEM_DATAHANDLERS = { 57 "com.ziclix.python.sql.JDBC20DataHandler" 58 }; 59 60 63 public DataHandler() {} 64 65 70 public String getMetaDataName(PyObject name) { 71 return ((name == Py.None) ? null : name.__str__().toString()); 72 } 73 74 82 public Procedure getProcedure(PyCursor cursor, PyObject name) throws SQLException { 83 return new Procedure(cursor, name); 84 } 85 86 94 public PyObject getRowId(Statement stmt) throws SQLException { 95 return Py.None; 96 } 97 98 102 public void preExecute(Statement stmt) throws SQLException { 103 return; 104 } 105 106 109 public void postExecute(Statement stmt) throws SQLException { 110 return; 111 } 112 113 122 public void setJDBCObject(PreparedStatement stmt, int index, PyObject object) throws SQLException { 123 124 try { 125 stmt.setObject(index, object.__tojava__(Object .class)); 126 } catch (Exception e) { 127 SQLException cause = null, ex = new SQLException ("error setting index [" + index + "]"); 128 129 if (e instanceof SQLException ) { 130 cause = (SQLException ) e; 131 } else { 132 cause = new SQLException (e.getMessage()); 133 } 134 135 ex.setNextException(cause); 136 137 throw ex; 138 } 139 } 140 141 152 public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, int type) throws SQLException { 153 154 try { 155 if (checkNull(stmt, index, object, type)) { 156 return; 157 } 158 159 switch (type) { 160 161 case Types.DATE: 162 Date date = (Date ) object.__tojava__(Date .class); 163 164 stmt.setDate(index, date); 165 break; 166 167 case Types.TIME: 168 Time time = (Time ) object.__tojava__(Time .class); 169 170 stmt.setTime(index, time); 171 break; 172 173 case Types.TIMESTAMP: 174 Timestamp timestamp = (Timestamp ) object.__tojava__(Timestamp .class); 175 176 stmt.setTimestamp(index, timestamp); 177 break; 178 179 case Types.LONGVARCHAR: 180 if (object instanceof PyFile) { 181 object = new PyString(((PyFile) object).read()); 182 } 183 184 String varchar = (String ) object.__tojava__(String .class); 185 Reader reader = new BufferedReader (new StringReader (varchar)); 186 187 stmt.setCharacterStream(index, reader, varchar.length()); 188 break; 189 190 case Types.BIT: 191 stmt.setBoolean(index, object.__nonzero__()); 192 break; 193 194 default : 195 if (object instanceof PyFile) { 196 object = new PyString(((PyFile) object).read()); 197 } 198 199 stmt.setObject(index, object.__tojava__(Object .class), type); 200 break; 201 } 202 } catch (Exception e) { 203 SQLException cause = null, ex = new SQLException ("error setting index [" + index + "], type [" + type + "]"); 204 205 if (e instanceof SQLException ) { 206 cause = (SQLException ) e; 207 } else { 208 cause = new SQLException (e.getMessage()); 209 } 210 211 ex.setNextException(cause); 212 213 throw ex; 214 } 215 } 216 217 228 public PyObject getPyObject(ResultSet set, int col, int type) throws SQLException { 229 230 PyObject obj = Py.None; 231 232 switch (type) { 233 234 case Types.CHAR: 235 case Types.VARCHAR: 236 String string = set.getString(col); 237 238 obj = (string == null) ? Py.None : Py.newString(string); 239 break; 240 241 case Types.LONGVARCHAR: 242 InputStream longvarchar = set.getAsciiStream(col); 243 244 if (longvarchar == null) { 245 obj = Py.None; 246 } else { 247 try { 248 longvarchar = new BufferedInputStream (longvarchar); 249 250 byte[] bytes = DataHandler.read(longvarchar); 251 252 if (bytes != null) { 253 obj = Py.newString(new String (bytes)); 254 } 255 } finally { 256 try { 257 longvarchar.close(); 258 } catch (Throwable t) {} 259 } 260 } 261 break; 262 263 case Types.NUMERIC: 264 case Types.DECIMAL: 265 BigDecimal bd = null; 266 267 try { 268 bd = set.getBigDecimal(col, set.getMetaData().getPrecision(col)); 269 } catch (Throwable t) { 270 bd = set.getBigDecimal(col, 10); 271 } 272 273 obj = (bd == null) ? Py.None : Py.newFloat(bd.doubleValue()); 274 break; 275 276 case Types.BIT: 277 obj = set.getBoolean(col) ? Py.One : Py.Zero; 278 break; 279 280 case Types.INTEGER: 281 case Types.TINYINT: 282 case Types.SMALLINT: 283 obj = Py.newInteger(set.getInt(col)); 284 break; 285 286 case Types.BIGINT: 287 obj = new PyLong(set.getLong(col)); 288 break; 289 290 case Types.FLOAT: 291 case Types.REAL: 292 case Types.DOUBLE: 293 obj = Py.newFloat(set.getFloat(col)); 294 break; 295 296 case Types.TIME: 297 obj = Py.java2py(set.getTime(col)); 298 break; 299 300 case Types.TIMESTAMP: 301 obj = Py.java2py(set.getTimestamp(col)); 302 break; 303 304 case Types.DATE: 305 obj = Py.java2py(set.getDate(col)); 306 break; 307 308 case Types.NULL: 309 obj = Py.None; 310 break; 311 312 case Types.OTHER: 313 obj = Py.java2py(set.getObject(col)); 314 break; 315 316 case Types.BINARY: 317 case Types.VARBINARY: 318 case Types.LONGVARBINARY: 319 obj = Py.java2py(set.getBytes(col)); 320 break; 321 322 default : 323 Integer [] vals = {new Integer (col), new Integer (type)}; 324 String msg = zxJDBC.getString("errorGettingIndex", vals); 325 326 throw new SQLException (msg); 327 } 328 329 return (set.wasNull() || (obj == null)) ? Py.None : obj; 330 } 331 332 341 public PyObject getPyObject(CallableStatement stmt, int col, int type) throws SQLException { 342 343 PyObject obj = Py.None; 344 345 switch (type) { 346 347 case Types.CHAR: 348 case Types.VARCHAR: 349 case Types.LONGVARCHAR: 350 String string = stmt.getString(col); 351 352 obj = (string == null) ? Py.None : Py.newString(string); 353 break; 354 355 case Types.NUMERIC: 356 case Types.DECIMAL: 357 BigDecimal bd = stmt.getBigDecimal(col, 10); 358 359 obj = (bd == null) ? Py.None : Py.newFloat(bd.doubleValue()); 360 break; 361 362 case Types.BIT: 363 obj = stmt.getBoolean(col) ? Py.One : Py.Zero; 364 break; 365 366 case Types.INTEGER: 367 case Types.TINYINT: 368 case Types.SMALLINT: 369 obj = Py.newInteger(stmt.getInt(col)); 370 break; 371 372 case Types.BIGINT: 373 obj = new PyLong(stmt.getLong(col)); 374 break; 375 376 case Types.FLOAT: 377 case Types.REAL: 378 case Types.DOUBLE: 379 obj = Py.newFloat(stmt.getFloat(col)); 380 break; 381 382 case Types.TIME: 383 obj = Py.java2py(stmt.getTime(col)); 384 break; 385 386 case Types.TIMESTAMP: 387 obj = Py.java2py(stmt.getTimestamp(col)); 388 break; 389 390 case Types.DATE: 391 obj = Py.java2py(stmt.getDate(col)); 392 break; 393 394 case Types.NULL: 395 obj = Py.None; 396 break; 397 398 case Types.OTHER: 399 obj = Py.java2py(stmt.getObject(col)); 400 break; 401 402 case Types.BINARY: 403 case Types.VARBINARY: 404 case Types.LONGVARBINARY: 405 obj = Py.java2py(stmt.getBytes(col)); 406 break; 407 408 default : 409 Integer [] vals = {new Integer (col), new Integer (type)}; 410 String msg = zxJDBC.getString("errorGettingIndex", vals); 411 412 throw new SQLException (msg); 413 } 414 415 return (stmt.wasNull() || (obj == null)) ? Py.None : obj; 416 } 417 418 431 public void registerOut(CallableStatement statement, int index, int colType, int dataType, String dataTypeName) throws SQLException { 432 433 try { 434 statement.registerOutParameter(index, dataType); 435 } catch (Throwable t) { 436 SQLException cause = null; 437 SQLException ex = new SQLException ("error setting index [" 438 + index + "], coltype [" + colType + "], datatype [" + dataType 439 + "], datatypename [" + dataTypeName + "]"); 440 441 if (t instanceof SQLException ) { 442 cause = (SQLException )t; 443 } else { 444 cause = new SQLException (t.getMessage()); 445 } 446 ex.setNextException(cause); 447 throw ex; 448 } 449 } 450 451 456 public static final boolean checkNull(PreparedStatement stmt, int index, PyObject object, int type) throws SQLException { 457 458 if ((object == null) || (Py.None == object)) { 459 stmt.setNull(index, type); 460 return true; 461 } 462 return false; 463 } 464 465 471 public static final byte[] read(InputStream stream) { 472 473 int b = -1, read = 0; 474 byte[] results = new byte[INITIAL_SIZE]; 475 476 try { 477 while ((b = stream.read()) != -1) { 478 if (results.length < (read + 1)) { 479 byte[] tmp = results; 480 results = new byte[results.length * 2]; 481 System.arraycopy(tmp, 0, results, 0, tmp.length); 482 } 483 results[read++] = (byte) b; 484 } 485 } catch (IOException e) { 486 throw zxJDBC.makeException(e); 487 } 488 489 byte[] tmp = results; 490 results = new byte[read]; 491 System.arraycopy(tmp, 0, results, 0, read); 492 return results; 493 } 494 495 500 public static final String read(Reader reader) { 501 502 int c = 0; 503 StringBuffer buffer = new StringBuffer (INITIAL_SIZE); 504 505 try { 506 while ((c = reader.read()) != -1) { 507 buffer.append((char) c); 508 } 509 } catch (IOException e) { 510 throw zxJDBC.makeException(e); 511 } 512 513 return buffer.toString(); 514 } 515 516 521 public static final DataHandler getSystemDataHandler() { 522 DataHandler dh = new DataHandler(); 523 524 for (int i = 0; i < SYSTEM_DATAHANDLERS.length; i++) { 525 try { 526 Class c = Class.forName(SYSTEM_DATAHANDLERS[i]); 527 Constructor cons = c.getConstructor(new Class []{DataHandler.class}); 528 dh = (DataHandler) cons.newInstance(new Object []{dh}); 529 } catch (Throwable t) {} 530 } 531 532 return dh; 533 } 534 535 540 public PyObject __chain__() { 541 return new PyList(new PyObject[] { Py.java2py(this) }); 542 } 543 544 547 public String toString() { 548 return getClass().getName(); 549 } 550 } 551 552 | Popular Tags |