@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.
@@ -1,4 +1,4 @@
1
- import { analyzeStatement, inferParamTypeEvidence, scanTableRefs, stripCommentsAndStrings, } from './query.js';
1
+ import { analyzeStatement, inferParamTypeEvidence, PLAIN_REF_RE, scanRelations, splitSelectList, scanTableRefs, stripCommentsAndStrings, } from './query.js';
2
2
  import { isSyqlTrivia, lexSyqlSqlSource, SyqlFrontendError, } from './syql-lexer.js';
3
3
  import { syqlRangeEndBind, syqlRangeStartBind } from './syql-semantics.js';
4
4
  const IDENT_SOURCE = '[A-Za-z_][A-Za-z0-9_]*';
@@ -188,6 +188,7 @@ const SQL_PAREN_KEYWORDS = new Set([
188
188
  'in',
189
189
  'join',
190
190
  'limit',
191
+ 'materialized',
191
192
  'not',
192
193
  'offset',
193
194
  'on',
@@ -303,15 +304,16 @@ class Validator {
303
304
  const activeStructure = this.#inspect(activeSql, location);
304
305
  this.#validateStatementShape(activeSql, activeStructure, logical.declaration, location);
305
306
  this.#validateDeterminism(activeSql, logical.declaration.statement.span);
306
- let refs;
307
+ let relations;
307
308
  try {
308
- refs = scanTableRefs(activeSql, this.#ir);
309
+ relations = scanRelations(activeSql, this.#ir);
309
310
  }
310
311
  catch (error) {
311
312
  // Preserve SQLite's source-spanned diagnostics for invalid relations.
312
313
  this.#validateSqlite(activeSql, logical, []);
313
314
  this.#fail('SYQL6002_INVALID_SQL', logical.declaration.statement.span, error instanceof Error ? error.message : String(error));
314
315
  }
316
+ const refs = relations.tables;
315
317
  this.#validatePortableProfile(activeSql, logical.declaration.statement.span, refs);
316
318
  this.#validateSqlite(activeSql, logical, refs);
317
319
  const bindSymbols = this.#bindSymbols(logical.declaration);
@@ -360,9 +362,10 @@ class Validator {
360
362
  this.#fail('SYQL6006_INVALID_SORT', logical.declaration.sort?.span ?? logical.declaration.statement.span, `sort profile ${profile.name} rejected by SQLite: ${message}`);
361
363
  }
362
364
  }
363
- const reactive = this.#inferReactive(logical, markerSql, refs, bindSymbols);
364
- const identity = this.#proveIdentity(logical.declaration, activeSql, refs, analysis);
365
- this.#validateStableOrder(logical.declaration, activeSql, activeStructure, sort, identity, analysis, refs);
365
+ const reactive = this.#inferReactive(logical, markerSql, relations, bindSymbols);
366
+ const identityTerms = this.#proveIdentity(logical.declaration, activeSql, relations, analysis);
367
+ const identity = identityTerms?.map((term) => term.field);
368
+ this.#validateStableOrder(logical.declaration, activeSql, activeStructure, sort, identityTerms);
366
369
  const reactiveWithIdentity = {
367
370
  ...reactive,
368
371
  ...(identity === undefined ? {} : { rowKey: identity }),
@@ -821,13 +824,29 @@ class Validator {
821
824
  }
822
825
  return resolved;
823
826
  }
824
- #inferReactive(logical, markerSql, refs, symbols) {
827
+ #inferReactive(logical, markerSql, relations, symbols) {
828
+ // Each instance is proven only by predicates of its own SELECT scope: the
829
+ // outer statement or a top-level CTE body. Positions are classified in
830
+ // the marker rendering; instances in the active rendering, whose CTE
831
+ // bodies are identical because `when` is confined to the outer clauses.
832
+ const refs = relations.tables;
833
+ const refScope = (ref) => relations.scopeAt(ref.start);
825
834
  const cleaned = stripCommentsAndStrings(markerSql);
826
835
  const structure = this.#inspect(cleaned, logical.declaration.statement.span.file);
836
+ const markerScopes = scanRelations(markerSql, this.#ir);
827
837
  const escapeRegex = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
828
838
  const requiredAt = (index) => {
839
+ const scope = markerScopes.scopeAt(index);
840
+ if (scope === 'nested')
841
+ return false;
842
+ const cte = markerScopes.cteScopes.find((candidate) => `cte:${candidate.name}` === scope);
843
+ const scopeItems = cte === undefined
844
+ ? structure.outer
845
+ : structure.tokens.filter((item) => item.depth === 1 &&
846
+ item.token.span.start.offset >= cte.start &&
847
+ item.token.span.end.offset <= cte.end);
829
848
  let whereStart;
830
- for (const item of structure.outer) {
849
+ for (const item of scopeItems) {
831
850
  if (item.token.span.start.offset >= index)
832
851
  break;
833
852
  const lower = tokenLower(item.token);
@@ -838,8 +857,10 @@ class Validator {
838
857
  }
839
858
  if (whereStart === undefined)
840
859
  return false;
841
- const clauseEnd = structure.outer.find((item) => item.token.span.start.offset > index &&
842
- OUTER_CLAUSE_ENDERS.has(tokenLower(item.token) ?? ''))?.token.span.start.offset ?? cleaned.length;
860
+ const clauseEnd = scopeItems.find((item) => item.token.span.start.offset > index &&
861
+ OUTER_CLAUSE_ENDERS.has(tokenLower(item.token) ?? ''))?.token.span.start.offset ??
862
+ cte?.end ??
863
+ cleaned.length;
843
864
  const matchStack = [];
844
865
  for (let cursor = whereStart; cursor < index; cursor += 1) {
845
866
  if (cleaned[cursor] === '(')
@@ -916,8 +937,8 @@ class Validator {
916
937
  nullExtended: outerKind === 'LEFT' ? 'introduced' : 'prior',
917
938
  };
918
939
  };
919
- const refByAlias = new Map(refs.map((ref) => [ref.alias.toLowerCase(), ref]));
920
- const scopeNode = (alias, column) => `${alias.toLowerCase()}\0${column.toLowerCase()}`;
940
+ const refByAlias = new Map(refs.map((ref) => [`${refScope(ref)}\0${ref.alias.toLowerCase()}`, ref]));
941
+ const scopeNode = (scope, alias, column) => `${scope}\0${alias.toLowerCase()}\0${column.toLowerCase()}`;
921
942
  const parents = new Map();
922
943
  const find = (node) => {
923
944
  const parent = parents.get(node);
@@ -975,8 +996,8 @@ class Validator {
975
996
  }
976
997
  return seen;
977
998
  };
978
- const scopeColumn = (alias, column) => {
979
- const ref = refByAlias.get(alias.toLowerCase());
999
+ const scopeColumn = (scope, alias, column) => {
1000
+ const ref = refByAlias.get(`${scope}\0${alias.toLowerCase()}`);
980
1001
  const table = this.#ir.tables.find((item) => item.name === ref?.table);
981
1002
  return (table?.scopes.some((scope) => scope.column.toLowerCase() === column.toLowerCase()) === true);
982
1003
  };
@@ -990,13 +1011,14 @@ class Validator {
990
1011
  const leftColumn = match[2];
991
1012
  const rightAlias = match[3];
992
1013
  const rightColumn = match[4];
993
- if (!scopeColumn(leftAlias, leftColumn) ||
994
- !scopeColumn(rightAlias, rightColumn)) {
1014
+ const index = match.index ?? 0;
1015
+ const scope = markerScopes.scopeAt(index);
1016
+ if (!scopeColumn(scope, leftAlias, leftColumn) ||
1017
+ !scopeColumn(scope, rightAlias, rightColumn)) {
995
1018
  continue;
996
1019
  }
997
- const index = match.index ?? 0;
998
- const leftNode = scopeNode(leftAlias, leftColumn);
999
- const rightNode = scopeNode(rightAlias, rightColumn);
1020
+ const leftNode = scopeNode(scope, leftAlias, leftColumn);
1021
+ const rightNode = scopeNode(scope, rightAlias, rightColumn);
1000
1022
  if (requiredAt(index)) {
1001
1023
  union(leftNode, rightNode);
1002
1024
  continue;
@@ -1041,6 +1063,8 @@ class Validator {
1041
1063
  const alias = escapeRegex(ref.alias);
1042
1064
  const column = escapeRegex(scope.column);
1043
1065
  const unqualifiedMatches = refs.filter((candidate) => {
1066
+ if (refScope(candidate) !== refScope(ref))
1067
+ return false;
1044
1068
  const other = this.#ir.tables.find((item) => item.name === candidate.table);
1045
1069
  return other?.columns.some((item) => item.name === scope.column);
1046
1070
  }).length;
@@ -1049,8 +1073,9 @@ class Validator {
1049
1073
  : `${alias}\\.${column}`;
1050
1074
  const found = [];
1051
1075
  const equality = new RegExp(`${subject}\\s*(?:=|==|\\bIS\\b)\\s*:([A-Za-z_][A-Za-z0-9_]*)\\b|:([A-Za-z_][A-Za-z0-9_]*)\\b\\s*(?:=|==)\\s*${subject}`, 'gi');
1076
+ const inScope = (index) => markerScopes.scopeAt(index) === refScope(ref) && requiredAt(index);
1052
1077
  for (const match of cleaned.matchAll(equality)) {
1053
- if (!requiredAt(match.index))
1078
+ if (!inScope(match.index))
1054
1079
  continue;
1055
1080
  const name = match[1] ?? match[2];
1056
1081
  if (name !== undefined && required.has(name)) {
@@ -1059,7 +1084,7 @@ class Validator {
1059
1084
  }
1060
1085
  const inList = new RegExp(`${subject}\\s+IN\\s*\\(([^)]*)\\)`, 'gi');
1061
1086
  for (const match of cleaned.matchAll(inList)) {
1062
- if (!requiredAt(match.index))
1087
+ if (!inScope(match.index))
1063
1088
  continue;
1064
1089
  const body = match[1] ?? '';
1065
1090
  const bodyStart = match.index + match[0].length - body.length - 1;
@@ -1091,7 +1116,7 @@ class Validator {
1091
1116
  : 'equal',
1092
1117
  };
1093
1118
  inferred.push(evidence);
1094
- directScopeEvidence.set(scopeNode(ref.alias, scope.column), evidence);
1119
+ directScopeEvidence.set(scopeNode(refScope(ref), ref.alias, scope.column), evidence);
1095
1120
  }
1096
1121
  }
1097
1122
  candidates.push({ ref, scopes: inferred });
@@ -1105,7 +1130,7 @@ class Validator {
1105
1130
  const direct = directByVariable.get(scope.variable);
1106
1131
  if (direct !== undefined)
1107
1132
  return [direct];
1108
- const reachable = reachableProofNodes(scopeNode(candidate.ref.alias, scope.column));
1133
+ const reachable = reachableProofNodes(scopeNode(refScope(candidate.ref), candidate.ref.alias, scope.column));
1109
1134
  const inherited = [...directScopeEvidence.entries()]
1110
1135
  .filter(([node]) => reachable.has(node))
1111
1136
  .map(([, evidence]) => evidence);
@@ -1292,14 +1317,13 @@ class Validator {
1292
1317
  .slice(insertion)
1293
1318
  .trimStart()}`.trim();
1294
1319
  }
1295
- #proveIdentity(query, sql, refs, analysis) {
1296
- const cleaned = stripCommentsAndStrings(sql);
1297
- const nonSimple = /\b(?:DISTINCT|GROUP\s+BY|UNION|INTERSECT|EXCEPT|LEFT\s+JOIN|RIGHT\s+JOIN|FULL\s+JOIN)\b/i.test(cleaned) ||
1298
- /\b(?:count|sum|total|avg|min|max|group_concat)\s*\(/i.test(cleaned) ||
1299
- (cleaned.match(/\bSELECT\b/gi)?.length ?? 0) !== 1 ||
1300
- new Set(refs.map((ref) => `${ref.table}\0${ref.alias}`)).size !==
1301
- refs.length ||
1302
- new Set(refs.map((ref) => ref.table)).size !== refs.length;
1320
+ /**
1321
+ * Proves the result identity per SELECT scope. Each physical relation of a
1322
+ * scope contributes its primary key and each CTE relation the identity of
1323
+ * its body, except that a physical relation joined by a required inner
1324
+ * `ON rel.pk = cte.key` equality to a CTE is determined by that CTE row.
1325
+ */
1326
+ #proveIdentity(query, sql, relations, analysis) {
1303
1327
  const resultNames = new Set();
1304
1328
  for (const column of analysis.columns) {
1305
1329
  if (!IDENT_RE.test(column.name)) {
@@ -1310,26 +1334,188 @@ class Validator {
1310
1334
  }
1311
1335
  resultNames.add(column.name);
1312
1336
  }
1313
- if (nonSimple || refs.length === 0)
1337
+ const cleaned = stripCommentsAndStrings(sql);
1338
+ const simple = (text) => !/\b(?:DISTINCT|GROUP\s+BY|UNION|INTERSECT|EXCEPT|LEFT\s+JOIN|RIGHT\s+JOIN|FULL\s+JOIN)\b/i.test(text) &&
1339
+ !/\b(?:count|sum|total|avg|min|max|group_concat)\s*\(/i.test(text) &&
1340
+ (text.match(/\bSELECT\b/gi)?.length ?? 0) === 1;
1341
+ const refs = relations.tables;
1342
+ if (relations.cteScopes.length === 0) {
1343
+ if (!simple(cleaned) ||
1344
+ refs.length === 0 ||
1345
+ new Set(refs.map((ref) => ref.table)).size !== refs.length) {
1346
+ return undefined;
1347
+ }
1348
+ const inferred = [];
1349
+ for (const ref of refs) {
1350
+ const primaryKey = this.#primaryKey(ref.table);
1351
+ const column = analysis.columns.find((candidate) => primaryKey !== undefined &&
1352
+ candidate.origin?.table === ref.table &&
1353
+ candidate.origin.column === primaryKey &&
1354
+ !candidate.nullable);
1355
+ if (column === undefined || primaryKey === undefined)
1356
+ return undefined;
1357
+ inferred.push({
1358
+ field: column.langName,
1359
+ qualifier: ref.alias,
1360
+ column: primaryKey,
1361
+ relation: ref.table,
1362
+ });
1363
+ }
1364
+ return inferred;
1365
+ }
1366
+ // A statement with CTEs: every SELECT is a scope, and each scope proves
1367
+ // its keys syntactically from its own projection.
1368
+ const lastCte = relations.cteScopes.at(-1);
1369
+ if (relations.recursive ||
1370
+ (cleaned.match(/\bSELECT\b/gi)?.length ?? 0) !==
1371
+ relations.cteScopes.length + 1) {
1372
+ return undefined;
1373
+ }
1374
+ const cteKeys = new Map();
1375
+ const scopeKeys = (scope, text) => {
1376
+ if (!simple(text))
1377
+ return undefined;
1378
+ const tables = refs.filter((ref) => relations.scopeAt(ref.start) === scope);
1379
+ const ctes = relations.cteRefs.filter((ref) => relations.scopeAt(ref.start) === scope);
1380
+ const aliases = [...tables, ...ctes].map((ref) => ref.alias.toLowerCase());
1381
+ if (tables.length + ctes.length === 0 ||
1382
+ new Set(tables.map((ref) => ref.table)).size !== tables.length ||
1383
+ new Set(aliases).size !== aliases.length) {
1384
+ return undefined;
1385
+ }
1386
+ const items = splitSelectList(text);
1387
+ if (items === null)
1388
+ return undefined;
1389
+ const projected = (qualifier, column) => items.findIndex((item) => {
1390
+ const plain = PLAIN_REF_RE.exec(item.expr);
1391
+ return (plain !== null &&
1392
+ plain[2]?.toLowerCase() === column.toLowerCase() &&
1393
+ (plain[1] === undefined
1394
+ ? aliases.length === 1
1395
+ : plain[1].toLowerCase() === qualifier.toLowerCase()));
1396
+ });
1397
+ // Required inner-join equalities: depth-0 AND conjuncts of ON clauses.
1398
+ const conjuncts = [];
1399
+ const tokens = significant(lexSyqlSqlSource('query SQL', text));
1400
+ let depth = 0;
1401
+ let on;
1402
+ const flush = () => {
1403
+ if (on === undefined)
1404
+ return;
1405
+ const parts = on.map((part) => part.join('').toLowerCase());
1406
+ if (!parts.some((part) => /\b(?:or|not|select|case|between|collate)\b/.test(part))) {
1407
+ conjuncts.push(...parts.map((part) => part.replace(/\s+/g, '')));
1408
+ }
1409
+ on = undefined;
1410
+ };
1411
+ for (const token of tokens) {
1412
+ if (token.text === ')')
1413
+ depth -= 1;
1414
+ const lower = tokenLower(token);
1415
+ if (depth === 0 &&
1416
+ token.kind === 'identifier' &&
1417
+ [
1418
+ 'on',
1419
+ 'join',
1420
+ 'where',
1421
+ 'group',
1422
+ 'order',
1423
+ 'limit',
1424
+ 'window',
1425
+ 'inner',
1426
+ 'cross',
1427
+ ].includes(lower ?? '')) {
1428
+ flush();
1429
+ if (lower === 'on')
1430
+ on = [[]];
1431
+ }
1432
+ else if (on !== undefined) {
1433
+ if (depth === 0 && lower === 'and')
1434
+ on.push([]);
1435
+ else
1436
+ on.at(-1)?.push(token.kind === 'identifier' ? `${token.text} ` : token.text);
1437
+ }
1438
+ if (token.text === '(')
1439
+ depth += 1;
1440
+ }
1441
+ flush();
1442
+ const keys = [];
1443
+ for (const ref of tables) {
1444
+ const primaryKey = this.#primaryKey(ref.table);
1445
+ if (primaryKey === undefined)
1446
+ return undefined;
1447
+ const left = `${ref.alias}.${primaryKey}`.toLowerCase();
1448
+ const determined = ctes.some((cte) => (cteKeys.get(cte.name) ?? []).some((key) => {
1449
+ const right = `${cte.alias}.${key}`.toLowerCase();
1450
+ return (conjuncts.includes(`${left}=${right}`) ||
1451
+ conjuncts.includes(`${left}==${right}`) ||
1452
+ conjuncts.includes(`${right}=${left}`) ||
1453
+ conjuncts.includes(`${right}==${left}`));
1454
+ }));
1455
+ if (determined)
1456
+ continue;
1457
+ const index = projected(ref.alias, primaryKey);
1458
+ if (index < 0)
1459
+ return undefined;
1460
+ keys.push({
1461
+ index,
1462
+ term: {
1463
+ qualifier: ref.alias,
1464
+ column: primaryKey,
1465
+ relation: ref.table,
1466
+ },
1467
+ });
1468
+ }
1469
+ for (const cte of ctes) {
1470
+ const bodyKeys = cteKeys.get(cte.name);
1471
+ if (bodyKeys === undefined)
1472
+ return undefined;
1473
+ for (const key of bodyKeys) {
1474
+ const index = projected(cte.alias, key);
1475
+ if (index < 0)
1476
+ return undefined;
1477
+ keys.push({
1478
+ index,
1479
+ term: { qualifier: cte.alias, column: key, relation: cte.name },
1480
+ });
1481
+ }
1482
+ }
1483
+ return keys;
1484
+ };
1485
+ for (const cte of relations.cteScopes) {
1486
+ if (cte.renamed)
1487
+ continue;
1488
+ const text = cleaned.slice(cte.start, cte.end);
1489
+ const keys = scopeKeys(`cte:${cte.name}`, text);
1490
+ const items = splitSelectList(text);
1491
+ if (keys === undefined || items === null)
1492
+ continue;
1493
+ cteKeys.set(cte.name, keys.map(({ index }) => {
1494
+ const item = items[index];
1495
+ return item.alias ?? PLAIN_REF_RE.exec(item.expr)?.[2];
1496
+ }));
1497
+ }
1498
+ const main = scopeKeys('main', cleaned.slice(lastCte.end + 1));
1499
+ if (main === undefined || main.length === 0)
1314
1500
  return undefined;
1315
1501
  const inferred = [];
1316
- for (const ref of refs) {
1317
- const table = this.#ir.tables.find((item) => item.name === ref.table);
1318
- const primaryKey = table?.primaryKey ??
1319
- (this.#ir.tables.some((item) => item.ftsIndexes.some((index) => index.name === ref.table))
1320
- ? '_syncular_source_id'
1321
- : undefined);
1322
- const column = analysis.columns.find((candidate) => primaryKey !== undefined &&
1323
- candidate.origin?.table === ref.table &&
1324
- candidate.origin.column === primaryKey &&
1325
- !candidate.nullable);
1326
- if (column === undefined)
1502
+ for (const { index, term } of main) {
1503
+ const column = analysis.columns[index];
1504
+ if (column === undefined || column.nullable)
1327
1505
  return undefined;
1328
- inferred.push(column.langName);
1506
+ inferred.push({ field: column.langName, ...term });
1329
1507
  }
1330
1508
  return inferred;
1331
1509
  }
1332
- #validateStableOrder(query, sql, structure, sort, identity, analysis, refs) {
1510
+ #primaryKey(tableName) {
1511
+ const table = this.#ir.tables.find((item) => item.name === tableName);
1512
+ if (table !== undefined)
1513
+ return table.primaryKey;
1514
+ return this.#ir.tables.some((item) => item.ftsIndexes.some((index) => index.name === tableName))
1515
+ ? '_syncular_source_id'
1516
+ : undefined;
1517
+ }
1518
+ #validateStableOrder(query, sql, structure, sort, identity) {
1333
1519
  const bounded = query.limit !== undefined ||
1334
1520
  structure.hasOuterLimit ||
1335
1521
  structure.hasOuterOffset;
@@ -1364,21 +1550,14 @@ class Validator {
1364
1550
  this.#fail('SYQL6006_INVALID_SORT', order.span, `order ${order.name} does not end in the complete identity tie-breaker`);
1365
1551
  }
1366
1552
  const suffix = terms.slice(terms.length - identity.length);
1367
- identity.forEach((field, index) => {
1368
- const result = analysis.columns.find((column) => column.langName === field);
1369
- const origin = result?.origin;
1553
+ identity.forEach((term, index) => {
1370
1554
  const match = /^(?:([A-Za-z_][A-Za-z0-9_]*)\.)?([A-Za-z_][A-Za-z0-9_]*)\s+(?:asc|desc)$/i.exec(suffix[index] ?? '');
1371
- if (origin === undefined ||
1372
- match === null ||
1373
- match[2] !== origin.column) {
1374
- this.#fail('SYQL6006_INVALID_SORT', order.span, `order ${order.name} must end with plain ${origin?.column ?? field} ASC/DESC identity term`);
1555
+ if (match === null || match[2] !== term.column) {
1556
+ this.#fail('SYQL6006_INVALID_SORT', order.span, `order ${order.name} must end with plain ${term.column} ASC/DESC identity term`);
1375
1557
  }
1376
1558
  const qualifier = match[1];
1377
- if (qualifier !== undefined) {
1378
- const ref = refs.find((candidate) => candidate.alias === qualifier);
1379
- if (ref?.table !== origin.table) {
1380
- this.#fail('SYQL6006_INVALID_SORT', order.span, `identity order qualifier ${qualifier} does not resolve to ${origin.table}`);
1381
- }
1559
+ if (qualifier !== undefined && qualifier !== term.qualifier) {
1560
+ this.#fail('SYQL6006_INVALID_SORT', order.span, `identity order qualifier ${qualifier} does not resolve to ${term.relation}`);
1382
1561
  }
1383
1562
  });
1384
1563
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/typegen",
3
- "version": "0.26.5",
3
+ "version": "0.27.0",
4
4
  "description": "Syncular schema-to-TypeScript type generator and the `syncular` CLI",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Benjamin Kniffler",
@@ -56,9 +56,9 @@
56
56
  "!dist/**/*.test.d.ts"
57
57
  ],
58
58
  "dependencies": {
59
- "@syncular/core": "0.26.5"
59
+ "@syncular/core": "0.27.0"
60
60
  },
61
61
  "devDependencies": {
62
- "@syncular/server": "0.26.5"
62
+ "@syncular/server": "0.27.0"
63
63
  }
64
64
  }