43
43
#define HAVE_BACKUP_API
44
44
#endif
45
45
46
+ #if SQLITE_VERSION_NUMBER >= 3014000
47
+ #define HAVE_TRACE_V2
48
+ #endif
49
+
46
50
_Py_IDENTIFIER (cursor );
47
51
48
52
static const char * const begin_statements [] = {
@@ -962,13 +966,28 @@ static int _progress_handler(void* user_arg)
962
966
return rc ;
963
967
}
964
968
969
+ #ifdef HAVE_TRACE_V2
970
+ /*
971
+ * From https://sqlite.org/c3ref/trace_v2.html:
972
+ * The integer return value from the callback is currently ignored, though this
973
+ * may change in future releases. Callback implementations should return zero
974
+ * to ensure future compatibility.
975
+ */
976
+ static int _trace_callback (unsigned int type , void * user_arg , void * prepared_statement , void * statement_string )
977
+ #else
965
978
static void _trace_callback (void * user_arg , const char * statement_string )
979
+ #endif
966
980
{
967
981
PyObject * py_statement = NULL ;
968
982
PyObject * ret = NULL ;
969
983
970
984
PyGILState_STATE gilstate ;
971
985
986
+ #ifdef HAVE_TRACE_V2
987
+ if (type != SQLITE_TRACE_STMT )
988
+ return 0 ;
989
+ #endif
990
+
972
991
gilstate = PyGILState_Ensure ();
973
992
py_statement = PyUnicode_DecodeUTF8 (statement_string ,
974
993
strlen (statement_string ), "replace" );
@@ -988,6 +1007,9 @@ static void _trace_callback(void* user_arg, const char* statement_string)
988
1007
}
989
1008
990
1009
PyGILState_Release (gilstate );
1010
+ #ifdef HAVE_TRACE_V2
1011
+ return 0 ;
1012
+ #endif
991
1013
}
992
1014
993
1015
static PyObject * pysqlite_connection_set_authorizer (pysqlite_Connection * self , PyObject * args , PyObject * kwargs )
@@ -1046,6 +1068,11 @@ static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* s
1046
1068
Py_RETURN_NONE ;
1047
1069
}
1048
1070
1071
+ /*
1072
+ * Ref.
1073
+ * - https://sqlite.org/c3ref/c_trace.html
1074
+ * - https://sqlite.org/c3ref/trace_v2.html
1075
+ */
1049
1076
static PyObject * pysqlite_connection_set_trace_callback (pysqlite_Connection * self , PyObject * args , PyObject * kwargs )
1050
1077
{
1051
1078
PyObject * trace_callback ;
@@ -1063,10 +1090,18 @@ static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* sel
1063
1090
1064
1091
if (trace_callback == Py_None ) {
1065
1092
/* None clears the trace callback previously set */
1093
+ #ifdef HAVE_TRACE_V2
1094
+ sqlite3_trace_v2 (self -> db , SQLITE_TRACE_STMT , 0 , 0 );
1095
+ #else
1066
1096
sqlite3_trace (self -> db , 0 , (void * )0 );
1097
+ #endif
1067
1098
Py_XSETREF (self -> function_pinboard_trace_callback , NULL );
1068
1099
} else {
1100
+ #ifdef HAVE_TRACE_V2
1101
+ sqlite3_trace_v2 (self -> db , SQLITE_TRACE_STMT , _trace_callback , trace_callback );
1102
+ #else
1069
1103
sqlite3_trace (self -> db , _trace_callback , trace_callback );
1104
+ #endif
1070
1105
Py_INCREF (trace_callback );
1071
1106
Py_XSETREF (self -> function_pinboard_trace_callback , trace_callback );
1072
1107
}
0 commit comments