@syncular/typegen 0.26.6 → 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/dist/query.d.ts +39 -0
- package/dist/query.js +124 -54
- package/dist/syql-validator.js +236 -57
- package/package.json +3 -3
- package/src/query.ts +178 -60
- package/src/syql-validator.ts +304 -81
package/src/syql-validator.ts
CHANGED
|
@@ -9,6 +9,12 @@ import {
|
|
|
9
9
|
type QueryNamingOptions,
|
|
10
10
|
type QueryReactiveMetadata,
|
|
11
11
|
type QueryScopeBinding,
|
|
12
|
+
type CteScope,
|
|
13
|
+
PLAIN_REF_RE,
|
|
14
|
+
type SelectItem,
|
|
15
|
+
type SqlRelations,
|
|
16
|
+
scanRelations,
|
|
17
|
+
splitSelectList,
|
|
12
18
|
scanTableRefs,
|
|
13
19
|
stripCommentsAndStrings,
|
|
14
20
|
type TableRef,
|
|
@@ -307,6 +313,7 @@ const SQL_PAREN_KEYWORDS = new Set([
|
|
|
307
313
|
'in',
|
|
308
314
|
'join',
|
|
309
315
|
'limit',
|
|
316
|
+
'materialized',
|
|
310
317
|
'not',
|
|
311
318
|
'offset',
|
|
312
319
|
'on',
|
|
@@ -332,6 +339,15 @@ const OUTER_CLAUSE_ENDERS = new Set([
|
|
|
332
339
|
'except',
|
|
333
340
|
]);
|
|
334
341
|
|
|
342
|
+
/** One identity field and the relation column that proves it. */
|
|
343
|
+
interface IdentityTerm {
|
|
344
|
+
readonly field: string;
|
|
345
|
+
readonly qualifier: string;
|
|
346
|
+
readonly column: string;
|
|
347
|
+
/** The physical table or CTE name, for diagnostics. */
|
|
348
|
+
readonly relation: string;
|
|
349
|
+
}
|
|
350
|
+
|
|
335
351
|
function typeText(type: SyqlValueType | IrColumnType): string {
|
|
336
352
|
return typeof type === 'string'
|
|
337
353
|
? type
|
|
@@ -450,9 +466,9 @@ class Validator {
|
|
|
450
466
|
location,
|
|
451
467
|
);
|
|
452
468
|
this.#validateDeterminism(activeSql, logical.declaration.statement.span);
|
|
453
|
-
let
|
|
469
|
+
let relations: SqlRelations;
|
|
454
470
|
try {
|
|
455
|
-
|
|
471
|
+
relations = scanRelations(activeSql, this.#ir);
|
|
456
472
|
} catch (error) {
|
|
457
473
|
// Preserve SQLite's source-spanned diagnostics for invalid relations.
|
|
458
474
|
this.#validateSqlite(activeSql, logical, []);
|
|
@@ -462,6 +478,7 @@ class Validator {
|
|
|
462
478
|
error instanceof Error ? error.message : String(error),
|
|
463
479
|
);
|
|
464
480
|
}
|
|
481
|
+
const refs = relations.tables;
|
|
465
482
|
this.#validatePortableProfile(
|
|
466
483
|
activeSql,
|
|
467
484
|
logical.declaration.statement.span,
|
|
@@ -556,21 +573,25 @@ class Validator {
|
|
|
556
573
|
}
|
|
557
574
|
}
|
|
558
575
|
|
|
559
|
-
const reactive = this.#inferReactive(
|
|
560
|
-
|
|
576
|
+
const reactive = this.#inferReactive(
|
|
577
|
+
logical,
|
|
578
|
+
markerSql,
|
|
579
|
+
relations,
|
|
580
|
+
bindSymbols,
|
|
581
|
+
);
|
|
582
|
+
const identityTerms = this.#proveIdentity(
|
|
561
583
|
logical.declaration,
|
|
562
584
|
activeSql,
|
|
563
|
-
|
|
585
|
+
relations,
|
|
564
586
|
analysis,
|
|
565
587
|
);
|
|
588
|
+
const identity = identityTerms?.map((term) => term.field);
|
|
566
589
|
this.#validateStableOrder(
|
|
567
590
|
logical.declaration,
|
|
568
591
|
activeSql,
|
|
569
592
|
activeStructure,
|
|
570
593
|
sort,
|
|
571
|
-
|
|
572
|
-
analysis,
|
|
573
|
-
refs,
|
|
594
|
+
identityTerms,
|
|
574
595
|
);
|
|
575
596
|
const reactiveWithIdentity: QueryReactiveMetadata = {
|
|
576
597
|
...reactive,
|
|
@@ -1245,9 +1266,15 @@ class Validator {
|
|
|
1245
1266
|
#inferReactive(
|
|
1246
1267
|
logical: SyqlLogicalQuery,
|
|
1247
1268
|
markerSql: string,
|
|
1248
|
-
|
|
1269
|
+
relations: SqlRelations,
|
|
1249
1270
|
symbols: ReadonlyMap<string, BindSymbol>,
|
|
1250
1271
|
): QueryReactiveMetadata {
|
|
1272
|
+
// Each instance is proven only by predicates of its own SELECT scope: the
|
|
1273
|
+
// outer statement or a top-level CTE body. Positions are classified in
|
|
1274
|
+
// the marker rendering; instances in the active rendering, whose CTE
|
|
1275
|
+
// bodies are identical because `when` is confined to the outer clauses.
|
|
1276
|
+
const refs = relations.tables;
|
|
1277
|
+
const refScope = (ref: TableRef): string => relations.scopeAt(ref.start);
|
|
1251
1278
|
type InferredScope = {
|
|
1252
1279
|
readonly binding: QueryScopeBinding;
|
|
1253
1280
|
readonly operator: 'equal' | 'in';
|
|
@@ -1261,11 +1288,26 @@ class Validator {
|
|
|
1261
1288
|
cleaned,
|
|
1262
1289
|
logical.declaration.statement.span.file,
|
|
1263
1290
|
);
|
|
1291
|
+
const markerScopes = scanRelations(markerSql, this.#ir);
|
|
1264
1292
|
const escapeRegex = (value: string): string =>
|
|
1265
1293
|
value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1266
1294
|
const requiredAt = (index: number): boolean => {
|
|
1295
|
+
const scope = markerScopes.scopeAt(index);
|
|
1296
|
+
if (scope === 'nested') return false;
|
|
1297
|
+
const cte = markerScopes.cteScopes.find(
|
|
1298
|
+
(candidate) => `cte:${candidate.name}` === scope,
|
|
1299
|
+
);
|
|
1300
|
+
const scopeItems =
|
|
1301
|
+
cte === undefined
|
|
1302
|
+
? structure.outer
|
|
1303
|
+
: structure.tokens.filter(
|
|
1304
|
+
(item) =>
|
|
1305
|
+
item.depth === 1 &&
|
|
1306
|
+
item.token.span.start.offset >= cte.start &&
|
|
1307
|
+
item.token.span.end.offset <= cte.end,
|
|
1308
|
+
);
|
|
1267
1309
|
let whereStart: number | undefined;
|
|
1268
|
-
for (const item of
|
|
1310
|
+
for (const item of scopeItems) {
|
|
1269
1311
|
if (item.token.span.start.offset >= index) break;
|
|
1270
1312
|
const lower = tokenLower(item.token);
|
|
1271
1313
|
if (lower === 'where') whereStart = item.token.span.end.offset;
|
|
@@ -1274,11 +1316,13 @@ class Validator {
|
|
|
1274
1316
|
if (whereStart === undefined) return false;
|
|
1275
1317
|
|
|
1276
1318
|
const clauseEnd =
|
|
1277
|
-
|
|
1319
|
+
scopeItems.find(
|
|
1278
1320
|
(item) =>
|
|
1279
1321
|
item.token.span.start.offset > index &&
|
|
1280
1322
|
OUTER_CLAUSE_ENDERS.has(tokenLower(item.token) ?? ''),
|
|
1281
|
-
)?.token.span.start.offset ??
|
|
1323
|
+
)?.token.span.start.offset ??
|
|
1324
|
+
cte?.end ??
|
|
1325
|
+
cleaned.length;
|
|
1282
1326
|
const matchStack: number[] = [];
|
|
1283
1327
|
for (let cursor = whereStart; cursor < index; cursor += 1) {
|
|
1284
1328
|
if (cleaned[cursor] === '(') matchStack.push(cursor);
|
|
@@ -1380,10 +1424,12 @@ class Validator {
|
|
|
1380
1424
|
};
|
|
1381
1425
|
};
|
|
1382
1426
|
const refByAlias = new Map(
|
|
1383
|
-
refs.map(
|
|
1427
|
+
refs.map(
|
|
1428
|
+
(ref) => [`${refScope(ref)}\0${ref.alias.toLowerCase()}`, ref] as const,
|
|
1429
|
+
),
|
|
1384
1430
|
);
|
|
1385
|
-
const scopeNode = (alias: string, column: string): string =>
|
|
1386
|
-
`${alias.toLowerCase()}\0${column.toLowerCase()}`;
|
|
1431
|
+
const scopeNode = (scope: string, alias: string, column: string): string =>
|
|
1432
|
+
`${scope}\0${alias.toLowerCase()}\0${column.toLowerCase()}`;
|
|
1387
1433
|
const parents = new Map<string, string>();
|
|
1388
1434
|
const find = (node: string): string => {
|
|
1389
1435
|
const parent = parents.get(node);
|
|
@@ -1437,8 +1483,12 @@ class Validator {
|
|
|
1437
1483
|
}
|
|
1438
1484
|
return seen;
|
|
1439
1485
|
};
|
|
1440
|
-
const scopeColumn = (
|
|
1441
|
-
|
|
1486
|
+
const scopeColumn = (
|
|
1487
|
+
scope: string,
|
|
1488
|
+
alias: string,
|
|
1489
|
+
column: string,
|
|
1490
|
+
): boolean => {
|
|
1491
|
+
const ref = refByAlias.get(`${scope}\0${alias.toLowerCase()}`);
|
|
1442
1492
|
const table = this.#ir.tables.find((item) => item.name === ref?.table);
|
|
1443
1493
|
return (
|
|
1444
1494
|
table?.scopes.some(
|
|
@@ -1459,15 +1509,16 @@ class Validator {
|
|
|
1459
1509
|
const leftColumn = match[2] as string;
|
|
1460
1510
|
const rightAlias = match[3] as string;
|
|
1461
1511
|
const rightColumn = match[4] as string;
|
|
1512
|
+
const index = match.index ?? 0;
|
|
1513
|
+
const scope = markerScopes.scopeAt(index);
|
|
1462
1514
|
if (
|
|
1463
|
-
!scopeColumn(leftAlias, leftColumn) ||
|
|
1464
|
-
!scopeColumn(rightAlias, rightColumn)
|
|
1515
|
+
!scopeColumn(scope, leftAlias, leftColumn) ||
|
|
1516
|
+
!scopeColumn(scope, rightAlias, rightColumn)
|
|
1465
1517
|
) {
|
|
1466
1518
|
continue;
|
|
1467
1519
|
}
|
|
1468
|
-
const
|
|
1469
|
-
const
|
|
1470
|
-
const rightNode = scopeNode(rightAlias, rightColumn);
|
|
1520
|
+
const leftNode = scopeNode(scope, leftAlias, leftColumn);
|
|
1521
|
+
const rightNode = scopeNode(scope, rightAlias, rightColumn);
|
|
1471
1522
|
if (requiredAt(index)) {
|
|
1472
1523
|
union(leftNode, rightNode);
|
|
1473
1524
|
continue;
|
|
@@ -1514,6 +1565,7 @@ class Validator {
|
|
|
1514
1565
|
const alias = escapeRegex(ref.alias);
|
|
1515
1566
|
const column = escapeRegex(scope.column);
|
|
1516
1567
|
const unqualifiedMatches = refs.filter((candidate) => {
|
|
1568
|
+
if (refScope(candidate) !== refScope(ref)) return false;
|
|
1517
1569
|
const other = this.#ir.tables.find(
|
|
1518
1570
|
(item) => item.name === candidate.table,
|
|
1519
1571
|
);
|
|
@@ -1528,8 +1580,10 @@ class Validator {
|
|
|
1528
1580
|
`${subject}\\s*(?:=|==|\\bIS\\b)\\s*:([A-Za-z_][A-Za-z0-9_]*)\\b|:([A-Za-z_][A-Za-z0-9_]*)\\b\\s*(?:=|==)\\s*${subject}`,
|
|
1529
1581
|
'gi',
|
|
1530
1582
|
);
|
|
1583
|
+
const inScope = (index: number): boolean =>
|
|
1584
|
+
markerScopes.scopeAt(index) === refScope(ref) && requiredAt(index);
|
|
1531
1585
|
for (const match of cleaned.matchAll(equality)) {
|
|
1532
|
-
if (!
|
|
1586
|
+
if (!inScope(match.index)) continue;
|
|
1533
1587
|
const name = match[1] ?? match[2];
|
|
1534
1588
|
if (name !== undefined && required.has(name)) {
|
|
1535
1589
|
found.push({ name, operator: 'equal' });
|
|
@@ -1537,7 +1591,7 @@ class Validator {
|
|
|
1537
1591
|
}
|
|
1538
1592
|
const inList = new RegExp(`${subject}\\s+IN\\s*\\(([^)]*)\\)`, 'gi');
|
|
1539
1593
|
for (const match of cleaned.matchAll(inList)) {
|
|
1540
|
-
if (!
|
|
1594
|
+
if (!inScope(match.index)) continue;
|
|
1541
1595
|
const body = match[1] ?? '';
|
|
1542
1596
|
const bodyStart = match.index + match[0].length - body.length - 1;
|
|
1543
1597
|
const bodyTokens = significant(
|
|
@@ -1576,7 +1630,10 @@ class Validator {
|
|
|
1576
1630
|
: 'equal',
|
|
1577
1631
|
};
|
|
1578
1632
|
inferred.push(evidence);
|
|
1579
|
-
directScopeEvidence.set(
|
|
1633
|
+
directScopeEvidence.set(
|
|
1634
|
+
scopeNode(refScope(ref), ref.alias, scope.column),
|
|
1635
|
+
evidence,
|
|
1636
|
+
);
|
|
1580
1637
|
}
|
|
1581
1638
|
}
|
|
1582
1639
|
candidates.push({ ref, scopes: inferred });
|
|
@@ -1594,7 +1651,7 @@ class Validator {
|
|
|
1594
1651
|
const direct = directByVariable.get(scope.variable);
|
|
1595
1652
|
if (direct !== undefined) return [direct];
|
|
1596
1653
|
const reachable = reachableProofNodes(
|
|
1597
|
-
scopeNode(candidate.ref.alias, scope.column),
|
|
1654
|
+
scopeNode(refScope(candidate.ref), candidate.ref.alias, scope.column),
|
|
1598
1655
|
);
|
|
1599
1656
|
const inherited = [...directScopeEvidence.entries()]
|
|
1600
1657
|
.filter(([node]) => reachable.has(node))
|
|
@@ -1887,23 +1944,18 @@ class Validator {
|
|
|
1887
1944
|
.trimStart()}`.trim();
|
|
1888
1945
|
}
|
|
1889
1946
|
|
|
1947
|
+
/**
|
|
1948
|
+
* Proves the result identity per SELECT scope. Each physical relation of a
|
|
1949
|
+
* scope contributes its primary key and each CTE relation the identity of
|
|
1950
|
+
* its body, except that a physical relation joined by a required inner
|
|
1951
|
+
* `ON rel.pk = cte.key` equality to a CTE is determined by that CTE row.
|
|
1952
|
+
*/
|
|
1890
1953
|
#proveIdentity(
|
|
1891
1954
|
query: SyqlQueryDeclaration,
|
|
1892
1955
|
sql: string,
|
|
1893
|
-
|
|
1956
|
+
relations: SqlRelations,
|
|
1894
1957
|
analysis: AnalyzedQuery,
|
|
1895
|
-
): readonly
|
|
1896
|
-
const cleaned = stripCommentsAndStrings(sql);
|
|
1897
|
-
const nonSimple =
|
|
1898
|
-
/\b(?:DISTINCT|GROUP\s+BY|UNION|INTERSECT|EXCEPT|LEFT\s+JOIN|RIGHT\s+JOIN|FULL\s+JOIN)\b/i.test(
|
|
1899
|
-
cleaned,
|
|
1900
|
-
) ||
|
|
1901
|
-
/\b(?:count|sum|total|avg|min|max|group_concat)\s*\(/i.test(cleaned) ||
|
|
1902
|
-
(cleaned.match(/\bSELECT\b/gi)?.length ?? 0) !== 1 ||
|
|
1903
|
-
new Set(refs.map((ref) => `${ref.table}\0${ref.alias}`)).size !==
|
|
1904
|
-
refs.length ||
|
|
1905
|
-
new Set(refs.map((ref) => ref.table)).size !== refs.length;
|
|
1906
|
-
|
|
1958
|
+
): readonly IdentityTerm[] | undefined {
|
|
1907
1959
|
const resultNames = new Set<string>();
|
|
1908
1960
|
for (const column of analysis.columns) {
|
|
1909
1961
|
if (!IDENT_RE.test(column.name)) {
|
|
@@ -1923,38 +1975,220 @@ class Validator {
|
|
|
1923
1975
|
resultNames.add(column.name);
|
|
1924
1976
|
}
|
|
1925
1977
|
|
|
1926
|
-
|
|
1927
|
-
const
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
(
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1978
|
+
const cleaned = stripCommentsAndStrings(sql);
|
|
1979
|
+
const simple = (text: string): boolean =>
|
|
1980
|
+
!/\b(?:DISTINCT|GROUP\s+BY|UNION|INTERSECT|EXCEPT|LEFT\s+JOIN|RIGHT\s+JOIN|FULL\s+JOIN)\b/i.test(
|
|
1981
|
+
text,
|
|
1982
|
+
) &&
|
|
1983
|
+
!/\b(?:count|sum|total|avg|min|max|group_concat)\s*\(/i.test(text) &&
|
|
1984
|
+
(text.match(/\bSELECT\b/gi)?.length ?? 0) === 1;
|
|
1985
|
+
const refs = relations.tables;
|
|
1986
|
+
if (relations.cteScopes.length === 0) {
|
|
1987
|
+
if (
|
|
1988
|
+
!simple(cleaned) ||
|
|
1989
|
+
refs.length === 0 ||
|
|
1990
|
+
new Set(refs.map((ref) => ref.table)).size !== refs.length
|
|
1991
|
+
) {
|
|
1992
|
+
return undefined;
|
|
1993
|
+
}
|
|
1994
|
+
const inferred: IdentityTerm[] = [];
|
|
1995
|
+
for (const ref of refs) {
|
|
1996
|
+
const primaryKey = this.#primaryKey(ref.table);
|
|
1997
|
+
const column = analysis.columns.find(
|
|
1998
|
+
(candidate) =>
|
|
1999
|
+
primaryKey !== undefined &&
|
|
2000
|
+
candidate.origin?.table === ref.table &&
|
|
2001
|
+
candidate.origin.column === primaryKey &&
|
|
2002
|
+
!candidate.nullable,
|
|
2003
|
+
);
|
|
2004
|
+
if (column === undefined || primaryKey === undefined) return undefined;
|
|
2005
|
+
inferred.push({
|
|
2006
|
+
field: column.langName,
|
|
2007
|
+
qualifier: ref.alias,
|
|
2008
|
+
column: primaryKey,
|
|
2009
|
+
relation: ref.table,
|
|
2010
|
+
});
|
|
2011
|
+
}
|
|
2012
|
+
return inferred;
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
// A statement with CTEs: every SELECT is a scope, and each scope proves
|
|
2016
|
+
// its keys syntactically from its own projection.
|
|
2017
|
+
const lastCte = relations.cteScopes.at(-1) as CteScope;
|
|
2018
|
+
if (
|
|
2019
|
+
relations.recursive ||
|
|
2020
|
+
(cleaned.match(/\bSELECT\b/gi)?.length ?? 0) !==
|
|
2021
|
+
relations.cteScopes.length + 1
|
|
2022
|
+
) {
|
|
2023
|
+
return undefined;
|
|
2024
|
+
}
|
|
2025
|
+
const cteKeys = new Map<string, readonly string[]>();
|
|
2026
|
+
const scopeKeys = (
|
|
2027
|
+
scope: string,
|
|
2028
|
+
text: string,
|
|
2029
|
+
):
|
|
2030
|
+
| { readonly index: number; readonly term: Omit<IdentityTerm, 'field'> }[]
|
|
2031
|
+
| undefined => {
|
|
2032
|
+
if (!simple(text)) return undefined;
|
|
2033
|
+
const tables = refs.filter(
|
|
2034
|
+
(ref) => relations.scopeAt(ref.start) === scope,
|
|
2035
|
+
);
|
|
2036
|
+
const ctes = relations.cteRefs.filter(
|
|
2037
|
+
(ref) => relations.scopeAt(ref.start) === scope,
|
|
2038
|
+
);
|
|
2039
|
+
const aliases = [...tables, ...ctes].map((ref) =>
|
|
2040
|
+
ref.alias.toLowerCase(),
|
|
2041
|
+
);
|
|
2042
|
+
if (
|
|
2043
|
+
tables.length + ctes.length === 0 ||
|
|
2044
|
+
new Set(tables.map((ref) => ref.table)).size !== tables.length ||
|
|
2045
|
+
new Set(aliases).size !== aliases.length
|
|
2046
|
+
) {
|
|
2047
|
+
return undefined;
|
|
2048
|
+
}
|
|
2049
|
+
const items = splitSelectList(text);
|
|
2050
|
+
if (items === null) return undefined;
|
|
2051
|
+
const projected = (qualifier: string, column: string): number =>
|
|
2052
|
+
items.findIndex((item) => {
|
|
2053
|
+
const plain = PLAIN_REF_RE.exec(item.expr);
|
|
2054
|
+
return (
|
|
2055
|
+
plain !== null &&
|
|
2056
|
+
plain[2]?.toLowerCase() === column.toLowerCase() &&
|
|
2057
|
+
(plain[1] === undefined
|
|
2058
|
+
? aliases.length === 1
|
|
2059
|
+
: plain[1].toLowerCase() === qualifier.toLowerCase())
|
|
2060
|
+
);
|
|
2061
|
+
});
|
|
2062
|
+
// Required inner-join equalities: depth-0 AND conjuncts of ON clauses.
|
|
2063
|
+
const conjuncts: string[] = [];
|
|
2064
|
+
const tokens = significant(lexSyqlSqlSource('query SQL', text));
|
|
2065
|
+
let depth = 0;
|
|
2066
|
+
let on: string[][] | undefined;
|
|
2067
|
+
const flush = (): void => {
|
|
2068
|
+
if (on === undefined) return;
|
|
2069
|
+
const parts = on.map((part) => part.join('').toLowerCase());
|
|
2070
|
+
if (
|
|
2071
|
+
!parts.some((part) =>
|
|
2072
|
+
/\b(?:or|not|select|case|between|collate)\b/.test(part),
|
|
2073
|
+
)
|
|
2074
|
+
) {
|
|
2075
|
+
conjuncts.push(...parts.map((part) => part.replace(/\s+/g, '')));
|
|
2076
|
+
}
|
|
2077
|
+
on = undefined;
|
|
2078
|
+
};
|
|
2079
|
+
for (const token of tokens) {
|
|
2080
|
+
if (token.text === ')') depth -= 1;
|
|
2081
|
+
const lower = tokenLower(token);
|
|
2082
|
+
if (
|
|
2083
|
+
depth === 0 &&
|
|
2084
|
+
token.kind === 'identifier' &&
|
|
2085
|
+
[
|
|
2086
|
+
'on',
|
|
2087
|
+
'join',
|
|
2088
|
+
'where',
|
|
2089
|
+
'group',
|
|
2090
|
+
'order',
|
|
2091
|
+
'limit',
|
|
2092
|
+
'window',
|
|
2093
|
+
'inner',
|
|
2094
|
+
'cross',
|
|
2095
|
+
].includes(lower ?? '')
|
|
2096
|
+
) {
|
|
2097
|
+
flush();
|
|
2098
|
+
if (lower === 'on') on = [[]];
|
|
2099
|
+
} else if (on !== undefined) {
|
|
2100
|
+
if (depth === 0 && lower === 'and') on.push([]);
|
|
2101
|
+
else
|
|
2102
|
+
on.at(-1)?.push(
|
|
2103
|
+
token.kind === 'identifier' ? `${token.text} ` : token.text,
|
|
2104
|
+
);
|
|
2105
|
+
}
|
|
2106
|
+
if (token.text === '(') depth += 1;
|
|
2107
|
+
}
|
|
2108
|
+
flush();
|
|
2109
|
+
const keys: { index: number; term: Omit<IdentityTerm, 'field'> }[] = [];
|
|
2110
|
+
for (const ref of tables) {
|
|
2111
|
+
const primaryKey = this.#primaryKey(ref.table);
|
|
2112
|
+
if (primaryKey === undefined) return undefined;
|
|
2113
|
+
const left = `${ref.alias}.${primaryKey}`.toLowerCase();
|
|
2114
|
+
const determined = ctes.some((cte) =>
|
|
2115
|
+
(cteKeys.get(cte.name) ?? []).some((key) => {
|
|
2116
|
+
const right = `${cte.alias}.${key}`.toLowerCase();
|
|
2117
|
+
return (
|
|
2118
|
+
conjuncts.includes(`${left}=${right}`) ||
|
|
2119
|
+
conjuncts.includes(`${left}==${right}`) ||
|
|
2120
|
+
conjuncts.includes(`${right}=${left}`) ||
|
|
2121
|
+
conjuncts.includes(`${right}==${left}`)
|
|
2122
|
+
);
|
|
2123
|
+
}),
|
|
2124
|
+
);
|
|
2125
|
+
if (determined) continue;
|
|
2126
|
+
const index = projected(ref.alias, primaryKey);
|
|
2127
|
+
if (index < 0) return undefined;
|
|
2128
|
+
keys.push({
|
|
2129
|
+
index,
|
|
2130
|
+
term: {
|
|
2131
|
+
qualifier: ref.alias,
|
|
2132
|
+
column: primaryKey,
|
|
2133
|
+
relation: ref.table,
|
|
2134
|
+
},
|
|
2135
|
+
});
|
|
2136
|
+
}
|
|
2137
|
+
for (const cte of ctes) {
|
|
2138
|
+
const bodyKeys = cteKeys.get(cte.name);
|
|
2139
|
+
if (bodyKeys === undefined) return undefined;
|
|
2140
|
+
for (const key of bodyKeys) {
|
|
2141
|
+
const index = projected(cte.alias, key);
|
|
2142
|
+
if (index < 0) return undefined;
|
|
2143
|
+
keys.push({
|
|
2144
|
+
index,
|
|
2145
|
+
term: { qualifier: cte.alias, column: key, relation: cte.name },
|
|
2146
|
+
});
|
|
2147
|
+
}
|
|
2148
|
+
}
|
|
2149
|
+
return keys;
|
|
2150
|
+
};
|
|
2151
|
+
for (const cte of relations.cteScopes) {
|
|
2152
|
+
if (cte.renamed) continue;
|
|
2153
|
+
const text = cleaned.slice(cte.start, cte.end);
|
|
2154
|
+
const keys = scopeKeys(`cte:${cte.name}`, text);
|
|
2155
|
+
const items = splitSelectList(text);
|
|
2156
|
+
if (keys === undefined || items === null) continue;
|
|
2157
|
+
cteKeys.set(
|
|
2158
|
+
cte.name,
|
|
2159
|
+
keys.map(({ index }) => {
|
|
2160
|
+
const item = items[index] as SelectItem;
|
|
2161
|
+
return item.alias ?? (PLAIN_REF_RE.exec(item.expr)?.[2] as string);
|
|
2162
|
+
}),
|
|
1943
2163
|
);
|
|
1944
|
-
|
|
1945
|
-
|
|
2164
|
+
}
|
|
2165
|
+
const main = scopeKeys('main', cleaned.slice(lastCte.end + 1));
|
|
2166
|
+
if (main === undefined || main.length === 0) return undefined;
|
|
2167
|
+
const inferred: IdentityTerm[] = [];
|
|
2168
|
+
for (const { index, term } of main) {
|
|
2169
|
+
const column = analysis.columns[index];
|
|
2170
|
+
if (column === undefined || column.nullable) return undefined;
|
|
2171
|
+
inferred.push({ field: column.langName, ...term });
|
|
1946
2172
|
}
|
|
1947
2173
|
return inferred;
|
|
1948
2174
|
}
|
|
1949
2175
|
|
|
2176
|
+
#primaryKey(tableName: string): string | undefined {
|
|
2177
|
+
const table = this.#ir.tables.find((item) => item.name === tableName);
|
|
2178
|
+
if (table !== undefined) return table.primaryKey;
|
|
2179
|
+
return this.#ir.tables.some((item) =>
|
|
2180
|
+
item.ftsIndexes.some((index) => index.name === tableName),
|
|
2181
|
+
)
|
|
2182
|
+
? '_syncular_source_id'
|
|
2183
|
+
: undefined;
|
|
2184
|
+
}
|
|
2185
|
+
|
|
1950
2186
|
#validateStableOrder(
|
|
1951
2187
|
query: SyqlQueryDeclaration,
|
|
1952
2188
|
sql: string,
|
|
1953
2189
|
structure: SqlStructure,
|
|
1954
2190
|
sort: SyqlValidatedSort | undefined,
|
|
1955
|
-
identity: readonly
|
|
1956
|
-
analysis: AnalyzedQuery,
|
|
1957
|
-
refs: readonly TableRef[],
|
|
2191
|
+
identity: readonly IdentityTerm[] | undefined,
|
|
1958
2192
|
): void {
|
|
1959
2193
|
const bounded =
|
|
1960
2194
|
query.limit !== undefined ||
|
|
@@ -2003,36 +2237,25 @@ class Validator {
|
|
|
2003
2237
|
);
|
|
2004
2238
|
}
|
|
2005
2239
|
const suffix = terms.slice(terms.length - identity.length);
|
|
2006
|
-
identity.forEach((
|
|
2007
|
-
const result = analysis.columns.find(
|
|
2008
|
-
(column) => column.langName === field,
|
|
2009
|
-
);
|
|
2010
|
-
const origin = result?.origin;
|
|
2240
|
+
identity.forEach((term, index) => {
|
|
2011
2241
|
const match =
|
|
2012
2242
|
/^(?:([A-Za-z_][A-Za-z0-9_]*)\.)?([A-Za-z_][A-Za-z0-9_]*)\s+(?:asc|desc)$/i.exec(
|
|
2013
2243
|
suffix[index] ?? '',
|
|
2014
2244
|
);
|
|
2015
|
-
if (
|
|
2016
|
-
origin === undefined ||
|
|
2017
|
-
match === null ||
|
|
2018
|
-
match[2] !== origin.column
|
|
2019
|
-
) {
|
|
2245
|
+
if (match === null || match[2] !== term.column) {
|
|
2020
2246
|
this.#fail(
|
|
2021
2247
|
'SYQL6006_INVALID_SORT',
|
|
2022
2248
|
order.span,
|
|
2023
|
-
`order ${order.name} must end with plain ${
|
|
2249
|
+
`order ${order.name} must end with plain ${term.column} ASC/DESC identity term`,
|
|
2024
2250
|
);
|
|
2025
2251
|
}
|
|
2026
2252
|
const qualifier = match[1];
|
|
2027
|
-
if (qualifier !== undefined) {
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
`identity order qualifier ${qualifier} does not resolve to ${origin.table}`,
|
|
2034
|
-
);
|
|
2035
|
-
}
|
|
2253
|
+
if (qualifier !== undefined && qualifier !== term.qualifier) {
|
|
2254
|
+
this.#fail(
|
|
2255
|
+
'SYQL6006_INVALID_SORT',
|
|
2256
|
+
order.span,
|
|
2257
|
+
`identity order qualifier ${qualifier} does not resolve to ${term.relation}`,
|
|
2258
|
+
);
|
|
2036
2259
|
}
|
|
2037
2260
|
});
|
|
2038
2261
|
}
|