Count the statements run whose SQL matches `pattern`, through the adapter.
(db: SqliteDatabase, pattern: RegExp)
| 33 | |
| 34 | /** Count the statements run whose SQL matches `pattern`, through the adapter. */ |
| 35 | function countStatements(db: SqliteDatabase, pattern: RegExp): { count: number } { |
| 36 | const counter = { count: 0 }; |
| 37 | const prepare = db.prepare.bind(db); |
| 38 | vi.spyOn(db, 'prepare').mockImplementation((sql: string) => { |
| 39 | const stmt = prepare(sql); |
| 40 | if (!pattern.test(sql)) return stmt; |
| 41 | const wrap = <T extends (...a: any[]) => any>(fn: T): T => ((...args: any[]) => { counter.count++; return fn(...args); }) as T; |
| 42 | return { ...stmt, all: wrap(stmt.all.bind(stmt)), get: wrap(stmt.get.bind(stmt)), iterate: wrap(stmt.iterate.bind(stmt)), run: stmt.run.bind(stmt) }; |
| 43 | }); |
| 44 | return counter; |
| 45 | } |
| 46 | |
| 47 | const DEF = ['class', 'interface', 'struct', 'union', 'trait', 'protocol', 'enum', 'type_alias']; |
| 48 | const CALL = ['function', 'method', 'component']; |
no test coverage detected