trilean-sql 2.2.40 → 2.2.42
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 +10 -4
- package/dist/compile.cjs +38 -10
- package/dist/compile.js +38 -10
- package/dist/guard.cjs +1 -1
- package/dist/guard.js +1 -1
- package/dist/options-DOE1_Cwr.cjs +126 -0
- package/dist/options-c1nHisgX.js +109 -0
- package/dist/options.cjs +4 -40
- package/dist/options.d.cts +8 -0
- package/dist/options.d.ts +8 -0
- package/dist/options.js +1 -38
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -112,7 +112,7 @@ Passing `options` widens the check: without them the walk is purely structural;
|
|
|
112
112
|
| `some`, `every` | a correlated scalar subquery over `collectionFor`'s table, combining each participating row's own vote with `MAX`/`CASE` to match the evaluator's OR/AND-fold absorption exactly (see [Collections](#collections)); refused unless `collectionFor` maps the `collection` key | same |
|
|
113
113
|
| `fold` (`max`/`min`) | the same correlated subquery, aggregating each participating item's own projected value with `MAX`/`MIN`, `NULL` the moment any participating item (or its `filter`) is indeterminate (see [Collections](#collections)); refused unless `collectionFor` maps the `collection` key. `fold` (`reduce`) is always refused — see [Refusal](#refusal) | same |
|
|
114
114
|
| `reference` | the mapped column, as a quoted identifier | same |
|
|
115
|
-
| `textLiteral`, `numberLiteral`, `booleanLiteral`, `instantLiteral` | a bind parameter, cast to `text`, `double precision`, `boolean`, `timestamptz` | a bare `?`, uncast |
|
|
115
|
+
| `textLiteral`, `numberLiteral`, `booleanLiteral`, `instantLiteral` | a bind parameter, cast to `text`, `double precision`, `boolean`, `timestamptz` | a bare `?`, uncast; an `instantLiteral` is a number, the milliseconds since the epoch, see [Dialects](#dialects) |
|
|
116
116
|
|
|
117
117
|
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.
|
|
118
118
|
|
|
@@ -142,7 +142,7 @@ A `fold(max|min)` whose projected item is statically known to be text or boolean
|
|
|
142
142
|
|
|
143
143
|
## Dialects
|
|
144
144
|
|
|
145
|
-
The dialects differ in
|
|
145
|
+
The dialects differ in four 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; the bare `NULL` in an empty `memberOf` carries a `::boolean` annotation only where there is a boolean type to annotate; and an instant is compared by its point in time, which PostgreSQL's `timestamptz` is already and SQLite reads from its text (see [Dialects](#dialects)). Everything else the compiler emits — the connectives, the six comparison operators, `=`/`<>`, `IN`/`NOT IN`, `IS NOT NULL`, quoted identifiers — is ANSI-standard and identical.
|
|
146
146
|
|
|
147
147
|
```ts
|
|
148
148
|
const { sql, params } = compilePredicateNode(rule, { dialect: "sqlite", columnFor });
|
|
@@ -159,7 +159,13 @@ Two things a SQLite caller has to supply that a PostgreSQL caller does not, both
|
|
|
159
159
|
|
|
160
160
|
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.
|
|
161
161
|
|
|
162
|
-
Instants
|
|
162
|
+
**Instants.** SQLite has no timestamp type, and its date functions read far more than an instant: the text `now` is the present time, a time of day is a day in the year 2000, and a number is a Julian day. Text compares by its characters, not by the instant, so a comparison over instants reads both sides as a point in time first, and only if the value is an instant in one grammar. Every ordering and equality comparison and every membership with an instant operand (an `instantLiteral`, a column declared `"timestamp"`, or a `fold` of `max` or `min` over one) compiles each operand to whole milliseconds since the epoch, as an integer. The SQL tests the shape of a stored value itself, with `GLOB` and `substr`, before it converts anything, so no value can make the result depend on the clock or on SQLite's readings of a date; the same grammar is applied to an `instantLiteral` when the tree is compiled, and the parameter is that number.
|
|
163
|
+
|
|
164
|
+
The grammar is: four-digit year, `-`, two-digit month, `-`, two-digit day, `T` or one space, two-digit hour, `:`, minute, `:`, second, an optional `.` and one or more fractional digits, then `Z` or a sign, two-digit hours, `:` and two-digit minutes. The value must also be a real moment: a month 1 to 12, a day the month has (no `02-30`, `04-31` or `1900-02-29`), an hour 0 to 23 (no `24:00:00`), a minute and a second 0 to 59 (no leap second), and an offset of at most `23:59`. A stored value that is not text in exactly this form is NULL, so every comparison over it is unknown: `now`, a time or a date alone, a trailing or leading space, a lower-case `t` or `z`, an offset without its colon, a form with no offset, `toUTCString()` output, an expanded or negative year, a number of any kind (a Julian day, Unix seconds or milliseconds), a blob, and a text with a NUL. An `instantLiteral` outside the grammar is bound as NULL, so a comparison against it is unknown. The fraction is truncated to three digits, as `Date.parse` does.
|
|
165
|
+
|
|
166
|
+
Inside the grammar SQLite, PostgreSQL and the evaluator (`Date.parse`) read the same instant, with three differences that are PostgreSQL's. It keeps microseconds where SQLite's comparison and the evaluator keep milliseconds, so a stored `.9996` is not equal to `.999` there and is equal in the other two (write instants to the millisecond, as `toISOString` does; tracked in [#164](https://github.com/ExaDev/trilean/issues/164)); it does not read year `0000`; and it refuses an offset beyond `15:59` with a query error. Outside the grammar the SQLite result is unknown while the evaluator may read a value: `Date.parse` also reads forms such as `Wed, 01 Jan 2020 00:00:00 GMT`, an offset without a colon, a date alone and a form with no offset (which it reads in the machine's time zone), and it reads `2020-01-01T24:00:00Z`, `2020-02-30T00:00:00Z` and a lower-case `z`. Write instants in the grammar, as `Date.prototype.toISOString` does.
|
|
167
|
+
|
|
168
|
+
**Cost.** The shape test and the conversion are evaluated in SQL for every row, once for each instant operand of a comparison that is a column (a `memberOf` has one; each side of a `compare` that is a column, and each `fold` item, has its own). One evaluation makes 39 scalar function calls (`length`, `substr`, `instr`, `strftime` and `CAST`) and 15 `GLOB` tests, and mentions the column 43 times, so a comparison over a table costs that much work per row scanned. A bare index on the stored text does not serve the comparison; an index on the same expression does, and then the expression is evaluated when the row is written and not when it is read.
|
|
163
169
|
|
|
164
170
|
## Refusal
|
|
165
171
|
|
|
@@ -167,7 +173,7 @@ The compiler never degrades. There is no best-effort fragment, no silently dropp
|
|
|
167
173
|
|
|
168
174
|
The check is an allow-list walk rather than a deny-list, so a node kind added to trilean after this version was written is refused by default instead of falling through to whatever branch happened to be last.
|
|
169
175
|
|
|
170
|
-
That guarantee is about the tree's structure, and it has one remaining limit that is about the *content* of a string operand rather than any node's kind, and is not reachable by a walk over kinds: 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.
|
|
176
|
+
That guarantee is about the tree's structure, and it has one remaining limit that is about the *content* of a string operand rather than any node's kind, and is not reachable by a walk over kinds: 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; the one grammar on which SQLite, PostgreSQL and the evaluator agree is stated under [Dialects](#dialects).
|
|
171
177
|
|
|
172
178
|
A `matches`/`notMatches` pattern would be the same kind of limit — matched by the server in PostgreSQL's own regular-expression language, not ECMAScript's — which is why it is refused rather than pushed down by default; see [Regular expressions](#regular-expressions). Setting `postgresRegexpPushdown: true` opts back into that limit deliberately, for patterns already known to be portable.
|
|
173
179
|
|
package/dist/compile.cjs
CHANGED
|
@@ -2,7 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_errors = require("./errors.cjs");
|
|
3
3
|
const require_guard = require("./guard.cjs");
|
|
4
4
|
const require_portable_pattern = require("./portable-pattern.cjs");
|
|
5
|
-
const require_options = require("./options.cjs");
|
|
5
|
+
const require_options = require("./options-DOE1_Cwr.cjs");
|
|
6
6
|
let trilean_regex = require("trilean-regex");
|
|
7
7
|
// src/compile.ts
|
|
8
8
|
const COMPARISON_SQL = {
|
|
@@ -124,21 +124,47 @@ function compilePortablePattern(node, context) {
|
|
|
124
124
|
});
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Whether an operand is an instant, as far as the compiler can tell without running anything: an `instantLiteral`, a column declared `"timestamp"`, or a `fold(max|min)` over one. A comparison or membership with any such operand compares all its operands as points in time ({@link DialectConfig.instantColumn}), since the other side of a comparison the evaluator answers definitely is an instant too.
|
|
129
|
+
*/
|
|
130
|
+
function isInstant(node, context) {
|
|
131
|
+
if (node.kind === "instantLiteral") return true;
|
|
132
|
+
if (node.kind === "reference") return bindingOf(context, node)?.binding.paramType === "timestamp";
|
|
133
|
+
if (node.kind === "fold" && node.combiner.mode !== "reduce" && typeof node.collection === "string" && context.options.collectionFor !== void 0) {
|
|
134
|
+
const binding = context.options.collectionFor(node.collection);
|
|
135
|
+
return isInstant(node.combiner.item, {
|
|
136
|
+
...context,
|
|
137
|
+
options: {
|
|
138
|
+
...context.options,
|
|
139
|
+
columnFor: binding.columnFor
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
/** Whether any of the operands of a comparison or membership is an instant. */
|
|
146
|
+
function anyInstant(nodes, context) {
|
|
147
|
+
return nodes.some((node) => isInstant(node, context));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Compiles an expression. `asInstant` renders an instant-valued operand as the dialect's comparable point in time rather than as its stored text ({@link DialectConfig.instantColumn}); it is set for every operand of a comparison or membership that has an instant operand, and reaches the item of a `fold` so a maximum or minimum is of points in time, not of text.
|
|
151
|
+
*/
|
|
152
|
+
function compileExpression(node, context, asInstant = false) {
|
|
128
153
|
switch (node.kind) {
|
|
129
154
|
case "reference": {
|
|
130
155
|
const resolved = bindingOf(context, node);
|
|
131
156
|
if (resolved === void 0) return refuse(node.kind, "expression");
|
|
132
|
-
|
|
157
|
+
const column = quoteColumn(resolved.referenceKey, resolved.binding);
|
|
158
|
+
return asInstant ? context.dialect.instantColumn(column) : column;
|
|
133
159
|
}
|
|
160
|
+
case "instantLiteral": return placeholder(context, asInstant ? context.dialect.instantParameter(node.value) : node.value, PARAM_TYPE_OF_LITERAL[node.kind]);
|
|
134
161
|
case "textLiteral":
|
|
135
162
|
case "numberLiteral":
|
|
136
|
-
case "booleanLiteral":
|
|
137
|
-
case "instantLiteral": return placeholder(context, node.value, PARAM_TYPE_OF_LITERAL[node.kind]);
|
|
163
|
+
case "booleanLiteral": return placeholder(context, node.value, PARAM_TYPE_OF_LITERAL[node.kind]);
|
|
138
164
|
case "fold": {
|
|
139
165
|
if (node.combiner.mode === "reduce") return refuse(node.kind, "expression");
|
|
140
166
|
const { table, join, itemContext, filterSql } = compileCollection(node, context, "expression");
|
|
141
|
-
const itemValueSql = compileExpression(node.combiner.item, itemContext);
|
|
167
|
+
const itemValueSql = compileExpression(node.combiner.item, itemContext, asInstant);
|
|
142
168
|
const participating = `SELECT "filter_ok", "item_value" FROM (SELECT ${filterSql ?? "TRUE"} AS "filter_ok", ${itemValueSql} AS "item_value" FROM ${table} WHERE ${join}) AS "t" WHERE "filter_ok" IS NULL OR "filter_ok"`;
|
|
143
169
|
return `(SELECT CASE WHEN MAX(CASE WHEN "filter_ok" IS NULL OR "item_value" IS NULL THEN 1 ELSE 0 END) = 1 THEN NULL ELSE ${node.combiner.mode === "max" ? "MAX" : "MIN"}("item_value") END FROM (${participating}) AS "v")`;
|
|
144
170
|
}
|
|
@@ -157,8 +183,9 @@ function compilePredicate(node, context) {
|
|
|
157
183
|
return `(${node.operands.map((operand) => compilePredicate(operand, context)).join(joiner)})`;
|
|
158
184
|
}
|
|
159
185
|
case "compare": {
|
|
160
|
-
const
|
|
161
|
-
const
|
|
186
|
+
const instants = anyInstant([node.left, node.right], context);
|
|
187
|
+
const left = compileExpression(node.left, context, instants);
|
|
188
|
+
const right = compileExpression(node.right, context, instants);
|
|
162
189
|
return `(${left} ${COMPARISON_SQL[node.op]} ${right})`;
|
|
163
190
|
}
|
|
164
191
|
case "textCompare": {
|
|
@@ -167,12 +194,13 @@ function compilePredicate(node, context) {
|
|
|
167
194
|
return `(${left} ${context.textComparison[node.op]} ${right})`;
|
|
168
195
|
}
|
|
169
196
|
case "memberOf": {
|
|
170
|
-
const
|
|
197
|
+
const instants = anyInstant([node.operand, ...node.candidates], context);
|
|
198
|
+
const operand = compileExpression(node.operand, context, instants);
|
|
171
199
|
if (node.candidates.length === 0) {
|
|
172
200
|
const nullLiteral = `NULL${context.dialect.emptyMemberOfNullSuffix}`;
|
|
173
201
|
return node.op === "in" ? `(${operand} IS NULL AND ${nullLiteral})` : `(${operand} IS NOT NULL OR ${nullLiteral})`;
|
|
174
202
|
}
|
|
175
|
-
const candidates = node.candidates.map((candidate) => compileExpression(candidate, context)).join(", ");
|
|
203
|
+
const candidates = node.candidates.map((candidate) => compileExpression(candidate, context, instants)).join(", ");
|
|
176
204
|
return `(${operand} ${node.op === "in" ? "IN" : "NOT IN"} (${candidates}))`;
|
|
177
205
|
}
|
|
178
206
|
case "exists": return `(${compileExpression(node.operand, context)} IS NOT NULL)`;
|
package/dist/compile.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { InvalidCollectionTableError, InvalidColumnError, UnsupportedNodeError } from "./errors.js";
|
|
2
2
|
import { findUnpushableNodeKind } from "./guard.js";
|
|
3
3
|
import { renderPostgresPattern, renderSqliteGlobPattern } from "./portable-pattern.js";
|
|
4
|
-
import {
|
|
4
|
+
import { r as assertImplementedDialect, t as DIALECT_CONFIG } from "./options-c1nHisgX.js";
|
|
5
5
|
import { parseRegex } from "trilean-regex";
|
|
6
6
|
// src/compile.ts
|
|
7
7
|
const COMPARISON_SQL = {
|
|
@@ -123,21 +123,47 @@ function compilePortablePattern(node, context) {
|
|
|
123
123
|
});
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Whether an operand is an instant, as far as the compiler can tell without running anything: an `instantLiteral`, a column declared `"timestamp"`, or a `fold(max|min)` over one. A comparison or membership with any such operand compares all its operands as points in time ({@link DialectConfig.instantColumn}), since the other side of a comparison the evaluator answers definitely is an instant too.
|
|
128
|
+
*/
|
|
129
|
+
function isInstant(node, context) {
|
|
130
|
+
if (node.kind === "instantLiteral") return true;
|
|
131
|
+
if (node.kind === "reference") return bindingOf(context, node)?.binding.paramType === "timestamp";
|
|
132
|
+
if (node.kind === "fold" && node.combiner.mode !== "reduce" && typeof node.collection === "string" && context.options.collectionFor !== void 0) {
|
|
133
|
+
const binding = context.options.collectionFor(node.collection);
|
|
134
|
+
return isInstant(node.combiner.item, {
|
|
135
|
+
...context,
|
|
136
|
+
options: {
|
|
137
|
+
...context.options,
|
|
138
|
+
columnFor: binding.columnFor
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
/** Whether any of the operands of a comparison or membership is an instant. */
|
|
145
|
+
function anyInstant(nodes, context) {
|
|
146
|
+
return nodes.some((node) => isInstant(node, context));
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Compiles an expression. `asInstant` renders an instant-valued operand as the dialect's comparable point in time rather than as its stored text ({@link DialectConfig.instantColumn}); it is set for every operand of a comparison or membership that has an instant operand, and reaches the item of a `fold` so a maximum or minimum is of points in time, not of text.
|
|
150
|
+
*/
|
|
151
|
+
function compileExpression(node, context, asInstant = false) {
|
|
127
152
|
switch (node.kind) {
|
|
128
153
|
case "reference": {
|
|
129
154
|
const resolved = bindingOf(context, node);
|
|
130
155
|
if (resolved === void 0) return refuse(node.kind, "expression");
|
|
131
|
-
|
|
156
|
+
const column = quoteColumn(resolved.referenceKey, resolved.binding);
|
|
157
|
+
return asInstant ? context.dialect.instantColumn(column) : column;
|
|
132
158
|
}
|
|
159
|
+
case "instantLiteral": return placeholder(context, asInstant ? context.dialect.instantParameter(node.value) : node.value, PARAM_TYPE_OF_LITERAL[node.kind]);
|
|
133
160
|
case "textLiteral":
|
|
134
161
|
case "numberLiteral":
|
|
135
|
-
case "booleanLiteral":
|
|
136
|
-
case "instantLiteral": return placeholder(context, node.value, PARAM_TYPE_OF_LITERAL[node.kind]);
|
|
162
|
+
case "booleanLiteral": return placeholder(context, node.value, PARAM_TYPE_OF_LITERAL[node.kind]);
|
|
137
163
|
case "fold": {
|
|
138
164
|
if (node.combiner.mode === "reduce") return refuse(node.kind, "expression");
|
|
139
165
|
const { table, join, itemContext, filterSql } = compileCollection(node, context, "expression");
|
|
140
|
-
const itemValueSql = compileExpression(node.combiner.item, itemContext);
|
|
166
|
+
const itemValueSql = compileExpression(node.combiner.item, itemContext, asInstant);
|
|
141
167
|
const participating = `SELECT "filter_ok", "item_value" FROM (SELECT ${filterSql ?? "TRUE"} AS "filter_ok", ${itemValueSql} AS "item_value" FROM ${table} WHERE ${join}) AS "t" WHERE "filter_ok" IS NULL OR "filter_ok"`;
|
|
142
168
|
return `(SELECT CASE WHEN MAX(CASE WHEN "filter_ok" IS NULL OR "item_value" IS NULL THEN 1 ELSE 0 END) = 1 THEN NULL ELSE ${node.combiner.mode === "max" ? "MAX" : "MIN"}("item_value") END FROM (${participating}) AS "v")`;
|
|
143
169
|
}
|
|
@@ -156,8 +182,9 @@ function compilePredicate(node, context) {
|
|
|
156
182
|
return `(${node.operands.map((operand) => compilePredicate(operand, context)).join(joiner)})`;
|
|
157
183
|
}
|
|
158
184
|
case "compare": {
|
|
159
|
-
const
|
|
160
|
-
const
|
|
185
|
+
const instants = anyInstant([node.left, node.right], context);
|
|
186
|
+
const left = compileExpression(node.left, context, instants);
|
|
187
|
+
const right = compileExpression(node.right, context, instants);
|
|
161
188
|
return `(${left} ${COMPARISON_SQL[node.op]} ${right})`;
|
|
162
189
|
}
|
|
163
190
|
case "textCompare": {
|
|
@@ -166,12 +193,13 @@ function compilePredicate(node, context) {
|
|
|
166
193
|
return `(${left} ${context.textComparison[node.op]} ${right})`;
|
|
167
194
|
}
|
|
168
195
|
case "memberOf": {
|
|
169
|
-
const
|
|
196
|
+
const instants = anyInstant([node.operand, ...node.candidates], context);
|
|
197
|
+
const operand = compileExpression(node.operand, context, instants);
|
|
170
198
|
if (node.candidates.length === 0) {
|
|
171
199
|
const nullLiteral = `NULL${context.dialect.emptyMemberOfNullSuffix}`;
|
|
172
200
|
return node.op === "in" ? `(${operand} IS NULL AND ${nullLiteral})` : `(${operand} IS NOT NULL OR ${nullLiteral})`;
|
|
173
201
|
}
|
|
174
|
-
const candidates = node.candidates.map((candidate) => compileExpression(candidate, context)).join(", ");
|
|
202
|
+
const candidates = node.candidates.map((candidate) => compileExpression(candidate, context, instants)).join(", ");
|
|
175
203
|
return `(${operand} ${node.op === "in" ? "IN" : "NOT IN"} (${candidates}))`;
|
|
176
204
|
}
|
|
177
205
|
case "exists": return `(${compileExpression(node.operand, context)} IS NOT NULL)`;
|
package/dist/guard.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_options = require("./options.cjs");
|
|
2
|
+
const require_options = require("./options-DOE1_Cwr.cjs");
|
|
3
3
|
const require_portable_pattern = require("./portable-pattern.cjs");
|
|
4
4
|
let trilean_regex = require("trilean-regex");
|
|
5
5
|
// src/guard.ts
|
package/dist/guard.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assertImplementedDialect } from "./options.js";
|
|
1
|
+
import { r as assertImplementedDialect } from "./options-c1nHisgX.js";
|
|
2
2
|
import { renderPostgresPattern, renderSqliteGlobPattern } from "./portable-pattern.js";
|
|
3
3
|
import { parseRegex } from "trilean-regex";
|
|
4
4
|
// src/guard.ts
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
const require_errors = require("./errors.cjs");
|
|
2
|
+
// src/internal/sqlite-instant.ts
|
|
3
|
+
/**
|
|
4
|
+
* How the SQLite dialect reads an instant, in one place.
|
|
5
|
+
*
|
|
6
|
+
* SQLite has no timestamp type, and its date functions read far more than an instant: the text `now` is the present time, a time of day is a date in the year 2000, a number is a Julian day. The evaluator reads none of these. So the SQL decides first, with a shape test that needs no extension, whether a stored value is an instant in exactly one grammar, and only then converts it; any other value is NULL (unknown) whatever the date functions would make of it, and the same grammar is applied to an `instantLiteral` when the tree is compiled.
|
|
7
|
+
*
|
|
8
|
+
* The grammar: four-digit year, `-`, two-digit month, `-`, two-digit day, `T` or one space, two-digit hour, `:`, minute, `:`, second, an optional `.` and one or more fractional digits, then `Z` or a sign, two-digit hours, `:` and two-digit minutes. The value must also be a real moment: a month 1 to 12, a day that exists in that month (so no `02-30`), an hour 0 to 23, a minute and second 0 to 59 (no leap second, no hour 24), and an offset of at most 23:59. Within it SQLite, PostgreSQL and the evaluator (`Date.parse`) read the same instant, to the millisecond; the fraction is truncated to three digits, as `Date.parse` does, and is not rounded as SQLite's own date functions would.
|
|
9
|
+
*/
|
|
10
|
+
/** The characters up to the seconds, in the form SQLite's `GLOB` reads: four digits, `-`, two, `-`, two, `T` or a space, two, `:`, two, `:`, two. */
|
|
11
|
+
const DATE_AND_TIME_SHAPE = "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][T ][0-9][0-9]:[0-9][0-9]:[0-9][0-9]";
|
|
12
|
+
const OFFSET_SHAPE = "[+-][0-9][0-9]:[0-9][0-9]";
|
|
13
|
+
/** The characters before the seconds end, which are the first nineteen of a value in the grammar. */
|
|
14
|
+
const SECONDS_END = 19;
|
|
15
|
+
/** Where the fraction's point, or the zone, starts (1-based). */
|
|
16
|
+
const AFTER_SECONDS = 20;
|
|
17
|
+
/** The width of a numeric offset, `+HH:MM`. */
|
|
18
|
+
const OFFSET_WIDTH = 6;
|
|
19
|
+
const MILLISECONDS_PER_SECOND = 1e3;
|
|
20
|
+
const MILLISECONDS_PER_MINUTE = 6e4;
|
|
21
|
+
const MINUTES_PER_HOUR = 60;
|
|
22
|
+
const FRACTION_DIGITS = 3;
|
|
23
|
+
/**
|
|
24
|
+
* The SQL that reads `column` (an expression of one SQLite value) as whole milliseconds since the epoch, or NULL when it is not an instant in the grammar above. `column` appears many times, so it must be a column reference or a placeholder-free expression.
|
|
25
|
+
*/
|
|
26
|
+
function sqliteInstantColumn(column) {
|
|
27
|
+
const length = `length(${column})`;
|
|
28
|
+
const isZulu = `${column} GLOB '*Z'`;
|
|
29
|
+
const fractionLength = `(${length} - ${String(AFTER_SECONDS)} - CASE WHEN ${isZulu} THEN 1 ELSE ${String(OFFSET_WIDTH)} END)`;
|
|
30
|
+
const zoneStart = `CASE WHEN ${isZulu} THEN ${length} ELSE ${length} - ${String(5)} END`;
|
|
31
|
+
const afterSeconds = `substr(${column}, ${String(AFTER_SECONDS)})`;
|
|
32
|
+
const fractionOnly = `substr(${column}, ${String(21)}, ${fractionLength})`;
|
|
33
|
+
const zoneShape = `(${afterSeconds} = 'Z' OR ${afterSeconds} GLOB '${OFFSET_SHAPE}' OR (substr(${column}, ${String(AFTER_SECONDS)}, 1) = '.' AND ${fractionLength} > 0 AND ${fractionOnly} NOT GLOB '*[^0-9]*' AND (${isZulu} OR ${column} GLOB '*${OFFSET_SHAPE}')))`;
|
|
34
|
+
const shape = `instr(CAST(${column} AS BLOB), x'00') = 0 AND ${column} GLOB '${DATE_AND_TIME_SHAPE}*' AND ${zoneShape}`;
|
|
35
|
+
const dateAndTime = `substr(${column}, 1, ${String(SECONDS_END)})`;
|
|
36
|
+
const real = [
|
|
37
|
+
`strftime('%Y-%m-%d', ${dateAndTime}) = substr(${column}, 1, 10)`,
|
|
38
|
+
`strftime('%s', ${dateAndTime}) IS NOT NULL`,
|
|
39
|
+
`substr(${column}, 12, 2) < '24'`,
|
|
40
|
+
`(${isZulu} OR (substr(${column}, ${zoneStart} + 1, 2) <= '23' AND substr(${column}, ${zoneStart} + 4, 2) <= '59'))`
|
|
41
|
+
].join(" AND ");
|
|
42
|
+
const fraction = `CASE WHEN substr(${column}, ${String(AFTER_SECONDS)}, 1) = '.' THEN CAST(substr(${fractionOnly} || '00', 1, ${String(FRACTION_DIGITS)}) AS INTEGER) ELSE 0 END`;
|
|
43
|
+
const offsetMinutes = `CASE WHEN ${isZulu} THEN 0 ELSE (CASE substr(${column}, ${zoneStart}, 1) WHEN '-' THEN -1 ELSE 1 END) * (CAST(substr(${column}, ${zoneStart} + 1, 2) AS INTEGER) * ${String(MINUTES_PER_HOUR)} + CAST(substr(${column}, ${zoneStart} + 4, 2) AS INTEGER)) END`;
|
|
44
|
+
return `(CASE WHEN ${shape} AND ${real} THEN CAST(strftime('%s', ${dateAndTime}) AS INTEGER) * ${String(MILLISECONDS_PER_SECOND)} + ${fraction} - (${offsetMinutes}) * ${String(MILLISECONDS_PER_MINUTE)} END)`;
|
|
45
|
+
}
|
|
46
|
+
const INSTANT = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})[T ](?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})(?:\.(?<fraction>\d+))?(?:Z|(?<sign>[+-])(?<zoneHour>\d{2}):(?<zoneMinute>\d{2}))$/u;
|
|
47
|
+
const LAST_HOUR = 23;
|
|
48
|
+
const LAST_MINUTE_OR_SECOND = 59;
|
|
49
|
+
/**
|
|
50
|
+
* The bind value of an `instantLiteral` for SQLite: the whole milliseconds since the epoch of a text in the grammar above, computed from its parts with the rules the SQL applies to a stored value, or `null` (unknown) for any other text.
|
|
51
|
+
*/
|
|
52
|
+
function sqliteInstantParameter(iso) {
|
|
53
|
+
const groups = INSTANT.exec(iso)?.groups;
|
|
54
|
+
if (groups === void 0) return null;
|
|
55
|
+
const part = (name) => Number(groups[name] ?? 0);
|
|
56
|
+
const [year, month, day] = [
|
|
57
|
+
part("year"),
|
|
58
|
+
part("month"),
|
|
59
|
+
part("day")
|
|
60
|
+
];
|
|
61
|
+
const moment = /* @__PURE__ */ new Date(0);
|
|
62
|
+
moment.setUTCFullYear(year, month - 1, day);
|
|
63
|
+
moment.setUTCHours(part("hour"), part("minute"), part("second"), 0);
|
|
64
|
+
if (!(moment.getUTCFullYear() === year && moment.getUTCMonth() === month - 1 && moment.getUTCDate() === day) || part("hour") > LAST_HOUR || part("minute") > LAST_MINUTE_OR_SECOND || part("second") > LAST_MINUTE_OR_SECOND || part("zoneHour") > LAST_HOUR || part("zoneMinute") > LAST_MINUTE_OR_SECOND) return null;
|
|
65
|
+
const milliseconds = Number(`${groups.fraction ?? ""}00`.slice(0, FRACTION_DIGITS));
|
|
66
|
+
const offset = (groups.sign === "-" ? -1 : 1) * (part("zoneHour") * MINUTES_PER_HOUR + part("zoneMinute")) * MILLISECONDS_PER_MINUTE;
|
|
67
|
+
return moment.getTime() + milliseconds - offset;
|
|
68
|
+
}
|
|
69
|
+
// src/options.ts
|
|
70
|
+
/** The PostgreSQL type each declared value kind is cast to. `timestamptz` rather than `timestamp`: trilean's `instant` is an ISO-8601 string that may carry an offset, and parsing one as a naive `timestamp` would silently discard it. */
|
|
71
|
+
const POSTGRES_TYPE_NAME = {
|
|
72
|
+
text: "text",
|
|
73
|
+
number: "double precision",
|
|
74
|
+
boolean: "boolean",
|
|
75
|
+
timestamp: "timestamptz"
|
|
76
|
+
};
|
|
77
|
+
const DIALECT_CONFIG = {
|
|
78
|
+
postgres: {
|
|
79
|
+
matches: "~",
|
|
80
|
+
notMatches: "!~",
|
|
81
|
+
portableMatches: "~",
|
|
82
|
+
portableNotMatches: "!~",
|
|
83
|
+
placeholder: (index, castTo) => `$${String(index)}::${POSTGRES_TYPE_NAME[castTo]}`,
|
|
84
|
+
emptyMemberOfNullSuffix: "::boolean",
|
|
85
|
+
instantColumn: (columnSql) => columnSql,
|
|
86
|
+
instantParameter: (iso) => iso
|
|
87
|
+
},
|
|
88
|
+
sqlite: {
|
|
89
|
+
matches: "REGEXP",
|
|
90
|
+
notMatches: "NOT REGEXP",
|
|
91
|
+
portableMatches: "GLOB",
|
|
92
|
+
portableNotMatches: "NOT GLOB",
|
|
93
|
+
placeholder: () => "?",
|
|
94
|
+
emptyMemberOfNullSuffix: "",
|
|
95
|
+
instantColumn: sqliteInstantColumn,
|
|
96
|
+
instantParameter: sqliteInstantParameter
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Refuses a dialect this version does not implement, before anything indexes a per-dialect table with it.
|
|
101
|
+
*
|
|
102
|
+
* Both entry points check their own argument, because both are reachable with a name the type describes but cannot enforce -- a dialect is exactly the sort of value that arrives as a configuration string asserted into the union at the boundary. Left unchecked, that name is not caught anywhere: the compiler reads an operator off `undefined` and throws a `TypeError` naming an internal field, and the guard, whose tables are only consulted for a node it actually objects to, answers "pushable" for a tree the compiler then fails on -- the one thing `findUnpushableNodeKind` exists to decide, decided wrongly.
|
|
103
|
+
*
|
|
104
|
+
* `DIALECT_CONFIG` is the list, rather than a second constant, so implementing a dialect cannot leave this behind.
|
|
105
|
+
*/
|
|
106
|
+
function assertImplementedDialect(dialect) {
|
|
107
|
+
if (!Object.hasOwn(DIALECT_CONFIG, dialect)) throw new require_errors.UnknownDialectError(dialect, Object.keys(DIALECT_CONFIG));
|
|
108
|
+
}
|
|
109
|
+
Object.defineProperty(exports, "DIALECT_CONFIG", {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
get: function() {
|
|
112
|
+
return DIALECT_CONFIG;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
Object.defineProperty(exports, "POSTGRES_TYPE_NAME", {
|
|
116
|
+
enumerable: true,
|
|
117
|
+
get: function() {
|
|
118
|
+
return POSTGRES_TYPE_NAME;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
Object.defineProperty(exports, "assertImplementedDialect", {
|
|
122
|
+
enumerable: true,
|
|
123
|
+
get: function() {
|
|
124
|
+
return assertImplementedDialect;
|
|
125
|
+
}
|
|
126
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { UnknownDialectError } from "./errors.js";
|
|
2
|
+
// src/internal/sqlite-instant.ts
|
|
3
|
+
/**
|
|
4
|
+
* How the SQLite dialect reads an instant, in one place.
|
|
5
|
+
*
|
|
6
|
+
* SQLite has no timestamp type, and its date functions read far more than an instant: the text `now` is the present time, a time of day is a date in the year 2000, a number is a Julian day. The evaluator reads none of these. So the SQL decides first, with a shape test that needs no extension, whether a stored value is an instant in exactly one grammar, and only then converts it; any other value is NULL (unknown) whatever the date functions would make of it, and the same grammar is applied to an `instantLiteral` when the tree is compiled.
|
|
7
|
+
*
|
|
8
|
+
* The grammar: four-digit year, `-`, two-digit month, `-`, two-digit day, `T` or one space, two-digit hour, `:`, minute, `:`, second, an optional `.` and one or more fractional digits, then `Z` or a sign, two-digit hours, `:` and two-digit minutes. The value must also be a real moment: a month 1 to 12, a day that exists in that month (so no `02-30`), an hour 0 to 23, a minute and second 0 to 59 (no leap second, no hour 24), and an offset of at most 23:59. Within it SQLite, PostgreSQL and the evaluator (`Date.parse`) read the same instant, to the millisecond; the fraction is truncated to three digits, as `Date.parse` does, and is not rounded as SQLite's own date functions would.
|
|
9
|
+
*/
|
|
10
|
+
/** The characters up to the seconds, in the form SQLite's `GLOB` reads: four digits, `-`, two, `-`, two, `T` or a space, two, `:`, two, `:`, two. */
|
|
11
|
+
const DATE_AND_TIME_SHAPE = "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][T ][0-9][0-9]:[0-9][0-9]:[0-9][0-9]";
|
|
12
|
+
const OFFSET_SHAPE = "[+-][0-9][0-9]:[0-9][0-9]";
|
|
13
|
+
/** The characters before the seconds end, which are the first nineteen of a value in the grammar. */
|
|
14
|
+
const SECONDS_END = 19;
|
|
15
|
+
/** Where the fraction's point, or the zone, starts (1-based). */
|
|
16
|
+
const AFTER_SECONDS = 20;
|
|
17
|
+
/** The width of a numeric offset, `+HH:MM`. */
|
|
18
|
+
const OFFSET_WIDTH = 6;
|
|
19
|
+
const MILLISECONDS_PER_SECOND = 1e3;
|
|
20
|
+
const MILLISECONDS_PER_MINUTE = 6e4;
|
|
21
|
+
const MINUTES_PER_HOUR = 60;
|
|
22
|
+
const FRACTION_DIGITS = 3;
|
|
23
|
+
/**
|
|
24
|
+
* The SQL that reads `column` (an expression of one SQLite value) as whole milliseconds since the epoch, or NULL when it is not an instant in the grammar above. `column` appears many times, so it must be a column reference or a placeholder-free expression.
|
|
25
|
+
*/
|
|
26
|
+
function sqliteInstantColumn(column) {
|
|
27
|
+
const length = `length(${column})`;
|
|
28
|
+
const isZulu = `${column} GLOB '*Z'`;
|
|
29
|
+
const fractionLength = `(${length} - ${String(AFTER_SECONDS)} - CASE WHEN ${isZulu} THEN 1 ELSE ${String(OFFSET_WIDTH)} END)`;
|
|
30
|
+
const zoneStart = `CASE WHEN ${isZulu} THEN ${length} ELSE ${length} - ${String(5)} END`;
|
|
31
|
+
const afterSeconds = `substr(${column}, ${String(AFTER_SECONDS)})`;
|
|
32
|
+
const fractionOnly = `substr(${column}, ${String(21)}, ${fractionLength})`;
|
|
33
|
+
const zoneShape = `(${afterSeconds} = 'Z' OR ${afterSeconds} GLOB '${OFFSET_SHAPE}' OR (substr(${column}, ${String(AFTER_SECONDS)}, 1) = '.' AND ${fractionLength} > 0 AND ${fractionOnly} NOT GLOB '*[^0-9]*' AND (${isZulu} OR ${column} GLOB '*${OFFSET_SHAPE}')))`;
|
|
34
|
+
const shape = `instr(CAST(${column} AS BLOB), x'00') = 0 AND ${column} GLOB '${DATE_AND_TIME_SHAPE}*' AND ${zoneShape}`;
|
|
35
|
+
const dateAndTime = `substr(${column}, 1, ${String(SECONDS_END)})`;
|
|
36
|
+
const real = [
|
|
37
|
+
`strftime('%Y-%m-%d', ${dateAndTime}) = substr(${column}, 1, 10)`,
|
|
38
|
+
`strftime('%s', ${dateAndTime}) IS NOT NULL`,
|
|
39
|
+
`substr(${column}, 12, 2) < '24'`,
|
|
40
|
+
`(${isZulu} OR (substr(${column}, ${zoneStart} + 1, 2) <= '23' AND substr(${column}, ${zoneStart} + 4, 2) <= '59'))`
|
|
41
|
+
].join(" AND ");
|
|
42
|
+
const fraction = `CASE WHEN substr(${column}, ${String(AFTER_SECONDS)}, 1) = '.' THEN CAST(substr(${fractionOnly} || '00', 1, ${String(FRACTION_DIGITS)}) AS INTEGER) ELSE 0 END`;
|
|
43
|
+
const offsetMinutes = `CASE WHEN ${isZulu} THEN 0 ELSE (CASE substr(${column}, ${zoneStart}, 1) WHEN '-' THEN -1 ELSE 1 END) * (CAST(substr(${column}, ${zoneStart} + 1, 2) AS INTEGER) * ${String(MINUTES_PER_HOUR)} + CAST(substr(${column}, ${zoneStart} + 4, 2) AS INTEGER)) END`;
|
|
44
|
+
return `(CASE WHEN ${shape} AND ${real} THEN CAST(strftime('%s', ${dateAndTime}) AS INTEGER) * ${String(MILLISECONDS_PER_SECOND)} + ${fraction} - (${offsetMinutes}) * ${String(MILLISECONDS_PER_MINUTE)} END)`;
|
|
45
|
+
}
|
|
46
|
+
const INSTANT = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})[T ](?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})(?:\.(?<fraction>\d+))?(?:Z|(?<sign>[+-])(?<zoneHour>\d{2}):(?<zoneMinute>\d{2}))$/u;
|
|
47
|
+
const LAST_HOUR = 23;
|
|
48
|
+
const LAST_MINUTE_OR_SECOND = 59;
|
|
49
|
+
/**
|
|
50
|
+
* The bind value of an `instantLiteral` for SQLite: the whole milliseconds since the epoch of a text in the grammar above, computed from its parts with the rules the SQL applies to a stored value, or `null` (unknown) for any other text.
|
|
51
|
+
*/
|
|
52
|
+
function sqliteInstantParameter(iso) {
|
|
53
|
+
const groups = INSTANT.exec(iso)?.groups;
|
|
54
|
+
if (groups === void 0) return null;
|
|
55
|
+
const part = (name) => Number(groups[name] ?? 0);
|
|
56
|
+
const [year, month, day] = [
|
|
57
|
+
part("year"),
|
|
58
|
+
part("month"),
|
|
59
|
+
part("day")
|
|
60
|
+
];
|
|
61
|
+
const moment = /* @__PURE__ */ new Date(0);
|
|
62
|
+
moment.setUTCFullYear(year, month - 1, day);
|
|
63
|
+
moment.setUTCHours(part("hour"), part("minute"), part("second"), 0);
|
|
64
|
+
if (!(moment.getUTCFullYear() === year && moment.getUTCMonth() === month - 1 && moment.getUTCDate() === day) || part("hour") > LAST_HOUR || part("minute") > LAST_MINUTE_OR_SECOND || part("second") > LAST_MINUTE_OR_SECOND || part("zoneHour") > LAST_HOUR || part("zoneMinute") > LAST_MINUTE_OR_SECOND) return null;
|
|
65
|
+
const milliseconds = Number(`${groups.fraction ?? ""}00`.slice(0, FRACTION_DIGITS));
|
|
66
|
+
const offset = (groups.sign === "-" ? -1 : 1) * (part("zoneHour") * MINUTES_PER_HOUR + part("zoneMinute")) * MILLISECONDS_PER_MINUTE;
|
|
67
|
+
return moment.getTime() + milliseconds - offset;
|
|
68
|
+
}
|
|
69
|
+
// src/options.ts
|
|
70
|
+
/** The PostgreSQL type each declared value kind is cast to. `timestamptz` rather than `timestamp`: trilean's `instant` is an ISO-8601 string that may carry an offset, and parsing one as a naive `timestamp` would silently discard it. */
|
|
71
|
+
const POSTGRES_TYPE_NAME = {
|
|
72
|
+
text: "text",
|
|
73
|
+
number: "double precision",
|
|
74
|
+
boolean: "boolean",
|
|
75
|
+
timestamp: "timestamptz"
|
|
76
|
+
};
|
|
77
|
+
const DIALECT_CONFIG = {
|
|
78
|
+
postgres: {
|
|
79
|
+
matches: "~",
|
|
80
|
+
notMatches: "!~",
|
|
81
|
+
portableMatches: "~",
|
|
82
|
+
portableNotMatches: "!~",
|
|
83
|
+
placeholder: (index, castTo) => `$${String(index)}::${POSTGRES_TYPE_NAME[castTo]}`,
|
|
84
|
+
emptyMemberOfNullSuffix: "::boolean",
|
|
85
|
+
instantColumn: (columnSql) => columnSql,
|
|
86
|
+
instantParameter: (iso) => iso
|
|
87
|
+
},
|
|
88
|
+
sqlite: {
|
|
89
|
+
matches: "REGEXP",
|
|
90
|
+
notMatches: "NOT REGEXP",
|
|
91
|
+
portableMatches: "GLOB",
|
|
92
|
+
portableNotMatches: "NOT GLOB",
|
|
93
|
+
placeholder: () => "?",
|
|
94
|
+
emptyMemberOfNullSuffix: "",
|
|
95
|
+
instantColumn: sqliteInstantColumn,
|
|
96
|
+
instantParameter: sqliteInstantParameter
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Refuses a dialect this version does not implement, before anything indexes a per-dialect table with it.
|
|
101
|
+
*
|
|
102
|
+
* Both entry points check their own argument, because both are reachable with a name the type describes but cannot enforce -- a dialect is exactly the sort of value that arrives as a configuration string asserted into the union at the boundary. Left unchecked, that name is not caught anywhere: the compiler reads an operator off `undefined` and throws a `TypeError` naming an internal field, and the guard, whose tables are only consulted for a node it actually objects to, answers "pushable" for a tree the compiler then fails on -- the one thing `findUnpushableNodeKind` exists to decide, decided wrongly.
|
|
103
|
+
*
|
|
104
|
+
* `DIALECT_CONFIG` is the list, rather than a second constant, so implementing a dialect cannot leave this behind.
|
|
105
|
+
*/
|
|
106
|
+
function assertImplementedDialect(dialect) {
|
|
107
|
+
if (!Object.hasOwn(DIALECT_CONFIG, dialect)) throw new UnknownDialectError(dialect, Object.keys(DIALECT_CONFIG));
|
|
108
|
+
}
|
|
109
|
+
export { POSTGRES_TYPE_NAME as n, assertImplementedDialect as r, DIALECT_CONFIG as t };
|
package/dist/options.cjs
CHANGED
|
@@ -1,41 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
text: "text",
|
|
7
|
-
number: "double precision",
|
|
8
|
-
boolean: "boolean",
|
|
9
|
-
timestamp: "timestamptz"
|
|
10
|
-
};
|
|
11
|
-
const DIALECT_CONFIG = {
|
|
12
|
-
postgres: {
|
|
13
|
-
matches: "~",
|
|
14
|
-
notMatches: "!~",
|
|
15
|
-
portableMatches: "~",
|
|
16
|
-
portableNotMatches: "!~",
|
|
17
|
-
placeholder: (index, castTo) => `$${String(index)}::${POSTGRES_TYPE_NAME[castTo]}`,
|
|
18
|
-
emptyMemberOfNullSuffix: "::boolean"
|
|
19
|
-
},
|
|
20
|
-
sqlite: {
|
|
21
|
-
matches: "REGEXP",
|
|
22
|
-
notMatches: "NOT REGEXP",
|
|
23
|
-
portableMatches: "GLOB",
|
|
24
|
-
portableNotMatches: "NOT GLOB",
|
|
25
|
-
placeholder: () => "?",
|
|
26
|
-
emptyMemberOfNullSuffix: ""
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Refuses a dialect this version does not implement, before anything indexes a per-dialect table with it.
|
|
31
|
-
*
|
|
32
|
-
* Both entry points check their own argument, because both are reachable with a name the type describes but cannot enforce -- a dialect is exactly the sort of value that arrives as a configuration string asserted into the union at the boundary. Left unchecked, that name is not caught anywhere: the compiler reads an operator off `undefined` and throws a `TypeError` naming an internal field, and the guard, whose tables are only consulted for a node it actually objects to, answers "pushable" for a tree the compiler then fails on -- the one thing `findUnpushableNodeKind` exists to decide, decided wrongly.
|
|
33
|
-
*
|
|
34
|
-
* `DIALECT_CONFIG` is the list, rather than a second constant, so implementing a dialect cannot leave this behind.
|
|
35
|
-
*/
|
|
36
|
-
function assertImplementedDialect(dialect) {
|
|
37
|
-
if (!Object.hasOwn(DIALECT_CONFIG, dialect)) throw new require_errors.UnknownDialectError(dialect, Object.keys(DIALECT_CONFIG));
|
|
38
|
-
}
|
|
39
|
-
exports.DIALECT_CONFIG = DIALECT_CONFIG;
|
|
40
|
-
exports.POSTGRES_TYPE_NAME = POSTGRES_TYPE_NAME;
|
|
41
|
-
exports.assertImplementedDialect = assertImplementedDialect;
|
|
2
|
+
const require_options = require("./options-DOE1_Cwr.cjs");
|
|
3
|
+
exports.DIALECT_CONFIG = require_options.DIALECT_CONFIG;
|
|
4
|
+
exports.POSTGRES_TYPE_NAME = require_options.POSTGRES_TYPE_NAME;
|
|
5
|
+
exports.assertImplementedDialect = require_options.assertImplementedDialect;
|
package/dist/options.d.cts
CHANGED
|
@@ -77,6 +77,14 @@ export interface DialectConfig {
|
|
|
77
77
|
placeholder: (index: number, castTo: SqlParamType) => string;
|
|
78
78
|
/** Appended to the bare `NULL` in the two forms an empty `memberOf` candidate list compiles to, for a dialect that needs the resulting expression annotated with a boolean type. */
|
|
79
79
|
emptyMemberOfNullSuffix: string;
|
|
80
|
+
/**
|
|
81
|
+
* Renders an instant-valued column expression as the value the dialect compares. PostgreSQL's `timestamptz` already is a point in time and needs nothing; SQLite holds an instant as text in whatever form the writer chose (an offset, a `Z`, any number of fractional digits), and text compares by its characters rather than by the instant, so the text is read as a point in time first: whole milliseconds since the epoch, as an integer.
|
|
82
|
+
*/
|
|
83
|
+
instantColumn: (columnSql: string) => string;
|
|
84
|
+
/**
|
|
85
|
+
* The bind value of an `instantLiteral`, for a comparison the dialect makes with {@link DialectConfig.instantColumn}. PostgreSQL parses the text itself and casts it. SQLite compares milliseconds since the epoch, read from a text in the one grammar of `internal/sqlite-instant.ts`; a text outside the grammar of `internal/sqlite-instant.ts` is `null`, which makes the comparison unknown.
|
|
86
|
+
*/
|
|
87
|
+
instantParameter: (iso: string) => unknown;
|
|
80
88
|
}
|
|
81
89
|
export declare const DIALECT_CONFIG: Readonly<Record<SqlDialect, DialectConfig>>;
|
|
82
90
|
/**
|
package/dist/options.d.ts
CHANGED
|
@@ -77,6 +77,14 @@ export interface DialectConfig {
|
|
|
77
77
|
placeholder: (index: number, castTo: SqlParamType) => string;
|
|
78
78
|
/** Appended to the bare `NULL` in the two forms an empty `memberOf` candidate list compiles to, for a dialect that needs the resulting expression annotated with a boolean type. */
|
|
79
79
|
emptyMemberOfNullSuffix: string;
|
|
80
|
+
/**
|
|
81
|
+
* Renders an instant-valued column expression as the value the dialect compares. PostgreSQL's `timestamptz` already is a point in time and needs nothing; SQLite holds an instant as text in whatever form the writer chose (an offset, a `Z`, any number of fractional digits), and text compares by its characters rather than by the instant, so the text is read as a point in time first: whole milliseconds since the epoch, as an integer.
|
|
82
|
+
*/
|
|
83
|
+
instantColumn: (columnSql: string) => string;
|
|
84
|
+
/**
|
|
85
|
+
* The bind value of an `instantLiteral`, for a comparison the dialect makes with {@link DialectConfig.instantColumn}. PostgreSQL parses the text itself and casts it. SQLite compares milliseconds since the epoch, read from a text in the one grammar of `internal/sqlite-instant.ts`; a text outside the grammar of `internal/sqlite-instant.ts` is `null`, which makes the comparison unknown.
|
|
86
|
+
*/
|
|
87
|
+
instantParameter: (iso: string) => unknown;
|
|
80
88
|
}
|
|
81
89
|
export declare const DIALECT_CONFIG: Readonly<Record<SqlDialect, DialectConfig>>;
|
|
82
90
|
/**
|
package/dist/options.js
CHANGED
|
@@ -1,38 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// src/options.ts
|
|
3
|
-
/** The PostgreSQL type each declared value kind is cast to. `timestamptz` rather than `timestamp`: trilean's `instant` is an ISO-8601 string that may carry an offset, and parsing one as a naive `timestamp` would silently discard it. */
|
|
4
|
-
const POSTGRES_TYPE_NAME = {
|
|
5
|
-
text: "text",
|
|
6
|
-
number: "double precision",
|
|
7
|
-
boolean: "boolean",
|
|
8
|
-
timestamp: "timestamptz"
|
|
9
|
-
};
|
|
10
|
-
const DIALECT_CONFIG = {
|
|
11
|
-
postgres: {
|
|
12
|
-
matches: "~",
|
|
13
|
-
notMatches: "!~",
|
|
14
|
-
portableMatches: "~",
|
|
15
|
-
portableNotMatches: "!~",
|
|
16
|
-
placeholder: (index, castTo) => `$${String(index)}::${POSTGRES_TYPE_NAME[castTo]}`,
|
|
17
|
-
emptyMemberOfNullSuffix: "::boolean"
|
|
18
|
-
},
|
|
19
|
-
sqlite: {
|
|
20
|
-
matches: "REGEXP",
|
|
21
|
-
notMatches: "NOT REGEXP",
|
|
22
|
-
portableMatches: "GLOB",
|
|
23
|
-
portableNotMatches: "NOT GLOB",
|
|
24
|
-
placeholder: () => "?",
|
|
25
|
-
emptyMemberOfNullSuffix: ""
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Refuses a dialect this version does not implement, before anything indexes a per-dialect table with it.
|
|
30
|
-
*
|
|
31
|
-
* Both entry points check their own argument, because both are reachable with a name the type describes but cannot enforce -- a dialect is exactly the sort of value that arrives as a configuration string asserted into the union at the boundary. Left unchecked, that name is not caught anywhere: the compiler reads an operator off `undefined` and throws a `TypeError` naming an internal field, and the guard, whose tables are only consulted for a node it actually objects to, answers "pushable" for a tree the compiler then fails on -- the one thing `findUnpushableNodeKind` exists to decide, decided wrongly.
|
|
32
|
-
*
|
|
33
|
-
* `DIALECT_CONFIG` is the list, rather than a second constant, so implementing a dialect cannot leave this behind.
|
|
34
|
-
*/
|
|
35
|
-
function assertImplementedDialect(dialect) {
|
|
36
|
-
if (!Object.hasOwn(DIALECT_CONFIG, dialect)) throw new UnknownDialectError(dialect, Object.keys(DIALECT_CONFIG));
|
|
37
|
-
}
|
|
38
|
-
export { DIALECT_CONFIG, POSTGRES_TYPE_NAME, assertImplementedDialect };
|
|
1
|
+
export { t as DIALECT_CONFIG, n as POSTGRES_TYPE_NAME, r as assertImplementedDialect } from "./options-c1nHisgX.js";
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trilean-sql",
|
|
3
3
|
"description": "Compiles a trilean predicate tree into a parameterised SQL WHERE fragment, pushing three-valued logic down onto the database's own NULL propagation instead of evaluating it in process.",
|
|
4
|
-
"version": "2.2.
|
|
4
|
+
"version": "2.2.42",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/ExaDev/trilean/issues"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"trilean": "1.13.
|
|
10
|
-
"trilean-regex": "1.0.
|
|
9
|
+
"trilean": "1.13.12",
|
|
10
|
+
"trilean-regex": "1.0.24"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@arethetypeswrong/cli": "^0.18.5",
|