1 9 package com.ziclix.python.sql; 10 11 import org.python.core.Py; 12 import org.python.core.PyClass; 13 import org.python.core.PyException; 14 import org.python.core.PyInteger; 15 import org.python.core.PyList; 16 import org.python.core.PyObject; 17 import org.python.core.PyString; 18 19 import java.sql.CallableStatement ; 20 import java.sql.PreparedStatement ; 21 import java.sql.SQLException ; 22 import java.sql.Statement ; 23 24 30 public class PyStatement extends PyObject { 31 32 35 public static final int STATEMENT_STATIC = 2; 36 37 40 public static final int STATEMENT_PREPARED = 4; 41 42 45 public static final int STATEMENT_CALLABLE = 8; 46 47 50 private int style; 51 52 55 private Object sql; 56 57 60 private boolean closed; 61 62 65 Statement statement; 66 67 74 public PyStatement(Statement statement, Object sql, int style) { 75 76 this.sql = sql; 77 this.style = style; 78 this.closed = false; 79 this.statement = statement; 80 } 81 82 88 public PyStatement(Statement statement, Procedure procedure) { 89 this(statement, procedure, STATEMENT_CALLABLE); 90 } 91 92 95 public static PyClass __class__; 96 97 102 protected PyClass getPyClass() { 103 return __class__; 104 } 105 106 109 protected static PyList __methods__; 110 111 114 protected static PyList __members__; 115 116 static { 117 PyObject[] m = new PyObject[1]; 118 119 m[0] = new PyString("close"); 120 __methods__ = new PyList(m); 121 m = new PyObject[3]; 122 m[0] = new PyString("style"); 123 m[1] = new PyString("closed"); 124 m[2] = new PyString("__statement__"); 125 __members__ = new PyList(m); 126 } 127 128 133 public PyString __str__() { 134 135 if (sql instanceof String ) { 136 return Py.newString((String ) sql); 137 } else if (sql instanceof Procedure) { 138 try { 139 return Py.newString(((Procedure) sql).toSql()); 140 } catch (SQLException e) { 141 throw zxJDBC.makeException(e); 142 } 143 } 144 145 return super.__str__(); 146 } 147 148 153 public PyString __repr__() { 154 155 StringBuffer buf = new StringBuffer ("<PyStatement object for ["); 157 158 buf.append(__str__().toString()); 159 buf.append("] at ").append(Py.id(this)).append(">"); 160 161 return Py.newString(buf.toString()); 162 } 163 164 169 public String toString() { 170 return __repr__().toString(); 171 } 172 173 179 public PyObject __findattr__(String name) { 180 181 if ("style".equals(name)) { 182 return Py.newInteger(style); 183 } else if ("closed".equals(name)) { 184 return Py.newBoolean(closed); 185 } else if ("__statement__".equals(name)) { 186 return Py.java2py(statement); 187 } else if ("__methods__".equals(name)) { 188 return __methods__; 189 } else if ("__members__".equals(name)) { 190 return __members__; 191 } 192 193 return super.__findattr__(name); 194 } 195 196 201 static public void classDictInit(PyObject dict) { 202 203 dict.__setitem__("__version__", Py.newString("$Revision: 1.6 $").__getslice__(Py.newInteger(11), Py.newInteger(-2), null)); 204 205 dict.__setitem__("classDictInit", null); 207 dict.__setitem__("statement", null); 208 dict.__setitem__("execute", null); 209 dict.__setitem__("prepare", null); 210 dict.__setitem__("STATEMENT_STATIC", null); 211 dict.__setitem__("STATEMENT_PREPARED", null); 212 dict.__setitem__("STATEMENT_CALLABLE", null); 213 } 214 215 218 public void __del__() { 219 close(); 220 } 221 222 230 public void execute(PyCursor cursor, PyObject params, PyObject bindings) throws SQLException { 231 232 if (this.closed) { 233 throw zxJDBC.makeException(zxJDBC.ProgrammingError, "statement is closed"); 234 } 235 236 this.prepare(cursor, params, bindings); 237 238 Fetch fetch = cursor.fetch; 239 240 switch (this.style) { 241 242 case STATEMENT_STATIC: 243 if (this.statement.execute((String ) this.sql)) { 244 fetch.add(this.statement.getResultSet()); 245 } 246 break; 247 248 case STATEMENT_PREPARED: 249 final PreparedStatement preparedStatement = (PreparedStatement ) this.statement; 250 251 if (preparedStatement.execute()) { 252 fetch.add(preparedStatement.getResultSet()); 253 } 254 break; 255 256 case STATEMENT_CALLABLE: 257 final CallableStatement callableStatement = (CallableStatement ) this.statement; 258 259 if (callableStatement.execute()) { 260 fetch.add(callableStatement.getResultSet()); 261 } 262 263 fetch.add(callableStatement, (Procedure) sql, params); 264 break; 265 266 default : 267 throw zxJDBC.makeException(zxJDBC.ProgrammingError, zxJDBC.getString("invalidStyle")); 268 } 269 } 270 271 279 private void prepare(PyCursor cursor, PyObject params, PyObject bindings) throws SQLException { 280 281 if ((params == Py.None) || (this.style == STATEMENT_STATIC)) { 282 return; 283 } 284 285 final DataHandler datahandler = cursor.datahandler; 287 int columns = 0, column = 0, index = params.__len__(); 288 final PreparedStatement preparedStatement = (PreparedStatement ) statement; 289 final Procedure procedure = (this.style == STATEMENT_CALLABLE) ? (Procedure) this.sql : null; 290 291 if (this.style != STATEMENT_CALLABLE) { 292 columns = params.__len__(); 293 294 preparedStatement.clearParameters(); 298 } else { 299 columns = (procedure.columns == Py.None) ? 0 : procedure.columns.__len__(); 300 } 301 302 while (columns-- > 0) { 304 column = columns + 1; 305 306 if ((procedure != null) && (!procedure.isInput(column))) { 307 continue; 308 } 309 310 PyObject param = params.__getitem__(--index); 312 313 if (bindings != Py.None) { 314 PyObject binding = bindings.__finditem__(Py.newInteger(index)); 315 316 if (binding != null) { 317 try { 318 int bindingValue = ((PyInteger)binding.__int__()).getValue(); 319 320 datahandler.setJDBCObject(preparedStatement, column, param, bindingValue); 321 } catch (PyException e) { 322 throw zxJDBC.makeException(zxJDBC.ProgrammingError, zxJDBC.getString("bindingValue")); 323 } 324 325 continue; 326 } 327 } 328 329 datahandler.setJDBCObject(preparedStatement, column, param); 330 } 331 332 return; 333 } 334 335 338 public void close() { 339 340 try { 341 this.statement.close(); 342 } catch (SQLException e) { 343 throw zxJDBC.makeException(e); 344 } finally { 345 this.closed = true; 346 } 347 } 348 } 349 | Popular Tags |