trilean-sql 1.0.1 → 1.2.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/README.md +65 -24
- package/dist/compile.cjs +30 -17
- package/dist/compile.d.cts +2 -1
- package/dist/compile.d.ts +2 -1
- package/dist/compile.js +30 -17
- package/dist/errors.cjs +20 -2
- package/dist/errors.d.cts +15 -3
- package/dist/errors.d.ts +15 -3
- package/dist/errors.js +20 -3
- package/dist/guard.cjs +49 -21
- package/dist/guard.d.cts +4 -0
- package/dist/guard.d.ts +4 -0
- package/dist/guard.js +49 -21
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/options.cjs +27 -0
- package/dist/options.d.cts +38 -6
- package/dist/options.d.ts +38 -6
- package/dist/options.js +26 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -65,13 +65,13 @@ The fragment is a self-contained boolean expression with no `WHERE` keyword of i
|
|
|
65
65
|
|
|
66
66
|
Returns `{ sql, params }`. Throws rather than approximating; see [Refusal](#refusal).
|
|
67
67
|
|
|
68
|
-
`options.dialect` is `"postgres"`. It is required rather than defaulted so that a
|
|
68
|
+
`options.dialect` is `"postgres"` or `"sqlite"`. It is required rather than defaulted so that a caller states the engine it is compiling for, instead of inheriting whichever one this package happened to implement first. See [Dialects](#dialects) for what differs between them; the tree, the refusals, and the three-valued guarantee do not.
|
|
69
69
|
|
|
70
70
|
`options.columnFor(referenceKey)` maps a `reference` node's key onto `{ column, paramType? }`. Only string keys reach it — trilean permits any JSON value as a key, and a non-string one is refused before the call. Throwing from `columnFor` is how you reject a key you have no column for; the exception propagates out unchanged rather than being wrapped. It is memoised for the duration of one compilation, so it is called once per distinct key however many times that key appears.
|
|
71
71
|
|
|
72
72
|
`column` may be qualified with dots (`"members.age"`); each segment is emitted as its own double-quoted identifier. Quoting is unconditional, which is what makes an arbitrary column name safe — an embedded quote is doubled, so a name carrying `"; DROP TABLE ...` becomes one inert identifier that simply does not exist. It also means the name is taken literally rather than case-folded, so return the column's real, case-exact name.
|
|
73
73
|
|
|
74
|
-
`paramType` declares the column's value kind: `"text"`, `"number"`, `"boolean"`, or `"timestamp"` (trilean's `instant`). It is optional and worth supplying — it does not change the emitted SQL, but it is the only thing that lets the compiler detect the operand-kind mismatches described under [Refusal](#refusal). Without it, a comparison
|
|
74
|
+
`paramType` declares the column's value kind: `"text"`, `"number"`, `"boolean"`, or `"timestamp"` (trilean's `instant`). It is optional and worth supplying — it does not change the emitted SQL, but it is the only thing that lets the compiler detect the operand-kind mismatches described under [Refusal](#refusal). Without it, a comparison the database would coerce into a definite answer where trilean returns `indeterminate` compiles silently.
|
|
75
75
|
|
|
76
76
|
### `findUnpushableNodeKind(node, options?)`
|
|
77
77
|
|
|
@@ -92,22 +92,43 @@ Passing `options` widens the check: without them the walk is purely structural;
|
|
|
92
92
|
|
|
93
93
|
## What compiles
|
|
94
94
|
|
|
95
|
-
| Node | PostgreSQL |
|
|
96
|
-
| --- | --- |
|
|
97
|
-
| `and`, `or`, `not` | `AND`, `OR`, `NOT` |
|
|
98
|
-
| `allOf`, `anyOf` | n-ary `AND`, `OR`; empty operands become each connective's identity, `TRUE` and `FALSE`, matching the evaluator's own fold |
|
|
99
|
-
| `compare` | `>`, `>=`, `<`, `<=`, `=`, `<>` |
|
|
100
|
-
| `textCompare` | `=`, `<>`, and `~` / `!~` for `matches` / `notMatches` |
|
|
101
|
-
| `memberOf` | `IN` / `NOT IN`, one parameter per candidate |
|
|
102
|
-
| `exists` | `IS NOT NULL` |
|
|
103
|
-
| `reference` | the mapped column, as a quoted identifier |
|
|
104
|
-
| `textLiteral`, `numberLiteral`, `booleanLiteral`, `instantLiteral` | a bind parameter, cast to `text`, `double precision`, `boolean`, `timestamptz` |
|
|
95
|
+
| Node | PostgreSQL | SQLite |
|
|
96
|
+
| --- | --- | --- |
|
|
97
|
+
| `and`, `or`, `not` | `AND`, `OR`, `NOT` | same |
|
|
98
|
+
| `allOf`, `anyOf` | n-ary `AND`, `OR`; empty operands become each connective's identity, `TRUE` and `FALSE`, matching the evaluator's own fold | same |
|
|
99
|
+
| `compare` | `>`, `>=`, `<`, `<=`, `=`, `<>` | same |
|
|
100
|
+
| `textCompare` | `=`, `<>`, and `~` / `!~` for `matches` / `notMatches` | `=`, `<>`, and `REGEXP` / `NOT REGEXP` |
|
|
101
|
+
| `memberOf` | `IN` / `NOT IN`, one parameter per candidate | same |
|
|
102
|
+
| `exists` | `IS NOT NULL` | same |
|
|
103
|
+
| `reference` | the mapped column, as a quoted identifier | same |
|
|
104
|
+
| `textLiteral`, `numberLiteral`, `booleanLiteral`, `instantLiteral` | a bind parameter, cast to `text`, `double precision`, `boolean`, `timestamptz` | a bare `?`, uncast |
|
|
105
105
|
|
|
106
|
-
Every literal in the tree becomes a parameter. Nothing but structure, operators and quoted identifiers is ever written into the returned `sql`, so a literal's content cannot alter the statement.
|
|
106
|
+
Every literal in the tree becomes a parameter. Nothing but structure, operators and quoted identifiers is ever written into the returned `sql`, so a literal's content cannot alter the statement. Identifier quoting is the same in both: each dot-separated segment double-quoted with any embedded quote doubled, which is ANSI-standard and not a per-dialect rule.
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
PostgreSQL placeholders are always cast. That is not decoration: PostgreSQL rejects `$1 < $2` outright because it cannot determine either parameter's type, and casting each placeholder to the type its own literal kind implies is what makes a fragment's meaning independent of how a particular driver decided to infer an untyped parameter. `timestamptz` rather than `timestamp`, because trilean's `instant` is an ISO-8601 string that may carry an offset and parsing one as a naive timestamp would silently discard it. SQLite placeholders carry neither a number nor a cast, because there is nothing to write: parameters bind by the order they appear, and there is no type to annotate. It answers a comparison between two bare `?` from the bound values themselves.
|
|
109
109
|
|
|
110
|
-
An empty `memberOf` candidate list is worth a note, because `IN ()` is a syntax error and the two constants it is tempting to fold to are both wrong. An empty `in` is false and an empty `notIn` is true only once the operand itself is known, and both stay unknown while it is `NULL`. The compiled forms — `(x IS NULL AND NULL::boolean)` and `(x IS NOT NULL OR NULL::boolean)
|
|
110
|
+
An empty `memberOf` candidate list is worth a note, because `IN ()` is a syntax error and the two constants it is tempting to fold to are both wrong. An empty `in` is false and an empty `notIn` is true only once the operand itself is known, and both stay unknown while it is `NULL`. The compiled forms — `(x IS NULL AND NULL::boolean)` and `(x IS NOT NULL OR NULL::boolean)`, and the same two without the cast under SQLite, which has no boolean type to annotate — reproduce that exactly, which a bare `FALSE`/`TRUE` would not, most visibly under a surrounding `NOT`.
|
|
111
|
+
|
|
112
|
+
## Dialects
|
|
113
|
+
|
|
114
|
+
The dialects differ in three places, and nowhere else. `matches`/`notMatches` compile to each engine's own regular-expression operator; placeholders are `$N::type` under PostgreSQL and a bare `?` under SQLite; and the bare `NULL` in an empty `memberOf` carries a `::boolean` annotation only where there is a boolean type to annotate. Everything else the compiler emits — the connectives, the six comparison operators, `=`/`<>`, `IN`/`NOT IN`, `IS NOT NULL`, quoted identifiers — is ANSI-standard and identical.
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
const { sql, params } = compilePredicateNode(rule, { dialect: "sqlite", columnFor });
|
|
118
|
+
// sql: (("age" >= ?) AND ("email" REGEXP ?))
|
|
119
|
+
// params: [18, "@example[.]com$"]
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
What compiles, what is refused, and the row-for-row agreement with `evaluatePredicate` are the same in both. The refusals in particular are not PostgreSQL caution carried over untested: SQLite's type affinity produces the same silent definite answers where trilean returns `wrong-type`, and `test/integration/sqlite.test.ts` measures each one against a real connection. A `TEXT`-affinity column compared to the number `5` is compared *as text*, so `'9' > 5` is true there while `'10' > 5` is not; a boolean is an integer, so `gt` on one answers by integer ordering; and `'abc' > 5`, with no column involved at all, answers `true` rather than erroring.
|
|
123
|
+
|
|
124
|
+
Two things a SQLite caller has to supply that a PostgreSQL caller does not, both of which fail loudly rather than silently:
|
|
125
|
+
|
|
126
|
+
- **A `REGEXP` function**, if the tree uses `matches` or `notMatches` — unless you set `sqliteRegexpAvailable: false`, in which case those two are refused at compile time instead. See [Regular expressions](#regular-expressions).
|
|
127
|
+
- **Booleans bound as `0`/`1`.** SQLite has no boolean type, and drivers do not agree on whether a JS boolean is bindable at all — better-sqlite3 rejects one outright (*"SQLite3 can only bind numbers, strings, bigints, buffers, and null"*). `params` carries the tree's own literals unchanged in every dialect, so converting them is the binding caller's job: `params.map((v) => (typeof v === "boolean" ? Number(v) : v))`.
|
|
128
|
+
|
|
129
|
+
A dialect this version does not implement is refused by name, from `compilePredicateNode` and `findUnpushableNodeKind` alike, with `UnknownDialectError`. `SqlDialect` is a closed union so TypeScript source cannot reach that, but a dialect read from configuration and asserted into the union at the boundary can, and reporting such a tree as pushable would promise a compilation that cannot happen.
|
|
130
|
+
|
|
131
|
+
Instants are the one place to be deliberate rather than merely careful. SQLite has no timestamp type either, so an `instantLiteral` is compared as text against whatever text the column holds. Offset-bearing ISO-8601 in a single common offset (`2020-01-01T00:00:00Z`) sorts chronologically as a string and compares correctly; mixed offsets, or a format that is not ISO-8601, do not. This is the SQLite counterpart of the PostgreSQL session-time-zone caveat under [Refusal](#refusal), and the same advice resolves both.
|
|
111
132
|
|
|
112
133
|
## Refusal
|
|
113
134
|
|
|
@@ -117,30 +138,50 @@ The check is an allow-list walk rather than a deny-list, so a node kind added to
|
|
|
117
138
|
|
|
118
139
|
That guarantee is about the tree's structure, and it has two limits, both about the *content* of a string operand rather than any node's kind, and neither reachable by a walk over kinds. A `matches`/`notMatches` pattern is matched by the server in PostgreSQL's own regular-expression language, not ECMAScript's — see [Regular expressions](#regular-expressions), which is where the row-for-row claim actually stops. An `instantLiteral` is parsed by PostgreSQL rather than by `Date`, so one carrying no UTC offset is read in the *database session's* time zone where trilean reads it in the Node process's, and one PostgreSQL cannot parse at all raises a query error at execution time rather than this exception (trilean answers `indeterminate` for the same string). Pass instants as offset-bearing ISO-8601, which both read identically.
|
|
119
140
|
|
|
120
|
-
|
|
141
|
+
Every refusal below applies to both dialects. What changes with the dialect is the `reason` text, which names the mechanism that actually applies to the engine you are compiling for — a `findUnpushableNodeKind` call given no `options` has no dialect to read and describes PostgreSQL, the one these refusals were first derived against.
|
|
142
|
+
|
|
143
|
+
**Kinds this version does not translate.** `some`, `every`, `fold`: these range over a collection the caller's resolvers supply, which is not the query's row set. `lookup`, `call`, `delegate`, `treeReference`: each is resolved by something the database has no access to — the caller's resolvers, its function registry, an external system. `conditional`: not implemented here. `accumulator`: only meaningful inside a `reduce` fold. `arithmetic` and `negate`: these carry and combine units, and pushing them down would drop that dimensional analysis without saying so. `durationLiteral`: trilean compares durations by normalising both operands to milliseconds, with no column-level equivalent to normalise against. `complexLiteral`: neither engine has a complex type.
|
|
121
144
|
|
|
122
|
-
**Shapes refused despite a supported kind.** A `reference` whose key is not a string, since there is nothing to map. A `reference` or `numberLiteral` carrying a `unit`: a unit on a reference asserts that the resolved value carries the same one, and a column has no unit for that assertion to be checked against. A `numberLiteral` of `NaN
|
|
145
|
+
**Shapes refused despite a supported kind.** A `reference` whose key is not a string, since there is nothing to map. A `reference` or `numberLiteral` carrying a `unit`: a unit on a reference asserts that the resolved value carries the same one, and a column has no unit for that assertion to be checked against. A `numberLiteral` of `NaN`, which trilean compares with `===` — under which NaN equals nothing including itself — and neither engine reproduces, for opposite reasons. PostgreSQL defines NaN as equal to itself and greater than every other double, so `NaN = NaN` selects every row there and none here. SQLite has no NaN at all and a driver binding one substitutes SQL `NULL`, so the same comparison is *indeterminate* there and matches nothing — which looks like agreement until you negate it, at which point trilean's definite `true` matches every row and SQLite's `NULL` still matches none. Infinities are not refused alongside it; every engine here orders them identically.
|
|
123
146
|
|
|
124
|
-
**Operand pairings the
|
|
147
|
+
**Operand pairings the engines would answer differently.** These are the reason to declare `paramType`, and they are only detectable where it is declared:
|
|
125
148
|
|
|
126
|
-
- A `compare` against text. trilean returns `wrong-type` and directs you to `textCompare
|
|
127
|
-
- An ordering `compare` (`gt`/`gte`/`lt`/`lte`) against a boolean. trilean has no order for booleans; PostgreSQL orders `false` before `true
|
|
149
|
+
- A `compare` against text. trilean returns `wrong-type` and directs you to `textCompare`. PostgreSQL orders the operand by collation and answers definitely; SQLite applies the text operand's own affinity to the other side and compares lexicographically, which is worse rather than better — `'9' > 5` is true and `'10' > 5` is not.
|
|
150
|
+
- An ordering `compare` (`gt`/`gte`/`lt`/`lte`) against a boolean. trilean has no order for booleans; PostgreSQL orders `false` before `true`, and SQLite orders the integers 0 and 1 it stores them as.
|
|
128
151
|
- A `textCompare` against a non-text operand, which trilean treats as `wrong-type`.
|
|
129
|
-
- Any comparison whose operands are of different declared kinds — a number against an instant, say. trilean calls that `wrong-type`;
|
|
152
|
+
- Any comparison whose operands are of different declared kinds — a number against an instant, say. trilean calls that `wrong-type`; both engines may coerce one to the other and answer definitely.
|
|
130
153
|
|
|
131
154
|
Left undeclared, these compile, and the divergence is real but invisible. That is the whole argument for supplying `paramType`.
|
|
132
155
|
|
|
133
156
|
## Regular expressions
|
|
134
157
|
|
|
135
|
-
`matches` and `notMatches` compile to
|
|
158
|
+
Under PostgreSQL, `matches` and `notMatches` compile to `~` and `!~`, so the pattern is matched by the server. PostgreSQL's advanced regular expressions and ECMAScript's `RegExp` are close but not the same language: shorthand classes and lookahead exist in both, and much everyday pattern syntax is portable, but they are separate implementations with their own escapes, quantifier subtleties and matching rules. A pattern that relies on ECMAScript-specific behaviour may match differently once pushed down. Keep patterns to portable syntax, or evaluate them in process.
|
|
159
|
+
|
|
160
|
+
Under SQLite there is no built-in regular-expression support at all. `REGEXP` is reserved syntax for a `regexp(pattern, value)` function the connection has to register itself — `X REGEXP Y` is exactly `regexp(Y, X)`, pattern first — and the dialect emits `REGEXP` / `NOT REGEXP` for the same two operators. An unregistered one is a query error, `no such function: REGEXP`, rather than a fragment that quietly matches nothing, so this is a documented environment requirement in the same class as the caveats above and not a hole in the compile-time guarantee. The upside is that the pattern is then matched by your own `RegExp`, so the ECMAScript-versus-server-dialect divergence above does not arise.
|
|
161
|
+
|
|
162
|
+
With better-sqlite3:
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
db.function("regexp", (pattern, text) =>
|
|
166
|
+
typeof pattern !== "string" || typeof text !== "string"
|
|
167
|
+
? null
|
|
168
|
+
: new RegExp(pattern).test(text)
|
|
169
|
+
? 1
|
|
170
|
+
: 0,
|
|
171
|
+
);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Both details are load-bearing rather than stylistic. Returning `null` for a NULL argument is what keeps the third value intact: SQLite does not propagate NULL through a user function on its own, so one answering `0` for a NULL value would make `NOT REGEXP` answer `TRUE` for a row whose value is unknown — the two-valued collapse this package exists to avoid. Returning `1`/`0` rather than a JS boolean is what better-sqlite3 accepts; a boolean is rejected from a user function (*"returned an invalid value"*) for the same reason it is rejected as a bound parameter.
|
|
175
|
+
|
|
176
|
+
Some SQLite-wire-compatible targets have no way to register a function at all — Cloudflare D1's Workers Binding API is the motivating case, with no hook for it in its API and [cloudflare/workers-sdk#2802](https://github.com/cloudflare/workers-sdk/issues/2802) still open. Against a target like that, the registration above is simply not possible, and compiling `matches`/`notMatches` to `REGEXP`/`NOT REGEXP` anyway produces SQL that always fails at query execution with `no such function: REGEXP` rather than failing at compile time the way every other unpushable shape does. Set `sqliteRegexpAvailable: false` in `SqlCompileOptions` for a target like this, and the SQLite dialect refuses `matches`/`notMatches` with `UnsupportedNodeError` — and `findUnpushableNodeKind` reports them unpushable — at compile time instead, so the caller falls back to in-process evaluation the same way it would for any other unpushable node. It defaults to `true`, so a caller with a registered function (better-sqlite3, say) sees no change. It has no effect under the `postgres` dialect, which never needs a registered function in the first place.
|
|
136
177
|
|
|
137
178
|
## Tests
|
|
138
179
|
|
|
139
180
|
The unit suite asserts compiled SQL text and parameter arrays per node kind. It cannot, on its own, establish anything about three-valued behaviour: `("age" > $1)` is only indeterminate-preserving because of what PostgreSQL's planner does with a `NULL` age, which is a fact about PostgreSQL rather than about the string.
|
|
140
181
|
|
|
141
|
-
So the integration suite (`pnpm test:integration`) seeds a table whose rows carry real `NULL`s, and for every case executes the compiled fragment as a `WHERE` clause *and* evaluates the same tree through trilean's own `evaluatePredicate` once per row, asserting the two agree on which rows match and which do not. Agreement on absence matters as much as on presence: the case that distinguishes three-valued logic from two-valued is the row that appears in neither a predicate nor its negation.
|
|
182
|
+
So the integration suite (`pnpm test:integration`) runs the same fixture against a real engine, once per implementation: PostgreSQL in an ephemeral container, PGlite in process, and SQLite in memory through better-sqlite3. Each seeds a table whose rows carry real `NULL`s, and for every case executes the compiled fragment as a `WHERE` clause *and* evaluates the same tree through trilean's own `evaluatePredicate` once per row, asserting the two agree on which rows match and which do not. Agreement on absence matters as much as on presence: the case that distinguishes three-valued logic from two-valued is the row that appears in neither a predicate nor its negation.
|
|
142
183
|
|
|
143
|
-
|
|
184
|
+
`postgres.test.ts` uses a real PostgreSQL server started as an ephemeral container, and needs a working Docker daemon. `pglite.test.ts` uses [PGlite](https://pglite.dev), which is PostgreSQL itself compiled to WebAssembly and run in process rather than a reimplementation of it, and needs nothing beyond Node — so the parity claim above is measurable on any machine, Docker or not, and PGlite is a verified target of the `postgres` dialect rather than an assumed one. `sqlite.test.ts` likewise needs nothing beyond Node, and also measures the divergences the guard's refusals exist to prevent — the NaN-to-NULL binding substitution, the lexicographic text comparison, the integer boolean ordering — rather than only asserting that each refusal fires, since inheriting PostgreSQL's refusal set would be worth nothing if SQLite happened to agree with trilean where PostgreSQL does not.
|
|
144
185
|
|
|
145
186
|
## Licence
|
|
146
187
|
|
package/dist/compile.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_errors = require("./errors.cjs");
|
|
3
|
-
const require_guard = require("./guard.cjs");
|
|
4
3
|
const require_options = require("./options.cjs");
|
|
4
|
+
const require_guard = require("./guard.cjs");
|
|
5
5
|
//#region src/compile.ts
|
|
6
6
|
const COMPARISON_SQL = {
|
|
7
7
|
gt: ">",
|
|
@@ -11,19 +11,23 @@ const COMPARISON_SQL = {
|
|
|
11
11
|
eq: "=",
|
|
12
12
|
neq: "<>"
|
|
13
13
|
};
|
|
14
|
-
/** `~` and `!~` are PostgreSQL's own regular-expression match operators, so a pattern is matched by the database rather than shipped back to be matched in process. See the "Regular expressions" caveat in README.md: PostgreSQL's advanced regular expressions and ECMAScript's are close but not the same language. */
|
|
15
|
-
const TEXT_COMPARISON_SQL = {
|
|
16
|
-
equals: "=",
|
|
17
|
-
notEquals: "<>",
|
|
18
|
-
matches: "~",
|
|
19
|
-
notMatches: "!~"
|
|
20
|
-
};
|
|
21
14
|
/**
|
|
22
|
-
* The
|
|
15
|
+
* The operator each `textCompare` op emits, for one dialect.
|
|
23
16
|
*
|
|
24
|
-
*
|
|
17
|
+
* `=` and `<>` are ANSI-standard equality and identical everywhere; only the two pattern operators are the dialect's own. Built as a complete record rather than resolved per node so that a `TextComparisonOperator` added to trilean later is a compile error here, instead of an undefined operator spliced into the emitted SQL.
|
|
18
|
+
*/
|
|
19
|
+
function textComparisonSqlFor(dialect) {
|
|
20
|
+
return {
|
|
21
|
+
equals: "=",
|
|
22
|
+
notEquals: "<>",
|
|
23
|
+
matches: dialect.matches,
|
|
24
|
+
notMatches: dialect.notMatches
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The value kind a literal placeholder is rendered for: the one implied by the literal's own trilean kind. What a dialect does with it -- PostgreSQL casts the placeholder to the corresponding type, SQLite ignores it -- is `DialectConfig.placeholder`'s business.
|
|
25
29
|
*
|
|
26
|
-
* A mapped column's declared `paramType` deliberately does not override this. It cannot differ: the guard has already refused any comparison whose operand kinds disagree, and each declared kind
|
|
30
|
+
* A mapped column's declared `paramType` deliberately does not override this. It cannot differ: the guard has already refused any comparison whose operand kinds disagree, and each declared kind implies the same SQL type as the literal kind it must then match. Consulting it here would be a branch that can never change the output.
|
|
27
31
|
*/
|
|
28
32
|
const PARAM_TYPE_OF_LITERAL = {
|
|
29
33
|
textLiteral: "text",
|
|
@@ -33,10 +37,10 @@ const PARAM_TYPE_OF_LITERAL = {
|
|
|
33
37
|
};
|
|
34
38
|
function placeholder(context, value, castTo) {
|
|
35
39
|
context.params.push(value);
|
|
36
|
-
return
|
|
40
|
+
return context.dialect.placeholder(context.params.length, castTo);
|
|
37
41
|
}
|
|
38
42
|
/**
|
|
39
|
-
* Renders a column as a
|
|
43
|
+
* Renders a column as a SQL identifier: each dot-separated segment double-quoted, with any embedded double quote doubled. Double-quoting is ANSI-standard and means the same thing in both dialects, so this needs no per-dialect branch.
|
|
40
44
|
*
|
|
41
45
|
* A column name cannot be a bind parameter -- it is part of the statement's structure, not its data -- so it is the one caller-supplied string that reaches the SQL text. Quoting it unconditionally is what keeps that safe: the doubling makes even a name containing `"; DROP TABLE ...` a single, inert identifier that simply does not exist. Quoting also means a name is taken literally rather than case-folded, so `columnFor` must return the column's real, case-exact name.
|
|
42
46
|
*/
|
|
@@ -97,11 +101,14 @@ function compilePredicate(node, context) {
|
|
|
97
101
|
case "textCompare": {
|
|
98
102
|
const left = compileExpression(node.left, context);
|
|
99
103
|
const right = compileExpression(node.right, context);
|
|
100
|
-
return `(${left} ${
|
|
104
|
+
return `(${left} ${context.textComparison[node.op]} ${right})`;
|
|
101
105
|
}
|
|
102
106
|
case "memberOf": {
|
|
103
107
|
const operand = compileExpression(node.operand, context);
|
|
104
|
-
if (node.candidates.length === 0)
|
|
108
|
+
if (node.candidates.length === 0) {
|
|
109
|
+
const nullLiteral = `NULL${context.dialect.emptyMemberOfNullSuffix}`;
|
|
110
|
+
return node.op === "in" ? `(${operand} IS NULL AND ${nullLiteral})` : `(${operand} IS NOT NULL OR ${nullLiteral})`;
|
|
111
|
+
}
|
|
105
112
|
const candidates = node.candidates.map((candidate) => compileExpression(candidate, context)).join(", ");
|
|
106
113
|
return `(${operand} ${node.op === "in" ? "IN" : "NOT IN"} (${candidates}))`;
|
|
107
114
|
}
|
|
@@ -110,16 +117,18 @@ function compilePredicate(node, context) {
|
|
|
110
117
|
return refuse(node.kind, "predicate");
|
|
111
118
|
}
|
|
112
119
|
/**
|
|
113
|
-
* Compiles a trilean predicate tree into a parameterised
|
|
120
|
+
* Compiles a trilean predicate tree into a parameterised boolean expression in the dialect `options` names.
|
|
114
121
|
*
|
|
115
122
|
* Three-valued logic is not reimplemented on top of SQL; it is delegated to it. SQL's `AND`, `OR` and `NOT` over `TRUE`/`FALSE`/`NULL` are Kleene's strong three-valued tables, which are the same tables trilean's own `combineAnd`, `combineOr` and `not` implement, and a comparison against a NULL column yields `NULL` exactly where the evaluator would have returned `indeterminate` from an unresolved reference. A row excluded by `WHERE` because its condition was unknown is therefore excluded for the same reason, and by the same rule, as a subject the evaluator declines to judge. No indeterminacy column, sentinel value or `CASE` scaffolding is emitted, because none is needed.
|
|
116
123
|
*
|
|
117
124
|
* Every caller-supplied literal becomes a bind parameter. Nothing but structure, operators, and quoted column identifiers is ever written into the returned `sql`.
|
|
118
125
|
*
|
|
126
|
+
* @throws {UnknownDialectError} if `options.dialect` names a dialect this version does not implement.
|
|
119
127
|
* @throws {UnsupportedNodeError} if any node in the tree is one this compiler will not translate -- see `findUnpushableNodeKind`, which this runs first and which a caller can run itself to choose between pushdown and in-process evaluation without provoking an exception.
|
|
120
128
|
* @throws {InvalidColumnError} if `columnFor` returns a column that cannot be rendered as an identifier.
|
|
121
129
|
*/
|
|
122
130
|
function compilePredicateNode(node, options) {
|
|
131
|
+
require_options.assertImplementedDialect(options.dialect);
|
|
123
132
|
const bindings = /* @__PURE__ */ new Map();
|
|
124
133
|
const memoised = {
|
|
125
134
|
dialect: options.dialect,
|
|
@@ -129,12 +138,16 @@ function compilePredicateNode(node, options) {
|
|
|
129
138
|
const binding = options.columnFor(referenceKey);
|
|
130
139
|
bindings.set(referenceKey, binding);
|
|
131
140
|
return binding;
|
|
132
|
-
}
|
|
141
|
+
},
|
|
142
|
+
sqliteRegexpAvailable: options.sqliteRegexpAvailable
|
|
133
143
|
};
|
|
134
144
|
const unpushable = require_guard.findUnpushableNodeKind(node, memoised);
|
|
135
145
|
if (unpushable !== void 0) throw new require_errors.UnsupportedNodeError(unpushable);
|
|
146
|
+
const dialect = require_options.DIALECT_CONFIG[options.dialect];
|
|
136
147
|
const context = {
|
|
137
148
|
options: memoised,
|
|
149
|
+
dialect,
|
|
150
|
+
textComparison: textComparisonSqlFor(dialect),
|
|
138
151
|
params: []
|
|
139
152
|
};
|
|
140
153
|
return {
|
package/dist/compile.d.cts
CHANGED
|
@@ -2,12 +2,13 @@ import { CompiledSql, SqlCompileOptions } from "./options.cjs";
|
|
|
2
2
|
import { PredicateNode } from "trilean";
|
|
3
3
|
//#region src/compile.d.ts
|
|
4
4
|
/**
|
|
5
|
-
* Compiles a trilean predicate tree into a parameterised
|
|
5
|
+
* Compiles a trilean predicate tree into a parameterised boolean expression in the dialect `options` names.
|
|
6
6
|
*
|
|
7
7
|
* Three-valued logic is not reimplemented on top of SQL; it is delegated to it. SQL's `AND`, `OR` and `NOT` over `TRUE`/`FALSE`/`NULL` are Kleene's strong three-valued tables, which are the same tables trilean's own `combineAnd`, `combineOr` and `not` implement, and a comparison against a NULL column yields `NULL` exactly where the evaluator would have returned `indeterminate` from an unresolved reference. A row excluded by `WHERE` because its condition was unknown is therefore excluded for the same reason, and by the same rule, as a subject the evaluator declines to judge. No indeterminacy column, sentinel value or `CASE` scaffolding is emitted, because none is needed.
|
|
8
8
|
*
|
|
9
9
|
* Every caller-supplied literal becomes a bind parameter. Nothing but structure, operators, and quoted column identifiers is ever written into the returned `sql`.
|
|
10
10
|
*
|
|
11
|
+
* @throws {UnknownDialectError} if `options.dialect` names a dialect this version does not implement.
|
|
11
12
|
* @throws {UnsupportedNodeError} if any node in the tree is one this compiler will not translate -- see `findUnpushableNodeKind`, which this runs first and which a caller can run itself to choose between pushdown and in-process evaluation without provoking an exception.
|
|
12
13
|
* @throws {InvalidColumnError} if `columnFor` returns a column that cannot be rendered as an identifier.
|
|
13
14
|
*/
|
package/dist/compile.d.ts
CHANGED
|
@@ -2,12 +2,13 @@ import { CompiledSql, SqlCompileOptions } from "./options.js";
|
|
|
2
2
|
import { PredicateNode } from "trilean";
|
|
3
3
|
//#region src/compile.d.ts
|
|
4
4
|
/**
|
|
5
|
-
* Compiles a trilean predicate tree into a parameterised
|
|
5
|
+
* Compiles a trilean predicate tree into a parameterised boolean expression in the dialect `options` names.
|
|
6
6
|
*
|
|
7
7
|
* Three-valued logic is not reimplemented on top of SQL; it is delegated to it. SQL's `AND`, `OR` and `NOT` over `TRUE`/`FALSE`/`NULL` are Kleene's strong three-valued tables, which are the same tables trilean's own `combineAnd`, `combineOr` and `not` implement, and a comparison against a NULL column yields `NULL` exactly where the evaluator would have returned `indeterminate` from an unresolved reference. A row excluded by `WHERE` because its condition was unknown is therefore excluded for the same reason, and by the same rule, as a subject the evaluator declines to judge. No indeterminacy column, sentinel value or `CASE` scaffolding is emitted, because none is needed.
|
|
8
8
|
*
|
|
9
9
|
* Every caller-supplied literal becomes a bind parameter. Nothing but structure, operators, and quoted column identifiers is ever written into the returned `sql`.
|
|
10
10
|
*
|
|
11
|
+
* @throws {UnknownDialectError} if `options.dialect` names a dialect this version does not implement.
|
|
11
12
|
* @throws {UnsupportedNodeError} if any node in the tree is one this compiler will not translate -- see `findUnpushableNodeKind`, which this runs first and which a caller can run itself to choose between pushdown and in-process evaluation without provoking an exception.
|
|
12
13
|
* @throws {InvalidColumnError} if `columnFor` returns a column that cannot be rendered as an identifier.
|
|
13
14
|
*/
|
package/dist/compile.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { InvalidColumnError, UnsupportedNodeError } from "./errors.js";
|
|
2
|
+
import { DIALECT_CONFIG, assertImplementedDialect } from "./options.js";
|
|
2
3
|
import { findUnpushableNodeKind } from "./guard.js";
|
|
3
|
-
import { POSTGRES_TYPE_NAME } from "./options.js";
|
|
4
4
|
//#region src/compile.ts
|
|
5
5
|
const COMPARISON_SQL = {
|
|
6
6
|
gt: ">",
|
|
@@ -10,19 +10,23 @@ const COMPARISON_SQL = {
|
|
|
10
10
|
eq: "=",
|
|
11
11
|
neq: "<>"
|
|
12
12
|
};
|
|
13
|
-
/** `~` and `!~` are PostgreSQL's own regular-expression match operators, so a pattern is matched by the database rather than shipped back to be matched in process. See the "Regular expressions" caveat in README.md: PostgreSQL's advanced regular expressions and ECMAScript's are close but not the same language. */
|
|
14
|
-
const TEXT_COMPARISON_SQL = {
|
|
15
|
-
equals: "=",
|
|
16
|
-
notEquals: "<>",
|
|
17
|
-
matches: "~",
|
|
18
|
-
notMatches: "!~"
|
|
19
|
-
};
|
|
20
13
|
/**
|
|
21
|
-
* The
|
|
14
|
+
* The operator each `textCompare` op emits, for one dialect.
|
|
22
15
|
*
|
|
23
|
-
*
|
|
16
|
+
* `=` and `<>` are ANSI-standard equality and identical everywhere; only the two pattern operators are the dialect's own. Built as a complete record rather than resolved per node so that a `TextComparisonOperator` added to trilean later is a compile error here, instead of an undefined operator spliced into the emitted SQL.
|
|
17
|
+
*/
|
|
18
|
+
function textComparisonSqlFor(dialect) {
|
|
19
|
+
return {
|
|
20
|
+
equals: "=",
|
|
21
|
+
notEquals: "<>",
|
|
22
|
+
matches: dialect.matches,
|
|
23
|
+
notMatches: dialect.notMatches
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The value kind a literal placeholder is rendered for: the one implied by the literal's own trilean kind. What a dialect does with it -- PostgreSQL casts the placeholder to the corresponding type, SQLite ignores it -- is `DialectConfig.placeholder`'s business.
|
|
24
28
|
*
|
|
25
|
-
* A mapped column's declared `paramType` deliberately does not override this. It cannot differ: the guard has already refused any comparison whose operand kinds disagree, and each declared kind
|
|
29
|
+
* A mapped column's declared `paramType` deliberately does not override this. It cannot differ: the guard has already refused any comparison whose operand kinds disagree, and each declared kind implies the same SQL type as the literal kind it must then match. Consulting it here would be a branch that can never change the output.
|
|
26
30
|
*/
|
|
27
31
|
const PARAM_TYPE_OF_LITERAL = {
|
|
28
32
|
textLiteral: "text",
|
|
@@ -32,10 +36,10 @@ const PARAM_TYPE_OF_LITERAL = {
|
|
|
32
36
|
};
|
|
33
37
|
function placeholder(context, value, castTo) {
|
|
34
38
|
context.params.push(value);
|
|
35
|
-
return
|
|
39
|
+
return context.dialect.placeholder(context.params.length, castTo);
|
|
36
40
|
}
|
|
37
41
|
/**
|
|
38
|
-
* Renders a column as a
|
|
42
|
+
* Renders a column as a SQL identifier: each dot-separated segment double-quoted, with any embedded double quote doubled. Double-quoting is ANSI-standard and means the same thing in both dialects, so this needs no per-dialect branch.
|
|
39
43
|
*
|
|
40
44
|
* A column name cannot be a bind parameter -- it is part of the statement's structure, not its data -- so it is the one caller-supplied string that reaches the SQL text. Quoting it unconditionally is what keeps that safe: the doubling makes even a name containing `"; DROP TABLE ...` a single, inert identifier that simply does not exist. Quoting also means a name is taken literally rather than case-folded, so `columnFor` must return the column's real, case-exact name.
|
|
41
45
|
*/
|
|
@@ -96,11 +100,14 @@ function compilePredicate(node, context) {
|
|
|
96
100
|
case "textCompare": {
|
|
97
101
|
const left = compileExpression(node.left, context);
|
|
98
102
|
const right = compileExpression(node.right, context);
|
|
99
|
-
return `(${left} ${
|
|
103
|
+
return `(${left} ${context.textComparison[node.op]} ${right})`;
|
|
100
104
|
}
|
|
101
105
|
case "memberOf": {
|
|
102
106
|
const operand = compileExpression(node.operand, context);
|
|
103
|
-
if (node.candidates.length === 0)
|
|
107
|
+
if (node.candidates.length === 0) {
|
|
108
|
+
const nullLiteral = `NULL${context.dialect.emptyMemberOfNullSuffix}`;
|
|
109
|
+
return node.op === "in" ? `(${operand} IS NULL AND ${nullLiteral})` : `(${operand} IS NOT NULL OR ${nullLiteral})`;
|
|
110
|
+
}
|
|
104
111
|
const candidates = node.candidates.map((candidate) => compileExpression(candidate, context)).join(", ");
|
|
105
112
|
return `(${operand} ${node.op === "in" ? "IN" : "NOT IN"} (${candidates}))`;
|
|
106
113
|
}
|
|
@@ -109,16 +116,18 @@ function compilePredicate(node, context) {
|
|
|
109
116
|
return refuse(node.kind, "predicate");
|
|
110
117
|
}
|
|
111
118
|
/**
|
|
112
|
-
* Compiles a trilean predicate tree into a parameterised
|
|
119
|
+
* Compiles a trilean predicate tree into a parameterised boolean expression in the dialect `options` names.
|
|
113
120
|
*
|
|
114
121
|
* Three-valued logic is not reimplemented on top of SQL; it is delegated to it. SQL's `AND`, `OR` and `NOT` over `TRUE`/`FALSE`/`NULL` are Kleene's strong three-valued tables, which are the same tables trilean's own `combineAnd`, `combineOr` and `not` implement, and a comparison against a NULL column yields `NULL` exactly where the evaluator would have returned `indeterminate` from an unresolved reference. A row excluded by `WHERE` because its condition was unknown is therefore excluded for the same reason, and by the same rule, as a subject the evaluator declines to judge. No indeterminacy column, sentinel value or `CASE` scaffolding is emitted, because none is needed.
|
|
115
122
|
*
|
|
116
123
|
* Every caller-supplied literal becomes a bind parameter. Nothing but structure, operators, and quoted column identifiers is ever written into the returned `sql`.
|
|
117
124
|
*
|
|
125
|
+
* @throws {UnknownDialectError} if `options.dialect` names a dialect this version does not implement.
|
|
118
126
|
* @throws {UnsupportedNodeError} if any node in the tree is one this compiler will not translate -- see `findUnpushableNodeKind`, which this runs first and which a caller can run itself to choose between pushdown and in-process evaluation without provoking an exception.
|
|
119
127
|
* @throws {InvalidColumnError} if `columnFor` returns a column that cannot be rendered as an identifier.
|
|
120
128
|
*/
|
|
121
129
|
function compilePredicateNode(node, options) {
|
|
130
|
+
assertImplementedDialect(options.dialect);
|
|
122
131
|
const bindings = /* @__PURE__ */ new Map();
|
|
123
132
|
const memoised = {
|
|
124
133
|
dialect: options.dialect,
|
|
@@ -128,12 +137,16 @@ function compilePredicateNode(node, options) {
|
|
|
128
137
|
const binding = options.columnFor(referenceKey);
|
|
129
138
|
bindings.set(referenceKey, binding);
|
|
130
139
|
return binding;
|
|
131
|
-
}
|
|
140
|
+
},
|
|
141
|
+
sqliteRegexpAvailable: options.sqliteRegexpAvailable
|
|
132
142
|
};
|
|
133
143
|
const unpushable = findUnpushableNodeKind(node, memoised);
|
|
134
144
|
if (unpushable !== void 0) throw new UnsupportedNodeError(unpushable);
|
|
145
|
+
const dialect = DIALECT_CONFIG[options.dialect];
|
|
135
146
|
const context = {
|
|
136
147
|
options: memoised,
|
|
148
|
+
dialect,
|
|
149
|
+
textComparison: textComparisonSqlFor(dialect),
|
|
137
150
|
params: []
|
|
138
151
|
};
|
|
139
152
|
return {
|
package/dist/errors.cjs
CHANGED
|
@@ -10,7 +10,7 @@ var TrileanSqlError = class extends Error {
|
|
|
10
10
|
/**
|
|
11
11
|
* A node the compiler refuses to translate. The compiler never degrades: there is no "best effort" fragment, no silently dropped conjunct, and no approximation that answers differently from the in-process evaluator. Either the whole tree compiles to SQL that agrees with `evaluatePredicate` on every row, or this is thrown and the caller evaluates in process instead.
|
|
12
12
|
*
|
|
13
|
-
* That guarantee is about the tree's *structure*, and it stops at two things whose meaning is a property of the operand's own content rather than of any node kind: a `matches`/`notMatches` pattern, which the server matches under
|
|
13
|
+
* That guarantee is about the tree's *structure*, and it stops at two things whose meaning is a property of the operand's own content rather than of any node kind: a `matches`/`notMatches` pattern, which the server matches under its own regular-expression language rather than ECMAScript's, and an `instantLiteral` string the database reads differently from trilean or cannot read at all (which fails as a query error at execution time, not as this exception). Both are covered in README.md; neither is detectable by a walk over node kinds.
|
|
14
14
|
*
|
|
15
15
|
* `nodeKind` is the offending node's own `kind`, `path` locates it inside the tree, and `reason` says why it is not pushable -- which is not always about the kind alone (a `reference` carrying a `unit`, or a `compare` whose operands are statically of different kinds, are both refused despite `reference` and `compare` being supported kinds).
|
|
16
16
|
*/
|
|
@@ -27,7 +27,24 @@ var UnsupportedNodeError = class extends TrileanSqlError {
|
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
29
|
/**
|
|
30
|
-
* A
|
|
30
|
+
* A dialect this version does not implement.
|
|
31
|
+
*
|
|
32
|
+
* `SqlDialect` is a closed union, so a caller who names a dialect in TypeScript source cannot reach this. A dialect read from configuration and asserted into the type at the boundary can, which is the case it exists for: without it the unimplemented name surfaces later as an internal `TypeError` from a table lookup that found nothing, naming neither the dialect nor the field it was reached from. Refusing by name at the entry point is the same convention `InvalidColumnError` applies to a `columnFor` result and the node walk applies to an unrecognised kind -- an input the type system describes but cannot enforce is checked once, where it arrives.
|
|
33
|
+
*
|
|
34
|
+
* `dialect` is the offending name and `implemented` lists the ones this version does compile.
|
|
35
|
+
*/
|
|
36
|
+
var UnknownDialectError = class extends TrileanSqlError {
|
|
37
|
+
dialect;
|
|
38
|
+
implemented;
|
|
39
|
+
constructor(dialect, implemented) {
|
|
40
|
+
super(`unknown dialect ${JSON.stringify(dialect)}: this version compiles ${implemented.map((name) => JSON.stringify(name)).join(", ")}`);
|
|
41
|
+
this.name = "UnknownDialectError";
|
|
42
|
+
this.dialect = dialect;
|
|
43
|
+
this.implemented = implemented;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* A `columnFor` result whose `column` cannot be rendered as a SQL identifier.
|
|
31
48
|
*
|
|
32
49
|
* A column name is an identifier, not a value, so it is the one part of the emitted SQL that cannot be parameterised -- it has to be written into the statement text. It is always emitted double-quoted with any embedded quote doubled, which makes an arbitrary string safe, so this is not the injection defence; it rejects the two shapes that quoting cannot rescue into a valid identifier, an empty name and an empty dot-separated segment.
|
|
33
50
|
*/
|
|
@@ -44,4 +61,5 @@ var InvalidColumnError = class extends TrileanSqlError {
|
|
|
44
61
|
//#endregion
|
|
45
62
|
exports.InvalidColumnError = InvalidColumnError;
|
|
46
63
|
exports.TrileanSqlError = TrileanSqlError;
|
|
64
|
+
exports.UnknownDialectError = UnknownDialectError;
|
|
47
65
|
exports.UnsupportedNodeError = UnsupportedNodeError;
|
package/dist/errors.d.cts
CHANGED
|
@@ -6,7 +6,7 @@ declare class TrileanSqlError extends Error {
|
|
|
6
6
|
/**
|
|
7
7
|
* A node the compiler refuses to translate. The compiler never degrades: there is no "best effort" fragment, no silently dropped conjunct, and no approximation that answers differently from the in-process evaluator. Either the whole tree compiles to SQL that agrees with `evaluatePredicate` on every row, or this is thrown and the caller evaluates in process instead.
|
|
8
8
|
*
|
|
9
|
-
* That guarantee is about the tree's *structure*, and it stops at two things whose meaning is a property of the operand's own content rather than of any node kind: a `matches`/`notMatches` pattern, which the server matches under
|
|
9
|
+
* That guarantee is about the tree's *structure*, and it stops at two things whose meaning is a property of the operand's own content rather than of any node kind: a `matches`/`notMatches` pattern, which the server matches under its own regular-expression language rather than ECMAScript's, and an `instantLiteral` string the database reads differently from trilean or cannot read at all (which fails as a query error at execution time, not as this exception). Both are covered in README.md; neither is detectable by a walk over node kinds.
|
|
10
10
|
*
|
|
11
11
|
* `nodeKind` is the offending node's own `kind`, `path` locates it inside the tree, and `reason` says why it is not pushable -- which is not always about the kind alone (a `reference` carrying a `unit`, or a `compare` whose operands are statically of different kinds, are both refused despite `reference` and `compare` being supported kinds).
|
|
12
12
|
*/
|
|
@@ -21,7 +21,19 @@ declare class UnsupportedNodeError extends TrileanSqlError {
|
|
|
21
21
|
}>);
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
|
-
* A
|
|
24
|
+
* A dialect this version does not implement.
|
|
25
|
+
*
|
|
26
|
+
* `SqlDialect` is a closed union, so a caller who names a dialect in TypeScript source cannot reach this. A dialect read from configuration and asserted into the type at the boundary can, which is the case it exists for: without it the unimplemented name surfaces later as an internal `TypeError` from a table lookup that found nothing, naming neither the dialect nor the field it was reached from. Refusing by name at the entry point is the same convention `InvalidColumnError` applies to a `columnFor` result and the node walk applies to an unrecognised kind -- an input the type system describes but cannot enforce is checked once, where it arrives.
|
|
27
|
+
*
|
|
28
|
+
* `dialect` is the offending name and `implemented` lists the ones this version does compile.
|
|
29
|
+
*/
|
|
30
|
+
declare class UnknownDialectError extends TrileanSqlError {
|
|
31
|
+
readonly dialect: string;
|
|
32
|
+
readonly implemented: readonly string[];
|
|
33
|
+
constructor(dialect: string, implemented: readonly string[]);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A `columnFor` result whose `column` cannot be rendered as a SQL identifier.
|
|
25
37
|
*
|
|
26
38
|
* A column name is an identifier, not a value, so it is the one part of the emitted SQL that cannot be parameterised -- it has to be written into the statement text. It is always emitted double-quoted with any embedded quote doubled, which makes an arbitrary string safe, so this is not the injection defence; it rejects the two shapes that quoting cannot rescue into a valid identifier, an empty name and an empty dot-separated segment.
|
|
27
39
|
*/
|
|
@@ -31,4 +43,4 @@ declare class InvalidColumnError extends TrileanSqlError {
|
|
|
31
43
|
constructor(referenceKey: string, column: string, reason: string);
|
|
32
44
|
}
|
|
33
45
|
//#endregion
|
|
34
|
-
export { InvalidColumnError, TrileanSqlError, UnsupportedNodeError };
|
|
46
|
+
export { InvalidColumnError, TrileanSqlError, UnknownDialectError, UnsupportedNodeError };
|
package/dist/errors.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ declare class TrileanSqlError extends Error {
|
|
|
6
6
|
/**
|
|
7
7
|
* A node the compiler refuses to translate. The compiler never degrades: there is no "best effort" fragment, no silently dropped conjunct, and no approximation that answers differently from the in-process evaluator. Either the whole tree compiles to SQL that agrees with `evaluatePredicate` on every row, or this is thrown and the caller evaluates in process instead.
|
|
8
8
|
*
|
|
9
|
-
* That guarantee is about the tree's *structure*, and it stops at two things whose meaning is a property of the operand's own content rather than of any node kind: a `matches`/`notMatches` pattern, which the server matches under
|
|
9
|
+
* That guarantee is about the tree's *structure*, and it stops at two things whose meaning is a property of the operand's own content rather than of any node kind: a `matches`/`notMatches` pattern, which the server matches under its own regular-expression language rather than ECMAScript's, and an `instantLiteral` string the database reads differently from trilean or cannot read at all (which fails as a query error at execution time, not as this exception). Both are covered in README.md; neither is detectable by a walk over node kinds.
|
|
10
10
|
*
|
|
11
11
|
* `nodeKind` is the offending node's own `kind`, `path` locates it inside the tree, and `reason` says why it is not pushable -- which is not always about the kind alone (a `reference` carrying a `unit`, or a `compare` whose operands are statically of different kinds, are both refused despite `reference` and `compare` being supported kinds).
|
|
12
12
|
*/
|
|
@@ -21,7 +21,19 @@ declare class UnsupportedNodeError extends TrileanSqlError {
|
|
|
21
21
|
}>);
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
|
-
* A
|
|
24
|
+
* A dialect this version does not implement.
|
|
25
|
+
*
|
|
26
|
+
* `SqlDialect` is a closed union, so a caller who names a dialect in TypeScript source cannot reach this. A dialect read from configuration and asserted into the type at the boundary can, which is the case it exists for: without it the unimplemented name surfaces later as an internal `TypeError` from a table lookup that found nothing, naming neither the dialect nor the field it was reached from. Refusing by name at the entry point is the same convention `InvalidColumnError` applies to a `columnFor` result and the node walk applies to an unrecognised kind -- an input the type system describes but cannot enforce is checked once, where it arrives.
|
|
27
|
+
*
|
|
28
|
+
* `dialect` is the offending name and `implemented` lists the ones this version does compile.
|
|
29
|
+
*/
|
|
30
|
+
declare class UnknownDialectError extends TrileanSqlError {
|
|
31
|
+
readonly dialect: string;
|
|
32
|
+
readonly implemented: readonly string[];
|
|
33
|
+
constructor(dialect: string, implemented: readonly string[]);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A `columnFor` result whose `column` cannot be rendered as a SQL identifier.
|
|
25
37
|
*
|
|
26
38
|
* A column name is an identifier, not a value, so it is the one part of the emitted SQL that cannot be parameterised -- it has to be written into the statement text. It is always emitted double-quoted with any embedded quote doubled, which makes an arbitrary string safe, so this is not the injection defence; it rejects the two shapes that quoting cannot rescue into a valid identifier, an empty name and an empty dot-separated segment.
|
|
27
39
|
*/
|
|
@@ -31,4 +43,4 @@ declare class InvalidColumnError extends TrileanSqlError {
|
|
|
31
43
|
constructor(referenceKey: string, column: string, reason: string);
|
|
32
44
|
}
|
|
33
45
|
//#endregion
|
|
34
|
-
export { InvalidColumnError, TrileanSqlError, UnsupportedNodeError };
|
|
46
|
+
export { InvalidColumnError, TrileanSqlError, UnknownDialectError, UnsupportedNodeError };
|
package/dist/errors.js
CHANGED
|
@@ -9,7 +9,7 @@ var TrileanSqlError = class extends Error {
|
|
|
9
9
|
/**
|
|
10
10
|
* A node the compiler refuses to translate. The compiler never degrades: there is no "best effort" fragment, no silently dropped conjunct, and no approximation that answers differently from the in-process evaluator. Either the whole tree compiles to SQL that agrees with `evaluatePredicate` on every row, or this is thrown and the caller evaluates in process instead.
|
|
11
11
|
*
|
|
12
|
-
* That guarantee is about the tree's *structure*, and it stops at two things whose meaning is a property of the operand's own content rather than of any node kind: a `matches`/`notMatches` pattern, which the server matches under
|
|
12
|
+
* That guarantee is about the tree's *structure*, and it stops at two things whose meaning is a property of the operand's own content rather than of any node kind: a `matches`/`notMatches` pattern, which the server matches under its own regular-expression language rather than ECMAScript's, and an `instantLiteral` string the database reads differently from trilean or cannot read at all (which fails as a query error at execution time, not as this exception). Both are covered in README.md; neither is detectable by a walk over node kinds.
|
|
13
13
|
*
|
|
14
14
|
* `nodeKind` is the offending node's own `kind`, `path` locates it inside the tree, and `reason` says why it is not pushable -- which is not always about the kind alone (a `reference` carrying a `unit`, or a `compare` whose operands are statically of different kinds, are both refused despite `reference` and `compare` being supported kinds).
|
|
15
15
|
*/
|
|
@@ -26,7 +26,24 @@ var UnsupportedNodeError = class extends TrileanSqlError {
|
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
28
|
/**
|
|
29
|
-
* A
|
|
29
|
+
* A dialect this version does not implement.
|
|
30
|
+
*
|
|
31
|
+
* `SqlDialect` is a closed union, so a caller who names a dialect in TypeScript source cannot reach this. A dialect read from configuration and asserted into the type at the boundary can, which is the case it exists for: without it the unimplemented name surfaces later as an internal `TypeError` from a table lookup that found nothing, naming neither the dialect nor the field it was reached from. Refusing by name at the entry point is the same convention `InvalidColumnError` applies to a `columnFor` result and the node walk applies to an unrecognised kind -- an input the type system describes but cannot enforce is checked once, where it arrives.
|
|
32
|
+
*
|
|
33
|
+
* `dialect` is the offending name and `implemented` lists the ones this version does compile.
|
|
34
|
+
*/
|
|
35
|
+
var UnknownDialectError = class extends TrileanSqlError {
|
|
36
|
+
dialect;
|
|
37
|
+
implemented;
|
|
38
|
+
constructor(dialect, implemented) {
|
|
39
|
+
super(`unknown dialect ${JSON.stringify(dialect)}: this version compiles ${implemented.map((name) => JSON.stringify(name)).join(", ")}`);
|
|
40
|
+
this.name = "UnknownDialectError";
|
|
41
|
+
this.dialect = dialect;
|
|
42
|
+
this.implemented = implemented;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* A `columnFor` result whose `column` cannot be rendered as a SQL identifier.
|
|
30
47
|
*
|
|
31
48
|
* A column name is an identifier, not a value, so it is the one part of the emitted SQL that cannot be parameterised -- it has to be written into the statement text. It is always emitted double-quoted with any embedded quote doubled, which makes an arbitrary string safe, so this is not the injection defence; it rejects the two shapes that quoting cannot rescue into a valid identifier, an empty name and an empty dot-separated segment.
|
|
32
49
|
*/
|
|
@@ -41,4 +58,4 @@ var InvalidColumnError = class extends TrileanSqlError {
|
|
|
41
58
|
}
|
|
42
59
|
};
|
|
43
60
|
//#endregion
|
|
44
|
-
export { InvalidColumnError, TrileanSqlError, UnsupportedNodeError };
|
|
61
|
+
export { InvalidColumnError, TrileanSqlError, UnknownDialectError, UnsupportedNodeError };
|