@syncular/typegen 0.57.0 → 0.59.0

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.
Files changed (60) hide show
  1. package/README.md +43 -25
  2. package/dist/cli.js +8 -3
  3. package/dist/emit-dart.d.ts +4 -3
  4. package/dist/emit-dart.js +39 -88
  5. package/dist/emit-kotlin.d.ts +3 -3
  6. package/dist/emit-kotlin.js +39 -86
  7. package/dist/emit-queries-dart.d.ts +1 -1
  8. package/dist/emit-queries-dart.js +17 -26
  9. package/dist/emit-queries-kotlin.d.ts +1 -1
  10. package/dist/emit-queries-kotlin.js +15 -24
  11. package/dist/emit-queries-rust.d.ts +1 -1
  12. package/dist/emit-queries-rust.js +1 -6
  13. package/dist/emit-queries-swift.d.ts +1 -1
  14. package/dist/emit-queries-swift.js +22 -26
  15. package/dist/emit-queries.d.ts +1 -1
  16. package/dist/emit-queries.js +1 -6
  17. package/dist/emit-swift.d.ts +3 -3
  18. package/dist/emit-swift.js +41 -89
  19. package/dist/emit.d.ts +1 -1
  20. package/dist/emit.js +33 -98
  21. package/dist/errors.d.ts +6 -2
  22. package/dist/errors.js +10 -2
  23. package/dist/generate.d.ts +4 -3
  24. package/dist/generate.js +32 -49
  25. package/dist/init.js +1 -1
  26. package/dist/ir.d.ts +11 -0
  27. package/dist/ir.js +66 -47
  28. package/dist/lower.d.ts +0 -7
  29. package/dist/lower.js +0 -62
  30. package/dist/migration-lock.d.ts +32 -23
  31. package/dist/migration-lock.js +115 -92
  32. package/dist/naming.d.ts +25 -0
  33. package/dist/naming.js +92 -0
  34. package/dist/query.d.ts +54 -14
  35. package/dist/query.js +484 -462
  36. package/dist/sql.js +18 -3
  37. package/dist/syql-lowering.js +10 -1
  38. package/dist/syql-validator.js +13 -220
  39. package/package.json +3 -3
  40. package/src/cli.ts +9 -3
  41. package/src/emit-dart.ts +41 -102
  42. package/src/emit-kotlin.ts +38 -102
  43. package/src/emit-queries-dart.ts +23 -32
  44. package/src/emit-queries-kotlin.ts +20 -30
  45. package/src/emit-queries-rust.ts +7 -12
  46. package/src/emit-queries-swift.ts +26 -32
  47. package/src/emit-queries.ts +5 -11
  48. package/src/emit-swift.ts +42 -104
  49. package/src/emit.ts +33 -116
  50. package/src/errors.ts +13 -2
  51. package/src/generate.ts +74 -76
  52. package/src/init.ts +4 -1
  53. package/src/ir.ts +87 -50
  54. package/src/lower.ts +0 -53
  55. package/src/migration-lock.ts +192 -137
  56. package/src/naming.ts +154 -0
  57. package/src/query.ts +615 -504
  58. package/src/sql.ts +30 -3
  59. package/src/syql-lowering.ts +15 -1
  60. package/src/syql-validator.ts +21 -279
package/dist/query.js CHANGED
@@ -63,8 +63,8 @@
63
63
  */
64
64
  import { TypegenError } from './errors.js';
65
65
  import { isSyqlTrivia, lexSyqlSqlSource } from './syql-lexer.js';
66
- import { findProjection, lowerProjection, mainVerbAfterWith } from './lower.js';
67
- import { buildNamingMap } from './naming.js';
66
+ import { findProjection, lowerProjection } from './lower.js';
67
+ import { buildNamingMap, validateQueryIdentifier, } from './naming.js';
68
68
  /** The SQL decltype keyword → §2.4 type map (mirrors sql.ts TYPE_MAP so a
69
69
  * plain column ref's decltype resolves to the exact IR type). */
70
70
  const DECLTYPE_MAP = {
@@ -91,6 +91,13 @@ const SYNC_VERSION_COLUMN = '_sync_version';
91
91
  /** Stable private identity shared by every declared local FTS5 projection. */
92
92
  const FTS_SOURCE_ID_COLUMN = '_syncular_source_id';
93
93
  const DEFAULT_NAMING = { naming: 'camel', targets: ['ts'] };
94
+ /** The public SYQL input an emitter binds by name. */
95
+ export function syqlInput(query, name) {
96
+ const input = query.syql?.inputs.find((candidate) => candidate.name === name);
97
+ if (input === undefined)
98
+ throw new Error(`unknown SYQL input ${name}`);
99
+ return input;
100
+ }
94
101
  // -- path → name --------------------------------------------------------------
95
102
  /** A single kebab-case path segment (folder) or filename stem. */
96
103
  const KEBAB_SEGMENT_RE = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
@@ -145,114 +152,80 @@ export function validateOverrideName(name, location) {
145
152
  }
146
153
  return name;
147
154
  }
148
- /** Split file content into statements on top-level `;` (string/comment-aware).
149
- * Blank/comment-only tails are dropped; each statement keeps its own leading
150
- * comment block (everything between the previous `;` and this SQL). */
151
- export function splitStatements(content) {
152
- const parts = [];
153
- let i = 0;
154
- let start = 0;
155
- while (i < content.length) {
156
- const ch = content[i];
157
- const next = content[i + 1];
158
- if (ch === '-' && next === '-') {
159
- const end = content.indexOf('\n', i);
160
- i = end === -1 ? content.length : end;
161
- }
162
- else if (ch === '/' && next === '*') {
163
- const end = content.indexOf('*/', i + 2);
164
- i = end === -1 ? content.length : end + 2;
155
+ // -- SQL tokens -----------------------------------------------------------------
156
+ //
157
+ // Every scan below reads the lossless SYQL lexer (`syql-lexer.ts`), so string
158
+ // literals, quoted identifiers, and comments are atomic tokens: their text is
159
+ // never split, rescanned, or rewritten. SQLite remains the authority on
160
+ // correctness.
161
+ /** Significant SQL tokens (no trivia) with each token's parenthesis depth,
162
+ * its innermost enclosing `(` (or -1), and every `(` index's matching `)`. */
163
+ function sqlStructure(sql, file = 'query SQL') {
164
+ const tokens = lexSyqlSqlSource(file, sql).filter((token) => !isSyqlTrivia(token) && token.kind !== 'eof');
165
+ const depths = [];
166
+ const parents = [];
167
+ const closes = new Map();
168
+ const stack = [];
169
+ tokens.forEach((token, index) => {
170
+ if (token.text === ')') {
171
+ const open = stack.pop();
172
+ if (open !== undefined)
173
+ closes.set(open, index);
165
174
  }
166
- else if (ch === "'") {
167
- let j = i + 1;
168
- for (;;) {
169
- if (j >= content.length)
170
- break;
171
- if (content[j] === "'") {
172
- if (content[j + 1] === "'")
173
- j += 2;
174
- else {
175
- j += 1;
176
- break;
177
- }
178
- }
179
- else
180
- j += 1;
175
+ parents[index] = stack.at(-1) ?? -1;
176
+ depths[index] = stack.length;
177
+ if (token.text === '(')
178
+ stack.push(index);
179
+ });
180
+ return { tokens, depths, parents, closes };
181
+ }
182
+ function lowerWord(token) {
183
+ return token?.kind === 'identifier' ? token.text.toLowerCase() : undefined;
184
+ }
185
+ /** Split file content into statements on top-level `;` tokens. A statement
186
+ * keeps its leading comment block; comment-only parts are dropped. The
187
+ * SELECT-only subset has no BEGIN/END blocks, so every `;` token ends one
188
+ * statement and a trailing `;` is optional. */
189
+ export function splitStatements(content, file = 'query SQL') {
190
+ const out = [];
191
+ let lead;
192
+ let hasSql = false;
193
+ for (const token of lexSyqlSqlSource(file, content)) {
194
+ if (token.kind === 'eof' || token.text === ';') {
195
+ if (lead !== undefined && hasSql) {
196
+ out.push({
197
+ text: content.slice(lead.span.start.offset, token.span.start.offset),
198
+ startLine: lead.span.start.line,
199
+ });
181
200
  }
182
- i = j;
201
+ lead = undefined;
202
+ hasSql = false;
183
203
  }
184
- else if (ch === ';') {
185
- parts.push({ text: content.slice(start, i), startOffset: start });
186
- i += 1;
187
- start = i;
204
+ else if (token.kind !== 'whitespace') {
205
+ lead ??= token;
206
+ hasSql ||= !isSyqlTrivia(token);
188
207
  }
189
- else {
190
- i += 1;
191
- }
192
- }
193
- if (start < content.length) {
194
- parts.push({ text: content.slice(start), startOffset: start });
195
- }
196
- const out = [];
197
- for (const part of parts) {
198
- // Drop parts whose SQL body (comments/whitespace stripped) is empty — a
199
- // trailing `;` or a comment-only tail is not a statement.
200
- if (stripCommentsAndStrings(part.text).trim().length === 0)
201
- continue;
202
- // Report the line of the statement's first NON-blank character (skip the
203
- // blank/whitespace run after the previous `;`), so the location points at
204
- // real content, not the terminator's trailing newline.
205
- const lead = part.text.length - part.text.replace(/^\s+/, '').length;
206
- const before = content.slice(0, part.startOffset + lead);
207
- const startLine = before.split('\n').length;
208
- out.push({ text: part.text.replace(/^\s+/, ''), startLine });
209
208
  }
210
209
  return out;
211
210
  }
212
- /** The leading comment block of a statement: the run of blank + line/block
213
- * comment lines before the first SQL token. `-- name:` / `-- param` markers are
214
- * scoped HERE (to the statement they directly precede), not file-wide. Returns
215
- * the `--` comment lines (block comments carry no markers we parse). */
216
- function leadingCommentBlock(statementText) {
217
- const lines = [];
218
- let i = 0;
219
- while (i < statementText.length) {
220
- // Skip leading whitespace on the line.
221
- while (i < statementText.length &&
222
- /[ \t\r]/.test(statementText[i])) {
223
- i += 1;
224
- }
225
- const ch = statementText[i];
226
- const next = statementText[i + 1];
227
- if (ch === '\n') {
228
- i += 1;
229
- continue;
230
- }
231
- if (ch === '-' && next === '-') {
232
- const end = statementText.indexOf('\n', i);
233
- const line = statementText.slice(i, end === -1 ? undefined : end);
234
- lines.push(line);
235
- i = end === -1 ? statementText.length : end + 1;
236
- continue;
237
- }
238
- if (ch === '/' && next === '*') {
239
- const end = statementText.indexOf('*/', i + 2);
240
- // Block comments carry no markers we parse; skip and keep scanning for
241
- // more leading comments (but they don't contribute -- name:/-- param).
242
- i = end === -1 ? statementText.length : end + 2;
243
- continue;
244
- }
245
- break; // first SQL token — the comment block ends here
211
+ /** The `--` comments before a statement's first SQL token. `-- name:` and
212
+ * `-- param` markers are scoped to the statement they precede. */
213
+ function leadingComments(statementText) {
214
+ const comments = [];
215
+ for (const token of lexSyqlSqlSource('query SQL', statementText)) {
216
+ if (!isSyqlTrivia(token))
217
+ break;
218
+ if (token.kind === 'line-comment')
219
+ comments.push(token.text);
246
220
  }
247
- return lines;
221
+ return comments;
248
222
  }
249
- const NAME_MARKER_RE = /^\s*--\s*name:\s*(\S+)\s*$/;
223
+ const NAME_MARKER_RE = /^--\s*name:\s*(\S+)\s*$/;
250
224
  /** The `-- name:` override in a statement's leading comment block, or null. */
251
225
  export function parseStatementName(statementText, location) {
252
- const lines = leadingCommentBlock(statementText);
253
226
  let found = null;
254
- for (const line of lines) {
255
- const m = NAME_MARKER_RE.exec(line);
227
+ for (const comment of leadingComments(statementText)) {
228
+ const m = NAME_MARKER_RE.exec(comment);
256
229
  if (m === null)
257
230
  continue;
258
231
  if (found !== null) {
@@ -262,7 +235,7 @@ export function parseStatementName(statementText, location) {
262
235
  }
263
236
  return found;
264
237
  }
265
- const PARAM_COMMENT_RE = /^\s*--\s*param\s+:([A-Za-z_][A-Za-z0-9_]*)\s+([A-Za-z_]+)\s*$/;
238
+ const PARAM_COMMENT_RE = /^--\s*param\s+:([A-Za-z_][A-Za-z0-9_]*)\s+([A-Za-z_]+)\s*$/;
266
239
  const PARAM_TYPES = new Set([
267
240
  'string',
268
241
  'integer',
@@ -273,16 +246,11 @@ const PARAM_TYPES = new Set([
273
246
  'blob_ref',
274
247
  'crdt',
275
248
  ]);
276
- /** Parse `-- param :name <type>` header lines. They must precede the SQL. */
249
+ /** Parse the `-- param :name <type>` markers of the leading comment block. */
277
250
  function parseCommentParams(file, raw) {
278
251
  const out = [];
279
- for (const line of raw.split('\n')) {
280
- const trimmed = line.trim();
281
- if (trimmed.length === 0)
282
- continue;
283
- if (!trimmed.startsWith('--'))
284
- break; // first non-comment line ends the header
285
- const match = PARAM_COMMENT_RE.exec(line);
252
+ for (const comment of leadingComments(raw)) {
253
+ const match = PARAM_COMMENT_RE.exec(comment);
286
254
  if (match === null)
287
255
  continue; // an ordinary comment, ignore
288
256
  const name = match[1];
@@ -294,67 +262,32 @@ function parseCommentParams(file, raw) {
294
262
  }
295
263
  return out;
296
264
  }
297
- /** Strip `--` and block comments so identifier scans don't hit commented SQL,
298
- * and string literals so `':x'` inside a string isn't read as a param. */
265
+ /** Blank comments and string/blob literals with spaces (newlines kept), so
266
+ * offset-preserving text scans never read inside them. */
299
267
  export function stripCommentsAndStrings(sql) {
300
- let out = '';
301
- let i = 0;
302
- while (i < sql.length) {
303
- const ch = sql[i];
304
- const next = sql[i + 1];
305
- if (ch === '-' && next === '-') {
306
- const end = sql.indexOf('\n', i);
307
- const stop = end === -1 ? sql.length : end;
308
- out += ' '.repeat(stop - i);
309
- i = stop;
310
- }
311
- else if (ch === '/' && next === '*') {
312
- const end = sql.indexOf('*/', i + 2);
313
- const stop = end === -1 ? sql.length : end + 2;
314
- out += sql.slice(i, stop).replace(/[^\n]/g, ' ');
315
- i = stop;
316
- }
317
- else if (ch === "'") {
318
- let stop = i + 1;
319
- while (stop < sql.length) {
320
- if (sql[stop] === "'" && sql[stop + 1] === "'") {
321
- stop += 2;
322
- continue;
323
- }
324
- if (sql[stop] === "'") {
325
- stop += 1;
326
- break;
327
- }
328
- stop += 1;
329
- }
330
- out += sql.slice(i, stop).replace(/[^\n]/g, ' ');
331
- i = stop;
332
- }
333
- else {
334
- out += ch;
335
- i += 1;
336
- }
337
- }
338
- return out;
268
+ return lexSyqlSqlSource('query SQL', sql)
269
+ .map((token) => token.kind === 'line-comment' ||
270
+ token.kind === 'block-comment' ||
271
+ token.kind === 'string' ||
272
+ token.kind === 'blob'
273
+ ? token.text.replace(/[^\n]/g, ' ')
274
+ : token.text)
275
+ .join('');
339
276
  }
340
- /** Distinct `:name` placeholders in first-occurrence order (dedup by name,
341
- * matching SQLite's paramsCount which counts distinct names). */
277
+ /** Distinct `:name` binds in first-occurrence order (SQLite's paramsCount
278
+ * counts distinct names). */
342
279
  function scanParamNames(sql) {
343
- const cleaned = stripCommentsAndStrings(sql);
344
- // `:name` but not `::` (Postgres cast) — our subset never has `::`.
345
- const re = /:([A-Za-z_][A-Za-z0-9_]*)/g;
346
- const seen = [];
347
- for (const m of cleaned.matchAll(re)) {
348
- const name = m[1];
349
- if (!seen.includes(name))
350
- seen.push(name);
280
+ const names = [];
281
+ for (const token of lexSyqlSqlSource('query SQL', sql)) {
282
+ const name = token.text.slice(1);
283
+ if (token.kind === 'bind' && !names.includes(name))
284
+ names.push(name);
351
285
  }
352
- return seen;
286
+ return names;
353
287
  }
354
- /** Rewrite `:name` → positional placeholders AND strip comments + collapse
355
- * whitespace, producing a clean single-line SQL string suitable for
356
- * embedding in a generated string literal. String literals are preserved
357
- * verbatim (their inner whitespace is not collapsed).
288
+ /** The emitted single-line statement: `:name` binds become positional, every
289
+ * whitespace/comment run between tokens becomes one space, and every other
290
+ * token (string literals, quoted identifiers) keeps its exact text.
358
291
  *
359
292
  * When every param occurs exactly once, plain `?` is emitted. When ANY
360
293
  * param repeats (the §7 neutralization guards mention a param in the guard
@@ -362,83 +295,27 @@ function scanParamNames(sql) {
362
295
  * form — one bound value per DISTINCT param, reuse resolved by SQLite —
363
296
  * so the generated bind array stays one-entry-per-param. */
364
297
  export function toPositionalSql(sql) {
365
- const tokens = [];
366
- let i = 0;
367
- while (i < sql.length) {
368
- const ch = sql[i];
369
- const next = sql[i + 1];
370
- if (ch === '-' && next === '-') {
371
- const end = sql.indexOf('\n', i);
372
- i = end === -1 ? sql.length : end + 1;
373
- tokens.push(' ');
374
- }
375
- else if (ch === '/' && next === '*') {
376
- const end = sql.indexOf('*/', i + 2);
377
- i = end === -1 ? sql.length : end + 2;
378
- tokens.push(' ');
379
- }
380
- else if (ch === "'") {
381
- // Preserve string literals verbatim (including any '' escapes).
382
- let j = i + 1;
383
- let lit = "'";
384
- for (;;) {
385
- if (j >= sql.length)
386
- break;
387
- if (sql[j] === "'") {
388
- if (sql[j + 1] === "'") {
389
- lit += "''";
390
- j += 2;
391
- }
392
- else {
393
- lit += "'";
394
- j += 1;
395
- break;
396
- }
397
- }
398
- else {
399
- lit += sql[j];
400
- j += 1;
401
- }
402
- }
403
- tokens.push(lit);
404
- i = j;
405
- }
406
- else if (ch === ':' && next !== undefined && /[A-Za-z_]/.test(next)) {
407
- let end = i + 1;
408
- while (end < sql.length && /[A-Za-z0-9_]/.test(sql[end])) {
409
- end += 1;
410
- }
411
- tokens.push({ param: sql.slice(i + 1, end) });
412
- i = end;
413
- }
414
- else if (/\s/.test(ch)) {
415
- tokens.push(' ');
416
- i += 1;
417
- }
418
- else {
419
- tokens.push(ch);
420
- i += 1;
421
- }
422
- }
423
- const order = [];
424
- let repeats = false;
298
+ const tokens = lexSyqlSqlSource('query SQL', sql);
299
+ const order = scanParamNames(sql);
300
+ const repeats = tokens.filter((token) => token.kind === 'bind').length > order.length;
301
+ let out = '';
302
+ let space = false;
425
303
  for (const token of tokens) {
426
- if (typeof token === 'string')
304
+ if (isSyqlTrivia(token) || token.kind === 'eof') {
305
+ space = out.length > 0;
427
306
  continue;
428
- if (order.includes(token.param))
429
- repeats = true;
430
- else
431
- order.push(token.param);
307
+ }
308
+ if (space)
309
+ out += ' ';
310
+ space = false;
311
+ out +=
312
+ token.kind !== 'bind'
313
+ ? token.text
314
+ : repeats
315
+ ? `?${order.indexOf(token.text.slice(1)) + 1}`
316
+ : '?';
432
317
  }
433
- const rendered = tokens
434
- .map((token) => {
435
- if (typeof token === 'string')
436
- return token;
437
- return repeats ? `?${order.indexOf(token.param) + 1}` : '?';
438
- })
439
- .join('');
440
- // Collapse runs of the whitespace placeholders; trim.
441
- return rendered.replace(/\s+/g, ' ').trim();
318
+ return out;
442
319
  }
443
320
  const IDENT = '[A-Za-z_][A-Za-z0-9_]*';
444
321
  // `FROM tbl [AS] a`, `JOIN tbl [AS] a`. We only need the referenced base
@@ -467,7 +344,8 @@ const RESERVED_ALIAS = new Set([
467
344
  'indexed',
468
345
  'not',
469
346
  ]);
470
- function sqlIdentifier(token) {
347
+ /** The name a plain or quoted identifier token spells. */
348
+ export function sqlIdentifier(token) {
471
349
  if (token?.kind === 'identifier')
472
350
  return token.text;
473
351
  if (token?.kind !== 'quoted-identifier')
@@ -482,7 +360,7 @@ export function scanTableRefs(sql, ir, lowered = false) {
482
360
  /** `lowered` scans emitted SQL, whose FTS mapping reads
483
361
  * ({@link lowerFtsSourceIds}) are internal and not relations of the query. */
484
362
  export function scanRelations(sql, ir, lowered = false) {
485
- const tokens = lexSyqlSqlSource('query SQL', sql).filter((token) => !isSyqlTrivia(token) && token.kind !== 'eof');
363
+ const { tokens, depths, parents, closes } = sqlStructure(sql);
486
364
  const known = new Map(ir.tables.flatMap((table) => [
487
365
  [table.name.toLowerCase(), table.name],
488
366
  ...table.ftsIndexes.map((index) => [index.name.toLowerCase(), index.name]),
@@ -490,21 +368,6 @@ export function scanRelations(sql, ir, lowered = false) {
490
368
  const mappings = new Set(lowered
491
369
  ? ir.tables.flatMap((table) => table.ftsIndexes.map((index) => ftsMappingTable(index.name).toLowerCase()))
492
370
  : []);
493
- const closes = new Map();
494
- const parents = [];
495
- const depths = [];
496
- const stack = [];
497
- tokens.forEach((token, index) => {
498
- if (token.text === ')') {
499
- const open = stack.pop();
500
- if (open !== undefined)
501
- closes.set(open, index);
502
- }
503
- parents[index] = stack.at(-1) ?? -1;
504
- depths[index] = stack.length;
505
- if (token.text === '(')
506
- stack.push(index);
507
- });
508
371
  const activeFromDepths = new Set();
509
372
  const clauseEnders = new Set([
510
373
  'where',
@@ -719,6 +582,212 @@ export function scanRelations(sql, ir, lowered = false) {
719
582
  },
720
583
  };
721
584
  }
585
+ const CLAUSE_WORDS = new Set([
586
+ 'where',
587
+ 'group',
588
+ 'having',
589
+ 'order',
590
+ 'limit',
591
+ 'window',
592
+ 'join',
593
+ 'inner',
594
+ 'left',
595
+ 'right',
596
+ 'full',
597
+ 'cross',
598
+ 'natural',
599
+ ]);
600
+ /**
601
+ * The scope-proof rule shared by `.sql` and `.syql` queries (SYQL §13). A
602
+ * comparison proves a scope only when it is a whole top-level conjunct of
603
+ * the WHERE clause of the SELECT scope that reads the instance: the outer
604
+ * statement or a top-level CTE body. Parentheses around a whole conjunct are
605
+ * transparent. Nothing else is a proof: a WHERE clause with a top-level OR, a
606
+ * compound SELECT scope, a nested subquery, and any other conjunct shape (NOT,
607
+ * a comparison of the comparison, a function, CASE, COLLATE, IS after the
608
+ * bind, a literal in an IN list). Column equalities also come from the
609
+ * conjuncts of an ON clause without OR, NOT, or a subquery.
610
+ */
611
+ export function scopeProofs(sql, ir) {
612
+ const { tokens, depths, closes } = sqlStructure(sql);
613
+ const relations = scanRelations(sql, ir);
614
+ const binds = [];
615
+ const equalities = [];
616
+ const tableOf = (ref) => ir.tables.find((table) => table.name === ref.table);
617
+ const scopeColumn = (scope, qualifier, column) => {
618
+ const name = column.toLowerCase();
619
+ const candidates = relations.tables.filter((ref) => relations.scopeAt(ref.start) === scope &&
620
+ (qualifier === undefined
621
+ ? tableOf(ref)?.columns.some((c) => c.name.toLowerCase() === name)
622
+ : ref.alias.toLowerCase() === qualifier.toLowerCase()));
623
+ const ref = candidates.length === 1 ? candidates[0] : undefined;
624
+ const declared = ref === undefined
625
+ ? undefined
626
+ : tableOf(ref)?.scopes.find((s) => s.column.toLowerCase() === name);
627
+ return ref === undefined || declared === undefined
628
+ ? undefined
629
+ : { scope, alias: ref.alias.toLowerCase(), column: name };
630
+ };
631
+ const columnAt = (index) => {
632
+ const first = sqlIdentifier(tokens[index]);
633
+ if (first === undefined)
634
+ return undefined;
635
+ if (tokens[index + 1]?.text !== '.')
636
+ return { qualifier: undefined, column: first, next: index + 1 };
637
+ const column = sqlIdentifier(tokens[index + 2]);
638
+ return column === undefined
639
+ ? undefined
640
+ : { qualifier: first, column, next: index + 3 };
641
+ };
642
+ const equal = (index) => tokens[index]?.text === '=' || tokens[index]?.text === '==';
643
+ const bindAt = (index) => tokens[index]?.kind === 'bind'
644
+ ? tokens[index].text.slice(1)
645
+ : undefined;
646
+ const conjuncts = (from, to) => {
647
+ const parts = [];
648
+ let start = from;
649
+ let cases = 0;
650
+ let between = false;
651
+ for (let index = from; index < to; index += 1) {
652
+ if (depths[index] !== depths[from])
653
+ continue;
654
+ const word = lowerWord(tokens[index]);
655
+ if (word === 'case')
656
+ cases += 1;
657
+ else if (word === 'end')
658
+ cases -= 1;
659
+ else if (cases > 0)
660
+ continue;
661
+ else if (word === 'or')
662
+ return [];
663
+ else if (word === 'between')
664
+ between = true;
665
+ else if (word === 'and' && between)
666
+ between = false;
667
+ else if (word === 'and') {
668
+ parts.push([start, index]);
669
+ start = index + 1;
670
+ }
671
+ }
672
+ parts.push([start, to]);
673
+ return parts.flatMap(([start, end]) => tokens[start]?.text === '(' &&
674
+ closes.get(start) === end - 1 &&
675
+ !['select', 'with', 'values'].includes(lowerWord(tokens[start + 1]) ?? '')
676
+ ? conjuncts(start + 1, end - 1)
677
+ : [[start, end]]);
678
+ };
679
+ const classify = (scope, start, end, join) => {
680
+ const reversed = bindAt(start);
681
+ const left = columnAt(reversed === undefined ? start : start + 2);
682
+ if (left === undefined)
683
+ return;
684
+ if (reversed !== undefined) {
685
+ const subject = scopeColumn(scope, left.qualifier, left.column);
686
+ if (join === undefined &&
687
+ equal(start + 1) &&
688
+ left.next === end &&
689
+ subject)
690
+ binds.push({ ...subject, operator: 'equal', params: [reversed] });
691
+ return;
692
+ }
693
+ const operator = left.next;
694
+ const bind = bindAt(operator + 1);
695
+ const list = tokens.slice(operator + 2, end - 1);
696
+ const right = columnAt(operator + 1);
697
+ if (join === undefined &&
698
+ bind !== undefined &&
699
+ operator + 2 === end &&
700
+ (equal(operator) || lowerWord(tokens[operator]) === 'is')) {
701
+ const subject = scopeColumn(scope, left.qualifier, left.column);
702
+ if (subject)
703
+ binds.push({ ...subject, operator: 'equal', params: [bind] });
704
+ }
705
+ else if (join === undefined &&
706
+ lowerWord(tokens[operator]) === 'in' &&
707
+ closes.get(operator + 1) === end - 1 &&
708
+ list.length % 2 === 1 &&
709
+ list.every((token, index) => index % 2 === 0 ? token.kind === 'bind' : token.text === ',')) {
710
+ const subject = scopeColumn(scope, left.qualifier, left.column);
711
+ const params = list.flatMap((token) => token.kind === 'bind' ? [token.text.slice(1)] : []);
712
+ if (subject)
713
+ binds.push({ ...subject, operator: 'in', params });
714
+ }
715
+ else if (equal(operator) &&
716
+ right?.next === end &&
717
+ left.qualifier !== undefined &&
718
+ right.qualifier !== undefined) {
719
+ const l = scopeColumn(scope, left.qualifier, left.column);
720
+ const r = scopeColumn(scope, right.qualifier, right.column);
721
+ if (l === undefined || r === undefined)
722
+ return;
723
+ if (join?.kind !== 'left' && join?.kind !== 'right') {
724
+ equalities.push({ target: l, source: r, symmetric: true });
725
+ return;
726
+ }
727
+ // An outer join's ON constrains matched rows of its null-extended side
728
+ // only; an equality with both operands on one side proves nothing.
729
+ if (join.introduced.has(l.alias) === join.introduced.has(r.alias))
730
+ return;
731
+ const [introduced, prior] = join.introduced.has(l.alias)
732
+ ? [l, r]
733
+ : [r, l];
734
+ equalities.push(join.kind === 'left'
735
+ ? { target: introduced, source: prior, symmetric: false }
736
+ : { target: prior, source: introduced, symmetric: false });
737
+ }
738
+ };
739
+ const ranges = [
740
+ { scope: 'main', from: 0, to: tokens.length },
741
+ ...relations.cteScopes.map((cte) => ({
742
+ scope: `cte:${cte.name}`,
743
+ from: tokens.findIndex((token) => token.span.start.offset >= cte.start),
744
+ to: tokens.findIndex((token) => token.span.start.offset >= cte.end),
745
+ })),
746
+ ];
747
+ for (const range of ranges) {
748
+ const own = [];
749
+ for (let index = range.from; index < range.to; index += 1) {
750
+ if (depths[index] === depths[range.from])
751
+ own.push(index);
752
+ }
753
+ const words = own.map((index) => lowerWord(tokens[index]) ?? '');
754
+ if (words.some((w) => w === 'union' || w === 'intersect' || w === 'except'))
755
+ continue;
756
+ own.forEach((at, position) => {
757
+ const word = words[position];
758
+ if (word !== 'where' && word !== 'on')
759
+ return;
760
+ let next = position + 1;
761
+ while (next < words.length && !CLAUSE_WORDS.has(words[next]))
762
+ next += 1;
763
+ const to = own[next] ?? range.to;
764
+ if (word === 'where') {
765
+ for (const [start, end] of conjuncts(at + 1, to))
766
+ classify(range.scope, start, end);
767
+ return;
768
+ }
769
+ if (tokens
770
+ .slice(at + 1, to)
771
+ .some((token) => ['or', 'not', 'select', 'with'].includes(lowerWord(token) ?? '')))
772
+ return;
773
+ const join = words.lastIndexOf('join', position);
774
+ if (join < 0)
775
+ return;
776
+ const kind = words[join - 1] === 'outer' ? words[join - 2] : words[join - 1];
777
+ if (kind === 'full')
778
+ return;
779
+ // The relations between JOIN and ON, a parenthesized group included.
780
+ const introduced = new Set(relations.tables
781
+ .filter((ref) => ref.start >
782
+ tokens[own[join]].span.start.offset &&
783
+ ref.start < tokens[at].span.start.offset)
784
+ .map((ref) => ref.alias.toLowerCase()));
785
+ for (const [start, end] of conjuncts(at + 1, to))
786
+ classify(range.scope, start, end, { kind, introduced });
787
+ });
788
+ }
789
+ return { binds, equalities };
790
+ }
722
791
  /** Split the outermost SELECT list of `sql` into items. Returns null when the
723
792
  * statement has no outer `SELECT … FROM` or projects `*` / `t.*`. */
724
793
  export function splitSelectList(sql) {
@@ -835,79 +904,9 @@ function applicationColumnType(column) {
835
904
  ? column.declaredType
836
905
  : column.type;
837
906
  }
838
- function inferParamType(paramName, sql, refs, ir) {
839
- const cleaned = stripCommentsAndStrings(sql);
840
- const byName = new Map(ir.tables.map((t) => [t.name, t]));
841
- const lookup = (qualifier, column) => {
842
- if (qualifier !== undefined) {
843
- const ref = refs.find((r) => r.alias === qualifier);
844
- const table = ref && byName.get(ref.table);
845
- const resolved = table?.columns.find((c) => c.name === column);
846
- return resolved === undefined ? null : applicationColumnType(resolved);
847
- }
848
- for (const ref of refs) {
849
- const table = byName.get(ref.table);
850
- const col = table?.columns.find((c) => c.name === column);
851
- if (col !== undefined)
852
- return applicationColumnType(col);
853
- }
854
- return null;
855
- };
856
- // `col = :name`, `col >= :name`, `col <> :name`, `col LIKE :name`, etc.
857
- const cmp = new RegExp(`(?:(${IDENT})\\.)?(${IDENT})\\s*(?:=|==|!=|<>|<=|>=|<|>|\\bLIKE\\b|\\bIS\\b)\\s*:${paramName}\\b`, 'i');
858
- const cmpM = cmp.exec(cleaned);
859
- if (cmpM !== null) {
860
- const t = lookup(cmpM[1], cmpM[2]);
861
- if (t !== null)
862
- return t;
863
- }
864
- // reversed: `:name = col`
865
- const rev = new RegExp(`:${paramName}\\b\\s*(?:=|==|!=|<>|<=|>=|<|>)\\s*(?:(${IDENT})\\.)?(${IDENT})`, 'i');
866
- const revM = rev.exec(cleaned);
867
- if (revM !== null) {
868
- const t = lookup(revM[1], revM[2]);
869
- if (t !== null)
870
- return t;
871
- }
872
- // `col IN (…, :name, …)` — find the nearest `col IN (` before the param.
873
- const inRe = new RegExp(`(?:(${IDENT})\\.)?(${IDENT})\\s+IN\\s*\\(([^)]*:${paramName}\\b[^)]*)\\)`, 'i');
874
- const inM = inRe.exec(cleaned);
875
- if (inM !== null) {
876
- const t = lookup(inM[1], inM[2]);
877
- if (t !== null)
878
- return t;
879
- }
880
- // `col BETWEEN :name AND …` / `col BETWEEN … AND :name` — both endpoints
881
- // take the column's type (the §4 from+to group shape).
882
- const btwLeft = new RegExp(`(?:(${IDENT})\\.)?(${IDENT})\\s+BETWEEN\\s+:${paramName}\\b`, 'i');
883
- const btwLeftM = btwLeft.exec(cleaned);
884
- if (btwLeftM !== null) {
885
- const t = lookup(btwLeftM[1], btwLeftM[2]);
886
- if (t !== null)
887
- return t;
888
- }
889
- const btwRight = new RegExp(`(?:(${IDENT})\\.)?(${IDENT})\\s+BETWEEN\\s+\\S+\\s+AND\\s+:${paramName}\\b`, 'i');
890
- const btwRightM = btwRight.exec(cleaned);
891
- if (btwRightM !== null) {
892
- const t = lookup(btwRightM[1], btwRightM[2]);
893
- if (t !== null)
894
- return t;
895
- }
896
- // `… LIKE <expr mentioning :name>` — a LIKE operand is TEXT, even inside
897
- // a concatenation (`title like '%' || :q || '%'`, the search-fragment
898
- // shape).
899
- const like = new RegExp(`\\bLIKE\\b[^()]*:${paramName}\\b`, 'i');
900
- if (like.test(cleaned))
901
- return 'string';
902
- const match = new RegExp(`\\bMATCH\\b\\s*:${paramName}\\b`, 'i');
903
- if (match.test(cleaned))
904
- return 'string';
905
- return null;
906
- }
907
907
  /**
908
- * Return every revision-1 column/LIKE type constraint for one bind. Unlike
909
- * `inferParamType` (the legacy first-match helper), SYQL uses the complete set
910
- * so conflicting checked uses cannot be silently accepted.
908
+ * Every column/LIKE/MATCH type constraint on one bind, in pattern order. A
909
+ * `.sql` query takes the first; SYQL rejects conflicting evidence.
911
910
  */
912
911
  export function inferParamTypeEvidence(paramName, sql, refs, ir) {
913
912
  const cleaned = stripCommentsAndStrings(sql);
@@ -923,14 +922,16 @@ export function inferParamTypeEvidence(paramName, sql, refs, ir) {
923
922
  evidence.push(type);
924
923
  return;
925
924
  }
926
- const matches = refs.flatMap((ref) => {
927
- const table = byName.get(ref.table);
928
- const item = table?.columns.find((candidate) => candidate.name === column);
929
- const type = item === undefined ? undefined : applicationColumnType(item);
930
- return type === undefined ? [] : [type];
931
- });
932
- if (matches.length === 1)
933
- evidence.push(matches[0]);
925
+ // An unqualified name read by several relations is evidence only when
926
+ // every candidate column has the same type.
927
+ const matches = new Set(refs.flatMap((ref) => {
928
+ const item = byName
929
+ .get(ref.table)
930
+ ?.columns.find((candidate) => candidate.name === column);
931
+ return item === undefined ? [] : [applicationColumnType(item)];
932
+ }));
933
+ if (matches.size === 1)
934
+ evidence.push(...matches);
934
935
  };
935
936
  const comparison = new RegExp(`(?:(${IDENT})\\.)?(${IDENT})\\s*(?:=|==|!=|<>|<=|>=|<|>|\\bLIKE\\b|\\bIS\\b)\\s*:${paramName}\\b`, 'gi');
936
937
  for (const match of cleaned.matchAll(comparison)) {
@@ -961,51 +962,41 @@ export function inferParamTypeEvidence(paramName, sql, refs, ir) {
961
962
  return evidence;
962
963
  }
963
964
  // -- fallback column typing (computed expressions) ----------------------------
964
- const AGG_RE = /\b(count|sum|total|avg|min|max)\s*\(/i;
965
- /** decltype-null column: the documented honest fallback. Aggregates and
966
- * arithmetic → nullable number; everything else → nullable string. */
967
- function fallbackColumnType(expr) {
968
- if (AGG_RE.test(expr) || /[+\-*/]/.test(expr))
969
- return 'float';
970
- return 'string';
971
- }
972
- function inferReactiveMetadata(sql, refs, ir, params, columns) {
973
- const conservative = /\b(?:UNION|EXCEPT|INTERSECT)\b/i.test(stripCommentsAndStrings(sql));
974
- const requiredAt = (index) => {
975
- const cleaned = stripCommentsAndStrings(sql);
976
- const where = /\bWHERE\b/i.exec(cleaned);
977
- if (where === null || index < where.index + where[0].length)
965
+ export const AGGREGATE_FUNCTIONS = new Set([
966
+ 'avg',
967
+ 'count',
968
+ 'group_concat',
969
+ 'max',
970
+ 'min',
971
+ 'sum',
972
+ 'total',
973
+ ]);
974
+ /** True when `expr` calls an aggregate outside a subquery. `min`/`max` with
975
+ * several arguments are scalar functions, and an OVER clause makes a window
976
+ * function, which aggregates no rows. */
977
+ function callsAggregate(expr) {
978
+ const { tokens, depths, parents, closes } = sqlStructure(expr);
979
+ return tokens.some((token, index) => {
980
+ const name = lowerWord(token) ?? '';
981
+ const close = closes.get(index + 1);
982
+ if (!AGGREGATE_FUNCTIONS.has(name) || close === undefined)
978
983
  return false;
979
- const matchStack = [];
980
- for (let cursor = where.index + where[0].length; cursor < index; cursor += 1) {
981
- if (cleaned[cursor] === '(')
982
- matchStack.push(cursor);
983
- else if (cleaned[cursor] === ')')
984
- matchStack.pop();
985
- }
986
- const stack = [];
987
- for (let cursor = where.index + where[0].length; cursor < cleaned.length; cursor += 1) {
988
- const char = cleaned[cursor];
989
- if (char === '(')
990
- stack.push(cursor);
991
- else if (char === ')')
992
- stack.pop();
993
- const atBoundary = cursor === 0 || !/[A-Za-z0-9_:]/.test(cleaned[cursor - 1] ?? '');
994
- if (cursor > index &&
995
- atBoundary &&
996
- /^(?:GROUP\s+BY|ORDER\s+BY|LIMIT|HAVING)\b/i.test(cleaned.slice(cursor))) {
997
- break;
998
- }
999
- if (atBoundary &&
1000
- /^OR\b/i.test(cleaned.slice(cursor)) &&
1001
- stack.length <= matchStack.length &&
1002
- stack.every((open, stackIndex) => matchStack[stackIndex] === open)) {
984
+ for (let open = parents[index] ?? -1; open >= 0; open = parents[open] ?? -1)
985
+ if (lowerWord(tokens[open + 1]) === 'select')
1003
986
  return false;
1004
- }
1005
- }
1006
- return true;
1007
- };
1008
- const byParam = new Map(params.map((param) => [param.name, param]));
987
+ let after = close + 1;
988
+ if (lowerWord(tokens[after]) === 'filter')
989
+ after = (closes.get(after + 1) ?? after) + 1;
990
+ const scalar = (name === 'min' || name === 'max') &&
991
+ tokens
992
+ .slice(index + 2, close)
993
+ .some((arg, at) => arg.text === ',' && depths[index + 2 + at] === depths[index + 2]);
994
+ return !scalar && lowerWord(tokens[after]) !== 'over';
995
+ });
996
+ }
997
+ function inferReactiveMetadata(sql, relations, ir, columns) {
998
+ const refs = relations.tables;
999
+ const proofs = scopeProofs(sql, ir).binds;
1009
1000
  const dependencies = [];
1010
1001
  const coverage = [];
1011
1002
  const ftsOwner = new Map(ir.tables.flatMap((table) => table.ftsIndexes.map((index) => [index.name, table.name])));
@@ -1014,43 +1005,23 @@ function inferReactiveMetadata(sql, refs, ir, params, columns) {
1014
1005
  ].sort()) {
1015
1006
  const table = ir.tables.find((candidate) => candidate.name === tableName);
1016
1007
  const tableRefs = refs.filter((ref) => ref.table === tableName);
1008
+ const ref = tableRefs.length === 1 ? tableRefs[0] : undefined;
1017
1009
  const scopes = [];
1018
- if (table !== undefined && tableRefs.length === 1 && !conservative) {
1019
- const qualifier = tableRefs[0]?.alias;
1010
+ if (table !== undefined && ref !== undefined) {
1020
1011
  for (const scope of table.scopes) {
1021
- const column = scope.column.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1022
- const qualified = qualifier === undefined
1023
- ? `(?<![A-Za-z0-9_.:])(?:${IDENT}\\.)?${column}\\b`
1024
- : `(?<![A-Za-z0-9_.:])(?:${qualifier.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\.)?${column}\\b`;
1025
- const found = [];
1026
- const equality = new RegExp(`${qualified}\\s*(?:=|==|\\bIS\\b)\\s*:(${IDENT})\\b|:(${IDENT})\\b\\s*(?:=|==)\\s*${qualified}`, 'gi');
1027
- for (const match of sql.matchAll(equality)) {
1028
- if (!requiredAt(match.index))
1029
- continue;
1030
- const name = match[1] ?? match[2];
1031
- const param = name === undefined ? undefined : byParam.get(name);
1032
- if (param !== undefined) {
1033
- found.push(param.name);
1034
- }
1035
- }
1036
- const inList = new RegExp(`${qualified}\\s+IN\\s*\\(([^)]*)\\)`, 'gi');
1037
- for (const match of sql.matchAll(inList)) {
1038
- if (!requiredAt(match.index))
1039
- continue;
1040
- for (const paramMatch of (match[1] ?? '').matchAll(/:([A-Za-z_][A-Za-z0-9_]*)/g)) {
1041
- const param = byParam.get(paramMatch[1]);
1042
- if (param !== undefined) {
1043
- found.push(param.name);
1044
- }
1045
- }
1046
- }
1047
- const unique = [...new Set(found)];
1048
- if (unique.length > 0) {
1012
+ const params = [
1013
+ ...new Set(proofs
1014
+ .filter((proof) => proof.scope === relations.scopeAt(ref.start) &&
1015
+ proof.alias === ref.alias.toLowerCase() &&
1016
+ proof.column === scope.column.toLowerCase())
1017
+ .flatMap((proof) => proof.params)),
1018
+ ];
1019
+ if (params.length > 0) {
1049
1020
  scopes.push({
1050
1021
  table: tableName,
1051
1022
  variable: scope.variable,
1052
1023
  pattern: scope.pattern,
1053
- params: unique,
1024
+ params,
1054
1025
  });
1055
1026
  }
1056
1027
  }
@@ -1180,24 +1151,8 @@ export function lowerFtsSourceIds(sql, ir) {
1180
1151
  const relations = scanRelations(sql, ir);
1181
1152
  if (!relations.tables.some((ref) => ftsNames.has(ref.table)))
1182
1153
  return sql;
1183
- const tokens = lexSyqlSqlSource('query SQL', sql).filter((token) => !isSyqlTrivia(token) && token.kind !== 'eof');
1184
- const depths = [];
1185
- const closes = new Map();
1186
- const stack = [];
1187
- tokens.forEach((token, index) => {
1188
- if (token.text === ')') {
1189
- const open = stack.pop();
1190
- if (open !== undefined)
1191
- closes.set(open, index);
1192
- }
1193
- depths[index] = stack.length;
1194
- if (token.text === '(')
1195
- stack.push(index);
1196
- });
1197
- const lower = (index) => {
1198
- const token = tokens[index];
1199
- return token?.kind === 'identifier' ? token.text.toLowerCase() : undefined;
1200
- };
1154
+ const { tokens, depths, closes } = sqlStructure(sql);
1155
+ const lower = (index) => lowerWord(tokens[index]);
1201
1156
  // Every SELECT scope as an offset range; the outer statement spans all.
1202
1157
  const scopes = [
1203
1158
  { start: -1, end: sql.length + 1 },
@@ -1338,16 +1293,19 @@ export function analyzeStatement(name, location, statementText, ir, db, naming =
1338
1293
  }
1339
1294
  // SELECT-only (the read tier). A `WITH` is allowed when its main statement
1340
1295
  // is a SELECT (SQLite also allows WITH … INSERT/UPDATE/DELETE — writes).
1341
- const firstKeyword = /^\s*([A-Za-z]+)/.exec(stripCommentsAndStrings(statementText).trimStart())?.[1];
1342
- const upperKeyword = firstKeyword?.toUpperCase();
1343
- if (upperKeyword === 'WITH') {
1344
- const main = mainVerbAfterWith(stripCommentsAndStrings(statementText));
1345
- if (main !== 'SELECT') {
1346
- throw new TypegenError(file, `named queries are SELECT-only (the read tier); this WITH statement's main verb is ${JSON.stringify(main ?? '')}. Writes go through mutate() (SPEC §7.1).`);
1296
+ // CTE bodies sit inside parentheses and a bare keyword cannot name a CTE,
1297
+ // so the first depth-0 verb after `WITH` is the main verb.
1298
+ const statement = sqlStructure(statementText, file);
1299
+ const firstKeyword = lowerWord(statement.tokens[0]);
1300
+ if (firstKeyword === 'with') {
1301
+ const main = statement.tokens.find((token, index) => statement.depths[index] === 0 &&
1302
+ ['select', 'values', 'insert', 'update', 'delete', 'replace'].includes(lowerWord(token) ?? ''));
1303
+ if (lowerWord(main) !== 'select') {
1304
+ throw new TypegenError(file, `named queries are SELECT-only (the read tier); this WITH statement's main verb is ${JSON.stringify(main?.text.toUpperCase() ?? '')}. Writes go through mutate() (SPEC §7.1).`);
1347
1305
  }
1348
1306
  }
1349
- else if (upperKeyword !== 'SELECT') {
1350
- throw new TypegenError(file, `named queries are SELECT-only (the read tier); found ${JSON.stringify(firstKeyword ?? '')}. Writes go through mutate() (SPEC §7.1).`);
1307
+ else if (firstKeyword !== 'select') {
1308
+ throw new TypegenError(file, `named queries are SELECT-only (the read tier); found ${JSON.stringify(statement.tokens[0]?.text ?? '')}. Writes go through mutate() (SPEC §7.1).`);
1351
1309
  }
1352
1310
  // Let SQLite validate + describe the AUTHORED query first. Any bad
1353
1311
  // reference throws here with SQLite's own message.
@@ -1412,6 +1370,38 @@ export function analyzeStatement(name, location, statementText, ir, db, naming =
1412
1370
  : { ...source, nullableByJoin: source.nullableByJoin || cteRef.nullable };
1413
1371
  };
1414
1372
  const items = splitSelectList(sql);
1373
+ // A bare column of a SELECT that aggregates without GROUP BY reads NULL
1374
+ // when no row matches, and a compound SELECT returns the rows of every
1375
+ // branch, so a column is nullable when any branch can yield NULL there.
1376
+ const outer = sqlStructure(sql);
1377
+ const cuts = outer.tokens.flatMap((token, index) => outer.depths[index] === 0 &&
1378
+ ['union', 'intersect', 'except'].includes(lowerWord(token) ?? '')
1379
+ ? [token.span]
1380
+ : []);
1381
+ const branches = [0, ...cuts.map((span) => span.end.offset)].map((start, index) => {
1382
+ const end = cuts[index]?.start.offset ?? sql.length;
1383
+ const branchItems = splitSelectList(sql.slice(start, end));
1384
+ const grouped = outer.tokens.some((token, at) => outer.depths[at] === 0 &&
1385
+ token.span.start.offset >= start &&
1386
+ token.span.start.offset < end &&
1387
+ lowerWord(token) === 'group');
1388
+ const aggregated = !grouped &&
1389
+ (branchItems?.some((item) => callsAggregate(item.expr)) ?? false);
1390
+ return { start, end, items: branchItems, aggregated };
1391
+ });
1392
+ const branchNullable = (index) => branches.some((branch, position) => {
1393
+ if (branch.aggregated)
1394
+ return true;
1395
+ if (position === 0)
1396
+ return false;
1397
+ const item = branch.items?.[index];
1398
+ const source = item === undefined
1399
+ ? null
1400
+ : resolveSource(item, scoped.tables.filter((ref) => scoped.scopeAt(ref.start) === 'main' &&
1401
+ ref.start >= branch.start &&
1402
+ ref.start < branch.end), ir);
1403
+ return source === null || source.column.nullable || source.nullableByJoin;
1404
+ });
1415
1405
  const columns = described.columnNames.map((colName, index) => {
1416
1406
  const decl = described.declaredTypes[index];
1417
1407
  const item = items?.[index];
@@ -1427,7 +1417,7 @@ export function analyzeStatement(name, location, statementText, ir, db, naming =
1427
1417
  name: sqlName,
1428
1418
  langName,
1429
1419
  type: 'integer',
1430
- nullable: syncVersionNullable,
1420
+ nullable: syncVersionNullable || branchNullable(index),
1431
1421
  fidelity: 'exact',
1432
1422
  };
1433
1423
  }
@@ -1435,24 +1425,44 @@ export function analyzeStatement(name, location, statementText, ir, db, naming =
1435
1425
  // Exact: app-facing queries read the decrypted local mirror. Encrypted
1436
1426
  // columns therefore expose their pre-wire declaredType, while the schema
1437
1427
  // IR and mutation codec continue to carry the bytes wire type.
1438
- const sourceType = source.column.encrypted === true &&
1439
- source.column.declaredType !== undefined
1440
- ? source.column.declaredType
1441
- : source.column.type;
1442
1428
  return {
1443
1429
  name: sqlName,
1444
1430
  langName,
1445
- type: sourceType,
1446
- nullable: source.column.nullable || source.nullableByJoin,
1431
+ type: applicationColumnType(source.column),
1432
+ nullable: source.column.nullable ||
1433
+ source.nullableByJoin ||
1434
+ branchNullable(index),
1447
1435
  fidelity: 'exact',
1448
1436
  origin: { table: source.table, column: source.column.name },
1449
1437
  };
1450
1438
  }
1451
- // Fallback: decltype affinity if present, else the expr-shape fallback.
1452
- const mapped = decl !== null && decl !== undefined
1453
- ? DECLTYPE_MAP[decl.toUpperCase()]
1439
+ // Computed column (SYQL §14.1): the type is the decltype SQLite reports,
1440
+ // else the target of a whole-expression CAST, else integer for a
1441
+ // whole-expression count(); anything else is refused.
1442
+ const { tokens, depths, closes } = sqlStructure(item?.expr ?? '');
1443
+ const whole = tokens[1]?.text === '(' && closes.get(1) === tokens.length - 1
1444
+ ? lowerWord(tokens[0])
1454
1445
  : undefined;
1455
- const type = mapped ?? (item !== undefined ? fallbackColumnType(item.expr) : 'string');
1446
+ const asAt = tokens.findLastIndex((token, at) => depths[at] === 1 && lowerWord(token) === 'as');
1447
+ const castType = tokens
1448
+ .flatMap((token, at) => at > asAt && depths[at] === 1 && token.kind === 'identifier'
1449
+ ? [token.text.toUpperCase()]
1450
+ : [])
1451
+ .join(' ');
1452
+ const type = (decl !== null && decl !== undefined
1453
+ ? DECLTYPE_MAP[decl.toUpperCase()]
1454
+ : undefined) ??
1455
+ // SQLite gives the other mapped names NUMERIC or BLOB affinity, which
1456
+ // can change the value's storage class.
1457
+ (whole === 'cast' &&
1458
+ asAt > 0 &&
1459
+ /^(INTEGER|INT|BIGINT|SMALLINT|TEXT|REAL|FLOAT|DOUBLE|BLOB|BOOLEAN|BOOL)$/.test(castType)
1460
+ ? DECLTYPE_MAP[castType]
1461
+ : undefined) ??
1462
+ (whole === 'count' ? 'integer' : undefined);
1463
+ if (type === undefined) {
1464
+ throw new TypegenError(file, 'computed result column has no declared type; wrap the expression in CAST(... AS <type>)', 'typegen.computed_column_type', { query: name, column: sqlName });
1465
+ }
1456
1466
  return {
1457
1467
  name: sqlName,
1458
1468
  langName,
@@ -1479,8 +1489,8 @@ export function analyzeStatement(name, location, statementText, ir, db, naming =
1479
1489
  if (commented !== undefined) {
1480
1490
  return { name: paramName, langName, type: commented, source: 'comment' };
1481
1491
  }
1482
- const inferred = inferParamType(paramName, sql, refs, ir);
1483
- if (inferred !== null) {
1492
+ const inferred = inferParamTypeEvidence(paramName, sql, refs, ir)[0];
1493
+ if (inferred !== undefined) {
1484
1494
  return { name: paramName, langName, type: inferred, source: 'inferred' };
1485
1495
  }
1486
1496
  throw new TypegenError(file, `cannot infer a type for param :${paramName} (it is not compared to a plain column). Add a header comment: \`-- param :${paramName} <type>\` (one of ${[...PARAM_TYPES].join(', ')}).`);
@@ -1491,29 +1501,41 @@ export function analyzeStatement(name, location, statementText, ir, db, naming =
1491
1501
  throw new TypegenError(file, `-- param :${cp.name} names a parameter the query does not use`);
1492
1502
  }
1493
1503
  }
1494
- const reactive = inferReactiveMetadata(sql, refs, ir, params, columns);
1504
+ validateQueryIdentifier(name, naming.targets, file, 'query name');
1505
+ for (const column of columns) {
1506
+ validateQueryIdentifier(column.langName, naming.targets, file, 'result column');
1507
+ }
1508
+ for (const param of params) {
1509
+ if (!internalParams.has(param.name))
1510
+ validateQueryIdentifier(param.langName, naming.targets, file, 'param');
1511
+ }
1512
+ const reactive = inferReactiveMetadata(sql, scoped, ir, columns);
1513
+ // The emitted statement is what SQLite checks: the positional form, with
1514
+ // FTS source-id lowering applied, must prepare to the same result columns
1515
+ // and take exactly one value per distinct param.
1495
1516
  const physical = lowerFtsSourceIds(sql, ir);
1496
- if (physical !== sql) {
1497
- const lowered = analyze(physical);
1498
- if (lowered.paramsCount !== described.paramsCount ||
1499
- lowered.columnNames.length !== described.columnNames.length ||
1500
- lowered.columnNames.some((n, i) => n !== described.columnNames[i])) {
1501
- throw new TypegenError(file, `internal: FTS source-id lowering changed the result columns [${described.columnNames.join(', ')}] to [${lowered.columnNames.join(', ')}]`);
1502
- }
1517
+ const positionalSql = toPositionalSql(physical);
1518
+ const emitted = analyze(positionalSql);
1519
+ if (emitted.paramsCount !== params.length ||
1520
+ emitted.columnNames.join('\0') !== described.columnNames.join('\0')) {
1521
+ throw new TypegenError(file, 'internal: the emitted positional SQL does not prepare to the analyzed columns and params', 'typegen.emitted_sql_mismatch', {
1522
+ columns: described.columnNames,
1523
+ emittedColumns: emitted.columnNames,
1524
+ params: params.length,
1525
+ emittedParams: emitted.paramsCount,
1526
+ });
1503
1527
  }
1504
1528
  return {
1505
1529
  name,
1506
1530
  file,
1507
1531
  sourceSql,
1508
1532
  sql: physical,
1509
- positionalSql: toPositionalSql(physical),
1510
- relations: scanTableRefs(toPositionalSql(physical), ir, true).map((ref) => ({
1533
+ positionalSql,
1534
+ relations: scanTableRefs(positionalSql, ir, true).map((ref) => ({
1511
1535
  table: ref.table,
1512
1536
  start: ref.start,
1513
1537
  end: ref.end,
1514
- ...(ref.explicitAlias === undefined
1515
- ? {}
1516
- : { alias: ref.explicitAlias }),
1538
+ ...(ref.explicitAlias === undefined ? {} : { alias: ref.explicitAlias }),
1517
1539
  })),
1518
1540
  params,
1519
1541
  columns,
@@ -1533,7 +1555,7 @@ export function analyzeStatement(name, location, statementText, ir, db, naming =
1533
1555
  * missing one errors, naming the file + the statement's position/first line.
1534
1556
  */
1535
1557
  export function analyzeQueryFile(relPath, content, ir, db, naming = DEFAULT_NAMING) {
1536
- const statements = splitStatements(content);
1558
+ const statements = splitStatements(content, relPath);
1537
1559
  if (statements.length === 0) {
1538
1560
  throw new TypegenError(relPath, 'query file is empty');
1539
1561
  }
@@ -1554,7 +1576,7 @@ export function analyzeQueryFile(relPath, content, ir, db, naming = DEFAULT_NAMI
1554
1576
  * (or a `-- name:` override) and analyze. Rejects a `;`-separated file loudly
1555
1577
  * — use {@link analyzeQueryFile} for the multi-statement contract. */
1556
1578
  export function analyzeQuery(file, raw, ir, db, naming = DEFAULT_NAMING) {
1557
- const statements = splitStatements(raw);
1579
+ const statements = splitStatements(raw, file);
1558
1580
  if (statements.length > 1) {
1559
1581
  throw new TypegenError(file, 'a query file holds exactly one SELECT statement here (found a `;` separating statements) — use analyzeQueryFile for multi-statement files');
1560
1582
  }