From: brian z. <bz...@us...> - 2001-12-08 15:44:32
|
Update of /cvsroot/jython/jython/com/ziclix/python/sql In directory usw-pr-cvs1:/tmp/cvs-serv19856/com/ziclix/python/sql Modified Files: PyCursor.java Log Message: only prepare statements with params Index: PyCursor.java =================================================================== RCS file: /cvsroot/jython/jython/com/ziclix/python/sql/PyCursor.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** PyCursor.java 2001/12/07 04:03:51 1.6 --- PyCursor.java 2001/12/08 15:44:29 1.7 *************** *** 49,53 **** /** Field sqlStatement */ ! protected PreparedStatement sqlStatement; // they are stateless instances, so we only need to instantiate it once --- 49,53 ---- /** Field sqlStatement */ ! protected Statement sqlStatement; // they are stateless instances, so we only need to instantiate it once *************** *** 143,147 **** if ("arraysize".equals(name)) { ! arraysize = ((PyInteger)value).getValue(); } else if ("datahandler".equals(name)) { this.datahandler = (DataHandler)value.__tojava__(DataHandler.class); --- 143,147 ---- if ("arraysize".equals(name)) { ! arraysize = value.__int__().getValue(); } else if ("datahandler".equals(name)) { this.datahandler = (DataHandler)value.__tojava__(DataHandler.class); *************** *** 160,164 **** if ("arraysize".equals(name)) { ! return new PyInteger(arraysize); } else if ("__methods__".equals(name)) { return __methods__; --- 160,164 ---- if ("arraysize".equals(name)) { ! return Py.newInteger(arraysize); } else if ("__methods__".equals(name)) { return __methods__; *************** *** 168,172 **** return this.fetch.getDescription(); } else if ("rowcount".equals(name)) { ! return new PyInteger(this.fetch.getRowCount()); } else if ("warnings".equals(name)) { return warnings; --- 168,172 ---- return this.fetch.getDescription(); } else if ("rowcount".equals(name)) { ! return Py.newInteger(this.fetch.getRowCount()); } else if ("warnings".equals(name)) { return warnings; *************** *** 274,282 **** * @param sqlString * @param maxRows max number of rows to be returned * @throws SQLException */ ! protected void prepareStatement(String sqlString, PyObject maxRows) throws SQLException { ! this.sqlStatement = this.connection.prepareStatement(sqlString); if (maxRows != Py.None) { --- 274,287 ---- * @param sqlString * @param maxRows max number of rows to be returned + * @param prepared if true, prepare the statement, otherwise create a normal statement * @throws SQLException */ ! protected void prepareStatement(String sqlString, PyObject maxRows, boolean prepared) throws SQLException { ! if (prepared) { ! this.sqlStatement = this.connection.prepareStatement(sqlString); ! } else { ! this.sqlStatement = this.connection.createStatement(); ! } if (maxRows != Py.None) { *************** *** 363,369 **** clear(); try { ! prepareStatement(sqlString, maxRows); ! execute(params, bindings); } catch (PyException e) { throw e; --- 368,390 ---- clear(); + boolean hasParams = hasParams(params); + try { ! prepareStatement(sqlString, maxRows, hasParams); ! ! if (hasParams) { ! execute(params, bindings); ! } else { ! this.datahandler.preExecute(this.sqlStatement); ! ! if (this.sqlStatement.execute(sqlString)) { ! create(this.sqlStatement.getResultSet()); ! } ! ! this.rowid = this.datahandler.getRowId(this.sqlStatement); ! ! addWarning(this.sqlStatement.getWarnings()); ! this.datahandler.postExecute(this.sqlStatement); ! } } catch (PyException e) { throw e; *************** *** 374,381 **** /** ! * Method execute * ! * @param PyObject params ! * @param PyObject bindings * * @throws SQLException --- 395,402 ---- /** ! * The workhorse for properly executing a prepared statement. * ! * @param PyObject params a non-None seq of sequences or entities ! * @param PyObject bindings an optional dictionary of index:DBApiType mappings * * @throws SQLException *************** *** 384,402 **** protected void execute(PyObject params, PyObject bindings) throws SQLException { - boolean seq = isSeq(params); - - // the optional argument better be a sequence - if (!seq && (Py.None != params)) { - throw zxJDBC.makeException(zxJDBC.ProgrammingError, zxJDBC.getString("optionalSecond")); - } - - boolean hasParams = (seq && (params.__len__() > 0)); - // if we have a sequence of sequences, let's run through them and finish ! if (hasParams && isSeqSeq(params)) { ! for (int i = 0; i < params.__len__(); i++) { PyObject param = params.__getitem__(i); - // [(3, 4)] or [(3, 4), (5, 6)] execute(param, bindings); } --- 405,415 ---- protected void execute(PyObject params, PyObject bindings) throws SQLException { // if we have a sequence of sequences, let's run through them and finish ! if (isSeqSeq(params)) { ! ! // [(3, 4)] or [(3, 4), (5, 6)] ! for (int i = 0, len = params.__len__(); i < len; i++) { PyObject param = params.__getitem__(i); execute(param, bindings); } *************** *** 405,447 **** return; } - - if (hasParams) { ! // clear the statement so all new bindings take affect ! this.sqlStatement.clearParameters(); ! // [3, 4] or (3, 4) ! for (int i = 0; i < params.__len__(); i++) { ! PyObject index = Py.newInteger(i); ! PyObject param = params.__getitem__(i); ! if (bindings != Py.None) { ! PyObject binding = bindings.__finditem__(index); ! if (binding != null) { ! try { ! int bindingValue = binding.__int__().getValue(); ! this.datahandler.setJDBCObject(this.sqlStatement, i + 1, param, bindingValue); ! continue; ! } catch (PyException e) { ! throw zxJDBC.makeException(zxJDBC.ProgrammingError, zxJDBC.getString("bindingValue")); ! } } - } ! this.datahandler.setJDBCObject(this.sqlStatement, i + 1, param); } } ! this.datahandler.preExecute(this.sqlStatement); ! this.sqlStatement.execute(); ! this.rowid = this.datahandler.getRowId(this.sqlStatement); ! create(this.sqlStatement.getResultSet()); ! this.datahandler.postExecute(this.sqlStatement); ! addWarning(this.sqlStatement.getWarnings()); return; --- 418,460 ---- return; } ! // [3, 4] or (3, 4) ! PreparedStatement preparedStatement = (PreparedStatement)this.sqlStatement; ! // clear the statement so all new bindings take affect ! preparedStatement.clearParameters(); ! for (int i = 0, len = params.__len__(); i < len; i++) { ! PyObject param = params.__getitem__(i); ! if (bindings != Py.None) { ! PyObject binding = bindings.__finditem__(Py.newInteger(i)); ! if (binding != null) { ! int bindingValue = 0; ! try { ! bindingValue = binding.__int__().getValue(); ! } catch (PyException e) { ! throw zxJDBC.makeException(zxJDBC.ProgrammingError, zxJDBC.getString("bindingValue")); } ! this.datahandler.setJDBCObject(preparedStatement, i + 1, param, bindingValue); ! ! continue; ! } } + + this.datahandler.setJDBCObject(preparedStatement, i + 1, param); } ! this.datahandler.preExecute(preparedStatement); ! preparedStatement.execute(); ! create(preparedStatement.getResultSet()); ! this.rowid = this.datahandler.getRowId(preparedStatement); ! addWarning(preparedStatement.getWarnings()); ! this.datahandler.postExecute(preparedStatement); return; *************** *** 587,591 **** --- 600,609 ---- /** + * Method isSeq + * + * @param PyObject object + * * @return true for any PyList, PyTuple or java.util.List + * */ protected boolean isSeq(PyObject object) { *************** *** 605,609 **** /** ! * @return true for any PyList, PyTuple or java.util.List of PyLists, PyTuples or java.util.Lists */ protected boolean isSeqSeq(PyObject object) { --- 623,656 ---- /** ! * Method hasParams ! * ! * @param PyObject params ! * ! * @return boolean ! * ! */ ! protected boolean hasParams(PyObject params) { ! ! if (Py.None == params) { ! return false; ! } ! ! boolean isSeq = isSeq(params); ! ! // the optional argument better be a sequence ! if (!isSeq) { ! throw zxJDBC.makeException(zxJDBC.ProgrammingError, zxJDBC.getString("optionalSecond")); ! } ! ! return params.__len__() > 0; ! } ! ! /** ! * Method isSeqSeq ! * ! * @param PyObject object ! * ! * @return true is a sequence of sequences ! * */ protected boolean isSeqSeq(PyObject object) { |