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 org.python.core.Py; 14 import org.python.core.PyFile; 15 import org.python.core.PyObject; 16 import org.python.core.PyString; 17 18 import java.io.InputStream ; 19 import java.sql.Blob ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 import java.sql.Statement ; 24 import java.sql.Types ; 25 26 33 public class InformixDataHandler extends FilterDataHandler { 34 35 40 public InformixDataHandler(DataHandler datahandler) { 41 super(datahandler); 42 } 43 44 51 public PyObject getRowId(Statement stmt) throws SQLException { 52 53 if (stmt instanceof com.informix.jdbc.IfmxStatement) { 54 return Py.newInteger(((com.informix.jdbc.IfmxStatement) stmt).getSerial()); 55 } 56 57 return super.getRowId(stmt); 58 } 59 60 69 public void setJDBCObject(PreparedStatement stmt, int index, PyObject object, int type) throws SQLException { 70 71 if (DataHandler.checkNull(stmt, index, object, type)) { 72 return; 73 } 74 75 switch (type) { 76 77 case Types.LONGVARCHAR: 78 79 String varchar; 80 if (object instanceof PyFile) { 82 varchar = ((PyFile) object).read(); 83 } else { 84 varchar = (String ) object.__tojava__(String .class); 85 } 86 stmt.setObject(index, varchar, type); 87 break; 88 89 case Types.OTHER: 90 91 stmt.setBoolean(index, object.__nonzero__()); 93 break; 94 95 default : 96 super.setJDBCObject(stmt, index, object, type); 97 } 98 } 99 100 108 public void setJDBCObject(PreparedStatement stmt, int index, PyObject object) throws SQLException { 109 110 if (object instanceof PyString) { 112 super.setJDBCObject(stmt, index, object, Types.VARCHAR); 113 } else { 114 super.setJDBCObject(stmt, index, object); 115 } 116 } 117 118 127 public PyObject getPyObject(ResultSet set, int col, int type) throws SQLException { 128 129 PyObject obj = Py.None; 130 131 switch (type) { 132 133 case Types.OTHER: 134 try { 135 136 obj = set.getBoolean(col) ? Py.One : Py.Zero; 138 } catch (SQLException e) { 139 obj = super.getPyObject(set, col, type); 140 } 141 break; 142 143 case Types.BLOB: 144 int major = set.getStatement().getConnection().getMetaData().getDriverMajorVersion(); 145 int minor = set.getStatement().getConnection().getMetaData().getDriverMinorVersion(); 146 147 if ((major <= 2) && (minor <= 11)) { 148 Blob blob = set.getBlob(col); 149 150 if (blob == null) { 151 obj = Py.None; 152 } else { 153 InputStream is = null; 154 155 try { 156 157 is = blob.getBinaryStream(); 159 obj = Py.java2py(DataHandler.read(is)); 160 } finally { 161 try { 162 is.close(); 163 } catch (Exception e) { 164 } 165 } 166 } 167 168 break; 169 } 170 default : 171 obj = super.getPyObject(set, col, type); 172 } 173 174 return (set.wasNull() || (obj == null)) ? Py.None : obj; 175 } 176 } 177 | Popular Tags |