@@ -220,4 +220,47 @@ describe('Statement#scanStatusV2()', function () {
220220 expect ( parentLoop . index ) . to . be . lessThan ( childLoop . index ) ;
221221 }
222222 } ) ;
223+
224+ it ( 'should reset scan status and allow collecting fresh statistics' , function ( ) {
225+ // Create an in-memory database for this test
226+ const memDb = new Database ( ':memory:' ) ;
227+
228+ // Seed the database with test data
229+ memDb . prepare ( 'CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT, value INTEGER)' ) . run ( ) ;
230+ const insertStmt = memDb . prepare ( 'INSERT INTO test_table (name, value) VALUES (?, ?)' ) ;
231+ for ( let i = 0 ; i < 100 ; i ++ ) {
232+ insertStmt . run ( `item_${ i } ` , i ) ;
233+ }
234+
235+ // Prepare a query
236+ const stmt = memDb . prepare ( 'SELECT * FROM test_table WHERE value > ? ORDER BY value' ) ;
237+
238+ // Run the query first time
239+ const rows1 = stmt . all ( 50 ) ;
240+ expect ( rows1 ) . to . have . lengthOf ( 49 ) ;
241+
242+ // Get scan status statistics after first run
243+ const nVisit1 = stmt . scanStatusV2 ( 0 , Database . SQLITE_SCANSTAT_NVISIT , 0 ) ;
244+ expect ( nVisit1 ) . to . be . a ( 'number' ) ;
245+ expect ( nVisit1 ) . to . be . greaterThan ( 0 ) ;
246+
247+ // Reset scan status
248+ const result = stmt . scanStatusReset ( ) ;
249+ expect ( result ) . to . equal ( stmt ) ; // Should return this for chaining
250+
251+ // Run the same query again
252+ const rows2 = stmt . all ( 50 ) ;
253+ expect ( rows2 ) . to . have . lengthOf ( 49 ) ;
254+
255+ // Get scan status statistics after second run (after reset)
256+ const nVisit2 = stmt . scanStatusV2 ( 0 , Database . SQLITE_SCANSTAT_NVISIT , 0 ) ;
257+ expect ( nVisit2 ) . to . be . a ( 'number' ) ;
258+ expect ( nVisit2 ) . to . be . greaterThan ( 0 ) ;
259+
260+ // After reset, the statistics should be fresh and equal to the first run
261+ // since we're running the same query with the same parameters
262+ expect ( nVisit2 ) . to . equal ( nVisit1 ) ;
263+
264+ memDb . close ( ) ;
265+ } ) ;
223266} ) ;
0 commit comments