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