-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextraction-vba-runtime-binding-sql.test.ts
More file actions
291 lines (260 loc) · 10.1 KB
/
Copy pathextraction-vba-runtime-binding-sql.test.ts
File metadata and controls
291 lines (260 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Issue #252 — SQL assigned to a binding property at runtime.
*
* This corpus binds its data in code, not in the form designer: there are
* 161 `.RowSource =` assignments across the `.cls` / `.bas` modules, while
* `RecordSource` and `ControlSource` appear ZERO times in `00_EXPEDIENTES`'s
* `.form.txt` files. The static `.form.txt` sweep (`vba-row-source` /
* `vba-record-source`) was therefore scanning the empty half of the problem
* and every table named in a runtime binding was invisible to the graph.
*
* The fix feeds the right-hand side of
* `<anything>.(RowSource|RecordSource|ControlSource|Filter|OrderBy) =`
* through the SAME pipeline the SQL wrapper path uses —
* `collectConcatFragments` then `scanSqlTables` — so the `?` sentinel for
* non-literal operands and the shared reserved-word reject list apply
* unchanged. The edges are tagged `vba-row-source-dynamic` so they stay
* separable from the static `vba-row-source` in audits.
*
* The suites below pin, in order:
*
* 1. the literal form on every one of the five binding properties;
* 2. the variable form, including #13 accumulate and #204 procedure scoping;
* 3. the negative half — a filter expression is not a FROM clause, a
* dynamically concatenated table name yields nothing, and a non-SQL
* payload (bare query name, `Value List`) is left alone;
* 4. an exhaustive guard that no `SQL_RESERVED_TABLE_TOKENS` word can ever
* be captured through the new path.
*
* Real extractor, no mocking.
*/
import { describe, it, expect } from 'vitest';
import { VbaExtractor } from '../src/extraction/vba-extractor';
import { SQL_RESERVED_TABLE_TOKENS } from '../src/extraction/sql-table-scan';
import type { Edge, Node } from '../src/types';
const FILE = 'src/forms/Form_Probe.cls';
const DYNAMIC = 'vba-row-source-dynamic';
const STATIC = 'vba-row-source';
const WRAPPER = 'vba-sql-table';
/** Wrap `body` in one procedure — the SQL sweep only runs inside one. */
function moduleWith(body: string[]): string {
return [
'Attribute VB_Name = "Form_Probe"',
'Public Sub Probe()',
...body.map((l) => ` ${l}`),
'End Sub',
'',
].join('\n');
}
function extract(source: string): { nodes: Node[]; edges: Edge[] } {
return new VbaExtractor(FILE, source).extract();
}
/** Distinct target names of `references` edges carrying `synthesizedBy`. */
function tablesFor(
result: { nodes: Node[]; edges: Edge[] },
synthesizedBy: string,
): string[] {
const names = new Set<string>();
for (const edge of result.edges) {
if (edge.kind !== 'references') continue;
if (edge.metadata?.synthesizedBy !== synthesizedBy) continue;
const name = result.nodes.find((n) => n.id === edge.target)?.name;
if (name) names.add(name);
}
return [...names].sort();
}
/** Table names reached by a single-procedure body, via the dynamic path. */
function dynamicTables(body: string[]): string[] {
return tablesFor(extract(moduleWith(body)), DYNAMIC);
}
describe('Issue #252 — literal SQL assigned to a binding property', () => {
it('captures the table of a RowSource literal', () => {
expect(
dynamicTables(['Me.cboUsuario.RowSource = "SELECT Id FROM TbUsuarios"']),
).toEqual(['TbUsuarios']);
});
it.each([
['RowSource', 'Me.cboUsuario.RowSource'],
['RecordSource', 'Me.RecordSource'],
['ControlSource', 'Me.txtNombre.ControlSource'],
['Filter', 'Me.Filter'],
['OrderBy', 'Me.OrderBy'],
])('covers the %s property', (_prop, lhs) => {
expect(dynamicTables([`${lhs} = "SELECT Id FROM TbUsuarios"`])).toEqual([
'TbUsuarios',
]);
});
it('resolves the bare `.Property =` form inside a With block', () => {
expect(
dynamicTables([
'With Me.cboUsuario',
' .RowSource = "SELECT Id FROM TbUsuarios"',
'End With',
]),
).toEqual(['TbUsuarios']);
});
it('captures every table of a multi-table statement', () => {
expect(
dynamicTables([
'Me.RecordSource = "SELECT * FROM TbExpedientes INNER JOIN TbUsuarios ON A.Id = B.Id"',
]),
).toEqual(['TbExpedientes', 'TbUsuarios']);
});
it('joins same-line `&` literal chains before scanning', () => {
expect(
dynamicTables([
'Me.RowSource = "SELECT Id " & "FROM TbUsuarios " & "WHERE Activo = 1"',
]),
).toEqual(['TbUsuarios']);
});
it('tags the edge `vba-row-source-dynamic`, never the static `vba-row-source`', () => {
const result = extract(
moduleWith(['Me.cboUsuario.RowSource = "SELECT Id FROM TbUsuarios"']),
);
expect(tablesFor(result, DYNAMIC)).toEqual(['TbUsuarios']);
expect(tablesFor(result, STATIC)).toEqual([]);
expect(tablesFor(result, WRAPPER)).toEqual([]);
});
it('leaves the wrapper path untouched on the same file', () => {
const result = extract(
moduleWith([
'getdb().Execute "DELETE FROM TbLog"',
'Me.RowSource = "SELECT Id FROM TbUsuarios"',
]),
);
expect(tablesFor(result, WRAPPER)).toEqual(['TbLog']);
expect(tablesFor(result, DYNAMIC)).toEqual(['TbUsuarios']);
});
});
describe('Issue #252 — the variable form', () => {
it('resolves `.RecordSource = strSQL` to the same tables as the literal form', () => {
const viaVariable = dynamicTables([
'strSQL = "SELECT Id FROM TbUsuarios"',
'Me.RecordSource = strSQL',
]);
const viaLiteral = dynamicTables([
'Me.RecordSource = "SELECT Id FROM TbUsuarios"',
]);
expect(viaVariable).toEqual(viaLiteral);
expect(viaVariable).toEqual(['TbUsuarios']);
});
it('keeps the #13 accumulate semantics — `sql = sql & "..."` adds, never replaces', () => {
expect(
dynamicTables([
'strSQL = "SELECT * FROM TbExpedientes"',
'strSQL = strSQL & " INNER JOIN TbUsuarios ON A.Id = B.Id"',
'Me.RecordSource = strSQL',
]),
).toEqual(['TbExpedientes', 'TbUsuarios']);
});
it('keeps the #204 procedure scoping — a name from another procedure does not leak', () => {
const source = [
'Attribute VB_Name = "Form_Probe"',
'Public Sub Builder()',
' strSQL = "SELECT * FROM TbExpedientes"',
'End Sub',
'Public Sub Binder()',
' Me.RecordSource = strSQL',
'End Sub',
'',
].join('\n');
expect(tablesFor(extract(source), DYNAMIC)).toEqual([]);
});
it('falls back to a module-level assignment, exactly as the wrapper path does', () => {
const source = [
'Attribute VB_Name = "Form_Probe"',
'Private strSQL As String',
'strSQL = "SELECT * FROM TbExpedientes"',
'Public Sub Binder()',
' Me.RecordSource = strSQL',
'End Sub',
'',
].join('\n');
expect(tablesFor(extract(source), DYNAMIC)).toEqual(['TbExpedientes']);
});
it('never guesses at an unknown identifier', () => {
expect(dynamicTables(['Me.RecordSource = strDesconocido'])).toEqual([]);
});
});
describe('Issue #252 — what must NOT become a table reference', () => {
it('a Filter expression is not a FROM clause', () => {
expect(dynamicTables(['Me.Filter = "Id = 1"'])).toEqual([]);
});
it('an OrderBy expression is not a FROM clause', () => {
expect(dynamicTables(['Me.OrderBy = "Nombre DESC"'])).toEqual([]);
});
it('a dynamically concatenated table name yields nothing — and never `FROM` or `WHERE`', () => {
const tables = dynamicTables([
'Me.cboUsuario.RowSource = "SELECT " & campo & " FROM " & tabla',
]);
expect(tables).toEqual([]);
expect(tables).not.toContain('FROM');
expect(tables).not.toContain('WHERE');
});
it('a dynamic table name followed by a WHERE clause never captures `WHERE`', () => {
const tables = dynamicTables([
'Me.RecordSource = "SELECT * FROM " & tabla & " WHERE Activo = 1"',
]);
expect(tables).toEqual([]);
expect(tables).not.toContain('WHERE');
});
it('a bare saved-query name is out of scope — nothing is guessed', () => {
expect(
dynamicTables(['Me.cboUsuario.RowSource = "qryUsuariosActivos"']),
).toEqual([]);
});
it('a Value List payload is out of scope', () => {
expect(
dynamicTables([
'Me.cboEstado.RowSourceType = "Value List"',
'Me.cboEstado.RowSource = "Alta;Baja;Pendiente"',
]),
).toEqual([]);
});
it('a ControlSource bound to a plain field name yields nothing', () => {
expect(dynamicTables(['Me.txtNombre.ControlSource = "Nombre"'])).toEqual([]);
});
it('a comparison is not an assignment', () => {
expect(
dynamicTables([
'If Me.Filter = "SELECT Id FROM TbUsuarios" Then',
'End If',
]),
).toEqual([]);
});
it('a commented-out binding is not scanned', () => {
expect(
dynamicTables(['\' Me.cboUsuario.RowSource = "SELECT Id FROM TbUsuarios"']),
).toEqual([]);
});
it('a sibling property whose name merely starts with a binding name is ignored', () => {
expect(dynamicTables(['Me.FilterOn = True'])).toEqual([]);
});
});
describe('Issue #252 — no reserved word can ever be captured', () => {
const RESERVED_SHAPES: string[] = [
'Me.RowSource = "SELECT " & campo & " FROM " & tabla',
'Me.RowSource = "SELECT * FROM " & tabla & " WHERE Activo = 1"',
'Me.RecordSource = "SELECT * FROM " & tabla & " ORDER BY Nombre"',
'Me.RecordSource = "SELECT * FROM " & tabla & " GROUP BY Id"',
'Me.RecordSource = "SELECT * FROM " & t1 & " INNER JOIN " & t2 & " ON A.Id = B.Id"',
'Me.RecordSource = "UPDATE " & tabla & " SET Activo = 1"',
'Me.RecordSource = "INSERT INTO " & tabla & " VALUES (1)"',
'Me.RecordSource = "SELECT * FROM " & tabla & " UNION SELECT * FROM " & otra',
'Me.Filter = "Id = 1"',
'Me.OrderBy = "Nombre DESC"',
'Me.cbo.RowSource = "SELECT TOP 10 * FROM " & tabla',
];
it.each(RESERVED_SHAPES)('captures no reserved word from: %s', (line) => {
for (const table of dynamicTables([line])) {
expect(SQL_RESERVED_TABLE_TOKENS.has(table.toUpperCase())).toBe(false);
}
});
it('captures no reserved word across every shape at once', () => {
const captured = dynamicTables(RESERVED_SHAPES);
const reserved = captured.filter((t) =>
SQL_RESERVED_TABLE_TOKENS.has(t.toUpperCase()),
);
expect(reserved).toEqual([]);
});
});