@syncular/typegen 0.26.5 → 0.27.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/src/query.ts CHANGED
@@ -64,7 +64,7 @@
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 { lowerProjection, mainVerbAfterWith } from './lower';
67
+ import { findProjection, lowerProjection, mainVerbAfterWith } from './lower';
68
68
  import { buildNamingMap, type NamingMode, type NamingTarget } from './naming';
69
69
 
70
70
  /** The SQL decltype keyword → §2.4 type map (mirrors sql.ts TYPE_MAP so a
@@ -747,7 +747,42 @@ function sqlIdentifier(token: SyqlToken | undefined): string | undefined {
747
747
  return quote === '[' ? value : value.split(`${quote}${quote}`).join(quote);
748
748
  }
749
749
 
750
+ /** A common table expression read in a FROM/JOIN position. */
751
+ export interface CteRef {
752
+ /** Lowercased CTE name. */
753
+ readonly name: string;
754
+ /** Alias (or the CTE name when un-aliased). */
755
+ readonly alias: string;
756
+ readonly start: number;
757
+ readonly nullable: boolean;
758
+ }
759
+
760
+ /** A CTE of the top-level `WITH` whose body is one SELECT scope. */
761
+ export interface CteScope {
762
+ /** Lowercased CTE name. */
763
+ readonly name: string;
764
+ /** Offsets of the body text between the CTE parentheses. */
765
+ readonly start: number;
766
+ readonly end: number;
767
+ /** True for `name(a, b) AS (...)`, which renames the body columns. */
768
+ readonly renamed: boolean;
769
+ }
770
+
771
+ export interface SqlRelations {
772
+ readonly tables: TableRef[];
773
+ readonly cteRefs: CteRef[];
774
+ readonly cteScopes: CteScope[];
775
+ readonly recursive: boolean;
776
+ /** `main` for the outer statement, `cte:<name>` directly inside a
777
+ * top-level CTE body, `nested` inside any other subquery. */
778
+ readonly scopeAt: (offset: number) => string;
779
+ }
780
+
750
781
  export function scanTableRefs(sql: string, ir: IrDocument): TableRef[] {
782
+ return scanRelations(sql, ir).tables;
783
+ }
784
+
785
+ export function scanRelations(sql: string, ir: IrDocument): SqlRelations {
751
786
  const tokens = lexSyqlSqlSource('query SQL', sql).filter(
752
787
  (token) => !isSyqlTrivia(token) && token.kind !== 'eof',
753
788
  );
@@ -820,31 +855,51 @@ export function scanTableRefs(sql: string, ir: IrDocument): TableRef[] {
820
855
  }
821
856
  }
822
857
  const ctes: { name: string; start: number; end: number }[] = [];
858
+ const cteScopes: (CteScope & { open: number })[] = [];
859
+ let recursive = false;
823
860
  for (let index = 0; index < tokens.length; index += 1) {
824
861
  const token = tokens[index];
825
862
  if (token?.kind !== 'identifier' || token.text.toLowerCase() !== 'with')
826
863
  continue;
827
864
  const scopeEnd = closes.get(parents[index] ?? -1) ?? tokens.length;
828
865
  let cursor = index + 1;
829
- if (tokens[cursor]?.text.toLowerCase() === 'recursive') cursor += 1;
866
+ if (tokens[cursor]?.text.toLowerCase() === 'recursive') {
867
+ recursive = true;
868
+ cursor += 1;
869
+ }
830
870
  for (;;) {
831
871
  const name = sqlIdentifier(tokens[cursor]);
832
872
  if (name === undefined) break;
833
873
  cursor += 1;
834
- if (tokens[cursor]?.text === '(')
835
- cursor = (closes.get(cursor) ?? scopeEnd) + 1;
874
+ const renamed = tokens[cursor]?.text === '(';
875
+ if (renamed) cursor = (closes.get(cursor) ?? scopeEnd) + 1;
836
876
  if (tokens[cursor]?.text.toLowerCase() !== 'as') break;
837
877
  cursor += 1;
838
878
  if (tokens[cursor]?.text.toLowerCase() === 'not') cursor += 1;
839
879
  if (tokens[cursor]?.text.toLowerCase() === 'materialized') cursor += 1;
840
880
  if (tokens[cursor]?.text !== '(') break;
841
881
  ctes.push({ name: name.toLowerCase(), start: index, end: scopeEnd });
882
+ const close = tokens[closes.get(cursor) ?? -1];
883
+ if (
884
+ index === 0 &&
885
+ close !== undefined &&
886
+ tokens[cursor + 1]?.text.toLowerCase() === 'select'
887
+ ) {
888
+ cteScopes.push({
889
+ name: name.toLowerCase(),
890
+ open: cursor,
891
+ start: (tokens[cursor] as SyqlToken).span.end.offset,
892
+ end: close.span.start.offset,
893
+ renamed,
894
+ });
895
+ }
842
896
  cursor = (closes.get(cursor) ?? scopeEnd) + 1;
843
897
  if (tokens[cursor]?.text !== ',') break;
844
898
  cursor += 1;
845
899
  }
846
900
  }
847
901
  const refs: TableRef[] = [];
902
+ const cteRefs: CteRef[] = [];
848
903
  const relationStartByDepth = new Map<number, number>();
849
904
  const nullableGroups: { start: number; end: number }[] = [];
850
905
  for (let index = 0; index < tokens.length; index += 1) {
@@ -872,6 +927,10 @@ export function scanTableRefs(sql: string, ir: IrDocument): TableRef[] {
872
927
  const ref = refs[prior];
873
928
  if (ref !== undefined) refs[prior] = { ...ref, nullable: true };
874
929
  }
930
+ // Conservative: any CTE read before a RIGHT/FULL join is optional.
931
+ cteRefs.forEach((ref, prior) => {
932
+ cteRefs[prior] = { ...ref, nullable: true };
933
+ });
875
934
  }
876
935
  if (
877
936
  (outerKind === 'left' || outerKind === 'full') &&
@@ -899,6 +958,24 @@ export function scanTableRefs(sql: string, ir: IrDocument): TableRef[] {
899
958
  'schema-qualified relations are unsupported; use application table names',
900
959
  );
901
960
  }
961
+ let aliasToken = tokens[cursor + 1];
962
+ if (
963
+ aliasToken?.kind === 'identifier' &&
964
+ aliasToken.text.toLowerCase() === 'as'
965
+ )
966
+ aliasToken = tokens[cursor + 2];
967
+ const explicitAlias =
968
+ aliasToken?.kind === 'quoted-identifier' ||
969
+ (aliasToken?.kind === 'identifier' &&
970
+ !RESERVED_ALIAS.has(aliasToken.text.toLowerCase()))
971
+ ? sqlIdentifier(aliasToken)
972
+ : undefined;
973
+ const nullable =
974
+ outerKind === 'left' ||
975
+ outerKind === 'full' ||
976
+ nullableGroups.some(
977
+ (group) => cursor > group.start && cursor < group.end,
978
+ );
902
979
  if (
903
980
  ctes.some(
904
981
  (cte) =>
@@ -906,8 +983,15 @@ export function scanTableRefs(sql: string, ir: IrDocument): TableRef[] {
906
983
  cursor > cte.start &&
907
984
  cursor < cte.end,
908
985
  )
909
- )
986
+ ) {
987
+ cteRefs.push({
988
+ name: rawTable.toLowerCase(),
989
+ alias: explicitAlias ?? rawTable,
990
+ start: tableToken.span.start.offset,
991
+ nullable,
992
+ });
910
993
  continue;
994
+ }
911
995
  const table = known.get(rawTable.toLowerCase());
912
996
  if (table === undefined)
913
997
  throw new TypegenError(
@@ -917,33 +1001,45 @@ export function scanTableRefs(sql: string, ir: IrDocument): TableRef[] {
917
1001
  const tableDepth = depths[cursor] ?? operatorDepth;
918
1002
  if (operator === 'from' || !relationStartByDepth.has(tableDepth))
919
1003
  relationStartByDepth.set(tableDepth, refs.length);
920
- let aliasToken = tokens[cursor + 1];
921
- if (
922
- aliasToken?.kind === 'identifier' &&
923
- aliasToken.text.toLowerCase() === 'as'
924
- )
925
- aliasToken = tokens[cursor + 2];
926
- const explicitAlias =
927
- aliasToken?.kind === 'quoted-identifier' ||
928
- (aliasToken?.kind === 'identifier' &&
929
- !RESERVED_ALIAS.has(aliasToken.text.toLowerCase()))
930
- ? sqlIdentifier(aliasToken)
931
- : undefined;
932
1004
  refs.push({
933
1005
  table,
934
1006
  start: tableToken.span.start.offset,
935
1007
  end: tableToken.span.end.offset,
936
1008
  alias: explicitAlias ?? table,
937
1009
  ...(explicitAlias === undefined ? {} : { explicitAlias }),
938
- nullable:
939
- outerKind === 'left' ||
940
- outerKind === 'full' ||
941
- nullableGroups.some(
942
- (group) => cursor > group.start && cursor < group.end,
943
- ),
1010
+ nullable,
944
1011
  });
945
1012
  }
946
- return refs;
1013
+ const selectOpens = tokens.flatMap((token, index) => {
1014
+ const close = tokens[closes.get(index) ?? -1];
1015
+ const next = tokens[index + 1];
1016
+ return token.text === '(' &&
1017
+ close !== undefined &&
1018
+ next?.kind === 'identifier' &&
1019
+ ['select', 'with', 'values'].includes(next.text.toLowerCase())
1020
+ ? [
1021
+ {
1022
+ index,
1023
+ start: token.span.start.offset,
1024
+ end: close.span.start.offset,
1025
+ },
1026
+ ]
1027
+ : [];
1028
+ });
1029
+ return {
1030
+ tables: refs,
1031
+ cteRefs,
1032
+ cteScopes: cteScopes.map(({ open: _open, ...scope }) => scope),
1033
+ recursive,
1034
+ scopeAt(offset) {
1035
+ const innermost = selectOpens
1036
+ .filter((open) => open.start < offset && offset < open.end)
1037
+ .at(-1);
1038
+ if (innermost === undefined) return 'main';
1039
+ const cte = cteScopes.find((scope) => scope.open === innermost.index);
1040
+ return cte === undefined ? 'nested' : `cte:${cte.name}`;
1041
+ },
1042
+ };
947
1043
  }
948
1044
 
949
1045
  // -- SELECT column → source resolution ---------------------------------------
@@ -955,53 +1051,40 @@ export function scanTableRefs(sql: string, ir: IrDocument): TableRef[] {
955
1051
  // FROM/JOIN tables. When found, that IR column is the source (exact fidelity);
956
1052
  // otherwise it is a computed expression (fallback fidelity).
957
1053
 
958
- interface SelectItem {
1054
+ export interface SelectItem {
959
1055
  /** The raw expression text (before any `AS alias`). */
960
1056
  readonly expr: string;
961
1057
  /** The explicit `AS alias`, if any. */
962
1058
  readonly alias: string | undefined;
963
1059
  }
964
1060
 
965
- /** Split a SELECT list on top-level commas (parens-aware). Returns null when
966
- * the query isn't a plain `SELECT … FROM …` we can split (e.g. `SELECT *`). */
967
- function splitSelectList(sql: string): SelectItem[] | null {
968
- const cleaned = stripCommentsAndStrings(sql).replace(/\s+/g, ' ').trim();
969
- const m = /^SELECT\s+(?:DISTINCT\s+)?(.*?)\sFROM\s/i.exec(cleaned);
970
- if (m === null || m[1] === undefined) return null;
971
- const list = m[1];
972
- if (list.trim() === '*' || /(^|,|\.)\s*\*/.test(list)) return null; // SELECT * / t.*
1061
+ /** Split the outermost SELECT list of `sql` into items. Returns null when the
1062
+ * statement has no outer `SELECT … FROM` or projects `*` / `t.*`. */
1063
+ export function splitSelectList(sql: string): SelectItem[] | null {
1064
+ const projection = findProjection(stripCommentsAndStrings(sql));
1065
+ if (projection === null) return null;
973
1066
  const items: SelectItem[] = [];
974
- let depth = 0;
975
- let start = 0;
976
- for (let i = 0; i <= list.length; i++) {
977
- const ch = list[i];
978
- if (ch === '(') depth += 1;
979
- else if (ch === ')') depth -= 1;
980
- if ((ch === ',' && depth === 0) || i === list.length) {
981
- const raw = list.slice(start, i).trim();
982
- start = i + 1;
983
- if (raw.length === 0) continue;
984
- // `expr AS alias` or `expr alias` (trailing bare identifier). Only treat
985
- // a trailing word as an alias when the expr has more than that word.
986
- // Projection lowering emits the alias double-quoted, so accept both.
987
- const asMatch =
988
- /^(.*?)\s+AS\s+(?:"((?:[^"]|"")*)"|([A-Za-z_][A-Za-z0-9_]*))$/i.exec(
989
- raw,
990
- );
991
- if (asMatch !== null) {
992
- items.push({
993
- expr: (asMatch[1] as string).trim(),
994
- alias: (asMatch[2] ?? (asMatch[3] as string)).replaceAll('""', '"'),
995
- });
996
- } else {
997
- items.push({ expr: raw, alias: undefined });
998
- }
1067
+ for (const { text } of projection.items) {
1068
+ const raw = text.replace(/\s+/g, ' ');
1069
+ if (raw === '*' || /\.\s*\*$/.test(raw)) return null;
1070
+ // `expr AS alias` or `expr alias` (trailing bare identifier). Only treat
1071
+ // a trailing word as an alias when the expr has more than that word.
1072
+ // Projection lowering emits the alias double-quoted, so accept both.
1073
+ const asMatch =
1074
+ /^(.*?)\s+AS\s+(?:"((?:[^"]|"")*)"|([A-Za-z_][A-Za-z0-9_]*))$/i.exec(raw);
1075
+ if (asMatch !== null) {
1076
+ items.push({
1077
+ expr: (asMatch[1] as string).trim(),
1078
+ alias: (asMatch[2] ?? (asMatch[3] as string)).replaceAll('""', '"'),
1079
+ });
1080
+ } else {
1081
+ items.push({ expr: raw, alias: undefined });
999
1082
  }
1000
1083
  }
1001
1084
  return items;
1002
1085
  }
1003
1086
 
1004
- const PLAIN_REF_RE = new RegExp(`^(?:(${IDENT})\\.)?(${IDENT})$`);
1087
+ export const PLAIN_REF_RE = new RegExp(`^(?:(${IDENT})\\.)?(${IDENT})$`);
1005
1088
 
1006
1089
  interface ResolvedSource {
1007
1090
  readonly table: string;
@@ -1614,11 +1697,46 @@ export function analyzeStatement(
1614
1697
 
1615
1698
  // Columns: pair bun's column names + decltypes with our source resolution
1616
1699
  // (against the LOWERED sql — aliased plain refs still resolve exactly).
1700
+ // Each item resolves against the relations of its own SELECT scope; a
1701
+ // qualified reference to a top-level CTE resolves through the CTE body.
1702
+ const scoped = scanRelations(sql, ir);
1703
+ const resolveInScope = (
1704
+ item: SelectItem,
1705
+ scope: string,
1706
+ ): ResolvedSource | null => {
1707
+ const direct = resolveSource(
1708
+ item,
1709
+ scoped.tables.filter((ref) => scoped.scopeAt(ref.start) === scope),
1710
+ ir,
1711
+ );
1712
+ const plain = PLAIN_REF_RE.exec(item.expr);
1713
+ if (direct !== null || plain?.[1] === undefined) return direct;
1714
+ const cteRef = scoped.cteRefs.find(
1715
+ (ref) =>
1716
+ scoped.scopeAt(ref.start) === scope &&
1717
+ ref.alias.toLowerCase() === plain[1]?.toLowerCase(),
1718
+ );
1719
+ const body = scoped.cteScopes.find((cte) => cte.name === cteRef?.name);
1720
+ if (cteRef === undefined || body === undefined || body.renamed) return null;
1721
+ const bodyItem = splitSelectList(sql.slice(body.start, body.end))?.find(
1722
+ (candidate) =>
1723
+ (
1724
+ candidate.alias ?? PLAIN_REF_RE.exec(candidate.expr)?.[2]
1725
+ )?.toLowerCase() === plain[2]?.toLowerCase(),
1726
+ );
1727
+ const source =
1728
+ bodyItem === undefined
1729
+ ? null
1730
+ : resolveInScope(bodyItem, `cte:${body.name}`);
1731
+ return source === null
1732
+ ? null
1733
+ : { ...source, nullableByJoin: source.nullableByJoin || cteRef.nullable };
1734
+ };
1617
1735
  const items = splitSelectList(sql);
1618
1736
  const columns: QueryColumn[] = described.columnNames.map((colName, index) => {
1619
1737
  const decl = described.declaredTypes[index];
1620
1738
  const item = items?.[index];
1621
- const source = item !== undefined ? resolveSource(item, refs, ir) : null;
1739
+ const source = item !== undefined ? resolveInScope(item, 'main') : null;
1622
1740
  const sqlName = sqlNames[index] ?? colName;
1623
1741
  const langName = langNames[index] ?? colName;
1624
1742
  const syncVersionNullable =