@crvouga/mockingbird-service-sqlite 0.1.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/AGENTS.md +170 -0
- package/COMPATIBILITY-AUDIT.md +236 -0
- package/COMPATIBILITY.md +102 -0
- package/LICENSE +21 -0
- package/README.md +405 -0
- package/compat/coverage.json +22231 -0
- package/compat/divergences.json +145 -0
- package/compat/fts-oracle-surface.json +198 -0
- package/compat/requirements.json +20929 -0
- package/compat/scenario-types.ts +63 -0
- package/compat/scenarios.ts +871 -0
- package/compat/smoke-baseline.json +3 -0
- package/compat/sqllogictest-skip.json +5 -0
- package/dist/api/database.d.ts +112 -0
- package/dist/api/snapshot.d.ts +21 -0
- package/dist/api/statement.d.ts +75 -0
- package/dist/ast/nodes.d.ts +476 -0
- package/dist/constraints/check.d.ts +11 -0
- package/dist/errors/index.d.ts +45 -0
- package/dist/executor/attach.d.ts +5 -0
- package/dist/executor/ddl.d.ts +10 -0
- package/dist/executor/dml.d.ts +7 -0
- package/dist/executor/env.d.ts +66 -0
- package/dist/executor/execute.d.ts +4 -0
- package/dist/executor/pragma-engine.d.ts +31 -0
- package/dist/executor/pragma.d.ts +4 -0
- package/dist/executor/result.d.ts +22 -0
- package/dist/executor/select.d.ts +7 -0
- package/dist/executor/simple-select.d.ts +9 -0
- package/dist/executor/triggers.d.ts +11 -0
- package/dist/executor/vtable.d.ts +12 -0
- package/dist/expressions/context.d.ts +30 -0
- package/dist/expressions/equals.d.ts +3 -0
- package/dist/expressions/eval.d.ts +12 -0
- package/dist/expressions/index.d.ts +3 -0
- package/dist/expressions/like.d.ts +16 -0
- package/dist/functions/aggregate.d.ts +8 -0
- package/dist/functions/datetime.d.ts +2 -0
- package/dist/functions/extensions.d.ts +7 -0
- package/dist/functions/index.d.ts +8 -0
- package/dist/functions/json.d.ts +6 -0
- package/dist/functions/math.d.ts +2 -0
- package/dist/functions/pragma-tvf.d.ts +8 -0
- package/dist/functions/registry.d.ts +39 -0
- package/dist/functions/scalar.d.ts +9 -0
- package/dist/functions/table-valued-registry.d.ts +11 -0
- package/dist/functions/table-valued.d.ts +13 -0
- package/dist/functions/window.d.ts +23 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +16010 -0
- package/dist/index.js.map +7 -0
- package/dist/indexes/index.d.ts +48 -0
- package/dist/indexes/keys.d.ts +15 -0
- package/dist/json/index.d.ts +7 -0
- package/dist/json/jsonb.d.ts +42 -0
- package/dist/json/ops.d.ts +24 -0
- package/dist/json/parse.d.ts +13 -0
- package/dist/json/path.d.ts +26 -0
- package/dist/json/stringify.d.ts +4 -0
- package/dist/json/tvf.d.ts +13 -0
- package/dist/json/types.d.ts +29 -0
- package/dist/lexer/tokenize.d.ts +23 -0
- package/dist/parser/index.d.ts +17 -0
- package/dist/parser/parser.d.ts +115 -0
- package/dist/planner/access.d.ts +50 -0
- package/dist/planner/index.d.ts +3 -0
- package/dist/runtime/assert.d.ts +11 -0
- package/dist/runtime/catch.d.ts +3 -0
- package/dist/runtime/clock.d.ts +14 -0
- package/dist/runtime/index.d.ts +3 -0
- package/dist/runtime/options.d.ts +25 -0
- package/dist/runtime/prng.d.ts +42 -0
- package/dist/schema/catalog.d.ts +14 -0
- package/dist/schema/master-sql.d.ts +10 -0
- package/dist/serialization/codec.d.ts +19 -0
- package/dist/serialization/index.d.ts +1 -0
- package/dist/serialization/wire.d.ts +61 -0
- package/dist/storage/columnar-slab.d.ts +58 -0
- package/dist/storage/database-state.d.ts +96 -0
- package/dist/storage/index.d.ts +6 -0
- package/dist/storage/row.d.ts +13 -0
- package/dist/storage/table.d.ts +119 -0
- package/dist/transactions/manager.d.ts +19 -0
- package/dist/types/collation.d.ts +4 -0
- package/dist/types/sqlite-atof.d.ts +4 -0
- package/dist/types/sqlite-real-format.d.ts +5 -0
- package/dist/types/strict.d.ts +6 -0
- package/dist/types/value.d.ts +88 -0
- package/dist/unstable.d.ts +20 -0
- package/dist/unstable.js +10343 -0
- package/dist/unstable.js.map +7 -0
- package/dist/vtable/fts/options.d.ts +34 -0
- package/dist/vtable/fts/porter.d.ts +2 -0
- package/dist/vtable/fts/query.d.ts +39 -0
- package/dist/vtable/fts/table.d.ts +78 -0
- package/dist/vtable/fts/tokenize.d.ts +20 -0
- package/dist/vtable/fts5.d.ts +1 -0
- package/dist/vtable/index.d.ts +2 -0
- package/dist/vtable/modules.d.ts +65 -0
- package/package.json +122 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Rowid } from "../storage/row.js";
|
|
2
|
+
import { type SqlValue } from "../types/value.js";
|
|
3
|
+
export declare class IndexStore {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
private entries;
|
|
6
|
+
private keyValues;
|
|
7
|
+
private sortedKeys;
|
|
8
|
+
private mapsShared;
|
|
9
|
+
frozen: boolean;
|
|
10
|
+
constructor(name?: string, entries?: ReadonlyMap<string, readonly Rowid[]>, keyValues?: ReadonlyMap<string, readonly SqlValue[]>);
|
|
11
|
+
checkUnique(values: readonly SqlValue[], rowid?: Rowid): void;
|
|
12
|
+
insert(values: readonly SqlValue[], rowid: Rowid, unique?: boolean): void;
|
|
13
|
+
lookup(values: readonly SqlValue[]): readonly Rowid[];
|
|
14
|
+
/** Leftmost prefix match: INDEX(a,b) used for WHERE a = ?. */
|
|
15
|
+
lookupPrefix(values: readonly SqlValue[]): readonly Rowid[];
|
|
16
|
+
/**
|
|
17
|
+
* Ordered scan of rowids for a single-column comparison.
|
|
18
|
+
* `op` applies to the first key component.
|
|
19
|
+
*/
|
|
20
|
+
rangeLookup(op: ">" | ">=" | "<" | "<=" | "between", bound: SqlValue, bound2?: SqlValue): readonly Rowid[];
|
|
21
|
+
orderedRowids(desc?: boolean): readonly Rowid[];
|
|
22
|
+
remove(values: readonly SqlValue[], rowid?: Rowid): boolean;
|
|
23
|
+
clear(): void;
|
|
24
|
+
clone(): IndexStore;
|
|
25
|
+
freeze(): void;
|
|
26
|
+
/** Sorted entries for SQLM v3 persistence. */
|
|
27
|
+
snapshotEntries(): Array<{
|
|
28
|
+
key: string;
|
|
29
|
+
rowids: Rowid[];
|
|
30
|
+
values: SqlValue[];
|
|
31
|
+
}>;
|
|
32
|
+
/** Sorted keys + rowids for SQLM v4 (values rebuilt from the table). */
|
|
33
|
+
snapshotKeys(): Array<{
|
|
34
|
+
key: string;
|
|
35
|
+
rowids: Rowid[];
|
|
36
|
+
}>;
|
|
37
|
+
rememberKeyValues(key: string, values: readonly SqlValue[]): void;
|
|
38
|
+
/** Key component values for snapshot encode (binary keys). */
|
|
39
|
+
keyValuesFor(key: string): SqlValue[];
|
|
40
|
+
get size(): number;
|
|
41
|
+
private orderedKeys;
|
|
42
|
+
private assertMutable;
|
|
43
|
+
private forkMaps;
|
|
44
|
+
}
|
|
45
|
+
/** Hash-join / covering-hash key: NULL components never match. */
|
|
46
|
+
export declare function serializeIndexKey(values: readonly SqlValue[]): string | null;
|
|
47
|
+
/** Index-store key: NULL is a distinct component so composite prefix lookups still hit. */
|
|
48
|
+
export declare function serializeIndexEntry(values: readonly SqlValue[]): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { IndexedColumn } from "../ast/nodes.js";
|
|
2
|
+
import type { Cell, ExecutionEnv } from "../executor/env.js";
|
|
3
|
+
import type { EvalContext } from "../expressions/context.js";
|
|
4
|
+
import type { IndexInfo } from "../storage/database-state.js";
|
|
5
|
+
import { type Row } from "../storage/row.js";
|
|
6
|
+
import type { Table } from "../storage/table.js";
|
|
7
|
+
import { type SqlValue } from "../types/value.js";
|
|
8
|
+
/** Heap cells for evaluating index expressions / partial `WHERE` via `ExecutionEnv`. */
|
|
9
|
+
export declare function heapRowCells(table: Table, row: Row, env?: ExecutionEnv): Cell[];
|
|
10
|
+
/** Indexed-column values for one heap row (expression indexes included when `ctx` is set). */
|
|
11
|
+
export declare function indexKeyValues(columns: readonly IndexedColumn[], row: Row, ctx?: EvalContext | null, table?: Table): SqlValue[];
|
|
12
|
+
/** Rebuild `index.store` from `table` using the same keys as DML maintenance. */
|
|
13
|
+
export declare function rebuildIndexFromTable(index: IndexInfo, table: Table, ctxForRow: (row: Row) => EvalContext): void;
|
|
14
|
+
/** Snapshot / no-`ExecutionEnv` evaluator for index expressions / partial predicates. */
|
|
15
|
+
export declare function tableRowEvalContext(table: Table, row: Row): EvalContext;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { decodeJsonb, encodeJsonb, jsonbToText, looksLikeJsonb, textToJsonb, walkJsonbTree, } from "./jsonb.js";
|
|
2
|
+
export { arrayInsertJson, ensureJson, extractOne, jsonArrowPath, jsonNodeToSql, mutateJson, patchJson, removeJson, sqlToJsonInput, sqlValueToJsonNode, toJsonbBlob, toJsonText, wrapJsonError, } from "./ops.js";
|
|
3
|
+
export { isValidJsonText, JsonParseError, jsonErrorPosition, parseJsonText } from "./parse.js";
|
|
4
|
+
export { parseJsonPath, pathGet } from "./path.js";
|
|
5
|
+
export { jsonQuoteString, prettyJson, stringifyJson } from "./stringify.js";
|
|
6
|
+
export { jsonEachRows, jsonTreeRows } from "./tvf.js";
|
|
7
|
+
export type { JsonNode, JsonTypeName } from "./types.js";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { JsonNode } from "./types.js";
|
|
2
|
+
/** JSONB element types (low nibble). */
|
|
3
|
+
export declare const JB: {
|
|
4
|
+
readonly NULL: 0;
|
|
5
|
+
readonly TRUE: 1;
|
|
6
|
+
readonly FALSE: 2;
|
|
7
|
+
readonly INT: 3;
|
|
8
|
+
readonly INT5: 4;
|
|
9
|
+
readonly FLOAT: 5;
|
|
10
|
+
readonly FLOAT5: 6;
|
|
11
|
+
readonly TEXT: 7;
|
|
12
|
+
readonly TEXTJ: 8;
|
|
13
|
+
readonly TEXT5: 9;
|
|
14
|
+
readonly TEXTRAW: 10;
|
|
15
|
+
readonly ARRAY: 11;
|
|
16
|
+
readonly OBJECT: 12;
|
|
17
|
+
};
|
|
18
|
+
export declare function encodeJsonb(node: JsonNode): Uint8Array;
|
|
19
|
+
export interface JsonbElement {
|
|
20
|
+
offset: number;
|
|
21
|
+
end: number;
|
|
22
|
+
type: number;
|
|
23
|
+
payloadOffset: number;
|
|
24
|
+
payloadSize: number;
|
|
25
|
+
}
|
|
26
|
+
export declare function readHeader(blob: Uint8Array, offset: number): JsonbElement;
|
|
27
|
+
export declare function looksLikeJsonb(blob: Uint8Array): boolean;
|
|
28
|
+
export declare function decodeJsonb(blob: Uint8Array): JsonNode;
|
|
29
|
+
/** Walk JSONB and collect element metadata for TVF id/parent assignment. */
|
|
30
|
+
export interface JsonbWalkEntry {
|
|
31
|
+
offset: number;
|
|
32
|
+
parent: number | null;
|
|
33
|
+
node: JsonNode;
|
|
34
|
+
key: string | number | null;
|
|
35
|
+
path: string;
|
|
36
|
+
fullkey: string;
|
|
37
|
+
/** For object entries, offset of the key header (used as id). */
|
|
38
|
+
idOffset: number;
|
|
39
|
+
}
|
|
40
|
+
export declare function walkJsonbTree(blob: Uint8Array): JsonbWalkEntry[];
|
|
41
|
+
export declare function jsonbToText(blob: Uint8Array): string;
|
|
42
|
+
export declare function textToJsonb(text: string): Uint8Array;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { SqlJsonText, type SqlValue } from "../types/value.js";
|
|
2
|
+
import { type JsonNode } from "./types.js";
|
|
3
|
+
export declare function sqlToJsonInput(value: SqlValue): JsonNode;
|
|
4
|
+
export declare function trySqlToJsonInput(value: SqlValue): JsonNode | null;
|
|
5
|
+
/** VALUE arg for insert/set/array/object — literal unless JSON subtype / JSONB. */
|
|
6
|
+
export declare function sqlValueToJsonNode(value: SqlValue): JsonNode;
|
|
7
|
+
export declare function jsonNodeToSql(node: JsonNode, mode: "sql" | "json" | "jsonb"): SqlValue;
|
|
8
|
+
export declare function jsonArrowPath(right: SqlValue): string;
|
|
9
|
+
export declare function extractOne(root: JsonNode, path: string): JsonNode | undefined;
|
|
10
|
+
export type MutateMode = "insert" | "replace" | "set";
|
|
11
|
+
/**
|
|
12
|
+
* json_array_insert / jsonb_array_insert — like json_replace, but PATH must end
|
|
13
|
+
* with an array index and the value is spliced in (existing elements shift right).
|
|
14
|
+
* https://www.sqlite.org/json1.html#jarrins
|
|
15
|
+
*/
|
|
16
|
+
export declare function arrayInsertJson(root: JsonNode, path: string, value: JsonNode): JsonNode;
|
|
17
|
+
export declare function mutateJson(root: JsonNode, path: string, value: JsonNode, mode: MutateMode): JsonNode;
|
|
18
|
+
export declare function removeJson(root: JsonNode, path: string): JsonNode;
|
|
19
|
+
/** RFC7396-ish JSON Merge Patch as implemented by SQLite json_patch. */
|
|
20
|
+
export declare function patchJson(target: JsonNode, patch: JsonNode): JsonNode;
|
|
21
|
+
export declare function wrapJsonError(e: unknown): never;
|
|
22
|
+
export declare function ensureJson(value: SqlValue): JsonNode;
|
|
23
|
+
export declare function toJsonbBlob(value: SqlValue): Uint8Array;
|
|
24
|
+
export declare function toJsonText(value: SqlValue): SqlJsonText;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SqliteError } from "../errors/index.js";
|
|
2
|
+
import type { JsonNode } from "./types.js";
|
|
3
|
+
export declare class JsonParseError extends Error {
|
|
4
|
+
readonly position: number;
|
|
5
|
+
constructor(message: string, position: number);
|
|
6
|
+
}
|
|
7
|
+
/** Parse JSON / JSON5-ish input into a JsonNode. Position is 1-based for errors. */
|
|
8
|
+
export declare function parseJsonText(input: string, options?: {
|
|
9
|
+
strictCanonical?: boolean;
|
|
10
|
+
}): JsonNode;
|
|
11
|
+
export declare function jsonErrorPosition(input: string): number;
|
|
12
|
+
export declare function isValidJsonText(input: string, flags?: number): boolean;
|
|
13
|
+
export declare function malformedJsonError(position?: number): SqliteError;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { JsonNode } from "./types.js";
|
|
2
|
+
export type PathStep = {
|
|
3
|
+
kind: "root";
|
|
4
|
+
} | {
|
|
5
|
+
kind: "key";
|
|
6
|
+
key: string;
|
|
7
|
+
} | {
|
|
8
|
+
kind: "index";
|
|
9
|
+
index: number;
|
|
10
|
+
} | {
|
|
11
|
+
kind: "append";
|
|
12
|
+
} | {
|
|
13
|
+
kind: "fromEnd";
|
|
14
|
+
n: number;
|
|
15
|
+
};
|
|
16
|
+
export declare function parseJsonPath(path: string): PathStep[];
|
|
17
|
+
/** Resolve path for read; returns undefined if missing. */
|
|
18
|
+
export declare function pathGet(root: JsonNode, steps: PathStep[]): JsonNode | undefined;
|
|
19
|
+
export interface PathParent {
|
|
20
|
+
parent: JsonNode;
|
|
21
|
+
step: PathStep;
|
|
22
|
+
/** For object: entry index; for array: element index (may be length for append). */
|
|
23
|
+
index: number;
|
|
24
|
+
}
|
|
25
|
+
/** Locate parent container for the final path step (for mutate). */
|
|
26
|
+
export declare function pathParent(root: JsonNode, steps: PathStep[]): PathParent | null;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SqlValue } from "../types/value.js";
|
|
2
|
+
export interface JsonTvfRow {
|
|
3
|
+
key: SqlValue;
|
|
4
|
+
value: SqlValue;
|
|
5
|
+
type: string;
|
|
6
|
+
atom: SqlValue;
|
|
7
|
+
id: number;
|
|
8
|
+
parent: SqlValue;
|
|
9
|
+
fullkey: string;
|
|
10
|
+
path: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function jsonEachRows(json: SqlValue, path?: SqlValue): JsonTvfRow[];
|
|
13
|
+
export declare function jsonTreeRows(json: SqlValue, path?: SqlValue): JsonTvfRow[];
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Internal JSON value tree used by sqlite-mem JSON1/JSONB. */
|
|
2
|
+
export type JsonNode = {
|
|
3
|
+
kind: "null";
|
|
4
|
+
} | {
|
|
5
|
+
kind: "true";
|
|
6
|
+
} | {
|
|
7
|
+
kind: "false";
|
|
8
|
+
} | {
|
|
9
|
+
kind: "integer";
|
|
10
|
+
text: string;
|
|
11
|
+
} | {
|
|
12
|
+
kind: "real";
|
|
13
|
+
text: string;
|
|
14
|
+
} | {
|
|
15
|
+
kind: "string";
|
|
16
|
+
value: string;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "array";
|
|
19
|
+
elements: JsonNode[];
|
|
20
|
+
} | {
|
|
21
|
+
kind: "object";
|
|
22
|
+
entries: Array<{
|
|
23
|
+
key: string;
|
|
24
|
+
value: JsonNode;
|
|
25
|
+
}>;
|
|
26
|
+
};
|
|
27
|
+
export type JsonTypeName = "null" | "true" | "false" | "integer" | "real" | "text" | "array" | "object";
|
|
28
|
+
export declare function jsonTypeName(node: JsonNode): JsonTypeName;
|
|
29
|
+
export declare function cloneJson(node: JsonNode): JsonNode;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Lexer token kind: keywords, literals, operators, and punctuation. */
|
|
2
|
+
export type TokenKind = "EOF" | "IDENT" | "STRING" | "NUMBER" | "BLOB" | "PARAM_POS" | "PARAM_NAMED" | "DOT" | "COMMA" | "SEMI" | "LPAREN" | "RPAREN" | "PLUS" | "MINUS" | "STAR" | "SLASH" | "PERCENT" | "EQ" | "EQEQ" | "NE" | "LT" | "LE" | "GT" | "GE" | "CONCAT" | "AMP" | "PIPE" | "LSHIFT" | "RSHIFT" | "JSON_ARROW" | "JSON_ARROW2" | "TILDE" | "ABORT" | "ACTION" | "ADD" | "AFTER" | "ALL" | "ALTER" | "ANALYZE" | "AND" | "AS" | "ASC" | "ATTACH" | "AUTOINCREMENT" | "BEFORE" | "BEGIN" | "BETWEEN" | "BY" | "CASCADE" | "CASE" | "CAST" | "CHECK" | "COLLATE" | "COLUMN" | "COMMIT" | "CONFLICT" | "CONSTRAINT" | "CREATE" | "CROSS" | "CURRENT" | "CURRENT_DATE" | "CURRENT_TIME" | "CURRENT_TIMESTAMP" | "DATABASE" | "DEFAULT" | "DEFERRABLE" | "DEFERRED" | "DELETE" | "DESC" | "DETACH" | "DISTINCT" | "DO" | "DROP" | "EACH" | "ELSE" | "END" | "ESCAPE" | "EXCEPT" | "EXCLUDE" | "EXCLUSIVE" | "EXISTS" | "EXPLAIN" | "FAIL" | "FILTER" | "FIRST" | "FOLLOWING" | "FOR" | "FOREIGN" | "FROM" | "FULL" | "FALSE" | "GENERATED" | "GLOB" | "GROUP" | "GROUPS" | "HAVING" | "IF" | "IGNORE" | "IMMEDIATE" | "IN" | "INDEX" | "INDEXED" | "INITIALLY" | "INNER" | "INSERT" | "INSTEAD" | "INTERSECT" | "INTO" | "IS" | "ISNULL" | "JOIN" | "KEY" | "LAST" | "LEFT" | "LIKE" | "LIMIT" | "MATCH" | "MATERIALIZED" | "NATURAL" | "NO" | "NOT" | "NOTHING" | "NOTNULL" | "NULL" | "NULLS" | "OF" | "OFFSET" | "ON" | "OR" | "ORDER" | "OTHERS" | "OUTER" | "OVER" | "PARTITION" | "PLAN" | "PRAGMA" | "PRECEDING" | "PRIMARY" | "QUERY" | "RAISE" | "RANGE" | "RECURSIVE" | "REFERENCES" | "REGEXP" | "REINDEX" | "RELEASE" | "RENAME" | "REPLACE" | "RESTRICT" | "RESPECT" | "RETURNING" | "RIGHT" | "ROLLBACK" | "ROW" | "ROWS" | "SAVEPOINT" | "SELECT" | "SET" | "TABLE" | "TEMP" | "TEMPORARY" | "THEN" | "TIES" | "TO" | "TRANSACTION" | "TRIGGER" | "TRUE" | "UNBOUNDED" | "UNION" | "UNIQUE" | "UPDATE" | "USING" | "VACUUM" | "VALUES" | "VIEW" | "VIRTUAL" | "WHEN" | "WHERE" | "WINDOW" | "WITH" | "WITHOUT";
|
|
3
|
+
/** A single lexer token from {@link tokenize}. */
|
|
4
|
+
export interface Token {
|
|
5
|
+
kind: TokenKind;
|
|
6
|
+
value: string;
|
|
7
|
+
/** For NUMBER/BLOB/STRING, the parsed value */
|
|
8
|
+
literal?: string | number | bigint | Uint8Array | null;
|
|
9
|
+
/** True when a NUMBER token used float/scientific syntax. */
|
|
10
|
+
forceReal?: boolean;
|
|
11
|
+
index?: number;
|
|
12
|
+
line: number;
|
|
13
|
+
column: number;
|
|
14
|
+
start: number;
|
|
15
|
+
end: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Lex `input` into SQLite tokens (keywords, literals, operators, parameters).
|
|
19
|
+
*
|
|
20
|
+
* @param input - SQL source text.
|
|
21
|
+
*/
|
|
22
|
+
export declare function tokenize(input: string): Token[];
|
|
23
|
+
export declare function isKeyword(kind: TokenKind): boolean;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Statement as ParsedStatement } from "../ast/nodes.js";
|
|
2
|
+
import { type ParsedUnit } from "./parser.js";
|
|
3
|
+
/** Parsed SQL statement AST (`SELECT`, `INSERT`, `CREATE TABLE`, …). */
|
|
4
|
+
export type { Statement as ParsedStatement } from "../ast/nodes.js";
|
|
5
|
+
export type { ParsedUnit } from "./parser.js";
|
|
6
|
+
export { parseTokens, parseTokenUnits } from "./parser.js";
|
|
7
|
+
/**
|
|
8
|
+
* Tokenize `sql` and parse every semicolon-separated statement.
|
|
9
|
+
*
|
|
10
|
+
* @param sql - SQLite SQL text.
|
|
11
|
+
* @returns AST statements in source order.
|
|
12
|
+
*/
|
|
13
|
+
export declare function parse(sql: string): ParsedStatement[];
|
|
14
|
+
/**
|
|
15
|
+
* Parse SQL into AST + per-statement source slices (for `sqlite_master.sql`).
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseUnits(sql: string): ParsedUnit[];
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { Expr, SelectStmt, Statement, WithClause } from "../ast/nodes.js";
|
|
2
|
+
import type { Token } from "../lexer/tokenize.js";
|
|
3
|
+
export interface ParsedUnit {
|
|
4
|
+
statement: Statement;
|
|
5
|
+
/** Source slice for this statement (no trailing semicolon); used for sqlite_master.sql. */
|
|
6
|
+
sql: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class Parser {
|
|
9
|
+
private readonly tokens;
|
|
10
|
+
private readonly source;
|
|
11
|
+
private pos;
|
|
12
|
+
constructor(tokens: Token[], source?: string);
|
|
13
|
+
private current;
|
|
14
|
+
private peek;
|
|
15
|
+
private at;
|
|
16
|
+
private check;
|
|
17
|
+
private advance;
|
|
18
|
+
private match;
|
|
19
|
+
private expect;
|
|
20
|
+
syntaxError(message: string): never;
|
|
21
|
+
/** Accept IDENT or keyword tokens as identifier names. */
|
|
22
|
+
parseIdent(): string;
|
|
23
|
+
private parseQualifiedTableName;
|
|
24
|
+
private parseTableName;
|
|
25
|
+
private parseAttachStmt;
|
|
26
|
+
private parseDetachStmt;
|
|
27
|
+
private optionalAlias;
|
|
28
|
+
parseStatements(): Statement[];
|
|
29
|
+
/** Parse statements with per-statement source slices for catalog `sql` text. */
|
|
30
|
+
parseUnits(): ParsedUnit[];
|
|
31
|
+
parseStatement(): Statement;
|
|
32
|
+
private parseAnalyzeStmt;
|
|
33
|
+
private parseReindexStmt;
|
|
34
|
+
private parseVacuumStmt;
|
|
35
|
+
private parseWithClause;
|
|
36
|
+
private parseOptionalWith;
|
|
37
|
+
parseSelectStmt(withClause?: WithClause | null): SelectStmt;
|
|
38
|
+
private parseSelectCore;
|
|
39
|
+
private parseCompoundTail;
|
|
40
|
+
private parseResultColumns;
|
|
41
|
+
private isSelectClauseStart;
|
|
42
|
+
private parseFromItem;
|
|
43
|
+
private makeJoin;
|
|
44
|
+
private parseJoinOp;
|
|
45
|
+
private parseFromPrimary;
|
|
46
|
+
private parseValuesAsSelect;
|
|
47
|
+
private parseOrderBy;
|
|
48
|
+
private parseLimit;
|
|
49
|
+
private parseInsertStmt;
|
|
50
|
+
private parseUpsertClause;
|
|
51
|
+
private parseSetItems;
|
|
52
|
+
private parseReturning;
|
|
53
|
+
private parseUpdateStmt;
|
|
54
|
+
private parseDeleteStmt;
|
|
55
|
+
private parseCreateStmt;
|
|
56
|
+
private parseCreateVirtualTable;
|
|
57
|
+
/** One comma-separated CREATE VIRTUAL TABLE module argument (FTS options, columns, …). */
|
|
58
|
+
private parseVirtualTableArg;
|
|
59
|
+
private parseIdentOrQuoted;
|
|
60
|
+
private parseVirtualTableOptionValue;
|
|
61
|
+
private parseIfNotExists;
|
|
62
|
+
private parseIfExists;
|
|
63
|
+
private mapUpdateOr;
|
|
64
|
+
private parseCreateTable;
|
|
65
|
+
private parseColumnDef;
|
|
66
|
+
/** Type names may include optional precision/scale: VARCHAR(10), DECIMAL(10,2). */
|
|
67
|
+
private parseTypeName;
|
|
68
|
+
private isColumnConstraintStart;
|
|
69
|
+
private parseColumnConstraint;
|
|
70
|
+
private parseGeneratedConstraint;
|
|
71
|
+
private parseReferencesConstraint;
|
|
72
|
+
private parseTableConstraint;
|
|
73
|
+
private parseIndexedColumns;
|
|
74
|
+
private parseOnConflict;
|
|
75
|
+
private parseConflictAction;
|
|
76
|
+
private parseFkAction;
|
|
77
|
+
private parseFkActions;
|
|
78
|
+
private parseDeferrable;
|
|
79
|
+
private parseOrConflict;
|
|
80
|
+
private parseCreateIndex;
|
|
81
|
+
private parseCreateView;
|
|
82
|
+
private parseCreateTrigger;
|
|
83
|
+
private parseTriggerBody;
|
|
84
|
+
private parseDropStmt;
|
|
85
|
+
private parseDropTable;
|
|
86
|
+
private parseDropIndex;
|
|
87
|
+
private parseDropView;
|
|
88
|
+
private parseDropTrigger;
|
|
89
|
+
private parseAlterTableStmt;
|
|
90
|
+
private parseBeginStmt;
|
|
91
|
+
private parseCommitStmt;
|
|
92
|
+
private parseRollbackStmt;
|
|
93
|
+
private parseSavepointStmt;
|
|
94
|
+
private parseReleaseStmt;
|
|
95
|
+
private parsePragmaStmt;
|
|
96
|
+
parseExpr(): Expr;
|
|
97
|
+
private parseExprPrec;
|
|
98
|
+
private peekBinaryOp;
|
|
99
|
+
private parseIsRhs;
|
|
100
|
+
private parseInRhs;
|
|
101
|
+
private parseLikeRhs;
|
|
102
|
+
private parsePostfix;
|
|
103
|
+
private isNotKeywordCompound;
|
|
104
|
+
private parseUnaryExpr;
|
|
105
|
+
private parsePrimaryExpr;
|
|
106
|
+
private parseIdentOrFunction;
|
|
107
|
+
private finishCall;
|
|
108
|
+
private parseCaseExpr;
|
|
109
|
+
private parseWindowSpec;
|
|
110
|
+
private parseFrameSpec;
|
|
111
|
+
private parseFrameBound;
|
|
112
|
+
}
|
|
113
|
+
export declare function parseTokens(tokens: Token[], source?: string): Statement[];
|
|
114
|
+
/** Parse SQL into AST statements paired with source slices. */
|
|
115
|
+
export declare function parseTokenUnits(tokens: Token[], source: string): ParsedUnit[];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Expr, FromItem, TableRef } from "../ast/nodes.js";
|
|
2
|
+
import type { ExecutionEnv } from "../executor/env.js";
|
|
3
|
+
import type { Row } from "../storage/row.js";
|
|
4
|
+
import type { Table } from "../storage/table.js";
|
|
5
|
+
import type { SqlValue } from "../types/value.js";
|
|
6
|
+
export interface ConstEquality {
|
|
7
|
+
table: string | null;
|
|
8
|
+
column: string;
|
|
9
|
+
valueExpr: Expr;
|
|
10
|
+
}
|
|
11
|
+
export declare function conjunctions(expr: Expr): Expr[];
|
|
12
|
+
export declare function equalityAgainstConst(expr: Expr): ConstEquality | null;
|
|
13
|
+
export declare function equalityAgainstExpr(expr: Expr): {
|
|
14
|
+
expr: Expr;
|
|
15
|
+
valueExpr: Expr;
|
|
16
|
+
} | null;
|
|
17
|
+
export declare function whereFullyCovered(where: Expr, coveredColumns: Set<string>, alias: string, tableName: string): boolean;
|
|
18
|
+
export interface TableRowLookup {
|
|
19
|
+
table: Table;
|
|
20
|
+
alias: string;
|
|
21
|
+
rows: Row[];
|
|
22
|
+
/** Lowercased columns whose equality predicates were applied by the access path. */
|
|
23
|
+
coveredColumns: Set<string>;
|
|
24
|
+
}
|
|
25
|
+
/** Look up rows for a simple `FROM table WHERE ...` using PK, a covering equality index, or an automatic hash. */
|
|
26
|
+
export declare function tryIndexedTableRows(from: FromItem, where: Expr, env: ExecutionEnv): TableRowLookup | null;
|
|
27
|
+
export declare function lookupTableRows(from: TableRef, where: Expr, env: ExecutionEnv): TableRowLookup | null;
|
|
28
|
+
export declare function tryIndexedOrder(from: FromItem, order: {
|
|
29
|
+
expr: Expr;
|
|
30
|
+
dir: "ASC" | "DESC";
|
|
31
|
+
}, env: ExecutionEnv, limit?: number): TableRowLookup | null;
|
|
32
|
+
export interface JoinProbe {
|
|
33
|
+
table: Table;
|
|
34
|
+
alias: string;
|
|
35
|
+
/** When true, ON is fully applied by the probe (skip re-eval). */
|
|
36
|
+
covering: boolean;
|
|
37
|
+
lookup: (leftCellsValue: (table: string | null, name: string) => SqlValue) => Row[];
|
|
38
|
+
}
|
|
39
|
+
/** Equality join keys extracted from ON (column = column), for hash join when no index probe. */
|
|
40
|
+
export interface JoinEqualityKeys {
|
|
41
|
+
/** Right-side column names in join-key order. */
|
|
42
|
+
rightColumns: string[];
|
|
43
|
+
/** Corresponding left-side (probe) column references. */
|
|
44
|
+
leftKeys: {
|
|
45
|
+
table: string | null;
|
|
46
|
+
column: string;
|
|
47
|
+
}[];
|
|
48
|
+
}
|
|
49
|
+
export declare function tryJoinEqualityKeys(right: FromItem, on: Expr | null, env?: ExecutionEnv): JoinEqualityKeys | null;
|
|
50
|
+
export declare function tryJoinProbe(right: FromItem, on: Expr | null, env: ExecutionEnv): JoinProbe | null;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Row } from "../storage/row.js";
|
|
2
|
+
import type { Table } from "../storage/table.js";
|
|
3
|
+
/** Always-on invariant check (Tiger Style — not stripped in production). */
|
|
4
|
+
export declare function assert(condition: boolean, message: string): asserts condition;
|
|
5
|
+
/** Exhaustive switch helper — call when a union case should be impossible. */
|
|
6
|
+
export declare function assertUnreachable(value: never, message?: string): never;
|
|
7
|
+
/** Row payload must align with table column count after insert/update. */
|
|
8
|
+
export declare function assertRowShape(table: Table, row: Row): void;
|
|
9
|
+
/** Maximum blob/string length enforced before allocation (matches bun:sqlite TOOBIG). */
|
|
10
|
+
export declare const SQLITE_MAX_LENGTH = 2147483647;
|
|
11
|
+
export declare function assertBlobLength(length: number, _feature: string): void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Fixed reference instant used when callers do not supply a clock (`2000-01-01T00:00:00.000Z`). */
|
|
2
|
+
export declare const DEFAULT_NOW: Date;
|
|
3
|
+
/** Function that returns the current instant for `date('now')` and related builtins. */
|
|
4
|
+
export type Clock = () => Date;
|
|
5
|
+
/**
|
|
6
|
+
* Clock that always returns a copy of `instant` (default {@link DEFAULT_NOW}).
|
|
7
|
+
*
|
|
8
|
+
* @throws {RangeError} If `instant` is an invalid `Date`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function fixedClock(instant?: Date): Clock;
|
|
11
|
+
/** Wall-clock `'now'` matching SQLite (`new Date()` each call). */
|
|
12
|
+
export declare function systemClock(): Clock;
|
|
13
|
+
/** Normalize a `Date`, {@link Clock}, `"system"`, or `undefined` into a {@link Clock}. */
|
|
14
|
+
export declare function resolveClock(now?: Date | Clock | "system"): Clock;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Clock } from "./clock.js";
|
|
2
|
+
/** Entropy source for `random()` / `randomblob()`. Default is seeded xorshift64*. */
|
|
3
|
+
export type RandomMode = "deterministic" | "os";
|
|
4
|
+
/** Options for {@link Database} construction. All fields are optional. */
|
|
5
|
+
export interface DatabaseOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Seed for deterministic `random()` (and any other PRNG-backed builtins).
|
|
8
|
+
* Defaults to {@link DEFAULT_DATABASE_SEED} (`1`). Ignored when {@link random} is `"os"`.
|
|
9
|
+
*/
|
|
10
|
+
seed?: number | bigint;
|
|
11
|
+
/**
|
|
12
|
+
* Entropy for `random()` / `randomblob()`.
|
|
13
|
+
* `"deterministic"` (default) uses {@link seed}. `"os"` uses CSPRNG like SQLite
|
|
14
|
+
* (not rolled back with transactions; not restored from snapshots).
|
|
15
|
+
*/
|
|
16
|
+
random?: RandomMode;
|
|
17
|
+
/**
|
|
18
|
+
* Clock for `date('now')` / `datetime('now')` / etc.
|
|
19
|
+
* Defaults to a fixed instant (`2000-01-01T00:00:00.000Z`).
|
|
20
|
+
* Pass a `Date`, `() => Date`, or `"system"` for wall-clock `'now'` like SQLite.
|
|
21
|
+
*/
|
|
22
|
+
now?: Date | Clock | "system";
|
|
23
|
+
}
|
|
24
|
+
/** Default {@link DatabaseOptions.seed} when constructing a {@link Database}. */
|
|
25
|
+
export declare const DEFAULT_DATABASE_SEED = 1;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic 64-bit PRNG (xorshift64*).
|
|
3
|
+
* Used for SQLite `random()` and any other nondeterministic builtins.
|
|
4
|
+
*/
|
|
5
|
+
export declare class Prng {
|
|
6
|
+
private state;
|
|
7
|
+
/**
|
|
8
|
+
* @param seed - Engine seed. `0` is replaced with a non-zero constant.
|
|
9
|
+
*/
|
|
10
|
+
constructor(seed?: number | bigint);
|
|
11
|
+
/** Next unsigned 64-bit value. */
|
|
12
|
+
nextU64(): bigint;
|
|
13
|
+
/** SQLite `random()` style signed 64-bit integer in [-2^63+1, 2^63-1]. */
|
|
14
|
+
nextSqliteRandom(): bigint;
|
|
15
|
+
/** Next float in `[0, 1)` from 53 bits of {@link nextU64}. */
|
|
16
|
+
nextFloat(): number;
|
|
17
|
+
/**
|
|
18
|
+
* Next integer in `[min, max]` (inclusive).
|
|
19
|
+
*
|
|
20
|
+
* @throws {RangeError} If `max < min`.
|
|
21
|
+
*/
|
|
22
|
+
nextInt(min: number, max: number): number;
|
|
23
|
+
/** Current unsigned 64-bit engine state (for snapshot / transaction rollback). */
|
|
24
|
+
getState(): bigint;
|
|
25
|
+
/** Restore unsigned 64-bit engine state from {@link getState}. */
|
|
26
|
+
setState(state: bigint): void;
|
|
27
|
+
/** Independent copy with the same engine state. */
|
|
28
|
+
clone(): Prng;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* CSPRNG-backed entropy for `random()` / `randomblob()` when {@link DatabaseOptions.random}
|
|
32
|
+
* is `"os"`. `setState` is a no-op so ROLLBACK / snapshot restore do not rewind draws.
|
|
33
|
+
*/
|
|
34
|
+
export declare class OsEntropy extends Prng {
|
|
35
|
+
constructor();
|
|
36
|
+
nextU64(): bigint;
|
|
37
|
+
getState(): bigint;
|
|
38
|
+
setState(_state: bigint): void;
|
|
39
|
+
clone(): Prng;
|
|
40
|
+
}
|
|
41
|
+
/** FNV-1a hash of `parts` as a signed 32-bit seed (for tests and custom PRNGs). */
|
|
42
|
+
export declare function deriveSeed(...parts: Array<number | string | bigint>): number;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DatabaseState } from "../storage/database-state.js";
|
|
2
|
+
import { Table } from "../storage/table.js";
|
|
3
|
+
export type SchemaObjectType = "table" | "index" | "view" | "trigger";
|
|
4
|
+
export interface SchemaCatalogRow {
|
|
5
|
+
type: SchemaObjectType;
|
|
6
|
+
name: string;
|
|
7
|
+
tbl_name: string;
|
|
8
|
+
rootpage: number;
|
|
9
|
+
sql: string | null;
|
|
10
|
+
}
|
|
11
|
+
export declare function schemaCatalogRows(state: DatabaseState): SchemaCatalogRow[];
|
|
12
|
+
export declare function buildSchemaCatalog(state: DatabaseState, name?: string): Table;
|
|
13
|
+
export declare function buildSqliteSchema(state: DatabaseState): Table;
|
|
14
|
+
export declare function buildSqliteMaster(state: DatabaseState): Table;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize CREATE source toward SQLite's `sqlite_master.sql` conventions:
|
|
3
|
+
* strip TEMP/TEMPORARY, IF NOT EXISTS, and schema qualifiers on the object name.
|
|
4
|
+
* Body text (from after the object name) is preserved byte-for-byte from the input.
|
|
5
|
+
*/
|
|
6
|
+
export declare function normalizeMasterSql(sql: string): string;
|
|
7
|
+
/** Append an ALTER TABLE ADD COLUMN definition into CREATE TABLE master sql (SQLite style). */
|
|
8
|
+
export declare function appendAddColumnToMasterSql(originalSql: string | null, alterSql: string | null): string | null;
|
|
9
|
+
/** Synthesize CTAS catalog SQL: `CREATE TABLE name(col1, col2, ...)`. */
|
|
10
|
+
export declare function synthesizeCtasMasterSql(tableName: string, columnNames: string[]): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DatabaseState } from "../storage/database-state.js";
|
|
2
|
+
/** PRNG + clock captured alongside schema/rows. */
|
|
3
|
+
export interface SnapshotRuntime {
|
|
4
|
+
prngState: bigint;
|
|
5
|
+
nowMs: number;
|
|
6
|
+
}
|
|
7
|
+
/** Result of {@link decodeDatabaseState}. */
|
|
8
|
+
export interface DecodedSnapshot {
|
|
9
|
+
state: DatabaseState;
|
|
10
|
+
runtime: SnapshotRuntime;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Encode catalog, rows, and runtime into a sqlite-mem snapshot blob (not a `.sqlite` file).
|
|
14
|
+
*/
|
|
15
|
+
export declare function encodeDatabaseState(state: DatabaseState, runtime: SnapshotRuntime): Uint8Array;
|
|
16
|
+
/**
|
|
17
|
+
* Decode a blob from {@link encodeDatabaseState} / {@link Database.snapshot}.
|
|
18
|
+
*/
|
|
19
|
+
export declare function decodeDatabaseState(snapshot: Uint8Array): DecodedSnapshot;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { type DecodedSnapshot, decodeDatabaseState, encodeDatabaseState, type SnapshotRuntime, } from "./codec.js";
|