1+ import { cachedStmt } from './cached-stmt.js' ;
2+
13// ─── Statement caches (one prepared statement per db instance) ────────────
2- // WeakMap keys on the db object so statements are GC'd when the db closes.
34const _getCfgBlocksStmt = new WeakMap ( ) ;
45const _getCfgEdgesStmt = new WeakMap ( ) ;
56const _deleteCfgEdgesStmt = new WeakMap ( ) ;
@@ -26,16 +27,13 @@ export function hasCfgTables(db) {
2627 * @returns {object[] }
2728 */
2829export function getCfgBlocks ( db , functionNodeId ) {
29- let stmt = _getCfgBlocksStmt . get ( db ) ;
30- if ( ! stmt ) {
31- stmt = db . prepare (
32- `SELECT id, block_index, block_type, start_line, end_line, label
33- FROM cfg_blocks WHERE function_node_id = ?
34- ORDER BY block_index` ,
35- ) ;
36- _getCfgBlocksStmt . set ( db , stmt ) ;
37- }
38- return stmt . all ( functionNodeId ) ;
30+ return cachedStmt (
31+ _getCfgBlocksStmt ,
32+ db ,
33+ `SELECT id, block_index, block_type, start_line, end_line, label
34+ FROM cfg_blocks WHERE function_node_id = ?
35+ ORDER BY block_index` ,
36+ ) . all ( functionNodeId ) ;
3937}
4038
4139/**
@@ -45,21 +43,18 @@ export function getCfgBlocks(db, functionNodeId) {
4543 * @returns {object[] }
4644 */
4745export function getCfgEdges ( db , functionNodeId ) {
48- let stmt = _getCfgEdgesStmt . get ( db ) ;
49- if ( ! stmt ) {
50- stmt = db . prepare (
51- `SELECT e.kind,
52- sb.block_index AS source_index, sb.block_type AS source_type,
53- tb.block_index AS target_index, tb.block_type AS target_type
54- FROM cfg_edges e
55- JOIN cfg_blocks sb ON e.source_block_id = sb.id
56- JOIN cfg_blocks tb ON e.target_block_id = tb.id
57- WHERE e.function_node_id = ?
58- ORDER BY sb.block_index, tb.block_index` ,
59- ) ;
60- _getCfgEdgesStmt . set ( db , stmt ) ;
61- }
62- return stmt . all ( functionNodeId ) ;
46+ return cachedStmt (
47+ _getCfgEdgesStmt ,
48+ db ,
49+ `SELECT e.kind,
50+ sb.block_index AS source_index, sb.block_type AS source_type,
51+ tb.block_index AS target_index, tb.block_type AS target_type
52+ FROM cfg_edges e
53+ JOIN cfg_blocks sb ON e.source_block_id = sb.id
54+ JOIN cfg_blocks tb ON e.target_block_id = tb.id
55+ WHERE e.function_node_id = ?
56+ ORDER BY sb.block_index, tb.block_index` ,
57+ ) . all ( functionNodeId ) ;
6358}
6459
6560/**
@@ -68,16 +63,10 @@ export function getCfgEdges(db, functionNodeId) {
6863 * @param {number } functionNodeId
6964 */
7065export function deleteCfgForNode ( db , functionNodeId ) {
71- let delEdges = _deleteCfgEdgesStmt . get ( db ) ;
72- if ( ! delEdges ) {
73- delEdges = db . prepare ( 'DELETE FROM cfg_edges WHERE function_node_id = ?' ) ;
74- _deleteCfgEdgesStmt . set ( db , delEdges ) ;
75- }
76- let delBlocks = _deleteCfgBlocksStmt . get ( db ) ;
77- if ( ! delBlocks ) {
78- delBlocks = db . prepare ( 'DELETE FROM cfg_blocks WHERE function_node_id = ?' ) ;
79- _deleteCfgBlocksStmt . set ( db , delBlocks ) ;
80- }
81- delEdges . run ( functionNodeId ) ;
82- delBlocks . run ( functionNodeId ) ;
66+ cachedStmt ( _deleteCfgEdgesStmt , db , 'DELETE FROM cfg_edges WHERE function_node_id = ?' ) . run (
67+ functionNodeId ,
68+ ) ;
69+ cachedStmt ( _deleteCfgBlocksStmt , db , 'DELETE FROM cfg_blocks WHERE function_node_id = ?' ) . run (
70+ functionNodeId ,
71+ ) ;
8372}
0 commit comments