1 9 package com.ziclix.python.sql.handler; 10 11 import com.ziclix.python.sql.DataHandler; 12 import com.ziclix.python.sql.FilterDataHandler; 13 import com.ziclix.python.sql.zxJDBC; 14 import oracle.jdbc.driver.OracleResultSet; 15 import oracle.jdbc.driver.OracleTypes; 16 import oracle.sql.BLOB; 17 import oracle.sql.ROWID; 18 import org.python.core.Py; 19 import org.python.core.PyObject; 20 21 import java.io.BufferedInputStream ; 22 import java.io.InputStream ; 23 import java.sql.CallableStatement ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Types ; 28 29 36 public class OracleDataHandler extends FilterDataHandler { 37 38 41 public OracleDataHandler(DataHandler datahandler) { 42 super(datahandler); 43 } 44 45 51 public String getMetaDataName(PyObject name) { 52 53 String metaName = super.getMetaDataName(name); 54 55 return (metaName == null) ? null : metaName.toUpperCase(); 56 } 57 58 61 public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, int type) throws SQLException { 62 63 if (DataHandler.checkNull(stmt, index, object, type)) { 64 return; 65 } 66 67 switch (type) { 68 69 case OracleTypes.ROWID: 70 stmt.setString(index, (String ) object.__tojava__(String .class)); 71 break; 72 73 case Types.DECIMAL: 74 75 Object input = object.__tojava__(Double .class); 77 78 if (input != Py.NoConversion) { 79 stmt.setDouble(index, ((Double ) input).doubleValue()); 80 81 break; 82 } 83 84 super.setJDBCObject(stmt, index, object, type); 85 break; 86 87 case Types.NUMERIC: 88 super.setJDBCObject(stmt, index, object, Types.DOUBLE); 89 break; 90 91 case Types.BLOB: 92 case Types.CLOB: 93 Integer [] vals = {new Integer (index), new Integer (type)}; 94 String msg = zxJDBC.getString("errorSettingIndex", vals); 95 96 throw new SQLException (msg); 97 default : 98 super.setJDBCObject(stmt, index, object, type); 99 } 100 } 101 102 105 public PyObject getPyObject(ResultSet set, int col, int type) throws SQLException { 106 107 PyObject obj = Py.None; 108 109 switch (type) { 110 111 case Types.BLOB: 112 BLOB blob = ((OracleResultSet) set).getBLOB(col); 113 114 if (blob == null) { 115 return Py.None; 116 } 117 118 InputStream stream = new BufferedInputStream (blob.getBinaryStream()); 119 120 obj = Py.java2py(DataHandler.read(stream)); 121 break; 122 123 case OracleTypes.ROWID: 124 ROWID rowid = ((OracleResultSet) set).getROWID(col); 125 126 if (rowid != null) { 127 obj = Py.java2py(rowid.stringValue()); 128 } 129 break; 130 131 default : 132 obj = super.getPyObject(set, col, type); 133 } 134 135 return (set.wasNull() ? Py.None : obj); 136 } 137 138 149 public void registerOut(CallableStatement statement, int index, int colType, int dataType, String dataTypeName) throws SQLException { 150 151 if (dataType == Types.OTHER) { 152 if ("REF CURSOR".equals(dataTypeName)) { 153 statement.registerOutParameter(index, OracleTypes.CURSOR); 154 155 return; 156 } else if ("PL/SQL RECORD".equals(dataTypeName)) { 157 statement.registerOutParameter(index, OracleTypes.CURSOR); 158 159 return; 160 } 161 } 162 163 super.registerOut(statement, index, colType, dataType, dataTypeName); 164 } 165 } 166 | Popular Tags |