From f624bf199839b9643a1af11c86e2621896f2805c Mon Sep 17 00:00:00 2001 From: Phil Krylov Date: Thu, 16 Nov 2023 12:51:15 +0100 Subject: [PATCH] fixed SQLITE_TRACE_CLOSE processing, thanks to @snaiperis --- contrib/hbsqlit3/core.c | 13 ++++++++++--- contrib/hbsqlit3/tests/backup.prg | 19 +++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/contrib/hbsqlit3/core.c b/contrib/hbsqlit3/core.c index 2aee282a42..9f72c91bd9 100644 --- a/contrib/hbsqlit3/core.c +++ b/contrib/hbsqlit3/core.c @@ -1832,19 +1832,26 @@ static int trace_handler( unsigned uType, void *cbTraceHandler, void * p, void * hb_vmSend( 3 ); break; case SQLITE_TRACE_ROW: - { HB_SYMBOL_UNUSED( x ); hb_vmPushPointer( p ); hb_vmSend( 2 ); break; - } case SQLITE_TRACE_CLOSE: { PHB_ITEM pItem = hb_itemNew( NULL ); - hb_sqlite3_itemPut( pItem, p, HB_SQLITE3_DB ); + HB_SQLITE3 * hbsqlite3 = ( HB_SQLITE3 * ) hb_xgrabz( sizeof( HB_SQLITE3 ) ); HB_SYMBOL_UNUSED( x ); + + hbsqlite3->db = p; + hb_sqlite3_itemPut( pItem, hbsqlite3, HB_SQLITE3_DB ); hb_vmPush( pItem ); hb_vmSend( 2 ); + + /* We don't want sqlite3_close() called recursively + * and don't want to implement a weak reference engine yet + * so we just clear the pointer before hb_itemRelease(). */ + hbsqlite3->db = NULL; + hb_itemRelease( pItem ); break; } } diff --git a/contrib/hbsqlit3/tests/backup.prg b/contrib/hbsqlit3/tests/backup.prg index 1f0e01c67b..90a17d0edd 100644 --- a/contrib/hbsqlit3/tests/backup.prg +++ b/contrib/hbsqlit3/tests/backup.prg @@ -63,19 +63,22 @@ #include "fileio.ch" -PROCEDURE init_trace( pDbDest, cPrefix ) +PROCEDURE init_trace( pDb, cPrefix ) LOCAL hFile IF sqlite3_libversion_number() < 3014000 - sqlite3_trace( pDbDest, .T., cPrefix + ".log" ) + sqlite3_trace( pDb, .T., cPrefix + ".log" ) ELSE hFile := FOpen( cPrefix + ".log", FO_READWRITE + HB_FO_CREAT ) FSeek( hFile, 0, FS_END ) - sqlite3_trace_v2( pDbDest, SQLITE_TRACE_STMT, {| nMask, pPreparedStatement, cOriginalSql | - HB_SYMBOL_UNUSED( nMask ) - IF hb_LeftEq( cOriginalSql, "--" ) - FWrite( hFile, cOriginalSql + hb_eol() ) - ELSE - FWrite( hFile, sqlite3_expanded_sql( pPreparedStatement ) + hb_eol() ) + sqlite3_trace_v2( pDb, SQLITE_TRACE_STMT + SQLITE_TRACE_CLOSE, {| nMask, p, x | + IF nMask == SQLITE_TRACE_STMT /* p is pPreparedStatement, x is cOriginalSql */ + IF hb_LeftEq( x, "--" ) + FWrite( hFile, x + hb_eol() ) + ELSE + FWrite( hFile, sqlite3_expanded_sql( p ) + hb_eol() ) + ENDIF + ELSEIF nMask == SQLITE_TRACE_CLOSE /* p is the database connection */ + FWrite( hFile, "Closing the database connection" + hb_eol() ) ENDIF RETURN 0 } )