@@ -1295,15 +1295,18 @@ private void ExecuteVacuum(string[] parts)
12951295 }
12961296
12971297 /// <summary>
1298- /// Executes UPDATE statement.
1298+ /// Executes UPDATE statement with RETURNING clause and change tracking support .
12991299 /// </summary>
13001300 private void ExecuteUpdate ( string sql , IWAL ? wal )
13011301 {
13021302 if ( isReadOnly )
13031303 throw new InvalidOperationException ( "Cannot update in readonly mode" ) ;
13041304
1305+ _lastChanges = 0 ;
1306+ var returningColumns = TryExtractReturningColumns ( sql , out var sqlWithoutReturning ) ;
1307+
13051308 // Parse UPDATE SQL: UPDATE table SET col=val WHERE condition
1306- var updateMatch = UpdateRegex . Match ( sql ) ;
1309+ var updateMatch = UpdateRegex . Match ( sqlWithoutReturning ) ;
13071310
13081311 if ( ! updateMatch . Success )
13091312 throw new InvalidOperationException ( $ "Invalid UPDATE syntax: { sql } ") ;
@@ -1335,20 +1338,36 @@ private void ExecuteUpdate(string sql, IWAL? wal)
13351338 }
13361339 }
13371340
1341+ // Count affected rows before update for change tracking
1342+ var affectedCount = table . Select ( whereClause , orderBy : null , asc : true , noEncrypt : false ) . Count ;
1343+
13381344 table . Update ( whereClause , updates ) ;
1339- wal ? . Log ( sql ) ;
1345+ _lastChanges = affectedCount ;
1346+ _totalChanges += affectedCount ;
1347+
1348+ // RETURNING: query updated rows after update
1349+ if ( returningColumns is not null )
1350+ {
1351+ var updatedRows = table . Select ( whereClause , orderBy : null , asc : true , noEncrypt : false ) ;
1352+ _pendingQueryResults = ProjectReturningRows ( updatedRows , returningColumns ) ;
1353+ }
1354+
1355+ wal ? . Log ( sqlWithoutReturning ) ;
13401356 }
13411357
13421358 /// <summary>
1343- /// Executes DELETE statement.
1359+ /// Executes DELETE statement with RETURNING clause and change tracking support .
13441360 /// </summary>
13451361 private void ExecuteDelete ( string sql , IWAL ? wal )
13461362 {
13471363 if ( isReadOnly )
13481364 throw new InvalidOperationException ( "Cannot delete in readonly mode" ) ;
13491365
1366+ _lastChanges = 0 ;
1367+ var returningColumns = TryExtractReturningColumns ( sql , out var sqlWithoutReturning ) ;
1368+
13501369 // Parse DELETE SQL: DELETE FROM table WHERE condition
1351- var deleteMatch = DeleteRegex . Match ( sql ) ;
1370+ var deleteMatch = DeleteRegex . Match ( sqlWithoutReturning ) ;
13521371
13531372 if ( ! deleteMatch . Success )
13541373 throw new InvalidOperationException ( $ "Invalid DELETE syntax: { sql } ") ;
@@ -1358,8 +1377,21 @@ private void ExecuteDelete(string sql, IWAL? wal)
13581377 throw new InvalidOperationException ( $ "Table { tableName } does not exist") ;
13591378
13601379 var whereClause = deleteMatch . Groups [ 2 ] . Value . Trim ( ) ;
1380+
1381+ // Capture rows before deletion for RETURNING and change tracking
1382+ var affectedRows = table . Select ( whereClause , orderBy : null , asc : true , noEncrypt : false ) ;
1383+ var affectedCount = affectedRows . Count ;
1384+
1385+ if ( returningColumns is not null )
1386+ {
1387+ _pendingQueryResults = ProjectReturningRows ( affectedRows , returningColumns ) ;
1388+ }
1389+
13611390 table . Delete ( whereClause ) ;
1362- wal ? . Log ( sql ) ;
1391+ _lastChanges = affectedCount ;
1392+ _totalChanges += affectedCount ;
1393+
1394+ wal ? . Log ( sqlWithoutReturning ) ;
13631395 }
13641396
13651397 /// <summary>
0 commit comments