2424#include "cursor.h"
2525#include "module.h"
2626#include "util.h"
27- #include "microprotocols.h"
28- #include "prepare_protocol.h"
2927
3028/* used to decide wether to call PyInt_FromLong or PyLong_FromLongLong */
3129#define INT32_MIN (-2147483647 - 1)
@@ -189,53 +187,6 @@ void build_row_cast_map(Cursor* self)
189187 }
190188}
191189
192- int _bind_parameter (Cursor * self , int pos , PyObject * parameter )
193- {
194- int rc = SQLITE_OK ;
195- long longval ;
196- #ifdef HAVE_LONG_LONG
197- PY_LONG_LONG longlongval ;
198- #endif
199- const char * buffer ;
200- char * string ;
201- int buflen ;
202- PyObject * stringval ;
203-
204- if (parameter == Py_None ) {
205- rc = sqlite3_bind_null (self -> statement -> st , pos );
206- } else if (PyInt_Check (parameter )) {
207- longval = PyInt_AsLong (parameter );
208- rc = sqlite3_bind_int64 (self -> statement -> st , pos , (sqlite_int64 )longval );
209- #ifdef HAVE_LONG_LONG
210- } else if (PyLong_Check (parameter )) {
211- longlongval = PyLong_AsLongLong (parameter );
212- /* in the overflow error case, longlongval is -1, and an exception is set */
213- rc = sqlite3_bind_int64 (self -> statement -> st , pos , (sqlite_int64 )longlongval );
214- #endif
215- } else if (PyFloat_Check (parameter )) {
216- rc = sqlite3_bind_double (self -> statement -> st , pos , PyFloat_AsDouble (parameter ));
217- } else if (PyBuffer_Check (parameter )) {
218- if (PyObject_AsCharBuffer (parameter , & buffer , & buflen ) == 0 ) {
219- rc = sqlite3_bind_blob (self -> statement -> st , pos , buffer , buflen , SQLITE_TRANSIENT );
220- } else {
221- PyErr_SetString (PyExc_ValueError , "could not convert BLOB to buffer" );
222- rc = -1 ;
223- }
224- } else if PyString_Check (parameter ) {
225- string = PyString_AsString (parameter );
226- rc = sqlite3_bind_text (self -> statement -> st , pos , string , -1 , SQLITE_TRANSIENT );
227- } else if PyUnicode_Check (parameter ) {
228- stringval = PyUnicode_AsUTF8String (parameter );
229- string = PyString_AsString (stringval );
230- rc = sqlite3_bind_text (self -> statement -> st , pos , string , -1 , SQLITE_TRANSIENT );
231- Py_DECREF (stringval );
232- } else {
233- rc = -1 ;
234- }
235-
236- return rc ;
237- }
238-
239190PyObject * _build_column_name (const char * colname )
240191{
241192 const char * pos ;
@@ -394,7 +345,6 @@ PyObject* _query_execute(Cursor* self, int multiple, PyObject* args)
394345 PyObject * parameters_list = NULL ;
395346 PyObject * parameters_iter = NULL ;
396347 PyObject * parameters = NULL ;
397- int num_params ;
398348 int i ;
399349 int rc ;
400350 PyObject * func_args ;
@@ -403,11 +353,7 @@ PyObject* _query_execute(Cursor* self, int multiple, PyObject* args)
403353 PY_LONG_LONG lastrowid ;
404354 int statement_type ;
405355 PyObject * descriptor ;
406- PyObject * current_param ;
407- PyObject * adapted ;
408356 PyObject * second_argument = NULL ;
409- int num_params_needed ;
410- const char * binding_name ;
411357 long rowcount = 0 ;
412358
413359 if (!check_thread (self -> connection ) || !check_connection (self -> connection )) {
@@ -557,10 +503,6 @@ PyObject* _query_execute(Cursor* self, int multiple, PyObject* args)
557503 statement_reset (self -> statement );
558504 statement_mark_dirty (self -> statement );
559505
560- Py_BEGIN_ALLOW_THREADS
561- num_params_needed = sqlite3_bind_parameter_count (self -> statement -> st );
562- Py_END_ALLOW_THREADS
563-
564506 while (1 ) {
565507 parameters = PyIter_Next (parameters_iter );
566508 if (!parameters ) {
@@ -569,71 +511,9 @@ PyObject* _query_execute(Cursor* self, int multiple, PyObject* args)
569511
570512 statement_mark_dirty (self -> statement );
571513
572- if (PyDict_Check (parameters )) {
573- /* parameters passed as dictionary */
574- for (i = 1 ; i <= num_params_needed ; i ++ ) {
575- Py_BEGIN_ALLOW_THREADS
576- binding_name = sqlite3_bind_parameter_name (self -> statement -> st , i );
577- Py_END_ALLOW_THREADS
578- if (!binding_name ) {
579- PyErr_Format (ProgrammingError , "Binding %d has no name, but you supplied a dictionary (which has only names)." , i );
580- goto error ;
581- }
582-
583- binding_name ++ ; /* skip first char (the colon) */
584- current_param = PyDict_GetItemString (parameters , binding_name );
585- if (!current_param ) {
586- PyErr_Format (ProgrammingError , "You did not supply a value for binding %d." , i );
587- goto error ;
588- }
589-
590- Py_INCREF (current_param );
591- adapted = microprotocols_adapt (current_param , (PyObject * )& SQLitePrepareProtocolType , NULL );
592- if (adapted ) {
593- Py_DECREF (current_param );
594- } else {
595- PyErr_Clear ();
596- adapted = current_param ;
597- }
598-
599- rc = _bind_parameter (self , i , adapted );
600- Py_DECREF (adapted );
601-
602- if (rc != SQLITE_OK ) {
603- PyErr_Format (InterfaceError , "Error binding parameter :%s - probably unsupported type." , binding_name );
604- goto error ;
605- }
606- }
607- } else {
608- /* parameters passed as sequence */
609- num_params = PySequence_Length (parameters );
610- if (num_params != num_params_needed ) {
611- PyErr_Format (ProgrammingError , "Incorrect number of bindings supplied. The current statement uses %d, and there are %d supplied." ,
612- num_params_needed , num_params );
613- goto error ;
614- }
615- for (i = 0 ; i < num_params ; i ++ ) {
616- current_param = PySequence_GetItem (parameters , i );
617- if (!current_param ) {
618- goto error ;
619- }
620- adapted = microprotocols_adapt (current_param , (PyObject * )& SQLitePrepareProtocolType , NULL );
621-
622- if (adapted ) {
623- Py_DECREF (current_param );
624- } else {
625- PyErr_Clear ();
626- adapted = current_param ;
627- }
628-
629- rc = _bind_parameter (self , i + 1 , adapted );
630- Py_DECREF (adapted );
631-
632- if (rc != SQLITE_OK ) {
633- PyErr_Format (InterfaceError , "Error binding parameter %d - probably unsupported type." , i );
634- goto error ;
635- }
636- }
514+ statement_bind_parameters (self -> statement , parameters );
515+ if (PyErr_Occurred ()) {
516+ goto error ;
637517 }
638518
639519 build_row_cast_map (self );
@@ -642,7 +522,7 @@ PyObject* _query_execute(Cursor* self, int multiple, PyObject* args)
642522 if (rc != SQLITE_DONE && rc != SQLITE_ROW ) {
643523 rc = statement_reset (self -> statement );
644524 if (rc == SQLITE_SCHEMA ) {
645- rc = statement_recompile (self -> statement );
525+ rc = statement_recompile (self -> statement , parameters );
646526 if (rc == SQLITE_OK ) {
647527 rc = _sqlite_step_with_busyhandler (self -> statement -> st , self -> connection );
648528 } else {
0 commit comments