@cleocode/nexus 2026.11.1-canary.7 → 2026.11.1-canary.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "schema": 2,
3
- "sourceHash": "36d8f56bffcf55109eb0940891868c155dc48a5bf8b31b2ce6445bb427881783"
3
+ "sourceHash": "07da4ff3db1c416639d36f00678f98310afbc2f9c2c83517b229caa1bdbd2897"
4
4
  }
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Bounded SQL extractor for the parse-loop pipeline (T14383).
3
+ *
4
+ * Reads SQL DDL text without a grammar: comments are blanked (offsets and line
5
+ * numbers are kept), statements are split on `;` outside string literals,
6
+ * quoted identifiers, dollar-quoted bodies and `BEGIN … END` / `CASE … END`
7
+ * blocks, and each statement is matched against the DDL forms below.
8
+ *
9
+ * - **Schema objects**: `CREATE TABLE`, `CREATE VIRTUAL TABLE` (e.g. FTS5),
10
+ * `CREATE VIEW`, `CREATE INDEX`. Node ids are `<path>::<sqlKind>:<name>`, so a
11
+ * trigger or index may share a table's name.
12
+ * - **Triggers**: `CREATE TRIGGER`, with the table it fires on and the tables
13
+ * its body writes or reads.
14
+ * - **Constraints**: `PRIMARY KEY`, `UNIQUE`, `CHECK` and `FOREIGN KEY` /
15
+ * `REFERENCES`, inline or table-level, recorded on the table node; a foreign
16
+ * key is also a reference to the table it names.
17
+ * - **Migrations**: `ALTER TABLE` (add/drop/rename column, rename table, add
18
+ * constraint) and `DROP TABLE|VIEW|INDEX|TRIGGER`, recorded as references
19
+ * from the file to the object it changes.
20
+ *
21
+ * The graph has no SQL node kinds yet, so SQL objects use the nearest existing
22
+ * kind and carry the exact SQL kind in `meta.sqlKind`: tables and views are
23
+ * `record`, indexes `property`, triggers `function`. SQL nodes are never
24
+ * registered in the symbol table, so no code call can resolve to them.
25
+ *
26
+ * An unterminated quote or block, or a statement no form matches, is recorded
27
+ * in `problems`; the parse loop then reports the file as partially read.
28
+ *
29
+ * Not extracted: DML statements, column-level types, routines (`CREATE
30
+ * FUNCTION|PROCEDURE`), literal SQL inside executable sources, and dynamic SQL.
31
+ *
32
+ * @task T14383
33
+ * @module pipeline/extractors/sql-extractor
34
+ */
35
+ import type { GraphNode, GraphRelation, GraphRelationType } from '@cleocode/contracts';
36
+ /** SQL object kinds the extractor emits as graph nodes. */
37
+ export type SqlObjectKind = 'table' | 'view' | 'index' | 'trigger';
38
+ /** One table constraint recorded on a table node's `meta.constraints`. */
39
+ export interface SqlConstraint {
40
+ /** Constraint form. */
41
+ type: 'primary-key' | 'unique' | 'check' | 'foreign-key';
42
+ /** Declared constraint name, when the SQL names it. */
43
+ name?: string;
44
+ /** Constrained columns (empty for a table-level CHECK). */
45
+ columns: string[];
46
+ /** Referenced table and columns of a foreign key. */
47
+ references?: {
48
+ table: string;
49
+ columns: string[];
50
+ };
51
+ }
52
+ /**
53
+ * A reference from an extracted node (or the file, for a migration statement)
54
+ * to a table or view named in SQL, resolved by {@link linkSqlReferences}.
55
+ */
56
+ export interface SqlTableReference {
57
+ /** Source node id, or the file path for a migration statement. */
58
+ source: string;
59
+ /** Referenced object name, lower-cased and unqualified. */
60
+ target: string;
61
+ /** Relation emitted once the target resolves. */
62
+ type: Extract<GraphRelationType, 'accesses' | 'queries'>;
63
+ /** Why the reference exists (e.g. `foreign key references`). */
64
+ reason: string;
65
+ }
66
+ /** Everything the extractor read from one SQL file. */
67
+ export interface SqlExtraction {
68
+ /** Repository-relative path of the SQL file. */
69
+ filePath: string;
70
+ /** Table, view, index and trigger nodes, first definition per name wins. */
71
+ nodes: GraphNode[];
72
+ /** References to tables and views, linked after every SQL file is read. */
73
+ references: SqlTableReference[];
74
+ /** `ALTER TABLE … RENAME TO` pairs (lower-cased old → new name). */
75
+ renames: {
76
+ from: string;
77
+ to: string;
78
+ }[];
79
+ /** Statements that are neither recognized DDL nor DML. */
80
+ unrecognizedStatements: number;
81
+ /**
82
+ * Why the extraction is partial: an unterminated quote or block (statement
83
+ * boundaries after it cannot be trusted) or unclassified statements. Empty
84
+ * when every statement was read.
85
+ */
86
+ problems: string[];
87
+ }
88
+ /** Result of linking every SQL file's references into the graph. */
89
+ export interface SqlLinkResult {
90
+ /** References resolved to at least one table or view definition. */
91
+ linked: number;
92
+ /** References whose target no indexed SQL file defines. */
93
+ unresolved: number;
94
+ }
95
+ /**
96
+ * Extract SQL schema objects, triggers, constraints and migration statements.
97
+ *
98
+ * @param filePath - Repository-relative path of the SQL file
99
+ * @param source - The file's text
100
+ * @returns Nodes for the file plus the table references to link across files
101
+ * @example
102
+ * ```ts
103
+ * const sql = extractSql('db/schema.sql', 'CREATE TABLE items (id INTEGER PRIMARY KEY);');
104
+ * sql.nodes[0]?.meta?.['sqlKind']; // 'table'
105
+ * ```
106
+ */
107
+ export declare function extractSql(filePath: string, source: string): SqlExtraction;
108
+ /**
109
+ * Link every SQL file's table references to the table and view nodes the
110
+ * indexed SQL files define. A definition in the referencing file wins; a name
111
+ * defined in exactly one other file links with import-scoped confidence; a
112
+ * name defined in several files links to each at ambiguous confidence.
113
+ * `ALTER TABLE … RENAME TO` makes the new name resolve to the renamed table.
114
+ *
115
+ * @param extractions - Every SQL file's extraction from this run
116
+ * @param graph - Graph that already holds the extracted nodes
117
+ * @returns How many references resolved and how many stayed unlinked
118
+ * @example
119
+ * ```ts
120
+ * const { linked, unresolved } = linkSqlReferences([extractSql('a.sql', sql)], graph);
121
+ * ```
122
+ */
123
+ export declare function linkSqlReferences(extractions: Iterable<SqlExtraction>, graph: {
124
+ addRelation(rel: GraphRelation): void;
125
+ }): SqlLinkResult;
126
+ //# sourceMappingURL=sql-extractor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sql-extractor.d.ts","sourceRoot":"","sources":["../../../../src/pipeline/extractors/sql-extractor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,KAAK,EACV,SAAS,EAET,aAAa,EACb,iBAAiB,EAClB,MAAM,qBAAqB,CAAC;AAG7B,2DAA2D;AAC3D,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC;AAEnE,0EAA0E;AAC1E,MAAM,WAAW,aAAa;IAC5B,uBAAuB;IACvB,IAAI,EAAE,aAAa,GAAG,QAAQ,GAAG,OAAO,GAAG,aAAa,CAAC;IACzD,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,2DAA2D;IAC3D,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,qDAAqD;IACrD,UAAU,CAAC,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;CACnD;AAED;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAChC,kEAAkE;IAClE,MAAM,EAAE,MAAM,CAAC;IACf,2DAA2D;IAC3D,MAAM,EAAE,MAAM,CAAC;IACf,iDAAiD;IACjD,IAAI,EAAE,OAAO,CAAC,iBAAiB,EAAE,UAAU,GAAG,SAAS,CAAC,CAAC;IACzD,gEAAgE;IAChE,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,uDAAuD;AACvD,MAAM,WAAW,aAAa;IAC5B,gDAAgD;IAChD,QAAQ,EAAE,MAAM,CAAC;IACjB,4EAA4E;IAC5E,KAAK,EAAE,SAAS,EAAE,CAAC;IACnB,2EAA2E;IAC3E,UAAU,EAAE,iBAAiB,EAAE,CAAC;IAChC,oEAAoE;IACpE,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IACxC,0DAA0D;IAC1D,sBAAsB,EAAE,MAAM,CAAC;IAC/B;;;;OAIG;IACH,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,oEAAoE;AACpE,MAAM,WAAW,aAAa;IAC5B,oEAAoE;IACpE,MAAM,EAAE,MAAM,CAAC;IACf,2DAA2D;IAC3D,UAAU,EAAE,MAAM,CAAC;CACpB;AAuVD;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,aAAa,CAoI1E;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,iBAAiB,CAC/B,WAAW,EAAE,QAAQ,CAAC,aAAa,CAAC,EACpC,KAAK,EAAE;IAAE,WAAW,CAAC,GAAG,EAAE,aAAa,GAAG,IAAI,CAAA;CAAE,GAC/C,aAAa,CAyCf"}
@@ -0,0 +1,547 @@
1
+ /**
2
+ * Bounded SQL extractor for the parse-loop pipeline (T14383).
3
+ *
4
+ * Reads SQL DDL text without a grammar: comments are blanked (offsets and line
5
+ * numbers are kept), statements are split on `;` outside string literals,
6
+ * quoted identifiers, dollar-quoted bodies and `BEGIN … END` / `CASE … END`
7
+ * blocks, and each statement is matched against the DDL forms below.
8
+ *
9
+ * - **Schema objects**: `CREATE TABLE`, `CREATE VIRTUAL TABLE` (e.g. FTS5),
10
+ * `CREATE VIEW`, `CREATE INDEX`. Node ids are `<path>::<sqlKind>:<name>`, so a
11
+ * trigger or index may share a table's name.
12
+ * - **Triggers**: `CREATE TRIGGER`, with the table it fires on and the tables
13
+ * its body writes or reads.
14
+ * - **Constraints**: `PRIMARY KEY`, `UNIQUE`, `CHECK` and `FOREIGN KEY` /
15
+ * `REFERENCES`, inline or table-level, recorded on the table node; a foreign
16
+ * key is also a reference to the table it names.
17
+ * - **Migrations**: `ALTER TABLE` (add/drop/rename column, rename table, add
18
+ * constraint) and `DROP TABLE|VIEW|INDEX|TRIGGER`, recorded as references
19
+ * from the file to the object it changes.
20
+ *
21
+ * The graph has no SQL node kinds yet, so SQL objects use the nearest existing
22
+ * kind and carry the exact SQL kind in `meta.sqlKind`: tables and views are
23
+ * `record`, indexes `property`, triggers `function`. SQL nodes are never
24
+ * registered in the symbol table, so no code call can resolve to them.
25
+ *
26
+ * An unterminated quote or block, or a statement no form matches, is recorded
27
+ * in `problems`; the parse loop then reports the file as partially read.
28
+ *
29
+ * Not extracted: DML statements, column-level types, routines (`CREATE
30
+ * FUNCTION|PROCEDURE`), literal SQL inside executable sources, and dynamic SQL.
31
+ *
32
+ * @task T14383
33
+ * @module pipeline/extractors/sql-extractor
34
+ */
35
+ import { confidenceLabelFromNumeric } from '@cleocode/contracts/graph.js';
36
+ const NODE_KIND = {
37
+ table: 'record',
38
+ view: 'record',
39
+ index: 'property',
40
+ trigger: 'function',
41
+ };
42
+ const NAME = String.raw `(?:"(?:[^"]|"")+"|\x60[^\x60]+\x60|\[[^\]]+\]|[A-Za-z_][\w$]*)`;
43
+ const QUALIFIED = String.raw `${NAME}(?:\s*\.\s*${NAME})?`;
44
+ const IF_NOT_EXISTS = String.raw `(?:IF\s+NOT\s+EXISTS\s+)?`;
45
+ const CREATE_TABLE = new RegExp(String.raw `^CREATE\s+(?:OR\s+REPLACE\s+)?(?:(?:GLOBAL|LOCAL)\s+)?(?:TEMP(?:ORARY)?\s+|UNLOGGED\s+)?TABLE\s+${IF_NOT_EXISTS}(${QUALIFIED})\s*([\s\S]*)$`, 'i');
46
+ const CREATE_VIRTUAL_TABLE = new RegExp(String.raw `^CREATE\s+VIRTUAL\s+TABLE\s+${IF_NOT_EXISTS}(${QUALIFIED})\s+USING\s+(\w+)\s*([\s\S]*)$`, 'i');
47
+ const CREATE_VIEW = new RegExp(String.raw `^CREATE\s+(?:OR\s+REPLACE\s+)?(?:TEMP(?:ORARY)?\s+)?(?:MATERIALIZED\s+)?VIEW\s+${IF_NOT_EXISTS}(${QUALIFIED})[\s\S]*?\bAS\b([\s\S]*)$`, 'i');
48
+ const CREATE_INDEX = new RegExp(String.raw `^CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:CONCURRENTLY\s+)?${IF_NOT_EXISTS}(${QUALIFIED})\s+ON\s+(?:ONLY\s+)?(${QUALIFIED})\s*(?:USING\s+\w+\s*)?\(([\s\S]*?)\)`, 'i');
49
+ const CREATE_TRIGGER = new RegExp(String.raw `^CREATE\s+(?:OR\s+REPLACE\s+)?(?:TEMP(?:ORARY)?\s+)?(?:CONSTRAINT\s+)?TRIGGER\s+${IF_NOT_EXISTS}(${QUALIFIED})([\s\S]*?)\bON\s+(${QUALIFIED})([\s\S]*)$`, 'i');
50
+ const ALTER_TABLE = new RegExp(String.raw `^ALTER\s+TABLE\s+(?:IF\s+EXISTS\s+)?(?:ONLY\s+)?(${QUALIFIED})\s+([\s\S]*)$`, 'i');
51
+ const DROP_OBJECT = new RegExp(String.raw `^DROP\s+(?:MATERIALIZED\s+)?(TABLE|VIEW|INDEX|TRIGGER)\s+(?:CONCURRENTLY\s+)?(?:IF\s+EXISTS\s+)?(${QUALIFIED})`, 'i');
52
+ /** DML, transaction control and session pragmas: statements, not schema. */
53
+ const NON_SCHEMA_STATEMENT = /^(?:SELECT|INSERT|UPDATE|DELETE|REPLACE|WITH|VALUES|BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE|PRAGMA|SET|VACUUM|ANALYZE|EXPLAIN|ATTACH|DETACH|REINDEX|GRANT|REVOKE|COMMENT)\b/i;
54
+ /** Tables a query body reads or writes. */
55
+ const TABLE_IN_BODY = new RegExp(String.raw `\b(FROM|JOIN|INTO|UPDATE)\s+(?:OR\s+\w+\s+)?(?:ONLY\s+)?(${QUALIFIED})`, 'gi');
56
+ const REFERENCES = new RegExp(String.raw `\bREFERENCES\s+(${QUALIFIED})\s*(?:\(([^)]*)\))?`, 'i');
57
+ /** Strip identifier quoting and any schema qualifier: `main."Items"` → `Items`. */
58
+ function unqualify(name) {
59
+ const parts = name.match(new RegExp(NAME, 'g')) ?? [name];
60
+ const last = parts[parts.length - 1] ?? name;
61
+ if (last.startsWith('"'))
62
+ return last.slice(1, -1).replace(/""/g, '"');
63
+ if (last.startsWith('`') || last.startsWith('['))
64
+ return last.slice(1, -1);
65
+ return last;
66
+ }
67
+ /** Lower-cased lookup key for an object name (unquoted SQL identifiers fold case). */
68
+ function nameKey(name) {
69
+ return unqualify(name).toLowerCase();
70
+ }
71
+ /** Split a comma list of column names, ignoring sort order and collation words. */
72
+ function columnList(text) {
73
+ return splitTopLevel(text)
74
+ .map((part) => part.trim().match(new RegExp(`^${NAME}`))?.[0])
75
+ .filter((part) => Boolean(part))
76
+ .map(unqualify);
77
+ }
78
+ /**
79
+ * Blank `--` and `/* … *\/` comments with spaces, keeping newlines and every
80
+ * string literal, so offsets and line numbers still match the original text.
81
+ */
82
+ function blankComments(source) {
83
+ let out = '';
84
+ let index = 0;
85
+ while (index < source.length) {
86
+ const char = source[index];
87
+ const next = source[index + 1];
88
+ if (char === '-' && next === '-') {
89
+ const end = source.indexOf('\n', index);
90
+ const stop = end === -1 ? source.length : end;
91
+ out += ' '.repeat(stop - index);
92
+ index = stop;
93
+ }
94
+ else if (char === '/' && next === '*') {
95
+ const end = source.indexOf('*/', index + 2);
96
+ const stop = end === -1 ? source.length : end + 2;
97
+ out += source.slice(index, stop).replace(/[^\n]/g, ' ');
98
+ index = stop;
99
+ }
100
+ else if (char === "'" || char === '"' || char === '`' || char === '[') {
101
+ const stop = quotedEnd(source, index);
102
+ out += source.slice(index, stop);
103
+ index = stop;
104
+ }
105
+ else if (char === '$') {
106
+ const tag = source.slice(index).match(/^\$[A-Za-z_]*\$/)?.[0];
107
+ const end = tag ? source.indexOf(tag, index + tag.length) : -1;
108
+ const stop = tag ? (end === -1 ? source.length : end + tag.length) : index + 1;
109
+ out += source.slice(index, stop);
110
+ index = stop;
111
+ }
112
+ else {
113
+ out += char;
114
+ index++;
115
+ }
116
+ }
117
+ return out;
118
+ }
119
+ /**
120
+ * End offset (exclusive) of the quoted span opening at `start`, or -1 when the
121
+ * span never closes. A Postgres `E'…'` string also takes backslash escapes.
122
+ */
123
+ function quotedSpanEnd(source, start) {
124
+ const open = source[start];
125
+ const close = open === '[' ? ']' : open;
126
+ const backslashEscapes = open === "'" && /[Ee]/.test(source[start - 1] ?? '') && !/[\w$]/.test(source[start - 2] ?? '');
127
+ let index = start + 1;
128
+ while (index < source.length) {
129
+ if (backslashEscapes && source[index] === '\\')
130
+ index += 2;
131
+ else if (source[index] === close) {
132
+ if (close !== ']' && source[index + 1] === close)
133
+ index += 2;
134
+ else
135
+ return index + 1;
136
+ }
137
+ else
138
+ index++;
139
+ }
140
+ return -1;
141
+ }
142
+ /** {@link quotedSpanEnd}, running an unterminated span to the end of `source`. */
143
+ function quotedEnd(source, start) {
144
+ const end = quotedSpanEnd(source, start);
145
+ return end === -1 ? source.length : end;
146
+ }
147
+ /**
148
+ * Split blanked SQL into statements on `;` at block depth zero. Inside a
149
+ * `CREATE TRIGGER` statement `BEGIN` opens a block; `CASE` always does; `END`
150
+ * closes one (`END IF|LOOP|WHILE|REPEAT` close nothing the splitter opened).
151
+ * Quoted and dollar-quoted spans never split a statement. `problems` names a
152
+ * quote or block still open at the end of the file, after which no statement
153
+ * boundary can be trusted.
154
+ */
155
+ function splitStatements(source) {
156
+ const statements = [];
157
+ const problems = [];
158
+ const lineOf = (offset) => source.slice(0, offset).split('\n').length;
159
+ let start = 0;
160
+ let depth = 0;
161
+ let index = 0;
162
+ const push = (end) => {
163
+ const raw = source.slice(start, end);
164
+ const lead = raw.length - raw.trimStart().length;
165
+ const text = raw.trim();
166
+ if (text)
167
+ statements.push({ text, start: start + lead, end: start + lead + text.length });
168
+ };
169
+ while (index < source.length) {
170
+ const char = source[index] ?? '';
171
+ if (char === "'" || char === '"' || char === '`' || char === '[') {
172
+ const end = quotedSpanEnd(source, index);
173
+ if (end === -1) {
174
+ problems.push(`unterminated ${char} quote opened on line ${lineOf(index)}`);
175
+ index = source.length;
176
+ }
177
+ else
178
+ index = end;
179
+ continue;
180
+ }
181
+ if (char === '$') {
182
+ const tag = source.slice(index).match(/^\$[A-Za-z_]*\$/)?.[0];
183
+ if (tag) {
184
+ const end = source.indexOf(tag, index + tag.length);
185
+ if (end === -1)
186
+ problems.push(`unterminated ${tag} body opened on line ${lineOf(index)}`);
187
+ index = end === -1 ? source.length : end + tag.length;
188
+ continue;
189
+ }
190
+ }
191
+ if (/[A-Za-z_]/.test(char) && !/[\w$]/.test(source[index - 1] ?? '')) {
192
+ const word = source.slice(index).match(/^[A-Za-z_]\w*/)?.[0] ?? char;
193
+ const upper = word.toUpperCase();
194
+ const closesOther = upper === 'END' && /^END\s+(?:IF|LOOP|WHILE|REPEAT)\b/i.test(source.slice(index));
195
+ if (upper === 'CASE')
196
+ depth++;
197
+ else if (upper === 'END' && depth > 0 && !closesOther)
198
+ depth--;
199
+ else if (upper === 'BEGIN' &&
200
+ /^CREATE\s+(?:OR\s+REPLACE\s+)?(?:TEMP(?:ORARY)?\s+)?(?:CONSTRAINT\s+)?TRIGGER\b/i.test(source.slice(start, index).trimStart()))
201
+ depth++;
202
+ index += word.length;
203
+ continue;
204
+ }
205
+ if (char === ';' && depth === 0) {
206
+ push(index);
207
+ start = index + 1;
208
+ }
209
+ index++;
210
+ }
211
+ if (depth > 0)
212
+ problems.push(`${depth} BEGIN or CASE block(s) still open at the end of the file`);
213
+ push(source.length);
214
+ return { statements, problems };
215
+ }
216
+ /** Split on commas at parenthesis depth zero, outside quoted spans. */
217
+ function splitTopLevel(text) {
218
+ const parts = [];
219
+ let depth = 0;
220
+ let start = 0;
221
+ let index = 0;
222
+ while (index < text.length) {
223
+ const char = text[index];
224
+ if (char === "'" || char === '"' || char === '`' || char === '[') {
225
+ index = quotedEnd(text, index);
226
+ continue;
227
+ }
228
+ if (char === '(')
229
+ depth++;
230
+ else if (char === ')')
231
+ depth--;
232
+ else if (char === ',' && depth === 0) {
233
+ parts.push(text.slice(start, index));
234
+ start = index + 1;
235
+ }
236
+ index++;
237
+ }
238
+ parts.push(text.slice(start));
239
+ return parts.filter((part) => part.trim());
240
+ }
241
+ /** Text inside the parentheses that open at the start of `text`, else null. */
242
+ function parenthesized(text) {
243
+ if (!text.startsWith('('))
244
+ return null;
245
+ let depth = 0;
246
+ let index = 0;
247
+ while (index < text.length) {
248
+ const char = text[index];
249
+ if (char === "'" || char === '"' || char === '`' || char === '[') {
250
+ index = quotedEnd(text, index);
251
+ continue;
252
+ }
253
+ if (char === '(')
254
+ depth++;
255
+ else if (char === ')' && --depth === 0)
256
+ return text.slice(1, index);
257
+ index++;
258
+ }
259
+ return null;
260
+ }
261
+ /** Constraints and column names declared in a `CREATE TABLE ( … )` body. */
262
+ function tableBody(body) {
263
+ const columns = [];
264
+ const constraints = [];
265
+ for (const rawPart of splitTopLevel(body)) {
266
+ let part = rawPart.trim();
267
+ let name;
268
+ const named = part.match(new RegExp(String.raw `^CONSTRAINT\s+(${NAME})\s+`, 'i'));
269
+ if (named?.[1]) {
270
+ name = unqualify(named[1]);
271
+ part = part.slice(named[0].length);
272
+ }
273
+ const withName = (constraint) => name ? { ...constraint, name } : constraint;
274
+ const tableLevel = part.match(/^(PRIMARY\s+KEY|UNIQUE|CHECK|FOREIGN\s+KEY)\b\s*/i);
275
+ if (tableLevel?.[1]) {
276
+ const form = tableLevel[1].toUpperCase().replace(/\s+/g, ' ');
277
+ const rest = part.slice(tableLevel[0].length);
278
+ const inner = parenthesized(rest) ?? '';
279
+ if (form === 'CHECK')
280
+ constraints.push(withName({ type: 'check', columns: [] }));
281
+ else if (form === 'FOREIGN KEY') {
282
+ const target = rest.match(REFERENCES);
283
+ constraints.push(withName({
284
+ type: 'foreign-key',
285
+ columns: columnList(inner),
286
+ ...(target?.[1]
287
+ ? {
288
+ references: {
289
+ table: unqualify(target[1]),
290
+ columns: columnList(target[2] ?? ''),
291
+ },
292
+ }
293
+ : {}),
294
+ }));
295
+ }
296
+ else
297
+ constraints.push(withName({
298
+ type: form === 'UNIQUE' ? 'unique' : 'primary-key',
299
+ columns: columnList(inner),
300
+ }));
301
+ continue;
302
+ }
303
+ const column = part.match(new RegExp(`^${NAME}`))?.[0];
304
+ if (!column)
305
+ continue;
306
+ const columnName = unqualify(column);
307
+ columns.push(columnName);
308
+ const definition = part.slice(column.length);
309
+ if (/\bPRIMARY\s+KEY\b/i.test(definition))
310
+ constraints.push({ type: 'primary-key', columns: [columnName] });
311
+ if (/\bUNIQUE\b/i.test(definition))
312
+ constraints.push({ type: 'unique', columns: [columnName] });
313
+ if (/\bCHECK\s*\(/i.test(definition))
314
+ constraints.push({ type: 'check', columns: [columnName] });
315
+ const target = definition.match(REFERENCES);
316
+ if (target?.[1])
317
+ constraints.push({
318
+ type: 'foreign-key',
319
+ columns: [columnName],
320
+ references: { table: unqualify(target[1]), columns: columnList(target[2] ?? '') },
321
+ });
322
+ }
323
+ return { columns, constraints };
324
+ }
325
+ /** Distinct tables a query body reads or writes, lower-cased. */
326
+ function tablesInBody(body) {
327
+ const tables = new Set();
328
+ for (const match of body.matchAll(TABLE_IN_BODY)) {
329
+ const name = match[2];
330
+ // `DELETE FROM x`, `INSERT INTO x`, `UPDATE x`, `FROM x`, `JOIN x` — skip keywords.
331
+ if (name && !/^(?:SELECT|SET|VALUES|DEFAULT|NEW|OLD)$/i.test(name))
332
+ tables.add(nameKey(name));
333
+ }
334
+ return [...tables];
335
+ }
336
+ /** 1-based line of an offset. */
337
+ function lineAt(lineStarts, offset) {
338
+ let low = 0;
339
+ let high = lineStarts.length - 1;
340
+ while (low < high) {
341
+ const mid = (low + high + 1) >> 1;
342
+ if ((lineStarts[mid] ?? 0) <= offset)
343
+ low = mid;
344
+ else
345
+ high = mid - 1;
346
+ }
347
+ return low + 1;
348
+ }
349
+ /**
350
+ * Extract SQL schema objects, triggers, constraints and migration statements.
351
+ *
352
+ * @param filePath - Repository-relative path of the SQL file
353
+ * @param source - The file's text
354
+ * @returns Nodes for the file plus the table references to link across files
355
+ * @example
356
+ * ```ts
357
+ * const sql = extractSql('db/schema.sql', 'CREATE TABLE items (id INTEGER PRIMARY KEY);');
358
+ * sql.nodes[0]?.meta?.['sqlKind']; // 'table'
359
+ * ```
360
+ */
361
+ export function extractSql(filePath, source) {
362
+ const blanked = blankComments(source);
363
+ const lineStarts = [0];
364
+ for (let index = 0; index < blanked.length; index++)
365
+ if (blanked[index] === '\n')
366
+ lineStarts.push(index + 1);
367
+ const nodes = new Map();
368
+ const references = [];
369
+ const renames = [];
370
+ let unrecognizedStatements = 0;
371
+ const addNode = (sqlKind, name, statement, meta = {}) => {
372
+ const objectName = unqualify(name);
373
+ // The SQL kind is part of the id: a trigger or index may share a table's name.
374
+ const id = `${filePath}::${sqlKind}:${objectName}`;
375
+ if (!nodes.has(id))
376
+ nodes.set(id, {
377
+ id,
378
+ kind: NODE_KIND[sqlKind],
379
+ name: objectName,
380
+ filePath,
381
+ startLine: lineAt(lineStarts, statement.start),
382
+ endLine: lineAt(lineStarts, Math.max(statement.start, statement.end - 1)),
383
+ language: 'sql',
384
+ exported: false,
385
+ meta: { sqlKind, ...meta },
386
+ });
387
+ return id;
388
+ };
389
+ const refer = (source, target, type, reason) => {
390
+ references.push({ source, target: nameKey(target), type, reason });
391
+ };
392
+ const split = splitStatements(blanked);
393
+ for (const statement of split.statements) {
394
+ const text = statement.text;
395
+ const virtual = text.match(CREATE_VIRTUAL_TABLE);
396
+ if (virtual?.[1] && virtual[2]) {
397
+ const args = parenthesized((virtual[3] ?? '').trim()) ?? '';
398
+ const columns = splitTopLevel(args)
399
+ .map((part) => part.trim())
400
+ .filter((part) => !part.includes('='))
401
+ .flatMap((part) => columnList(part));
402
+ addNode('table', virtual[1], statement, {
403
+ virtual: true,
404
+ module: virtual[2].toLowerCase(),
405
+ columns,
406
+ });
407
+ continue;
408
+ }
409
+ const table = text.match(CREATE_TABLE);
410
+ if (table?.[1]) {
411
+ const rest = (table[2] ?? '').trim();
412
+ const body = parenthesized(rest);
413
+ const { columns, constraints } = body ? tableBody(body) : { columns: [], constraints: [] };
414
+ const id = addNode('table', table[1], statement, { columns, constraints });
415
+ for (const constraint of constraints)
416
+ if (constraint.references)
417
+ refer(id, constraint.references.table, 'accesses', 'foreign key references');
418
+ if (!body && /^AS\b/i.test(rest))
419
+ for (const source of tablesInBody(rest))
420
+ refer(id, source, 'queries', 'table created from');
421
+ continue;
422
+ }
423
+ const view = text.match(CREATE_VIEW);
424
+ if (view?.[1]) {
425
+ const id = addNode('view', view[1], statement);
426
+ for (const source of tablesInBody(view[2] ?? ''))
427
+ refer(id, source, 'queries', 'view selects from');
428
+ continue;
429
+ }
430
+ const index = text.match(CREATE_INDEX);
431
+ if (index?.[1] && index[2]) {
432
+ const id = addNode('index', index[1], statement, {
433
+ table: unqualify(index[2]),
434
+ columns: columnList(index[3] ?? ''),
435
+ unique: /^CREATE\s+UNIQUE\b/i.test(text),
436
+ });
437
+ refer(id, index[2], 'accesses', 'index on table');
438
+ continue;
439
+ }
440
+ const trigger = text.match(CREATE_TRIGGER);
441
+ if (trigger?.[1] && trigger[3]) {
442
+ const timing = (trigger[2] ?? '').replace(/\s+/g, ' ').trim();
443
+ const id = addNode('trigger', trigger[1], statement, {
444
+ table: unqualify(trigger[3]),
445
+ event: timing,
446
+ });
447
+ refer(id, trigger[3], 'accesses', 'trigger fires on table');
448
+ for (const target of tablesInBody(trigger[4] ?? ''))
449
+ refer(id, target, 'queries', 'trigger body reads or writes');
450
+ continue;
451
+ }
452
+ const alter = text.match(ALTER_TABLE);
453
+ if (alter?.[1]) {
454
+ const action = (alter[2] ?? '').trim();
455
+ const renamed = action.match(new RegExp(String.raw `^RENAME\s+TO\s+(${QUALIFIED})`, 'i'));
456
+ if (renamed?.[1])
457
+ renames.push({ from: nameKey(alter[1]), to: nameKey(renamed[1]) });
458
+ const verb = action.match(/^\w+(?:\s+(?:COLUMN|CONSTRAINT|TO))?/i)?.[0] ?? 'ALTER';
459
+ refer(filePath, alter[1], 'accesses', `migration: ALTER TABLE ${verb.toUpperCase()}`);
460
+ const target = action.match(REFERENCES);
461
+ if (target?.[1])
462
+ refer(filePath, target[1], 'accesses', 'migration: foreign key references');
463
+ continue;
464
+ }
465
+ const drop = text.match(DROP_OBJECT);
466
+ if (drop?.[1] && drop[2]) {
467
+ // A dropped index or trigger names no table; only tables and views link.
468
+ if (/^(?:TABLE|VIEW)$/i.test(drop[1]))
469
+ refer(filePath, drop[2], 'accesses', `migration: DROP ${drop[1].toUpperCase()}`);
470
+ continue;
471
+ }
472
+ if (!NON_SCHEMA_STATEMENT.test(text))
473
+ unrecognizedStatements++;
474
+ }
475
+ const problems = [...split.problems];
476
+ if (unrecognizedStatements)
477
+ problems.push(`${unrecognizedStatements} statement(s) matched no recognized SQL form`);
478
+ return {
479
+ filePath,
480
+ nodes: [...nodes.values()],
481
+ references,
482
+ renames,
483
+ unrecognizedStatements,
484
+ problems,
485
+ };
486
+ }
487
+ /**
488
+ * Link every SQL file's table references to the table and view nodes the
489
+ * indexed SQL files define. A definition in the referencing file wins; a name
490
+ * defined in exactly one other file links with import-scoped confidence; a
491
+ * name defined in several files links to each at ambiguous confidence.
492
+ * `ALTER TABLE … RENAME TO` makes the new name resolve to the renamed table.
493
+ *
494
+ * @param extractions - Every SQL file's extraction from this run
495
+ * @param graph - Graph that already holds the extracted nodes
496
+ * @returns How many references resolved and how many stayed unlinked
497
+ * @example
498
+ * ```ts
499
+ * const { linked, unresolved } = linkSqlReferences([extractSql('a.sql', sql)], graph);
500
+ * ```
501
+ */
502
+ export function linkSqlReferences(extractions, graph) {
503
+ const all = [...extractions];
504
+ const definitions = new Map();
505
+ for (const extraction of all)
506
+ for (const node of extraction.nodes) {
507
+ const sqlKind = node.meta?.['sqlKind'];
508
+ if (sqlKind !== 'table' && sqlKind !== 'view')
509
+ continue;
510
+ const key = node.name.toLowerCase();
511
+ definitions.set(key, [...(definitions.get(key) ?? []), node]);
512
+ }
513
+ for (const extraction of all)
514
+ for (const { from, to } of extraction.renames) {
515
+ const renamed = definitions.get(from);
516
+ if (renamed && !definitions.has(to))
517
+ definitions.set(to, renamed);
518
+ }
519
+ let linked = 0;
520
+ let unresolved = 0;
521
+ for (const extraction of all)
522
+ for (const reference of extraction.references) {
523
+ const candidates = definitions.get(reference.target) ?? [];
524
+ const local = candidates.filter((node) => node.filePath === extraction.filePath);
525
+ const targets = local.length ? local : candidates;
526
+ if (targets.length === 0) {
527
+ unresolved++;
528
+ continue;
529
+ }
530
+ const confidence = local.length ? 0.95 : targets.length === 1 ? 0.9 : 0.5;
531
+ linked++;
532
+ for (const target of targets) {
533
+ if (target.id === reference.source)
534
+ continue;
535
+ graph.addRelation({
536
+ source: reference.source,
537
+ target: target.id,
538
+ type: reference.type,
539
+ confidence,
540
+ confidenceLabel: confidenceLabelFromNumeric(confidence),
541
+ reason: reference.reason,
542
+ });
543
+ }
544
+ }
545
+ return { linked, unresolved };
546
+ }
547
+ //# sourceMappingURL=sql-extractor.js.map