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,29 @@ 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
+ }
990
+ #endif
991
+
972
992
gilstate = PyGILState_Ensure ();
973
993
py_statement = PyUnicode_DecodeUTF8 (statement_string ,
974
994
strlen (statement_string ), "replace" );
@@ -988,6 +1008,9 @@ static void _trace_callback(void* user_arg, const char* statement_string)
988
1008
}
989
1009
990
1010
PyGILState_Release (gilstate );
1011
+ #ifdef HAVE_TRACE_V2
1012
+ return 0 ;
1013
+ #endif
991
1014
}
992
1015
993
1016
static PyObject * pysqlite_connection_set_authorizer (pysqlite_Connection * self , PyObject * args , PyObject * kwargs )
@@ -1046,6 +1069,11 @@ static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* s
1046
1069
Py_RETURN_NONE ;
1047
1070
}
1048
1071
1072
+ /*
1073
+ * Ref.
1074
+ * - https://sqlite.org/c3ref/c_trace.html
1075
+ * - https://sqlite.org/c3ref/trace_v2.html
1076
+ */
1049
1077
static PyObject * pysqlite_connection_set_trace_callback (pysqlite_Connection * self , PyObject * args , PyObject * kwargs )
1050
1078
{
1051
1079
PyObject * trace_callback ;
@@ -1063,10 +1091,18 @@ static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* sel
1063
1091
1064
1092
if (trace_callback == Py_None ) {
1065
1093
/* None clears the trace callback previously set */
1094
+ #ifdef HAVE_TRACE_V2
1095
+ sqlite3_trace_v2 (self -> db , SQLITE_TRACE_STMT , 0 , 0 );
1096
+ #else
1066
1097
sqlite3_trace (self -> db , 0 , (void * )0 );
1098
+ #endif
1067
1099
Py_XSETREF (self -> function_pinboard_trace_callback , NULL );
1068
1100
} else {
1101
+ #ifdef HAVE_TRACE_V2
1102
+ sqlite3_trace_v2 (self -> db , SQLITE_TRACE_STMT , _trace_callback , trace_callback );
1103
+ #else
1069
1104
sqlite3_trace (self -> db , _trace_callback , trace_callback );
1105
+ #endif
1070
1106
Py_INCREF (trace_callback );
1071
1107
Py_XSETREF (self -> function_pinboard_trace_callback , trace_callback );
1072
1108
}
0 commit comments