@syncular/typegen 0.58.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/src/query.ts CHANGED
@@ -64,8 +64,13 @@
64
64
  import { TypegenError } from './errors';
65
65
  import { isSyqlTrivia, lexSyqlSqlSource, type SyqlToken } from './syql-lexer';
66
66
  import type { IrColumn, IrColumnType, IrDocument, IrTable } from './ir';
67
- import { findProjection, lowerProjection, mainVerbAfterWith } from './lower';
68
- import { buildNamingMap, type NamingMode, type NamingTarget } from './naming';
67
+ import { findProjection, lowerProjection } from './lower';
68
+ import {
69
+ buildNamingMap,
70
+ type NamingMode,
71
+ type NamingTarget,
72
+ validateQueryIdentifier,
73
+ } from './naming';
69
74
 
70
75
  /** The SQL decltype keyword → §2.4 type map (mirrors sql.ts TYPE_MAP so a
71
76
  * plain column ref's decltype resolves to the exact IR type). */
@@ -116,7 +121,7 @@ export interface QueryColumn {
116
121
  readonly type: IrColumnType;
117
122
  readonly nullable: boolean;
118
123
  /** `exact` = resolved to an IR column (plain ref); `fallback` = a computed
119
- * expression typed by the documented honest fallback. */
124
+ * expression typed by its `CAST` or `count()` (SYQL §14.1). */
120
125
  readonly fidelity: 'exact' | 'fallback';
121
126
  /** Proven physical origin for reactive row identity and diagnostics. */
122
127
  readonly origin?: { readonly table: string; readonly column: string };
@@ -304,6 +309,16 @@ export interface AnalyzedQuery {
304
309
  readonly syql?: QuerySyqlMetadata;
305
310
  }
306
311
 
312
+ /** The public SYQL input an emitter binds by name. */
313
+ export function syqlInput(
314
+ query: AnalyzedQuery,
315
+ name: string,
316
+ ): QuerySyqlPublicInput {
317
+ const input = query.syql?.inputs.find((candidate) => candidate.name === name);
318
+ if (input === undefined) throw new Error(`unknown SYQL input ${name}`);
319
+ return input;
320
+ }
321
+
307
322
  // -- path → name --------------------------------------------------------------
308
323
 
309
324
  /** A single kebab-case path segment (folder) or filename stem. */
@@ -381,13 +396,38 @@ export function validateOverrideName(name: string, location: string): string {
381
396
  return name;
382
397
  }
383
398
 
384
- // -- statement splitting ------------------------------------------------------
399
+ // -- SQL tokens -----------------------------------------------------------------
385
400
  //
386
- // A file may hold multiple statements separated by top-level `;`. We split on
387
- // semicolons that are NOT inside a single-quoted string, a `--` line comment,
388
- // or a `/* */` block comment. The SELECT-only subset has no BEGIN/END blocks,
389
- // so top-level semicolons are unambiguous statement terminators. A trailing
390
- // semicolon on the last statement is optional (an empty tail is dropped).
401
+ // Every scan below reads the lossless SYQL lexer (`syql-lexer.ts`), so string
402
+ // literals, quoted identifiers, and comments are atomic tokens: their text is
403
+ // never split, rescanned, or rewritten. SQLite remains the authority on
404
+ // correctness.
405
+
406
+ /** Significant SQL tokens (no trivia) with each token's parenthesis depth,
407
+ * its innermost enclosing `(` (or -1), and every `(` index's matching `)`. */
408
+ function sqlStructure(sql: string, file = 'query SQL') {
409
+ const tokens = lexSyqlSqlSource(file, sql).filter(
410
+ (token) => !isSyqlTrivia(token) && token.kind !== 'eof',
411
+ );
412
+ const depths: number[] = [];
413
+ const parents: number[] = [];
414
+ const closes = new Map<number, number>();
415
+ const stack: number[] = [];
416
+ tokens.forEach((token, index) => {
417
+ if (token.text === ')') {
418
+ const open = stack.pop();
419
+ if (open !== undefined) closes.set(open, index);
420
+ }
421
+ parents[index] = stack.at(-1) ?? -1;
422
+ depths[index] = stack.length;
423
+ if (token.text === '(') stack.push(index);
424
+ });
425
+ return { tokens, depths, parents, closes };
426
+ }
427
+
428
+ function lowerWord(token: SyqlToken | undefined): string | undefined {
429
+ return token?.kind === 'identifier' ? token.text.toLowerCase() : undefined;
430
+ }
391
431
 
392
432
  export interface SplitStatement {
393
433
  /** The raw statement text (leading comment block + SQL), as written. */
@@ -396,113 +436,56 @@ export interface SplitStatement {
396
436
  readonly startLine: number;
397
437
  }
398
438
 
399
- /** Split file content into statements on top-level `;` (string/comment-aware).
400
- * Blank/comment-only tails are dropped; each statement keeps its own leading
401
- * comment block (everything between the previous `;` and this SQL). */
402
- export function splitStatements(content: string): SplitStatement[] {
403
- const parts: { text: string; startOffset: number }[] = [];
404
- let i = 0;
405
- let start = 0;
406
- while (i < content.length) {
407
- const ch = content[i] as string;
408
- const next = content[i + 1];
409
- if (ch === '-' && next === '-') {
410
- const end = content.indexOf('\n', i);
411
- i = end === -1 ? content.length : end;
412
- } else if (ch === '/' && next === '*') {
413
- const end = content.indexOf('*/', i + 2);
414
- i = end === -1 ? content.length : end + 2;
415
- } else if (ch === "'") {
416
- let j = i + 1;
417
- for (;;) {
418
- if (j >= content.length) break;
419
- if (content[j] === "'") {
420
- if (content[j + 1] === "'") j += 2;
421
- else {
422
- j += 1;
423
- break;
424
- }
425
- } else j += 1;
439
+ /** Split file content into statements on top-level `;` tokens. A statement
440
+ * keeps its leading comment block; comment-only parts are dropped. The
441
+ * SELECT-only subset has no BEGIN/END blocks, so every `;` token ends one
442
+ * statement and a trailing `;` is optional. */
443
+ export function splitStatements(
444
+ content: string,
445
+ file = 'query SQL',
446
+ ): SplitStatement[] {
447
+ const out: SplitStatement[] = [];
448
+ let lead: SyqlToken | undefined;
449
+ let hasSql = false;
450
+ for (const token of lexSyqlSqlSource(file, content)) {
451
+ if (token.kind === 'eof' || token.text === ';') {
452
+ if (lead !== undefined && hasSql) {
453
+ out.push({
454
+ text: content.slice(lead.span.start.offset, token.span.start.offset),
455
+ startLine: lead.span.start.line,
456
+ });
426
457
  }
427
- i = j;
428
- } else if (ch === ';') {
429
- parts.push({ text: content.slice(start, i), startOffset: start });
430
- i += 1;
431
- start = i;
432
- } else {
433
- i += 1;
458
+ lead = undefined;
459
+ hasSql = false;
460
+ } else if (token.kind !== 'whitespace') {
461
+ lead ??= token;
462
+ hasSql ||= !isSyqlTrivia(token);
434
463
  }
435
464
  }
436
- if (start < content.length) {
437
- parts.push({ text: content.slice(start), startOffset: start });
438
- }
439
- const out: SplitStatement[] = [];
440
- for (const part of parts) {
441
- // Drop parts whose SQL body (comments/whitespace stripped) is empty — a
442
- // trailing `;` or a comment-only tail is not a statement.
443
- if (stripCommentsAndStrings(part.text).trim().length === 0) continue;
444
- // Report the line of the statement's first NON-blank character (skip the
445
- // blank/whitespace run after the previous `;`), so the location points at
446
- // real content, not the terminator's trailing newline.
447
- const lead = part.text.length - part.text.replace(/^\s+/, '').length;
448
- const before = content.slice(0, part.startOffset + lead);
449
- const startLine = before.split('\n').length;
450
- out.push({ text: part.text.replace(/^\s+/, ''), startLine });
451
- }
452
465
  return out;
453
466
  }
454
467
 
455
- /** The leading comment block of a statement: the run of blank + line/block
456
- * comment lines before the first SQL token. `-- name:` / `-- param` markers are
457
- * scoped HERE (to the statement they directly precede), not file-wide. Returns
458
- * the `--` comment lines (block comments carry no markers we parse). */
459
- function leadingCommentBlock(statementText: string): string[] {
460
- const lines: string[] = [];
461
- let i = 0;
462
- while (i < statementText.length) {
463
- // Skip leading whitespace on the line.
464
- while (
465
- i < statementText.length &&
466
- /[ \t\r]/.test(statementText[i] as string)
467
- ) {
468
- i += 1;
469
- }
470
- const ch = statementText[i];
471
- const next = statementText[i + 1];
472
- if (ch === '\n') {
473
- i += 1;
474
- continue;
475
- }
476
- if (ch === '-' && next === '-') {
477
- const end = statementText.indexOf('\n', i);
478
- const line = statementText.slice(i, end === -1 ? undefined : end);
479
- lines.push(line);
480
- i = end === -1 ? statementText.length : end + 1;
481
- continue;
482
- }
483
- if (ch === '/' && next === '*') {
484
- const end = statementText.indexOf('*/', i + 2);
485
- // Block comments carry no markers we parse; skip and keep scanning for
486
- // more leading comments (but they don't contribute -- name:/-- param).
487
- i = end === -1 ? statementText.length : end + 2;
488
- continue;
489
- }
490
- break; // first SQL token — the comment block ends here
468
+ /** The `--` comments before a statement's first SQL token. `-- name:` and
469
+ * `-- param` markers are scoped to the statement they precede. */
470
+ function leadingComments(statementText: string): string[] {
471
+ const comments: string[] = [];
472
+ for (const token of lexSyqlSqlSource('query SQL', statementText)) {
473
+ if (!isSyqlTrivia(token)) break;
474
+ if (token.kind === 'line-comment') comments.push(token.text);
491
475
  }
492
- return lines;
476
+ return comments;
493
477
  }
494
478
 
495
- const NAME_MARKER_RE = /^\s*--\s*name:\s*(\S+)\s*$/;
479
+ const NAME_MARKER_RE = /^--\s*name:\s*(\S+)\s*$/;
496
480
 
497
481
  /** The `-- name:` override in a statement's leading comment block, or null. */
498
482
  export function parseStatementName(
499
483
  statementText: string,
500
484
  location: string,
501
485
  ): string | null {
502
- const lines = leadingCommentBlock(statementText);
503
486
  let found: string | null = null;
504
- for (const line of lines) {
505
- const m = NAME_MARKER_RE.exec(line);
487
+ for (const comment of leadingComments(statementText)) {
488
+ const m = NAME_MARKER_RE.exec(comment);
506
489
  if (m === null) continue;
507
490
  if (found !== null) {
508
491
  throw new TypegenError(
@@ -515,20 +498,13 @@ export function parseStatementName(
515
498
  return found;
516
499
  }
517
500
 
518
- // -- lightweight SQL scanning -------------------------------------------------
519
- //
520
- // We do NOT re-implement SQLite's parser (that is what prepare() is for). We
521
- // scan the text only for: header `-- param` comments, `:name` placeholders,
522
- // FROM/JOIN table identifiers, and SELECT-column → source resolution. SQLite
523
- // remains the authority on correctness.
524
-
525
501
  interface CommentParam {
526
502
  readonly name: string;
527
503
  readonly type: IrColumnType;
528
504
  }
529
505
 
530
506
  const PARAM_COMMENT_RE =
531
- /^\s*--\s*param\s+:([A-Za-z_][A-Za-z0-9_]*)\s+([A-Za-z_]+)\s*$/;
507
+ /^--\s*param\s+:([A-Za-z_][A-Za-z0-9_]*)\s+([A-Za-z_]+)\s*$/;
532
508
 
533
509
  const PARAM_TYPES = new Set<string>([
534
510
  'string',
@@ -541,14 +517,11 @@ const PARAM_TYPES = new Set<string>([
541
517
  'crdt',
542
518
  ]);
543
519
 
544
- /** Parse `-- param :name <type>` header lines. They must precede the SQL. */
520
+ /** Parse the `-- param :name <type>` markers of the leading comment block. */
545
521
  function parseCommentParams(file: string, raw: string): CommentParam[] {
546
522
  const out: CommentParam[] = [];
547
- for (const line of raw.split('\n')) {
548
- const trimmed = line.trim();
549
- if (trimmed.length === 0) continue;
550
- if (!trimmed.startsWith('--')) break; // first non-comment line ends the header
551
- const match = PARAM_COMMENT_RE.exec(line);
523
+ for (const comment of leadingComments(raw)) {
524
+ const match = PARAM_COMMENT_RE.exec(comment);
552
525
  if (match === null) continue; // an ordinary comment, ignore
553
526
  const name = match[1] as string;
554
527
  const type = (match[2] as string).toLowerCase();
@@ -563,65 +536,35 @@ function parseCommentParams(file: string, raw: string): CommentParam[] {
563
536
  return out;
564
537
  }
565
538
 
566
- /** Strip `--` and block comments so identifier scans don't hit commented SQL,
567
- * and string literals so `':x'` inside a string isn't read as a param. */
539
+ /** Blank comments and string/blob literals with spaces (newlines kept), so
540
+ * offset-preserving text scans never read inside them. */
568
541
  export function stripCommentsAndStrings(sql: string): string {
569
- let out = '';
570
- let i = 0;
571
- while (i < sql.length) {
572
- const ch = sql[i] as string;
573
- const next = sql[i + 1];
574
- if (ch === '-' && next === '-') {
575
- const end = sql.indexOf('\n', i);
576
- const stop = end === -1 ? sql.length : end;
577
- out += ' '.repeat(stop - i);
578
- i = stop;
579
- } else if (ch === '/' && next === '*') {
580
- const end = sql.indexOf('*/', i + 2);
581
- const stop = end === -1 ? sql.length : end + 2;
582
- out += sql.slice(i, stop).replace(/[^\n]/g, ' ');
583
- i = stop;
584
- } else if (ch === "'") {
585
- let stop = i + 1;
586
- while (stop < sql.length) {
587
- if (sql[stop] === "'" && sql[stop + 1] === "'") {
588
- stop += 2;
589
- continue;
590
- }
591
- if (sql[stop] === "'") {
592
- stop += 1;
593
- break;
594
- }
595
- stop += 1;
596
- }
597
- out += sql.slice(i, stop).replace(/[^\n]/g, ' ');
598
- i = stop;
599
- } else {
600
- out += ch;
601
- i += 1;
602
- }
603
- }
604
- return out;
542
+ return lexSyqlSqlSource('query SQL', sql)
543
+ .map((token) =>
544
+ token.kind === 'line-comment' ||
545
+ token.kind === 'block-comment' ||
546
+ token.kind === 'string' ||
547
+ token.kind === 'blob'
548
+ ? token.text.replace(/[^\n]/g, ' ')
549
+ : token.text,
550
+ )
551
+ .join('');
605
552
  }
606
553
 
607
- /** Distinct `:name` placeholders in first-occurrence order (dedup by name,
608
- * matching SQLite's paramsCount which counts distinct names). */
554
+ /** Distinct `:name` binds in first-occurrence order (SQLite's paramsCount
555
+ * counts distinct names). */
609
556
  function scanParamNames(sql: string): string[] {
610
- const cleaned = stripCommentsAndStrings(sql);
611
- // `:name` but not `::` (Postgres cast) — our subset never has `::`.
612
- const re = /:([A-Za-z_][A-Za-z0-9_]*)/g;
613
- const seen: string[] = [];
614
- for (const m of cleaned.matchAll(re)) {
615
- const name = m[1] as string;
616
- if (!seen.includes(name)) seen.push(name);
557
+ const names: string[] = [];
558
+ for (const token of lexSyqlSqlSource('query SQL', sql)) {
559
+ const name = token.text.slice(1);
560
+ if (token.kind === 'bind' && !names.includes(name)) names.push(name);
617
561
  }
618
- return seen;
562
+ return names;
619
563
  }
620
564
 
621
- /** Rewrite `:name` → positional placeholders AND strip comments + collapse
622
- * whitespace, producing a clean single-line SQL string suitable for
623
- * embedding in a generated string literal. String literals are preserved
624
- * verbatim (their inner whitespace is not collapsed).
565
+ /** The emitted single-line statement: `:name` binds become positional, every
566
+ * whitespace/comment run between tokens becomes one space, and every other
567
+ * token (string literals, quoted identifiers) keeps its exact text.
625
568
  *
626
569
  * When every param occurs exactly once, plain `?` is emitted. When ANY
627
570
  * param repeats (the §7 neutralization guards mention a param in the guard
@@ -629,74 +572,27 @@ function scanParamNames(sql: string): string[] {
629
572
  * form — one bound value per DISTINCT param, reuse resolved by SQLite —
630
573
  * so the generated bind array stays one-entry-per-param. */
631
574
  export function toPositionalSql(sql: string): string {
632
- interface ParamToken {
633
- readonly param: string;
634
- }
635
- const tokens: (string | ParamToken)[] = [];
636
- let i = 0;
637
- while (i < sql.length) {
638
- const ch = sql[i] as string;
639
- const next = sql[i + 1];
640
- if (ch === '-' && next === '-') {
641
- const end = sql.indexOf('\n', i);
642
- i = end === -1 ? sql.length : end + 1;
643
- tokens.push(' ');
644
- } else if (ch === '/' && next === '*') {
645
- const end = sql.indexOf('*/', i + 2);
646
- i = end === -1 ? sql.length : end + 2;
647
- tokens.push(' ');
648
- } else if (ch === "'") {
649
- // Preserve string literals verbatim (including any '' escapes).
650
- let j = i + 1;
651
- let lit = "'";
652
- for (;;) {
653
- if (j >= sql.length) break;
654
- if (sql[j] === "'") {
655
- if (sql[j + 1] === "'") {
656
- lit += "''";
657
- j += 2;
658
- } else {
659
- lit += "'";
660
- j += 1;
661
- break;
662
- }
663
- } else {
664
- lit += sql[j];
665
- j += 1;
666
- }
667
- }
668
- tokens.push(lit);
669
- i = j;
670
- } else if (ch === ':' && next !== undefined && /[A-Za-z_]/.test(next)) {
671
- let end = i + 1;
672
- while (end < sql.length && /[A-Za-z0-9_]/.test(sql[end] as string)) {
673
- end += 1;
674
- }
675
- tokens.push({ param: sql.slice(i + 1, end) });
676
- i = end;
677
- } else if (/\s/.test(ch)) {
678
- tokens.push(' ');
679
- i += 1;
680
- } else {
681
- tokens.push(ch);
682
- i += 1;
683
- }
684
- }
685
- const order: string[] = [];
686
- let repeats = false;
575
+ const tokens = lexSyqlSqlSource('query SQL', sql);
576
+ const order = scanParamNames(sql);
577
+ const repeats =
578
+ tokens.filter((token) => token.kind === 'bind').length > order.length;
579
+ let out = '';
580
+ let space = false;
687
581
  for (const token of tokens) {
688
- if (typeof token === 'string') continue;
689
- if (order.includes(token.param)) repeats = true;
690
- else order.push(token.param);
582
+ if (isSyqlTrivia(token) || token.kind === 'eof') {
583
+ space = out.length > 0;
584
+ continue;
585
+ }
586
+ if (space) out += ' ';
587
+ space = false;
588
+ out +=
589
+ token.kind !== 'bind'
590
+ ? token.text
591
+ : repeats
592
+ ? `?${order.indexOf(token.text.slice(1)) + 1}`
593
+ : '?';
691
594
  }
692
- const rendered = tokens
693
- .map((token) => {
694
- if (typeof token === 'string') return token;
695
- return repeats ? `?${order.indexOf(token.param) + 1}` : '?';
696
- })
697
- .join('');
698
- // Collapse runs of the whitespace placeholders; trim.
699
- return rendered.replace(/\s+/g, ' ').trim();
595
+ return out;
700
596
  }
701
597
 
702
598
  // -- FROM/JOIN table resolution ----------------------------------------------
@@ -739,7 +635,10 @@ const RESERVED_ALIAS = new Set([
739
635
  'indexed',
740
636
  'not',
741
637
  ]);
742
- function sqlIdentifier(token: SyqlToken | undefined): string | undefined {
638
+ /** The name a plain or quoted identifier token spells. */
639
+ export function sqlIdentifier(
640
+ token: SyqlToken | undefined,
641
+ ): string | undefined {
743
642
  if (token?.kind === 'identifier') return token.text;
744
643
  if (token?.kind !== 'quoted-identifier') return undefined;
745
644
  const quote = token.text[0];
@@ -793,9 +692,7 @@ export function scanRelations(
793
692
  ir: IrDocument,
794
693
  lowered = false,
795
694
  ): SqlRelations {
796
- const tokens = lexSyqlSqlSource('query SQL', sql).filter(
797
- (token) => !isSyqlTrivia(token) && token.kind !== 'eof',
798
- );
695
+ const { tokens, depths, parents, closes } = sqlStructure(sql);
799
696
  const known = new Map(
800
697
  ir.tables.flatMap((table) => [
801
698
  [table.name.toLowerCase(), table.name] as const,
@@ -813,19 +710,6 @@ export function scanRelations(
813
710
  )
814
711
  : [],
815
712
  );
816
- const closes = new Map<number, number>();
817
- const parents: number[] = [];
818
- const depths: number[] = [];
819
- const stack: number[] = [];
820
- tokens.forEach((token, index) => {
821
- if (token.text === ')') {
822
- const open = stack.pop();
823
- if (open !== undefined) closes.set(open, index);
824
- }
825
- parents[index] = stack.at(-1) ?? -1;
826
- depths[index] = stack.length;
827
- if (token.text === '(') stack.push(index);
828
- });
829
713
  const activeFromDepths = new Set<number>();
830
714
  const clauseEnders = new Set([
831
715
  'where',
@@ -1062,6 +946,268 @@ export function scanRelations(
1062
946
  };
1063
947
  }
1064
948
 
949
+ // -- scope proofs (SYQL §13) ----------------------------------------------------
950
+
951
+ /** A declared scope column of one table instance: the instance's SELECT scope
952
+ * ({@link SqlRelations.scopeAt}), lowercased alias, and lowercased column. */
953
+ export interface ScopeColumn {
954
+ readonly scope: string;
955
+ readonly alias: string;
956
+ readonly column: string;
957
+ }
958
+
959
+ export interface ScopeProofs {
960
+ /** `column = :bind`, `:bind = column`, `column IS :bind`, and
961
+ * `column IN (:a, :b)` conjuncts of a WHERE clause. */
962
+ readonly binds: readonly (ScopeColumn & {
963
+ readonly operator: 'equal' | 'in';
964
+ readonly params: readonly string[];
965
+ })[];
966
+ /** Qualified `a.x = b.y` conjuncts. A symmetric equality (a WHERE conjunct
967
+ * or an inner-join ON conjunct) proves each operand from the other; an
968
+ * outer-join ON conjunct proves only `target` from `source`. */
969
+ readonly equalities: readonly {
970
+ readonly target: ScopeColumn;
971
+ readonly source: ScopeColumn;
972
+ readonly symmetric: boolean;
973
+ }[];
974
+ }
975
+
976
+ const CLAUSE_WORDS = new Set([
977
+ 'where',
978
+ 'group',
979
+ 'having',
980
+ 'order',
981
+ 'limit',
982
+ 'window',
983
+ 'join',
984
+ 'inner',
985
+ 'left',
986
+ 'right',
987
+ 'full',
988
+ 'cross',
989
+ 'natural',
990
+ ]);
991
+
992
+ /**
993
+ * The scope-proof rule shared by `.sql` and `.syql` queries (SYQL §13). A
994
+ * comparison proves a scope only when it is a whole top-level conjunct of
995
+ * the WHERE clause of the SELECT scope that reads the instance: the outer
996
+ * statement or a top-level CTE body. Parentheses around a whole conjunct are
997
+ * transparent. Nothing else is a proof: a WHERE clause with a top-level OR, a
998
+ * compound SELECT scope, a nested subquery, and any other conjunct shape (NOT,
999
+ * a comparison of the comparison, a function, CASE, COLLATE, IS after the
1000
+ * bind, a literal in an IN list). Column equalities also come from the
1001
+ * conjuncts of an ON clause without OR, NOT, or a subquery.
1002
+ */
1003
+ export function scopeProofs(sql: string, ir: IrDocument): ScopeProofs {
1004
+ const { tokens, depths, closes } = sqlStructure(sql);
1005
+ const relations = scanRelations(sql, ir);
1006
+ const binds: ScopeProofs['binds'][number][] = [];
1007
+ const equalities: ScopeProofs['equalities'][number][] = [];
1008
+ const tableOf = (ref: TableRef) =>
1009
+ ir.tables.find((table) => table.name === ref.table);
1010
+ const scopeColumn = (
1011
+ scope: string,
1012
+ qualifier: string | undefined,
1013
+ column: string,
1014
+ ): ScopeColumn | undefined => {
1015
+ const name = column.toLowerCase();
1016
+ const candidates = relations.tables.filter(
1017
+ (ref) =>
1018
+ relations.scopeAt(ref.start) === scope &&
1019
+ (qualifier === undefined
1020
+ ? tableOf(ref)?.columns.some((c) => c.name.toLowerCase() === name)
1021
+ : ref.alias.toLowerCase() === qualifier.toLowerCase()),
1022
+ );
1023
+ const ref = candidates.length === 1 ? candidates[0] : undefined;
1024
+ const declared =
1025
+ ref === undefined
1026
+ ? undefined
1027
+ : tableOf(ref)?.scopes.find((s) => s.column.toLowerCase() === name);
1028
+ return ref === undefined || declared === undefined
1029
+ ? undefined
1030
+ : { scope, alias: ref.alias.toLowerCase(), column: name };
1031
+ };
1032
+ const columnAt = (index: number) => {
1033
+ const first = sqlIdentifier(tokens[index]);
1034
+ if (first === undefined) return undefined;
1035
+ if (tokens[index + 1]?.text !== '.')
1036
+ return { qualifier: undefined, column: first, next: index + 1 };
1037
+ const column = sqlIdentifier(tokens[index + 2]);
1038
+ return column === undefined
1039
+ ? undefined
1040
+ : { qualifier: first, column, next: index + 3 };
1041
+ };
1042
+ const equal = (index: number) =>
1043
+ tokens[index]?.text === '=' || tokens[index]?.text === '==';
1044
+ const bindAt = (index: number) =>
1045
+ tokens[index]?.kind === 'bind'
1046
+ ? (tokens[index] as SyqlToken).text.slice(1)
1047
+ : undefined;
1048
+ const conjuncts = (from: number, to: number): [number, number][] => {
1049
+ const parts: [number, number][] = [];
1050
+ let start = from;
1051
+ let cases = 0;
1052
+ let between = false;
1053
+ for (let index = from; index < to; index += 1) {
1054
+ if (depths[index] !== depths[from]) continue;
1055
+ const word = lowerWord(tokens[index]);
1056
+ if (word === 'case') cases += 1;
1057
+ else if (word === 'end') cases -= 1;
1058
+ else if (cases > 0) continue;
1059
+ else if (word === 'or') return [];
1060
+ else if (word === 'between') between = true;
1061
+ else if (word === 'and' && between) between = false;
1062
+ else if (word === 'and') {
1063
+ parts.push([start, index]);
1064
+ start = index + 1;
1065
+ }
1066
+ }
1067
+ parts.push([start, to]);
1068
+ return parts.flatMap(([start, end]) =>
1069
+ tokens[start]?.text === '(' &&
1070
+ closes.get(start) === end - 1 &&
1071
+ !['select', 'with', 'values'].includes(lowerWord(tokens[start + 1]) ?? '')
1072
+ ? conjuncts(start + 1, end - 1)
1073
+ : [[start, end]],
1074
+ );
1075
+ };
1076
+ const classify = (
1077
+ scope: string,
1078
+ start: number,
1079
+ end: number,
1080
+ join?: {
1081
+ readonly kind: string | undefined;
1082
+ readonly introduced: ReadonlySet<string>;
1083
+ },
1084
+ ): void => {
1085
+ const reversed = bindAt(start);
1086
+ const left = columnAt(reversed === undefined ? start : start + 2);
1087
+ if (left === undefined) return;
1088
+ if (reversed !== undefined) {
1089
+ const subject = scopeColumn(scope, left.qualifier, left.column);
1090
+ if (
1091
+ join === undefined &&
1092
+ equal(start + 1) &&
1093
+ left.next === end &&
1094
+ subject
1095
+ )
1096
+ binds.push({ ...subject, operator: 'equal', params: [reversed] });
1097
+ return;
1098
+ }
1099
+ const operator = left.next;
1100
+ const bind = bindAt(operator + 1);
1101
+ const list = tokens.slice(operator + 2, end - 1);
1102
+ const right = columnAt(operator + 1);
1103
+ if (
1104
+ join === undefined &&
1105
+ bind !== undefined &&
1106
+ operator + 2 === end &&
1107
+ (equal(operator) || lowerWord(tokens[operator]) === 'is')
1108
+ ) {
1109
+ const subject = scopeColumn(scope, left.qualifier, left.column);
1110
+ if (subject)
1111
+ binds.push({ ...subject, operator: 'equal', params: [bind] });
1112
+ } else if (
1113
+ join === undefined &&
1114
+ lowerWord(tokens[operator]) === 'in' &&
1115
+ closes.get(operator + 1) === end - 1 &&
1116
+ list.length % 2 === 1 &&
1117
+ list.every((token, index) =>
1118
+ index % 2 === 0 ? token.kind === 'bind' : token.text === ',',
1119
+ )
1120
+ ) {
1121
+ const subject = scopeColumn(scope, left.qualifier, left.column);
1122
+ const params = list.flatMap((token) =>
1123
+ token.kind === 'bind' ? [token.text.slice(1)] : [],
1124
+ );
1125
+ if (subject) binds.push({ ...subject, operator: 'in', params });
1126
+ } else if (
1127
+ equal(operator) &&
1128
+ right?.next === end &&
1129
+ left.qualifier !== undefined &&
1130
+ right.qualifier !== undefined
1131
+ ) {
1132
+ const l = scopeColumn(scope, left.qualifier, left.column);
1133
+ const r = scopeColumn(scope, right.qualifier, right.column);
1134
+ if (l === undefined || r === undefined) return;
1135
+ if (join?.kind !== 'left' && join?.kind !== 'right') {
1136
+ equalities.push({ target: l, source: r, symmetric: true });
1137
+ return;
1138
+ }
1139
+ // An outer join's ON constrains matched rows of its null-extended side
1140
+ // only; an equality with both operands on one side proves nothing.
1141
+ if (join.introduced.has(l.alias) === join.introduced.has(r.alias)) return;
1142
+ const [introduced, prior] = join.introduced.has(l.alias)
1143
+ ? [l, r]
1144
+ : [r, l];
1145
+ equalities.push(
1146
+ join.kind === 'left'
1147
+ ? { target: introduced, source: prior, symmetric: false }
1148
+ : { target: prior, source: introduced, symmetric: false },
1149
+ );
1150
+ }
1151
+ };
1152
+ const ranges = [
1153
+ { scope: 'main', from: 0, to: tokens.length },
1154
+ ...relations.cteScopes.map((cte) => ({
1155
+ scope: `cte:${cte.name}`,
1156
+ from: tokens.findIndex((token) => token.span.start.offset >= cte.start),
1157
+ to: tokens.findIndex((token) => token.span.start.offset >= cte.end),
1158
+ })),
1159
+ ];
1160
+ for (const range of ranges) {
1161
+ const own: number[] = [];
1162
+ for (let index = range.from; index < range.to; index += 1) {
1163
+ if (depths[index] === depths[range.from]) own.push(index);
1164
+ }
1165
+ const words = own.map((index) => lowerWord(tokens[index]) ?? '');
1166
+ if (words.some((w) => w === 'union' || w === 'intersect' || w === 'except'))
1167
+ continue;
1168
+ own.forEach((at, position) => {
1169
+ const word = words[position];
1170
+ if (word !== 'where' && word !== 'on') return;
1171
+ let next = position + 1;
1172
+ while (next < words.length && !CLAUSE_WORDS.has(words[next] as string))
1173
+ next += 1;
1174
+ const to = own[next] ?? range.to;
1175
+ if (word === 'where') {
1176
+ for (const [start, end] of conjuncts(at + 1, to))
1177
+ classify(range.scope, start, end);
1178
+ return;
1179
+ }
1180
+ if (
1181
+ tokens
1182
+ .slice(at + 1, to)
1183
+ .some((token) =>
1184
+ ['or', 'not', 'select', 'with'].includes(lowerWord(token) ?? ''),
1185
+ )
1186
+ )
1187
+ return;
1188
+ const join = words.lastIndexOf('join', position);
1189
+ if (join < 0) return;
1190
+ const kind =
1191
+ words[join - 1] === 'outer' ? words[join - 2] : words[join - 1];
1192
+ if (kind === 'full') return;
1193
+ // The relations between JOIN and ON, a parenthesized group included.
1194
+ const introduced = new Set(
1195
+ relations.tables
1196
+ .filter(
1197
+ (ref) =>
1198
+ ref.start >
1199
+ (tokens[own[join] as number] as SyqlToken).span.start.offset &&
1200
+ ref.start < (tokens[at] as SyqlToken).span.start.offset,
1201
+ )
1202
+ .map((ref) => ref.alias.toLowerCase()),
1203
+ );
1204
+ for (const [start, end] of conjuncts(at + 1, to))
1205
+ classify(range.scope, start, end, { kind, introduced });
1206
+ });
1207
+ }
1208
+ return { binds, equalities };
1209
+ }
1210
+
1065
1211
  // -- SELECT column → source resolution ---------------------------------------
1066
1212
  //
1067
1213
  // For each result column bun gives us (name + decltype), we resolve its origin
@@ -1224,95 +1370,9 @@ function applicationColumnType(column: IrColumn): IrColumnType {
1224
1370
  : column.type;
1225
1371
  }
1226
1372
 
1227
- function inferParamType(
1228
- paramName: string,
1229
- sql: string,
1230
- refs: readonly TableRef[],
1231
- ir: IrDocument,
1232
- ): IrColumnType | null {
1233
- const cleaned = stripCommentsAndStrings(sql);
1234
- const byName = new Map(ir.tables.map((t) => [t.name, t] as const));
1235
- const lookup = (
1236
- qualifier: string | undefined,
1237
- column: string,
1238
- ): IrColumnType | null => {
1239
- if (qualifier !== undefined) {
1240
- const ref = refs.find((r) => r.alias === qualifier);
1241
- const table = ref && byName.get(ref.table);
1242
- const resolved = table?.columns.find((c) => c.name === column);
1243
- return resolved === undefined ? null : applicationColumnType(resolved);
1244
- }
1245
- for (const ref of refs) {
1246
- const table = byName.get(ref.table);
1247
- const col = table?.columns.find((c) => c.name === column);
1248
- if (col !== undefined) return applicationColumnType(col);
1249
- }
1250
- return null;
1251
- };
1252
- // `col = :name`, `col >= :name`, `col <> :name`, `col LIKE :name`, etc.
1253
- const cmp = new RegExp(
1254
- `(?:(${IDENT})\\.)?(${IDENT})\\s*(?:=|==|!=|<>|<=|>=|<|>|\\bLIKE\\b|\\bIS\\b)\\s*:${paramName}\\b`,
1255
- 'i',
1256
- );
1257
- const cmpM = cmp.exec(cleaned);
1258
- if (cmpM !== null) {
1259
- const t = lookup(cmpM[1], cmpM[2] as string);
1260
- if (t !== null) return t;
1261
- }
1262
- // reversed: `:name = col`
1263
- const rev = new RegExp(
1264
- `:${paramName}\\b\\s*(?:=|==|!=|<>|<=|>=|<|>)\\s*(?:(${IDENT})\\.)?(${IDENT})`,
1265
- 'i',
1266
- );
1267
- const revM = rev.exec(cleaned);
1268
- if (revM !== null) {
1269
- const t = lookup(revM[1], revM[2] as string);
1270
- if (t !== null) return t;
1271
- }
1272
- // `col IN (…, :name, …)` — find the nearest `col IN (` before the param.
1273
- const inRe = new RegExp(
1274
- `(?:(${IDENT})\\.)?(${IDENT})\\s+IN\\s*\\(([^)]*:${paramName}\\b[^)]*)\\)`,
1275
- 'i',
1276
- );
1277
- const inM = inRe.exec(cleaned);
1278
- if (inM !== null) {
1279
- const t = lookup(inM[1], inM[2] as string);
1280
- if (t !== null) return t;
1281
- }
1282
- // `col BETWEEN :name AND …` / `col BETWEEN … AND :name` — both endpoints
1283
- // take the column's type (the §4 from+to group shape).
1284
- const btwLeft = new RegExp(
1285
- `(?:(${IDENT})\\.)?(${IDENT})\\s+BETWEEN\\s+:${paramName}\\b`,
1286
- 'i',
1287
- );
1288
- const btwLeftM = btwLeft.exec(cleaned);
1289
- if (btwLeftM !== null) {
1290
- const t = lookup(btwLeftM[1], btwLeftM[2] as string);
1291
- if (t !== null) return t;
1292
- }
1293
- const btwRight = new RegExp(
1294
- `(?:(${IDENT})\\.)?(${IDENT})\\s+BETWEEN\\s+\\S+\\s+AND\\s+:${paramName}\\b`,
1295
- 'i',
1296
- );
1297
- const btwRightM = btwRight.exec(cleaned);
1298
- if (btwRightM !== null) {
1299
- const t = lookup(btwRightM[1], btwRightM[2] as string);
1300
- if (t !== null) return t;
1301
- }
1302
- // `… LIKE <expr mentioning :name>` — a LIKE operand is TEXT, even inside
1303
- // a concatenation (`title like '%' || :q || '%'`, the search-fragment
1304
- // shape).
1305
- const like = new RegExp(`\\bLIKE\\b[^()]*:${paramName}\\b`, 'i');
1306
- if (like.test(cleaned)) return 'string';
1307
- const match = new RegExp(`\\bMATCH\\b\\s*:${paramName}\\b`, 'i');
1308
- if (match.test(cleaned)) return 'string';
1309
- return null;
1310
- }
1311
-
1312
1373
  /**
1313
- * Return every revision-1 column/LIKE type constraint for one bind. Unlike
1314
- * `inferParamType` (the legacy first-match helper), SYQL uses the complete set
1315
- * so conflicting checked uses cannot be silently accepted.
1374
+ * Every column/LIKE/MATCH type constraint on one bind, in pattern order. A
1375
+ * `.sql` query takes the first; SYQL rejects conflicting evidence.
1316
1376
  */
1317
1377
  export function inferParamTypeEvidence(
1318
1378
  paramName: string,
@@ -1336,15 +1396,17 @@ export function inferParamTypeEvidence(
1336
1396
  if (type !== undefined) evidence.push(type);
1337
1397
  return;
1338
1398
  }
1339
- const matches = refs.flatMap((ref) => {
1340
- const table = byName.get(ref.table);
1341
- const item = table?.columns.find(
1342
- (candidate) => candidate.name === column,
1343
- );
1344
- const type = item === undefined ? undefined : applicationColumnType(item);
1345
- return type === undefined ? [] : [type];
1346
- });
1347
- if (matches.length === 1) evidence.push(matches[0] as IrColumnType);
1399
+ // An unqualified name read by several relations is evidence only when
1400
+ // every candidate column has the same type.
1401
+ const matches = new Set(
1402
+ refs.flatMap((ref) => {
1403
+ const item = byName
1404
+ .get(ref.table)
1405
+ ?.columns.find((candidate) => candidate.name === column);
1406
+ return item === undefined ? [] : [applicationColumnType(item)];
1407
+ }),
1408
+ );
1409
+ if (matches.size === 1) evidence.push(...matches);
1348
1410
  };
1349
1411
 
1350
1412
  const comparison = new RegExp(
@@ -1391,68 +1453,50 @@ export function inferParamTypeEvidence(
1391
1453
 
1392
1454
  // -- fallback column typing (computed expressions) ----------------------------
1393
1455
 
1394
- const AGG_RE = /\b(count|sum|total|avg|min|max)\s*\(/i;
1456
+ export const AGGREGATE_FUNCTIONS = new Set([
1457
+ 'avg',
1458
+ 'count',
1459
+ 'group_concat',
1460
+ 'max',
1461
+ 'min',
1462
+ 'sum',
1463
+ 'total',
1464
+ ]);
1395
1465
 
1396
- /** decltype-null column: the documented honest fallback. Aggregates and
1397
- * arithmetic → nullable number; everything else → nullable string. */
1398
- function fallbackColumnType(expr: string): IrColumnType {
1399
- if (AGG_RE.test(expr) || /[+\-*/]/.test(expr)) return 'float';
1400
- return 'string';
1466
+ /** True when `expr` calls an aggregate outside a subquery. `min`/`max` with
1467
+ * several arguments are scalar functions, and an OVER clause makes a window
1468
+ * function, which aggregates no rows. */
1469
+ function callsAggregate(expr: string): boolean {
1470
+ const { tokens, depths, parents, closes } = sqlStructure(expr);
1471
+ return tokens.some((token, index) => {
1472
+ const name = lowerWord(token) ?? '';
1473
+ const close = closes.get(index + 1);
1474
+ if (!AGGREGATE_FUNCTIONS.has(name) || close === undefined) return false;
1475
+ for (let open = parents[index] ?? -1; open >= 0; open = parents[open] ?? -1)
1476
+ if (lowerWord(tokens[open + 1]) === 'select') return false;
1477
+ let after = close + 1;
1478
+ if (lowerWord(tokens[after]) === 'filter')
1479
+ after = (closes.get(after + 1) ?? after) + 1;
1480
+ const scalar =
1481
+ (name === 'min' || name === 'max') &&
1482
+ tokens
1483
+ .slice(index + 2, close)
1484
+ .some(
1485
+ (arg, at) =>
1486
+ arg.text === ',' && depths[index + 2 + at] === depths[index + 2],
1487
+ );
1488
+ return !scalar && lowerWord(tokens[after]) !== 'over';
1489
+ });
1401
1490
  }
1402
1491
 
1403
1492
  function inferReactiveMetadata(
1404
1493
  sql: string,
1405
- refs: readonly TableRef[],
1494
+ relations: SqlRelations,
1406
1495
  ir: IrDocument,
1407
- params: readonly QueryParam[],
1408
1496
  columns: readonly QueryColumn[],
1409
1497
  ): QueryReactiveMetadata {
1410
- const conservative = /\b(?:UNION|EXCEPT|INTERSECT)\b/i.test(
1411
- stripCommentsAndStrings(sql),
1412
- );
1413
- const requiredAt = (index: number): boolean => {
1414
- const cleaned = stripCommentsAndStrings(sql);
1415
- const where = /\bWHERE\b/i.exec(cleaned);
1416
- if (where === null || index < where.index + where[0].length) return false;
1417
- const matchStack: number[] = [];
1418
- for (
1419
- let cursor = where.index + where[0].length;
1420
- cursor < index;
1421
- cursor += 1
1422
- ) {
1423
- if (cleaned[cursor] === '(') matchStack.push(cursor);
1424
- else if (cleaned[cursor] === ')') matchStack.pop();
1425
- }
1426
- const stack: number[] = [];
1427
- for (
1428
- let cursor = where.index + where[0].length;
1429
- cursor < cleaned.length;
1430
- cursor += 1
1431
- ) {
1432
- const char = cleaned[cursor];
1433
- if (char === '(') stack.push(cursor);
1434
- else if (char === ')') stack.pop();
1435
- const atBoundary =
1436
- cursor === 0 || !/[A-Za-z0-9_:]/.test(cleaned[cursor - 1] ?? '');
1437
- if (
1438
- cursor > index &&
1439
- atBoundary &&
1440
- /^(?:GROUP\s+BY|ORDER\s+BY|LIMIT|HAVING)\b/i.test(cleaned.slice(cursor))
1441
- ) {
1442
- break;
1443
- }
1444
- if (
1445
- atBoundary &&
1446
- /^OR\b/i.test(cleaned.slice(cursor)) &&
1447
- stack.length <= matchStack.length &&
1448
- stack.every((open, stackIndex) => matchStack[stackIndex] === open)
1449
- ) {
1450
- return false;
1451
- }
1452
- }
1453
- return true;
1454
- };
1455
- const byParam = new Map(params.map((param) => [param.name, param] as const));
1498
+ const refs = relations.tables;
1499
+ const proofs = scopeProofs(sql, ir).binds;
1456
1500
  const dependencies: Array<{
1457
1501
  table: string;
1458
1502
  scopes: QueryScopeBinding[];
@@ -1469,47 +1513,28 @@ function inferReactiveMetadata(
1469
1513
  ].sort()) {
1470
1514
  const table = ir.tables.find((candidate) => candidate.name === tableName);
1471
1515
  const tableRefs = refs.filter((ref) => ref.table === tableName);
1516
+ const ref = tableRefs.length === 1 ? tableRefs[0] : undefined;
1472
1517
  const scopes: QueryScopeBinding[] = [];
1473
- if (table !== undefined && tableRefs.length === 1 && !conservative) {
1474
- const qualifier = tableRefs[0]?.alias;
1518
+ if (table !== undefined && ref !== undefined) {
1475
1519
  for (const scope of table.scopes) {
1476
- const column = scope.column.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1477
- const qualified =
1478
- qualifier === undefined
1479
- ? `(?<![A-Za-z0-9_.:])(?:${IDENT}\\.)?${column}\\b`
1480
- : `(?<![A-Za-z0-9_.:])(?:${qualifier.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\.)?${column}\\b`;
1481
- const found: string[] = [];
1482
- const equality = new RegExp(
1483
- `${qualified}\\s*(?:=|==|\\bIS\\b)\\s*:(${IDENT})\\b|:(${IDENT})\\b\\s*(?:=|==)\\s*${qualified}`,
1484
- 'gi',
1485
- );
1486
- for (const match of sql.matchAll(equality)) {
1487
- if (!requiredAt(match.index)) continue;
1488
- const name = match[1] ?? match[2];
1489
- const param = name === undefined ? undefined : byParam.get(name);
1490
- if (param !== undefined) {
1491
- found.push(param.name);
1492
- }
1493
- }
1494
- const inList = new RegExp(`${qualified}\\s+IN\\s*\\(([^)]*)\\)`, 'gi');
1495
- for (const match of sql.matchAll(inList)) {
1496
- if (!requiredAt(match.index)) continue;
1497
- for (const paramMatch of (match[1] ?? '').matchAll(
1498
- /:([A-Za-z_][A-Za-z0-9_]*)/g,
1499
- )) {
1500
- const param = byParam.get(paramMatch[1] as string);
1501
- if (param !== undefined) {
1502
- found.push(param.name);
1503
- }
1504
- }
1505
- }
1506
- const unique = [...new Set(found)];
1507
- if (unique.length > 0) {
1520
+ const params = [
1521
+ ...new Set(
1522
+ proofs
1523
+ .filter(
1524
+ (proof) =>
1525
+ proof.scope === relations.scopeAt(ref.start) &&
1526
+ proof.alias === ref.alias.toLowerCase() &&
1527
+ proof.column === scope.column.toLowerCase(),
1528
+ )
1529
+ .flatMap((proof) => proof.params),
1530
+ ),
1531
+ ];
1532
+ if (params.length > 0) {
1508
1533
  scopes.push({
1509
1534
  table: tableName,
1510
1535
  variable: scope.variable,
1511
1536
  pattern: scope.pattern,
1512
- params: unique,
1537
+ params,
1513
1538
  });
1514
1539
  }
1515
1540
  }
@@ -1669,24 +1694,8 @@ export function lowerFtsSourceIds(sql: string, ir: IrDocument): string {
1669
1694
  );
1670
1695
  const relations = scanRelations(sql, ir);
1671
1696
  if (!relations.tables.some((ref) => ftsNames.has(ref.table))) return sql;
1672
- const tokens = lexSyqlSqlSource('query SQL', sql).filter(
1673
- (token) => !isSyqlTrivia(token) && token.kind !== 'eof',
1674
- );
1675
- const depths: number[] = [];
1676
- const closes = new Map<number, number>();
1677
- const stack: number[] = [];
1678
- tokens.forEach((token, index) => {
1679
- if (token.text === ')') {
1680
- const open = stack.pop();
1681
- if (open !== undefined) closes.set(open, index);
1682
- }
1683
- depths[index] = stack.length;
1684
- if (token.text === '(') stack.push(index);
1685
- });
1686
- const lower = (index: number): string | undefined => {
1687
- const token = tokens[index];
1688
- return token?.kind === 'identifier' ? token.text.toLowerCase() : undefined;
1689
- };
1697
+ const { tokens, depths, closes } = sqlStructure(sql);
1698
+ const lower = (index: number) => lowerWord(tokens[index]);
1690
1699
  // Every SELECT scope as an offset range; the outer statement spans all.
1691
1700
  const scopes = [
1692
1701
  { start: -1, end: sql.length + 1 },
@@ -1857,22 +1866,28 @@ export function analyzeStatement(
1857
1866
  }
1858
1867
  // SELECT-only (the read tier). A `WITH` is allowed when its main statement
1859
1868
  // is a SELECT (SQLite also allows WITH … INSERT/UPDATE/DELETE — writes).
1860
- const firstKeyword = /^\s*([A-Za-z]+)/.exec(
1861
- stripCommentsAndStrings(statementText).trimStart(),
1862
- )?.[1];
1863
- const upperKeyword = firstKeyword?.toUpperCase();
1864
- if (upperKeyword === 'WITH') {
1865
- const main = mainVerbAfterWith(stripCommentsAndStrings(statementText));
1866
- if (main !== 'SELECT') {
1869
+ // CTE bodies sit inside parentheses and a bare keyword cannot name a CTE,
1870
+ // so the first depth-0 verb after `WITH` is the main verb.
1871
+ const statement = sqlStructure(statementText, file);
1872
+ const firstKeyword = lowerWord(statement.tokens[0]);
1873
+ if (firstKeyword === 'with') {
1874
+ const main = statement.tokens.find(
1875
+ (token, index) =>
1876
+ statement.depths[index] === 0 &&
1877
+ ['select', 'values', 'insert', 'update', 'delete', 'replace'].includes(
1878
+ lowerWord(token) ?? '',
1879
+ ),
1880
+ );
1881
+ if (lowerWord(main) !== 'select') {
1867
1882
  throw new TypegenError(
1868
1883
  file,
1869
- `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).`,
1884
+ `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).`,
1870
1885
  );
1871
1886
  }
1872
- } else if (upperKeyword !== 'SELECT') {
1887
+ } else if (firstKeyword !== 'select') {
1873
1888
  throw new TypegenError(
1874
1889
  file,
1875
- `named queries are SELECT-only (the read tier); found ${JSON.stringify(firstKeyword ?? '')}. Writes go through mutate() (SPEC §7.1).`,
1890
+ `named queries are SELECT-only (the read tier); found ${JSON.stringify(statement.tokens[0]?.text ?? '')}. Writes go through mutate() (SPEC §7.1).`,
1876
1891
  );
1877
1892
  }
1878
1893
 
@@ -1979,6 +1994,53 @@ export function analyzeStatement(
1979
1994
  : { ...source, nullableByJoin: source.nullableByJoin || cteRef.nullable };
1980
1995
  };
1981
1996
  const items = splitSelectList(sql);
1997
+ // A bare column of a SELECT that aggregates without GROUP BY reads NULL
1998
+ // when no row matches, and a compound SELECT returns the rows of every
1999
+ // branch, so a column is nullable when any branch can yield NULL there.
2000
+ const outer = sqlStructure(sql);
2001
+ const cuts = outer.tokens.flatMap((token, index) =>
2002
+ outer.depths[index] === 0 &&
2003
+ ['union', 'intersect', 'except'].includes(lowerWord(token) ?? '')
2004
+ ? [token.span]
2005
+ : [],
2006
+ );
2007
+ const branches = [0, ...cuts.map((span) => span.end.offset)].map(
2008
+ (start, index) => {
2009
+ const end = cuts[index]?.start.offset ?? sql.length;
2010
+ const branchItems = splitSelectList(sql.slice(start, end));
2011
+ const grouped = outer.tokens.some(
2012
+ (token, at) =>
2013
+ outer.depths[at] === 0 &&
2014
+ token.span.start.offset >= start &&
2015
+ token.span.start.offset < end &&
2016
+ lowerWord(token) === 'group',
2017
+ );
2018
+ const aggregated =
2019
+ !grouped &&
2020
+ (branchItems?.some((item) => callsAggregate(item.expr)) ?? false);
2021
+ return { start, end, items: branchItems, aggregated };
2022
+ },
2023
+ );
2024
+ const branchNullable = (index: number): boolean =>
2025
+ branches.some((branch, position) => {
2026
+ if (branch.aggregated) return true;
2027
+ if (position === 0) return false;
2028
+ const item = branch.items?.[index];
2029
+ const source =
2030
+ item === undefined
2031
+ ? null
2032
+ : resolveSource(
2033
+ item,
2034
+ scoped.tables.filter(
2035
+ (ref) =>
2036
+ scoped.scopeAt(ref.start) === 'main' &&
2037
+ ref.start >= branch.start &&
2038
+ ref.start < branch.end,
2039
+ ),
2040
+ ir,
2041
+ );
2042
+ return source === null || source.column.nullable || source.nullableByJoin;
2043
+ });
1982
2044
  const columns: QueryColumn[] = described.columnNames.map((colName, index) => {
1983
2045
  const decl = described.declaredTypes[index];
1984
2046
  const item = items?.[index];
@@ -1998,7 +2060,7 @@ export function analyzeStatement(
1998
2060
  name: sqlName,
1999
2061
  langName,
2000
2062
  type: 'integer',
2001
- nullable: syncVersionNullable,
2063
+ nullable: syncVersionNullable || branchNullable(index),
2002
2064
  fidelity: 'exact',
2003
2065
  };
2004
2066
  }
@@ -2006,27 +2068,58 @@ export function analyzeStatement(
2006
2068
  // Exact: app-facing queries read the decrypted local mirror. Encrypted
2007
2069
  // columns therefore expose their pre-wire declaredType, while the schema
2008
2070
  // IR and mutation codec continue to carry the bytes wire type.
2009
- const sourceType =
2010
- source.column.encrypted === true &&
2011
- source.column.declaredType !== undefined
2012
- ? source.column.declaredType
2013
- : source.column.type;
2014
2071
  return {
2015
2072
  name: sqlName,
2016
2073
  langName,
2017
- type: sourceType,
2018
- nullable: source.column.nullable || source.nullableByJoin,
2074
+ type: applicationColumnType(source.column),
2075
+ nullable:
2076
+ source.column.nullable ||
2077
+ source.nullableByJoin ||
2078
+ branchNullable(index),
2019
2079
  fidelity: 'exact',
2020
2080
  origin: { table: source.table, column: source.column.name },
2021
2081
  };
2022
2082
  }
2023
- // Fallback: decltype affinity if present, else the expr-shape fallback.
2024
- const mapped =
2025
- decl !== null && decl !== undefined
2026
- ? DECLTYPE_MAP[decl.toUpperCase()]
2083
+ // Computed column (SYQL §14.1): the type is the decltype SQLite reports,
2084
+ // else the target of a whole-expression CAST, else integer for a
2085
+ // whole-expression count(); anything else is refused.
2086
+ const { tokens, depths, closes } = sqlStructure(item?.expr ?? '');
2087
+ const whole =
2088
+ tokens[1]?.text === '(' && closes.get(1) === tokens.length - 1
2089
+ ? lowerWord(tokens[0])
2027
2090
  : undefined;
2091
+ const asAt = tokens.findLastIndex(
2092
+ (token, at) => depths[at] === 1 && lowerWord(token) === 'as',
2093
+ );
2094
+ const castType = tokens
2095
+ .flatMap((token, at) =>
2096
+ at > asAt && depths[at] === 1 && token.kind === 'identifier'
2097
+ ? [token.text.toUpperCase()]
2098
+ : [],
2099
+ )
2100
+ .join(' ');
2028
2101
  const type =
2029
- mapped ?? (item !== undefined ? fallbackColumnType(item.expr) : 'string');
2102
+ (decl !== null && decl !== undefined
2103
+ ? DECLTYPE_MAP[decl.toUpperCase()]
2104
+ : undefined) ??
2105
+ // SQLite gives the other mapped names NUMERIC or BLOB affinity, which
2106
+ // can change the value's storage class.
2107
+ (whole === 'cast' &&
2108
+ asAt > 0 &&
2109
+ /^(INTEGER|INT|BIGINT|SMALLINT|TEXT|REAL|FLOAT|DOUBLE|BLOB|BOOLEAN|BOOL)$/.test(
2110
+ castType,
2111
+ )
2112
+ ? DECLTYPE_MAP[castType]
2113
+ : undefined) ??
2114
+ (whole === 'count' ? 'integer' : undefined);
2115
+ if (type === undefined) {
2116
+ throw new TypegenError(
2117
+ file,
2118
+ 'computed result column has no declared type; wrap the expression in CAST(... AS <type>)',
2119
+ 'typegen.computed_column_type',
2120
+ { query: name, column: sqlName },
2121
+ );
2122
+ }
2030
2123
  return {
2031
2124
  name: sqlName,
2032
2125
  langName,
@@ -2065,8 +2158,8 @@ export function analyzeStatement(
2065
2158
  if (commented !== undefined) {
2066
2159
  return { name: paramName, langName, type: commented, source: 'comment' };
2067
2160
  }
2068
- const inferred = inferParamType(paramName, sql, refs, ir);
2069
- if (inferred !== null) {
2161
+ const inferred = inferParamTypeEvidence(paramName, sql, refs, ir)[0];
2162
+ if (inferred !== undefined) {
2070
2163
  return { name: paramName, langName, type: inferred, source: 'inferred' };
2071
2164
  }
2072
2165
  throw new TypegenError(
@@ -2084,21 +2177,43 @@ export function analyzeStatement(
2084
2177
  }
2085
2178
  }
2086
2179
 
2087
- const reactive = inferReactiveMetadata(sql, refs, ir, params, columns);
2180
+ validateQueryIdentifier(name, naming.targets, file, 'query name');
2181
+ for (const column of columns) {
2182
+ validateQueryIdentifier(
2183
+ column.langName,
2184
+ naming.targets,
2185
+ file,
2186
+ 'result column',
2187
+ );
2188
+ }
2189
+ for (const param of params) {
2190
+ if (!internalParams.has(param.name))
2191
+ validateQueryIdentifier(param.langName, naming.targets, file, 'param');
2192
+ }
2088
2193
 
2194
+ const reactive = inferReactiveMetadata(sql, scoped, ir, columns);
2195
+
2196
+ // The emitted statement is what SQLite checks: the positional form, with
2197
+ // FTS source-id lowering applied, must prepare to the same result columns
2198
+ // and take exactly one value per distinct param.
2089
2199
  const physical = lowerFtsSourceIds(sql, ir);
2090
- if (physical !== sql) {
2091
- const lowered = analyze(physical);
2092
- if (
2093
- lowered.paramsCount !== described.paramsCount ||
2094
- lowered.columnNames.length !== described.columnNames.length ||
2095
- lowered.columnNames.some((n, i) => n !== described.columnNames[i])
2096
- ) {
2097
- throw new TypegenError(
2098
- file,
2099
- `internal: FTS source-id lowering changed the result columns [${described.columnNames.join(', ')}] to [${lowered.columnNames.join(', ')}]`,
2100
- );
2101
- }
2200
+ const positionalSql = toPositionalSql(physical);
2201
+ const emitted = analyze(positionalSql);
2202
+ if (
2203
+ emitted.paramsCount !== params.length ||
2204
+ emitted.columnNames.join('\0') !== described.columnNames.join('\0')
2205
+ ) {
2206
+ throw new TypegenError(
2207
+ file,
2208
+ 'internal: the emitted positional SQL does not prepare to the analyzed columns and params',
2209
+ 'typegen.emitted_sql_mismatch',
2210
+ {
2211
+ columns: described.columnNames,
2212
+ emittedColumns: emitted.columnNames,
2213
+ params: params.length,
2214
+ emittedParams: emitted.paramsCount,
2215
+ },
2216
+ );
2102
2217
  }
2103
2218
 
2104
2219
  return {
@@ -2106,17 +2221,13 @@ export function analyzeStatement(
2106
2221
  file,
2107
2222
  sourceSql,
2108
2223
  sql: physical,
2109
- positionalSql: toPositionalSql(physical),
2110
- relations: scanTableRefs(toPositionalSql(physical), ir, true).map(
2111
- (ref) => ({
2112
- table: ref.table,
2113
- start: ref.start,
2114
- end: ref.end,
2115
- ...(ref.explicitAlias === undefined
2116
- ? {}
2117
- : { alias: ref.explicitAlias }),
2118
- }),
2119
- ),
2224
+ positionalSql,
2225
+ relations: scanTableRefs(positionalSql, ir, true).map((ref) => ({
2226
+ table: ref.table,
2227
+ start: ref.start,
2228
+ end: ref.end,
2229
+ ...(ref.explicitAlias === undefined ? {} : { alias: ref.explicitAlias }),
2230
+ })),
2120
2231
  params,
2121
2232
  columns,
2122
2233
  tables,
@@ -2142,7 +2253,7 @@ export function analyzeQueryFile(
2142
2253
  db: QueryDb,
2143
2254
  naming: QueryNamingOptions = DEFAULT_NAMING,
2144
2255
  ): AnalyzedQuery[] {
2145
- const statements = splitStatements(content);
2256
+ const statements = splitStatements(content, relPath);
2146
2257
  if (statements.length === 0) {
2147
2258
  throw new TypegenError(relPath, 'query file is empty');
2148
2259
  }
@@ -2174,7 +2285,7 @@ export function analyzeQuery(
2174
2285
  db: QueryDb,
2175
2286
  naming: QueryNamingOptions = DEFAULT_NAMING,
2176
2287
  ): AnalyzedQuery {
2177
- const statements = splitStatements(raw);
2288
+ const statements = splitStatements(raw, file);
2178
2289
  if (statements.length > 1) {
2179
2290
  throw new TypegenError(
2180
2291
  file,