@syncular/typegen 0.26.6 → 0.28.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.
package/dist/query.d.ts CHANGED
@@ -236,7 +236,46 @@ export interface TableRef {
236
236
  /** True when an enclosing flat join chain can null-extend this relation. */
237
237
  readonly nullable: boolean;
238
238
  }
239
+ /** A common table expression read in a FROM/JOIN position. */
240
+ export interface CteRef {
241
+ /** Lowercased CTE name. */
242
+ readonly name: string;
243
+ /** Alias (or the CTE name when un-aliased). */
244
+ readonly alias: string;
245
+ readonly start: number;
246
+ readonly nullable: boolean;
247
+ }
248
+ /** A CTE of the top-level `WITH` whose body is one SELECT scope. */
249
+ export interface CteScope {
250
+ /** Lowercased CTE name. */
251
+ readonly name: string;
252
+ /** Offsets of the body text between the CTE parentheses. */
253
+ readonly start: number;
254
+ readonly end: number;
255
+ /** True for `name(a, b) AS (...)`, which renames the body columns. */
256
+ readonly renamed: boolean;
257
+ }
258
+ export interface SqlRelations {
259
+ readonly tables: TableRef[];
260
+ readonly cteRefs: CteRef[];
261
+ readonly cteScopes: CteScope[];
262
+ readonly recursive: boolean;
263
+ /** `main` for the outer statement, `cte:<name>` directly inside a
264
+ * top-level CTE body, `nested` inside any other subquery. */
265
+ readonly scopeAt: (offset: number) => string;
266
+ }
239
267
  export declare function scanTableRefs(sql: string, ir: IrDocument): TableRef[];
268
+ export declare function scanRelations(sql: string, ir: IrDocument): SqlRelations;
269
+ export interface SelectItem {
270
+ /** The raw expression text (before any `AS alias`). */
271
+ readonly expr: string;
272
+ /** The explicit `AS alias`, if any. */
273
+ readonly alias: string | undefined;
274
+ }
275
+ /** Split the outermost SELECT list of `sql` into items. Returns null when the
276
+ * statement has no outer `SELECT … FROM` or projects `*` / `t.*`. */
277
+ export declare function splitSelectList(sql: string): SelectItem[] | null;
278
+ export declare const PLAIN_REF_RE: RegExp;
240
279
  /**
241
280
  * Return every revision-1 column/LIKE type constraint for one bind. Unlike
242
281
  * `inferParamType` (the legacy first-match helper), SYQL uses the complete set
package/dist/query.js CHANGED
@@ -63,7 +63,7 @@
63
63
  */
64
64
  import { TypegenError } from './errors.js';
65
65
  import { isSyqlTrivia, lexSyqlSqlSource } from './syql-lexer.js';
66
- import { lowerProjection, mainVerbAfterWith } from './lower.js';
66
+ import { findProjection, lowerProjection, mainVerbAfterWith } from './lower.js';
67
67
  import { buildNamingMap } 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). */
@@ -477,6 +477,9 @@ function sqlIdentifier(token) {
477
477
  return quote === '[' ? value : value.split(`${quote}${quote}`).join(quote);
478
478
  }
479
479
  export function scanTableRefs(sql, ir) {
480
+ return scanRelations(sql, ir).tables;
481
+ }
482
+ export function scanRelations(sql, ir) {
480
483
  const tokens = lexSyqlSqlSource('query SQL', sql).filter((token) => !isSyqlTrivia(token) && token.kind !== 'eof');
481
484
  const known = new Map(ir.tables.flatMap((table) => [
482
485
  [table.name.toLowerCase(), table.name],
@@ -537,20 +540,25 @@ export function scanTableRefs(sql, ir) {
537
540
  }
538
541
  }
539
542
  const ctes = [];
543
+ const cteScopes = [];
544
+ let recursive = false;
540
545
  for (let index = 0; index < tokens.length; index += 1) {
541
546
  const token = tokens[index];
542
547
  if (token?.kind !== 'identifier' || token.text.toLowerCase() !== 'with')
543
548
  continue;
544
549
  const scopeEnd = closes.get(parents[index] ?? -1) ?? tokens.length;
545
550
  let cursor = index + 1;
546
- if (tokens[cursor]?.text.toLowerCase() === 'recursive')
551
+ if (tokens[cursor]?.text.toLowerCase() === 'recursive') {
552
+ recursive = true;
547
553
  cursor += 1;
554
+ }
548
555
  for (;;) {
549
556
  const name = sqlIdentifier(tokens[cursor]);
550
557
  if (name === undefined)
551
558
  break;
552
559
  cursor += 1;
553
- if (tokens[cursor]?.text === '(')
560
+ const renamed = tokens[cursor]?.text === '(';
561
+ if (renamed)
554
562
  cursor = (closes.get(cursor) ?? scopeEnd) + 1;
555
563
  if (tokens[cursor]?.text.toLowerCase() !== 'as')
556
564
  break;
@@ -562,6 +570,18 @@ export function scanTableRefs(sql, ir) {
562
570
  if (tokens[cursor]?.text !== '(')
563
571
  break;
564
572
  ctes.push({ name: name.toLowerCase(), start: index, end: scopeEnd });
573
+ const close = tokens[closes.get(cursor) ?? -1];
574
+ if (index === 0 &&
575
+ close !== undefined &&
576
+ tokens[cursor + 1]?.text.toLowerCase() === 'select') {
577
+ cteScopes.push({
578
+ name: name.toLowerCase(),
579
+ open: cursor,
580
+ start: tokens[cursor].span.end.offset,
581
+ end: close.span.start.offset,
582
+ renamed,
583
+ });
584
+ }
565
585
  cursor = (closes.get(cursor) ?? scopeEnd) + 1;
566
586
  if (tokens[cursor]?.text !== ',')
567
587
  break;
@@ -569,6 +589,7 @@ export function scanTableRefs(sql, ir) {
569
589
  }
570
590
  }
571
591
  const refs = [];
592
+ const cteRefs = [];
572
593
  const relationStartByDepth = new Map();
573
594
  const nullableGroups = [];
574
595
  for (let index = 0; index < tokens.length; index += 1) {
@@ -596,6 +617,10 @@ export function scanTableRefs(sql, ir) {
596
617
  if (ref !== undefined)
597
618
  refs[prior] = { ...ref, nullable: true };
598
619
  }
620
+ // Conservative: any CTE read before a RIGHT/FULL join is optional.
621
+ cteRefs.forEach((ref, prior) => {
622
+ cteRefs[prior] = { ...ref, nullable: true };
623
+ });
599
624
  }
600
625
  if ((outerKind === 'left' || outerKind === 'full') &&
601
626
  tokens[cursor]?.text === '(') {
@@ -617,16 +642,6 @@ export function scanTableRefs(sql, ir) {
617
642
  if (tokens[cursor + 1]?.text === '.') {
618
643
  throw new TypegenError('query SQL', 'schema-qualified relations are unsupported; use application table names');
619
644
  }
620
- if (ctes.some((cte) => cte.name === rawTable.toLowerCase() &&
621
- cursor > cte.start &&
622
- cursor < cte.end))
623
- continue;
624
- const table = known.get(rawTable.toLowerCase());
625
- if (table === undefined)
626
- throw new TypegenError('query SQL', `unresolved table relation ${JSON.stringify(rawTable)}`);
627
- const tableDepth = depths[cursor] ?? operatorDepth;
628
- if (operator === 'from' || !relationStartByDepth.has(tableDepth))
629
- relationStartByDepth.set(tableDepth, refs.length);
630
645
  let aliasToken = tokens[cursor + 1];
631
646
  if (aliasToken?.kind === 'identifier' &&
632
647
  aliasToken.text.toLowerCase() === 'as')
@@ -636,61 +651,95 @@ export function scanTableRefs(sql, ir) {
636
651
  !RESERVED_ALIAS.has(aliasToken.text.toLowerCase()))
637
652
  ? sqlIdentifier(aliasToken)
638
653
  : undefined;
654
+ const nullable = outerKind === 'left' ||
655
+ outerKind === 'full' ||
656
+ nullableGroups.some((group) => cursor > group.start && cursor < group.end);
657
+ if (ctes.some((cte) => cte.name === rawTable.toLowerCase() &&
658
+ cursor > cte.start &&
659
+ cursor < cte.end)) {
660
+ cteRefs.push({
661
+ name: rawTable.toLowerCase(),
662
+ alias: explicitAlias ?? rawTable,
663
+ start: tableToken.span.start.offset,
664
+ nullable,
665
+ });
666
+ continue;
667
+ }
668
+ const table = known.get(rawTable.toLowerCase());
669
+ if (table === undefined)
670
+ throw new TypegenError('query SQL', `unresolved table relation ${JSON.stringify(rawTable)}`);
671
+ const tableDepth = depths[cursor] ?? operatorDepth;
672
+ if (operator === 'from' || !relationStartByDepth.has(tableDepth))
673
+ relationStartByDepth.set(tableDepth, refs.length);
639
674
  refs.push({
640
675
  table,
641
676
  start: tableToken.span.start.offset,
642
677
  end: tableToken.span.end.offset,
643
678
  alias: explicitAlias ?? table,
644
679
  ...(explicitAlias === undefined ? {} : { explicitAlias }),
645
- nullable: outerKind === 'left' ||
646
- outerKind === 'full' ||
647
- nullableGroups.some((group) => cursor > group.start && cursor < group.end),
680
+ nullable,
648
681
  });
649
682
  }
650
- return refs;
683
+ const selectOpens = tokens.flatMap((token, index) => {
684
+ const close = tokens[closes.get(index) ?? -1];
685
+ const next = tokens[index + 1];
686
+ return token.text === '(' &&
687
+ close !== undefined &&
688
+ next?.kind === 'identifier' &&
689
+ ['select', 'with', 'values'].includes(next.text.toLowerCase())
690
+ ? [
691
+ {
692
+ index,
693
+ start: token.span.start.offset,
694
+ end: close.span.start.offset,
695
+ },
696
+ ]
697
+ : [];
698
+ });
699
+ return {
700
+ tables: refs,
701
+ cteRefs,
702
+ cteScopes: cteScopes.map(({ open: _open, ...scope }) => scope),
703
+ recursive,
704
+ scopeAt(offset) {
705
+ const innermost = selectOpens
706
+ .filter((open) => open.start < offset && offset < open.end)
707
+ .at(-1);
708
+ if (innermost === undefined)
709
+ return 'main';
710
+ const cte = cteScopes.find((scope) => scope.open === innermost.index);
711
+ return cte === undefined ? 'nested' : `cte:${cte.name}`;
712
+ },
713
+ };
651
714
  }
652
- /** Split a SELECT list on top-level commas (parens-aware). Returns null when
653
- * the query isn't a plain `SELECT … FROM …` we can split (e.g. `SELECT *`). */
654
- function splitSelectList(sql) {
655
- const cleaned = stripCommentsAndStrings(sql).replace(/\s+/g, ' ').trim();
656
- const m = /^SELECT\s+(?:DISTINCT\s+)?(.*?)\sFROM\s/i.exec(cleaned);
657
- if (m === null || m[1] === undefined)
715
+ /** Split the outermost SELECT list of `sql` into items. Returns null when the
716
+ * statement has no outer `SELECT … FROM` or projects `*` / `t.*`. */
717
+ export function splitSelectList(sql) {
718
+ const projection = findProjection(stripCommentsAndStrings(sql));
719
+ if (projection === null)
658
720
  return null;
659
- const list = m[1];
660
- if (list.trim() === '*' || /(^|,|\.)\s*\*/.test(list))
661
- return null; // SELECT * / t.*
662
721
  const items = [];
663
- let depth = 0;
664
- let start = 0;
665
- for (let i = 0; i <= list.length; i++) {
666
- const ch = list[i];
667
- if (ch === '(')
668
- depth += 1;
669
- else if (ch === ')')
670
- depth -= 1;
671
- if ((ch === ',' && depth === 0) || i === list.length) {
672
- const raw = list.slice(start, i).trim();
673
- start = i + 1;
674
- if (raw.length === 0)
675
- continue;
676
- // `expr AS alias` or `expr alias` (trailing bare identifier). Only treat
677
- // a trailing word as an alias when the expr has more than that word.
678
- // Projection lowering emits the alias double-quoted, so accept both.
679
- const asMatch = /^(.*?)\s+AS\s+(?:"((?:[^"]|"")*)"|([A-Za-z_][A-Za-z0-9_]*))$/i.exec(raw);
680
- if (asMatch !== null) {
681
- items.push({
682
- expr: asMatch[1].trim(),
683
- alias: (asMatch[2] ?? asMatch[3]).replaceAll('""', '"'),
684
- });
685
- }
686
- else {
687
- items.push({ expr: raw, alias: undefined });
688
- }
722
+ for (const { text } of projection.items) {
723
+ const raw = text.replace(/\s+/g, ' ');
724
+ if (raw === '*' || /\.\s*\*$/.test(raw))
725
+ return null;
726
+ // `expr AS alias` or `expr alias` (trailing bare identifier). Only treat
727
+ // a trailing word as an alias when the expr has more than that word.
728
+ // Projection lowering emits the alias double-quoted, so accept both.
729
+ const asMatch = /^(.*?)\s+AS\s+(?:"((?:[^"]|"")*)"|([A-Za-z_][A-Za-z0-9_]*))$/i.exec(raw);
730
+ if (asMatch !== null) {
731
+ items.push({
732
+ expr: asMatch[1].trim(),
733
+ alias: (asMatch[2] ?? asMatch[3]).replaceAll('""', '"'),
734
+ });
735
+ }
736
+ else {
737
+ items.push({ expr: raw, alias: undefined });
689
738
  }
690
739
  }
691
740
  return items;
692
741
  }
693
- const PLAIN_REF_RE = new RegExp(`^(?:(${IDENT})\\.)?(${IDENT})$`);
742
+ export const PLAIN_REF_RE = new RegExp(`^(?:(${IDENT})\\.)?(${IDENT})$`);
694
743
  function declaredFtsProjection(ir, tableName) {
695
744
  return ir.tables.some((table) => table.ftsIndexes.some((index) => index.name === tableName));
696
745
  }
@@ -1136,11 +1185,32 @@ export function analyzeStatement(name, location, statementText, ir, db, naming =
1136
1185
  }
1137
1186
  // Columns: pair bun's column names + decltypes with our source resolution
1138
1187
  // (against the LOWERED sql — aliased plain refs still resolve exactly).
1188
+ // Each item resolves against the relations of its own SELECT scope; a
1189
+ // qualified reference to a top-level CTE resolves through the CTE body.
1190
+ const scoped = scanRelations(sql, ir);
1191
+ const resolveInScope = (item, scope) => {
1192
+ const direct = resolveSource(item, scoped.tables.filter((ref) => scoped.scopeAt(ref.start) === scope), ir);
1193
+ const plain = PLAIN_REF_RE.exec(item.expr);
1194
+ if (direct !== null || plain?.[1] === undefined)
1195
+ return direct;
1196
+ const cteRef = scoped.cteRefs.find((ref) => scoped.scopeAt(ref.start) === scope &&
1197
+ ref.alias.toLowerCase() === plain[1]?.toLowerCase());
1198
+ const body = scoped.cteScopes.find((cte) => cte.name === cteRef?.name);
1199
+ if (cteRef === undefined || body === undefined || body.renamed)
1200
+ return null;
1201
+ const bodyItem = splitSelectList(sql.slice(body.start, body.end))?.find((candidate) => (candidate.alias ?? PLAIN_REF_RE.exec(candidate.expr)?.[2])?.toLowerCase() === plain[2]?.toLowerCase());
1202
+ const source = bodyItem === undefined
1203
+ ? null
1204
+ : resolveInScope(bodyItem, `cte:${body.name}`);
1205
+ return source === null
1206
+ ? null
1207
+ : { ...source, nullableByJoin: source.nullableByJoin || cteRef.nullable };
1208
+ };
1139
1209
  const items = splitSelectList(sql);
1140
1210
  const columns = described.columnNames.map((colName, index) => {
1141
1211
  const decl = described.declaredTypes[index];
1142
1212
  const item = items?.[index];
1143
- const source = item !== undefined ? resolveSource(item, refs, ir) : null;
1213
+ const source = item !== undefined ? resolveInScope(item, 'main') : null;
1144
1214
  const sqlName = sqlNames[index] ?? colName;
1145
1215
  const langName = langNames[index] ?? colName;
1146
1216
  const syncVersionNullable = item !== undefined ? syncVersionSourceNullability(item, refs) : null;