@@ -904,6 +904,38 @@ static int _progress_handler(void* user_arg)
904904 return rc ;
905905}
906906
907+ static void _trace_callback (void * user_arg , const char * statement_string )
908+ {
909+ PyObject * py_statement = NULL ;
910+ PyObject * ret = NULL ;
911+
912+ #ifdef WITH_THREAD
913+ PyGILState_STATE gilstate ;
914+
915+ gilstate = PyGILState_Ensure ();
916+ #endif
917+ py_statement = PyUnicode_DecodeUTF8 (statement_string ,
918+ strlen (statement_string ), "replace" );
919+ if (py_statement ) {
920+ ret = PyObject_CallFunctionObjArgs ((PyObject * )user_arg , py_statement , NULL );
921+ Py_DECREF (py_statement );
922+ }
923+
924+ if (ret ) {
925+ Py_DECREF (ret );
926+ } else {
927+ if (_enable_callback_tracebacks ) {
928+ PyErr_Print ();
929+ } else {
930+ PyErr_Clear ();
931+ }
932+ }
933+
934+ #ifdef WITH_THREAD
935+ PyGILState_Release (gilstate );
936+ #endif
937+ }
938+
907939static PyObject * pysqlite_connection_set_authorizer (pysqlite_Connection * self , PyObject * args , PyObject * kwargs )
908940{
909941 PyObject * authorizer_cb ;
@@ -963,6 +995,34 @@ static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* s
963995 return Py_None ;
964996}
965997
998+ static PyObject * pysqlite_connection_set_trace_callback (pysqlite_Connection * self , PyObject * args , PyObject * kwargs )
999+ {
1000+ PyObject * trace_callback ;
1001+
1002+ static char * kwlist [] = { "trace_callback" , NULL };
1003+
1004+ if (!pysqlite_check_thread (self ) || !pysqlite_check_connection (self )) {
1005+ return NULL ;
1006+ }
1007+
1008+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "O:set_trace_callback" ,
1009+ kwlist , & trace_callback )) {
1010+ return NULL ;
1011+ }
1012+
1013+ if (trace_callback == Py_None ) {
1014+ /* None clears the trace callback previously set */
1015+ sqlite3_trace (self -> db , 0 , (void * )0 );
1016+ } else {
1017+ if (PyDict_SetItem (self -> function_pinboard , trace_callback , Py_None ) == -1 )
1018+ return NULL ;
1019+ sqlite3_trace (self -> db , _trace_callback , trace_callback );
1020+ }
1021+
1022+ Py_INCREF (Py_None );
1023+ return Py_None ;
1024+ }
1025+
9661026#ifdef HAVE_LOAD_EXTENSION
9671027static PyObject * pysqlite_enable_load_extension (pysqlite_Connection * self , PyObject * args )
9681028{
@@ -1516,6 +1576,8 @@ static PyMethodDef connection_methods[] = {
15161576 #endif
15171577 {"set_progress_handler" , (PyCFunction )pysqlite_connection_set_progress_handler , METH_VARARGS |METH_KEYWORDS ,
15181578 PyDoc_STR ("Sets progress handler callback. Non-standard." )},
1579+ {"set_trace_callback" , (PyCFunction )pysqlite_connection_set_trace_callback , METH_VARARGS |METH_KEYWORDS ,
1580+ PyDoc_STR ("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard." )},
15191581 {"execute" , (PyCFunction )pysqlite_connection_execute , METH_VARARGS ,
15201582 PyDoc_STR ("Executes a SQL statement. Non-standard." )},
15211583 {"executemany" , (PyCFunction )pysqlite_connection_executemany , METH_VARARGS ,
0 commit comments