@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/sql.ts CHANGED
@@ -242,7 +242,11 @@ function tokenizeStatements(sql: string, source: string): Token[][] {
242
242
  );
243
243
  }
244
244
  i = end;
245
- } else if (DIGIT.test(ch)) {
245
+ } else if (
246
+ DIGIT.test(ch) ||
247
+ ((ch === '-' || ch === '+') && DIGIT.test(sql[i + 1] ?? ''))
248
+ ) {
249
+ // A leading sign belongs to the number: `DEFAULT -1` is a literal.
246
250
  let end = i + 1;
247
251
  while (end < sql.length && /[0-9.]/.test(sql[end] as string)) end += 1;
248
252
  current.push({ kind: 'number', text: sql.slice(i, end) });
@@ -333,8 +337,12 @@ class Cursor {
333
337
  }
334
338
  }
335
339
 
336
- fail(message: string): never {
337
- throw new TypegenError(this.source, message);
340
+ fail(
341
+ message: string,
342
+ code?: string,
343
+ details?: Readonly<Record<string, unknown>>,
344
+ ): never {
345
+ throw new TypegenError(this.source, message, code, details);
338
346
  }
339
347
  }
340
348
 
@@ -401,6 +409,25 @@ function parseColumnDef(cursor: Cursor, allowPrimaryKey: boolean): ColumnDef {
401
409
  `column ${name}: DEFAULT expressions are unsupported (only literals)`,
402
410
  );
403
411
  }
412
+ // A word is a literal only as one of SQLite's keyword literals, so
413
+ // `DEFAULT NOT NULL` cannot swallow the NOT of a constraint.
414
+ if (
415
+ value.kind === 'word' &&
416
+ ![
417
+ 'NULL',
418
+ 'TRUE',
419
+ 'FALSE',
420
+ 'CURRENT_TIME',
421
+ 'CURRENT_DATE',
422
+ 'CURRENT_TIMESTAMP',
423
+ ].includes(value.text.toUpperCase())
424
+ ) {
425
+ cursor.fail(
426
+ 'DEFAULT needs a literal value: a number, a string, NULL, TRUE, FALSE, or CURRENT_TIME/DATE/TIMESTAMP',
427
+ 'typegen.invalid_default',
428
+ { column: name, found: value.text },
429
+ );
430
+ }
404
431
  // Literal defaults are accepted and ignored: typegen extracts the
405
432
  // schema shape; running migrations is the host's job.
406
433
  } else if (word === 'REFERENCES') {
@@ -1,7 +1,7 @@
1
1
  /** Revision-1 SYQL logical-to-physical lowering (§§15–17). */
2
2
  import { TypegenError } from './errors';
3
3
  import type { IrDocument } from './ir';
4
- import { buildNamingMap } from './naming';
4
+ import { buildNamingMap, validateQueryIdentifier } from './naming';
5
5
  import {
6
6
  type AnalyzedQuery,
7
7
  analyzeStatement,
@@ -364,6 +364,20 @@ function publicInputs(
364
364
  maxSize: validated.limit.maxSize,
365
365
  });
366
366
  }
367
+ for (const input of result) {
368
+ for (const langName of [
369
+ input.langName,
370
+ ...(input.kind === 'group' ? input.members : []).map((m) => m.langName),
371
+ ...(input.kind === 'sort' ? input.profiles : []).map((p) => p.langName),
372
+ ]) {
373
+ validateQueryIdentifier(
374
+ langName,
375
+ naming.targets,
376
+ query.module.file,
377
+ `query ${declaration.name} input`,
378
+ );
379
+ }
380
+ }
367
381
  return result;
368
382
  }
369
383
 
@@ -1,6 +1,7 @@
1
1
  /** Schema-aware revision-1 SYQL validation (§§5, 8–13). */
2
2
  import type { IrColumnType, IrDocument } from './ir';
3
3
  import {
4
+ AGGREGATE_FUNCTIONS,
4
5
  type AnalyzedQuery,
5
6
  analyzeStatement,
6
7
  inferParamTypeEvidence,
@@ -12,8 +13,11 @@ import {
12
13
  type CteScope,
13
14
  PLAIN_REF_RE,
14
15
  type SelectItem,
16
+ type ScopeColumn,
15
17
  type SqlRelations,
16
18
  scanRelations,
19
+ scopeProofs,
20
+ sqlIdentifier,
17
21
  splitSelectList,
18
22
  scanTableRefs,
19
23
  stripCommentsAndStrings,
@@ -112,7 +116,6 @@ interface SqliteDiagnostic {
112
116
  readonly message: string;
113
117
  }
114
118
 
115
- const IDENT_SOURCE = '[A-Za-z_][A-Za-z0-9_]*';
116
119
  const IDENT_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
117
120
  const SQLITE_SUBJECT_RE = '[A-Za-z_][A-Za-z0-9_$]*';
118
121
  const NONDETERMINISTIC_FUNCTIONS = new Set([
@@ -186,24 +189,6 @@ function suggestion(authored: string, candidates: readonly string[]): string {
186
189
  return nearest === undefined ? '' : `; did you mean \`${nearest}\`?`;
187
190
  }
188
191
 
189
- function identifierText(token: SyqlToken | undefined): string | undefined {
190
- if (token?.kind === 'identifier') return token.text;
191
- if (token?.kind !== 'quoted-identifier') return undefined;
192
- if (token.text.startsWith('[') && token.text.endsWith(']')) {
193
- return token.text.slice(1, -1).replaceAll(']]', ']');
194
- }
195
- const quote = token.text[0];
196
- return token.text.slice(1, -1).replaceAll(`${quote}${quote}`, quote ?? '');
197
- }
198
- const AGGREGATE_FUNCTIONS = new Set([
199
- 'avg',
200
- 'count',
201
- 'group_concat',
202
- 'max',
203
- 'min',
204
- 'sum',
205
- 'total',
206
- ]);
207
192
  /**
208
193
  * The function surface of the standard SQLite 3.46.0 amalgamation.
209
194
  *
@@ -1158,7 +1143,7 @@ class Validator {
1158
1143
  const tokens = significant(logical.declaration.statement.tokens);
1159
1144
  const [qualifier, column] = subject.split('.', 2);
1160
1145
  const matches = (token: SyqlToken | undefined, expected: string): boolean =>
1161
- identifierText(token)?.toLowerCase() === expected.toLowerCase();
1146
+ sqlIdentifier(token)?.toLowerCase() === expected.toLowerCase();
1162
1147
 
1163
1148
  if (focus === 'table') {
1164
1149
  for (let index = 0; index < tokens.length - 1; index += 1) {
@@ -1295,154 +1280,11 @@ class Validator {
1295
1280
  readonly ref: TableRef;
1296
1281
  readonly scopes: readonly InferredScope[];
1297
1282
  };
1298
- const cleaned = stripCommentsAndStrings(markerSql);
1299
- const structure = this.#inspect(
1300
- cleaned,
1301
- logical.declaration.statement.span.file,
1302
- );
1303
- const markerScopes = scanRelations(markerSql, this.#ir);
1304
- const escapeRegex = (value: string): string =>
1305
- value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1306
- const requiredAt = (index: number): boolean => {
1307
- const scope = markerScopes.scopeAt(index);
1308
- if (scope === 'nested') return false;
1309
- const cte = markerScopes.cteScopes.find(
1310
- (candidate) => `cte:${candidate.name}` === scope,
1311
- );
1312
- const scopeItems =
1313
- cte === undefined
1314
- ? structure.outer
1315
- : structure.tokens.filter(
1316
- (item) =>
1317
- item.depth === 1 &&
1318
- item.token.span.start.offset >= cte.start &&
1319
- item.token.span.end.offset <= cte.end,
1320
- );
1321
- let whereStart: number | undefined;
1322
- for (const item of scopeItems) {
1323
- if (item.token.span.start.offset >= index) break;
1324
- const lower = tokenLower(item.token);
1325
- if (lower === 'where') whereStart = item.token.span.end.offset;
1326
- else if (OUTER_CLAUSE_ENDERS.has(lower ?? '')) whereStart = undefined;
1327
- }
1328
- if (whereStart === undefined) return false;
1329
-
1330
- const clauseEnd =
1331
- scopeItems.find(
1332
- (item) =>
1333
- item.token.span.start.offset > index &&
1334
- OUTER_CLAUSE_ENDERS.has(tokenLower(item.token) ?? ''),
1335
- )?.token.span.start.offset ??
1336
- cte?.end ??
1337
- cleaned.length;
1338
- const matchStack: number[] = [];
1339
- for (let cursor = whereStart; cursor < index; cursor += 1) {
1340
- if (cleaned[cursor] === '(') matchStack.push(cursor);
1341
- else if (cleaned[cursor] === ')') matchStack.pop();
1342
- }
1343
-
1344
- // Parenthesized boolean expressions are valid, but predicates inside a
1345
- // CTE or subquery are not outer proof obligations for sync coverage.
1346
- if (
1347
- matchStack.some((open) =>
1348
- /\b(?:SELECT|WITH)\b/i.test(cleaned.slice(open + 1, index)),
1349
- )
1350
- ) {
1351
- return false;
1352
- }
1353
-
1354
- // Negated equality does not constrain the result to that scope.
1355
- const prefix = cleaned.slice(whereStart, index);
1356
- const boundaries = [...prefix.matchAll(/\b(?:AND|OR)\b/gi)].map(
1357
- (match) => (match.index ?? -1) + match[0].length,
1358
- );
1359
- const lastBoundary = Math.max(prefix.lastIndexOf('('), ...boundaries);
1360
- if (/\bNOT\b/i.test(prefix.slice(lastBoundary + 1))) return false;
1361
- if (
1362
- matchStack.some((open) =>
1363
- /\bNOT\s*$/i.test(cleaned.slice(whereStart, open)),
1364
- )
1365
- ) {
1366
- return false;
1367
- }
1368
-
1369
- const stack: number[] = [];
1370
- for (let cursor = whereStart; cursor < clauseEnd; cursor += 1) {
1371
- const char = cleaned[cursor];
1372
- if (char === '(') stack.push(cursor);
1373
- else if (char === ')') stack.pop();
1374
- if (
1375
- (cursor === 0 || !/[A-Za-z0-9_:]/.test(cleaned[cursor - 1] ?? '')) &&
1376
- /^OR\b/i.test(cleaned.slice(cursor)) &&
1377
- stack.length <= matchStack.length &&
1378
- stack.every((open, stackIndex) => matchStack[stackIndex] === open)
1379
- ) {
1380
- return false;
1381
- }
1382
- }
1383
- return true;
1384
- };
1385
- type JoinEqualityContext =
1386
- | { readonly kind: 'inner' }
1387
- | {
1388
- readonly kind: 'outer';
1389
- /** Lowercased alias introduced by the JOIN that owns this ON. */
1390
- readonly introducedAlias: string;
1391
- /** The operand side this outer join null-extends. An outer join's
1392
- * ON constrains matched rows of the null-extended side only;
1393
- * preserved-side rows appear even when the ON predicate fails. */
1394
- readonly nullExtended: 'introduced' | 'prior';
1395
- };
1396
- const joinEqualityContextAt = (
1397
- index: number,
1398
- ): JoinEqualityContext | undefined => {
1399
- const prefix = cleaned.slice(0, index);
1400
- const clauses = [
1401
- ...prefix.matchAll(
1402
- /\b(ON|JOIN|WHERE|GROUP\s+BY|ORDER\s+BY|HAVING|LIMIT|UNION|EXCEPT|INTERSECT)\b/gi,
1403
- ),
1404
- ];
1405
- const last = clauses.at(-1);
1406
- if (last?.[1]?.toUpperCase() !== 'ON') return undefined;
1407
- const start = (last.index ?? 0) + last[0].length;
1408
- const next =
1409
- /\b(?:JOIN|WHERE|GROUP\s+BY|ORDER\s+BY|HAVING|LIMIT|UNION|EXCEPT|INTERSECT)\b/i.exec(
1410
- cleaned.slice(index),
1411
- );
1412
- const end = next === null ? cleaned.length : index + next.index;
1413
- const clause = cleaned.slice(start, end);
1414
- if (/\b(?:OR|NOT|SELECT|WITH)\b/i.test(clause)) return undefined;
1415
- const join = clauses.at(-2);
1416
- if (join?.[1]?.toUpperCase() !== 'JOIN') return undefined;
1417
- const joinIndex = join.index ?? 0;
1418
- const outerKind = /\b(LEFT|RIGHT|FULL)(?:\s+OUTER)?\s*$/i
1419
- .exec(prefix.slice(0, joinIndex))?.[1]
1420
- ?.toUpperCase();
1421
- if (outerKind === undefined) return { kind: 'inner' };
1422
- // A FULL join preserves both operands, so its ON constrains neither.
1423
- if (outerKind === 'FULL') return undefined;
1424
- const introducedAlias = (
1425
- prefix
1426
- .slice(joinIndex + join[0].length, last.index)
1427
- .match(new RegExp(IDENT_SOURCE, 'g')) ?? []
1428
- )
1429
- .filter((word) => word.toUpperCase() !== 'AS')
1430
- .at(-1)
1431
- ?.toLowerCase();
1432
- if (introducedAlias === undefined) return undefined;
1433
- return {
1434
- kind: 'outer',
1435
- introducedAlias,
1436
- nullExtended: outerKind === 'LEFT' ? 'introduced' : 'prior',
1437
- };
1438
- };
1439
- const refByAlias = new Map(
1440
- refs.map(
1441
- (ref) => [`${refScope(ref)}\0${ref.alias.toLowerCase()}`, ref] as const,
1442
- ),
1443
- );
1283
+ const proofs = scopeProofs(markerSql, this.#ir);
1444
1284
  const scopeNode = (scope: string, alias: string, column: string): string =>
1445
1285
  `${scope}\0${alias.toLowerCase()}\0${column.toLowerCase()}`;
1286
+ const nodeOf = (column: ScopeColumn): string =>
1287
+ scopeNode(column.scope, column.alias, column.column);
1446
1288
  const parents = new Map<string, string>();
1447
1289
  const find = (node: string): string => {
1448
1290
  const parent = parents.get(node);
@@ -1496,67 +1338,11 @@ class Validator {
1496
1338
  }
1497
1339
  return seen;
1498
1340
  };
1499
- const scopeColumn = (
1500
- scope: string,
1501
- alias: string,
1502
- column: string,
1503
- ): boolean => {
1504
- const ref = refByAlias.get(`${scope}\0${alias.toLowerCase()}`);
1505
- const table = this.#ir.tables.find((item) => item.name === ref?.table);
1506
- return (
1507
- table?.scopes.some(
1508
- (scope) => scope.column.toLowerCase() === column.toLowerCase(),
1509
- ) === true
1510
- );
1511
- };
1512
- // Scope inheritance across a join relies on a satisfied predicate proving
1513
- // both operands non-NULL. `IS` is null-safe (`NULL IS NULL` holds), so it
1514
- // would let a null-extended row on an outer join's preserved side carry a
1515
- // scope proof it never satisfies; only `=`/`==` qualify here.
1516
- const qualifiedEquality = new RegExp(
1517
- `(${IDENT_SOURCE})\\.(${IDENT_SOURCE})\\s*(?:=|==)\\s*(${IDENT_SOURCE})\\.(${IDENT_SOURCE})`,
1518
- 'gi',
1519
- );
1520
- for (const match of cleaned.matchAll(qualifiedEquality)) {
1521
- const leftAlias = match[1] as string;
1522
- const leftColumn = match[2] as string;
1523
- const rightAlias = match[3] as string;
1524
- const rightColumn = match[4] as string;
1525
- const index = match.index ?? 0;
1526
- const scope = markerScopes.scopeAt(index);
1527
- if (
1528
- !scopeColumn(scope, leftAlias, leftColumn) ||
1529
- !scopeColumn(scope, rightAlias, rightColumn)
1530
- ) {
1531
- continue;
1532
- }
1533
- const leftNode = scopeNode(scope, leftAlias, leftColumn);
1534
- const rightNode = scopeNode(scope, rightAlias, rightColumn);
1535
- if (requiredAt(index)) {
1536
- union(leftNode, rightNode);
1537
- continue;
1538
- }
1539
- const context = joinEqualityContextAt(index);
1540
- if (context === undefined) continue;
1541
- if (context.kind === 'inner') {
1542
- // An inner join's ON filters every result row, so the equality
1543
- // constrains both operands symmetrically.
1544
- union(leftNode, rightNode);
1545
- continue;
1546
- }
1547
- const leftIntroduced =
1548
- leftAlias.toLowerCase() === context.introducedAlias;
1549
- const rightIntroduced =
1550
- rightAlias.toLowerCase() === context.introducedAlias;
1551
- // An equality with both operands on one side of the outer join proves
1552
- // nothing across it; preserved-side rows appear regardless of the ON.
1553
- if (leftIntroduced === rightIntroduced) continue;
1554
- const introducedNode = leftIntroduced ? leftNode : rightNode;
1555
- const priorNode = leftIntroduced ? rightNode : leftNode;
1556
- if (context.nullExtended === 'introduced') {
1557
- addInheritEdge(introducedNode, priorNode);
1341
+ for (const equality of proofs.equalities) {
1342
+ if (equality.symmetric) {
1343
+ union(nodeOf(equality.target), nodeOf(equality.source));
1558
1344
  } else {
1559
- addInheritEdge(priorNode, introducedNode);
1345
+ addInheritEdge(nodeOf(equality.target), nodeOf(equality.source));
1560
1346
  }
1561
1347
  }
1562
1348
  const required = new Set(
@@ -1575,60 +1361,16 @@ class Validator {
1575
1361
  if (table === undefined) continue;
1576
1362
  const inferred: InferredScope[] = [];
1577
1363
  for (const scope of table.scopes) {
1578
- const alias = escapeRegex(ref.alias);
1579
- const column = escapeRegex(scope.column);
1580
- const unqualifiedMatches = refs.filter((candidate) => {
1581
- if (refScope(candidate) !== refScope(ref)) return false;
1582
- const other = this.#ir.tables.find(
1583
- (item) => item.name === candidate.table,
1584
- );
1585
- return other?.columns.some((item) => item.name === scope.column);
1586
- }).length;
1587
- const subject =
1588
- unqualifiedMatches === 1
1589
- ? `(?<![A-Za-z0-9_.:])(?:${alias}\\.)?${column}\\b`
1590
- : `(?<![A-Za-z0-9_.:])${alias}\\.${column}\\b`;
1591
- const found: Array<{ name: string; operator: 'equal' | 'in' }> = [];
1592
- const equality = new RegExp(
1593
- `${subject}\\s*(?:=|==|\\bIS\\b)\\s*:([A-Za-z_][A-Za-z0-9_]*)\\b|:([A-Za-z_][A-Za-z0-9_]*)\\b\\s*(?:=|==)\\s*${subject}`,
1594
- 'gi',
1595
- );
1596
- const inScope = (index: number): boolean =>
1597
- markerScopes.scopeAt(index) === refScope(ref) && requiredAt(index);
1598
- for (const match of cleaned.matchAll(equality)) {
1599
- if (!inScope(match.index)) continue;
1600
- const name = match[1] ?? match[2];
1601
- if (name !== undefined && required.has(name)) {
1602
- found.push({ name, operator: 'equal' });
1603
- }
1604
- }
1605
- const inList = new RegExp(`${subject}\\s+IN\\s*\\(([^)]*)\\)`, 'gi');
1606
- for (const match of cleaned.matchAll(inList)) {
1607
- if (!inScope(match.index)) continue;
1608
- const body = match[1] ?? '';
1609
- const bodyStart = match.index + match[0].length - body.length - 1;
1610
- const bodyTokens = significant(
1611
- lexSyqlSqlSource(
1612
- logical.declaration.statement.span.file,
1613
- markerSql.slice(bodyStart, bodyStart + body.length),
1614
- ),
1615
- );
1616
- const validBindList = bodyTokens.every((token, index) =>
1617
- index % 2 === 0
1618
- ? token.kind === 'bind'
1619
- : token.kind === 'punctuation' && token.text === ',',
1364
+ const node = scopeNode(refScope(ref), ref.alias, scope.column);
1365
+ const found = proofs.binds
1366
+ .filter(
1367
+ (proof) =>
1368
+ nodeOf(proof) === node &&
1369
+ proof.params.every((name) => required.has(name)),
1370
+ )
1371
+ .flatMap((proof) =>
1372
+ proof.params.map((name) => ({ name, operator: proof.operator })),
1620
1373
  );
1621
- const params = bodyTokens
1622
- .filter((_, index) => index % 2 === 0)
1623
- .map((token) => token.text.slice(1));
1624
- if (
1625
- validBindList &&
1626
- params.length > 0 &&
1627
- params.every((name) => required.has(name))
1628
- ) {
1629
- for (const name of params) found.push({ name, operator: 'in' });
1630
- }
1631
- }
1632
1374
  const params = [...new Set(found.map((item) => item.name))];
1633
1375
  if (params.length > 0) {
1634
1376
  const evidence: InferredScope = {