import type { Node } from '@pgsql/types'; import type { NodeSpec } from './18/runtime-schema'; import { runtimeSchema } from './18/runtime-schema'; const schemaMap = new Map(runtimeSchema.map((spec: NodeSpec) => [spec.name, spec])); export type NodeTag = keyof Node; export class NodePath { constructor( public tag: TTag, public node: any, public parent: NodePath | null = null, public keyPath: readonly (string | number)[] = [] ) {} get path(): (string | number)[] { return [...this.keyPath]; } get key(): string | number { return this.keyPath[this.keyPath.length - 1] ?? ''; } } export type Walker = ( path: TNodePath, ) => boolean | void; export type Visitor = { [key: string]: Walker; }; /** * Walks the tree of PostgreSQL AST nodes using runtime schema for precise traversal. * * This is the SQL-only primitive: it knows nothing about PL/pgSQL bodies or * statement context. Prefer `walk`, which dispatches over any AST shape and * threads a {@link WalkContext} through the visitors. * * If a callback returns `false`, the walk will continue to the next sibling * node, rather than recurse into the children of the current node. */ export function walkSqlAst( root: any, callback: Walker | Visitor, parent: NodePath | null = null, keyPath: readonly (string | number)[] = [], ): void { const actualCallback: Walker = typeof callback === 'function' ? callback : (path: NodePath) => { const visitor = callback as Visitor; const visitFn = visitor[path.tag]; return visitFn ? visitFn(path) : undefined; }; if (Array.isArray(root)) { root.forEach((node, index) => { walkSqlAst(node, actualCallback, parent, [...keyPath, index]); }); } else if (typeof root === 'object' && root !== null) { const keys = Object.keys(root); if (keys.length === 1 && /^[A-Z]/.test(keys[0])) { walkNode(keys[0], root[keys[0]], actualCallback, parent, keyPath); return; } if (parent === null && keyPath.length === 0) { const rootTag = detectUntaggedRootTag(root); if (rootTag) { walkNode(rootTag, root, actualCallback, parent, keyPath); return; } } for (const key of keys) { walkSqlAst(root[key], actualCallback, parent, [...keyPath, key]); } } } /** * libpg-query returns the top-level ParseResult/ScanResult as a bare object * (no `{ParseResult: {...}}` wrapper). Detect those shapes at the root so * their visitors — and typed descendants like RawStmt — are dispatched. */ function detectUntaggedRootTag(root: any): string | null { if (Array.isArray(root.stmts) && typeof root.version === 'number') { return 'ParseResult'; } if (Array.isArray(root.tokens) && typeof root.version === 'number') { return 'ScanResult'; } return null; } function isTaggedNode(value: any): boolean { if (typeof value !== 'object' || value === null || Array.isArray(value)) { return false; } const keys = Object.keys(value); return keys.length === 1 && /^[A-Z]/.test(keys[0]); } function walkNode( tag: string, nodeData: any, actualCallback: Walker, parent: NodePath | null, keyPath: readonly (string | number)[], ): void { if (typeof nodeData !== 'object' || nodeData === null) { return; } const path = new NodePath(tag, nodeData, parent, keyPath); if (actualCallback(path) === false) { return; } const nodeSpec = schemaMap.get(tag); if (nodeSpec) { for (const field of nodeSpec.fields) { // Check if field type is 'Node' or any other node type (e.g., 'WithClause', 'SelectStmt', etc.) const isNodeType = field.type === 'Node' || schemaMap.has(field.type); if (isNodeType && nodeData[field.name] != null) { const value = nodeData[field.name]; if (field.isArray && Array.isArray(value)) { value.forEach((item, index) => { walkFieldValue(item, field.type, actualCallback, path, [...path.keyPath, field.name, index]); }); } else if (!field.isArray) { walkFieldValue(value, field.type, actualCallback, path, [...path.keyPath, field.name]); } } } } else { for (const key in nodeData) { const value = nodeData[key]; if (Array.isArray(value)) { value.forEach((item, index) => { if (typeof item === 'object' && item !== null) { walkSqlAst(item, actualCallback, path, [...path.keyPath, key, index]); } }); } else if (typeof value === 'object' && value !== null) { walkSqlAst(value, actualCallback, path, [...path.keyPath, key]); } } } } function walkFieldValue( value: any, declaredType: string, actualCallback: Walker, parent: NodePath | null, keyPath: readonly (string | number)[], ): void { if (typeof value !== 'object' || value === null) { return; } // Concrete typed fields (e.g. CreatePolicyStmt.table: RangeVar) are stored // as bare untagged objects in libpg_query JSON — synthesize the tag from // the runtime schema so the visitor is dispatched for them. if ( declaredType !== 'Node' && schemaMap.has(declaredType) && !isTaggedNode(value) ) { walkNode(declaredType, value, actualCallback, parent, keyPath); } else { walkSqlAst(value, actualCallback, parent, keyPath); } }