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.PyObject; 14 import org.python.core.PyString; 15 16 import java.io.BufferedInputStream ; 17 import java.io.BufferedReader ; 18 import java.io.ByteArrayInputStream ; 19 import java.io.InputStream ; 20 import java.io.InputStreamReader ; 21 import java.io.Reader ; 22 import java.math.BigDecimal ; 23 import java.sql.Blob ; 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 JDBC20DataHandler extends FilterDataHandler { 37 38 41 public JDBC20DataHandler(DataHandler datahandler) { 42 super(datahandler); 43 } 44 45 54 public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, int type) throws SQLException { 55 56 if (DataHandler.checkNull(stmt, index, object, type)) { 57 return; 58 } 59 60 switch (type) { 61 62 case Types.CLOB: 63 if (object instanceof PyFile) { 64 object = new PyString(((PyFile) object).read()); 65 } 66 67 String clob = (String ) object.__tojava__(String .class); 68 int length = clob.length(); 69 InputStream stream = new ByteArrayInputStream (clob.getBytes()); 70 71 stream = new BufferedInputStream (stream); 72 73 stmt.setBinaryStream(index, stream, length); 74 75 break; 79 80 case Types.BLOB: 81 byte[] lob = null; 82 Object jobject = null; 83 84 if (object instanceof PyFile) { 85 jobject = object.__tojava__(InputStream .class); 86 } else { 87 jobject = object.__tojava__(Object .class); 88 } 89 90 if (jobject instanceof InputStream ) { 92 lob = DataHandler.read(new BufferedInputStream ((InputStream ) jobject)); 93 } else if (jobject instanceof byte[]) { 94 lob = (byte[]) jobject; 95 } 96 97 if (lob != null) { 98 stmt.setBytes(index, lob); 99 100 break; 101 } 102 default : 103 super.setJDBCObject(stmt, index, object, type); 104 break; 105 } 106 } 107 108 117 public PyObject getPyObject(ResultSet set, int col, int type) throws SQLException { 118 119 PyObject obj = Py.None; 120 121 switch (type) { 122 123 case Types.NUMERIC: 124 case Types.DECIMAL: 125 126 try { 128 BigDecimal bd = set.getBigDecimal(col); 129 130 obj = (bd == null) ? Py.None : Py.newFloat(bd.doubleValue()); 131 } catch (SQLException e) { 132 obj = super.getPyObject(set, col, type); 133 } 134 break; 135 136 case Types.CLOB: 137 138 148 Reader reader = null; 149 150 try { 151 InputStream stream = set.getBinaryStream(col); 152 153 if (stream == null) { 154 obj = Py.None; 155 } else { 156 reader = new InputStreamReader (stream); 157 reader = new BufferedReader (reader); 158 obj = Py.newString(DataHandler.read(reader)); 159 } 160 } finally { 161 if (reader != null) { 162 try { 163 reader.close(); 164 } catch (Exception e) { 165 } 166 } 167 } 168 break; 169 170 case Types.BLOB: 171 Blob blob = set.getBlob(col); 172 173 if (blob == null) { 174 obj = Py.None; 175 } else { 176 InputStream stream = null; 177 178 try { 179 stream = blob.getBinaryStream(); 180 stream = new BufferedInputStream (stream); 181 obj = Py.java2py(DataHandler.read(stream)); 182 } finally { 183 if (stream != null) { 184 try { 185 stream.close(); 186 } catch (Exception e) { 187 } 188 } 189 } 190 } 191 break; 192 193 case Types.ARRAY: 194 obj = Py.java2py(set.getArray(col).getArray()); 195 break; 196 197 default : 198 return super.getPyObject(set, col, type); 199 } 200 201 return (set.wasNull() || (obj == null)) ? Py.None : obj; 202 } 203 } 204 | Popular Tags |