@crvouga/sqlite-mem 1.1.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.
@@ -1,5 +1,5 @@
1
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" | "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" | "RETURNING" | "RIGHT" | "ROLLBACK" | "ROW" | "ROWS" | "SAVEPOINT" | "SELECT" | "SET" | "TABLE" | "TEMP" | "TEMPORARY" | "THEN" | "TIES" | "TO" | "TRANSACTION" | "TRIGGER" | "UNBOUNDED" | "UNION" | "UNIQUE" | "UPDATE" | "USING" | "VACUUM" | "VALUES" | "VIEW" | "VIRTUAL" | "WHEN" | "WHERE" | "WINDOW" | "WITH" | "WITHOUT";
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" | "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
3
  /** A single lexer token from {@link tokenize}. */
4
4
  export interface Token {
5
5
  kind: TokenKind;
@@ -1,4 +1,4 @@
1
- import type { Expr, SelectStmt, Statement } from "../ast/nodes.js";
1
+ import type { Expr, SelectStmt, Statement, WithClause } from "../ast/nodes.js";
2
2
  import type { Token } from "../lexer/tokenize.js";
3
3
  export declare class Parser {
4
4
  private readonly tokens;
@@ -26,7 +26,7 @@ export declare class Parser {
26
26
  private parseVacuumStmt;
27
27
  private parseWithClause;
28
28
  private parseOptionalWith;
29
- parseSelectStmt(): SelectStmt;
29
+ parseSelectStmt(withClause?: WithClause | null): SelectStmt;
30
30
  private parseSelectCore;
31
31
  private parseCompoundTail;
32
32
  private parseResultColumns;
@@ -8,5 +8,7 @@ export type Clock = () => Date;
8
8
  * @throws {RangeError} If `instant` is an invalid `Date`.
9
9
  */
10
10
  export declare function fixedClock(instant?: Date): Clock;
11
- /** Normalize a `Date`, {@link Clock}, or `undefined` into a {@link Clock}. */
12
- export declare function resolveClock(now?: Date | Clock): 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;
@@ -1,3 +1,3 @@
1
- export { type Clock, DEFAULT_NOW, fixedClock, resolveClock } from "./clock.js";
2
- export { type DatabaseOptions, DEFAULT_DATABASE_SEED } from "./options.js";
3
- export { deriveSeed, Prng } from "./prng.js";
1
+ export { type Clock, DEFAULT_NOW, fixedClock, resolveClock, systemClock } from "./clock.js";
2
+ export { type DatabaseOptions, DEFAULT_DATABASE_SEED, type RandomMode } from "./options.js";
3
+ export { deriveSeed, OsEntropy, Prng } from "./prng.js";
@@ -1,17 +1,25 @@
1
1
  import type { Clock } from "./clock.js";
2
+ /** Entropy source for `random()` / `randomblob()`. Default is seeded xorshift64*. */
3
+ export type RandomMode = "deterministic" | "os";
2
4
  /** Options for {@link Database} construction. All fields are optional. */
3
5
  export interface DatabaseOptions {
4
6
  /**
5
7
  * Seed for deterministic `random()` (and any other PRNG-backed builtins).
6
- * Defaults to {@link DEFAULT_DATABASE_SEED} (`1`).
8
+ * Defaults to {@link DEFAULT_DATABASE_SEED} (`1`). Ignored when {@link random} is `"os"`.
7
9
  */
8
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;
9
17
  /**
10
18
  * Clock for `date('now')` / `datetime('now')` / etc.
11
19
  * Defaults to a fixed instant (`2000-01-01T00:00:00.000Z`).
12
- * Pass a `Date` or `() => Date` to override.
20
+ * Pass a `Date`, `() => Date`, or `"system"` for wall-clock `'now'` like SQLite.
13
21
  */
14
- now?: Date | Clock;
22
+ now?: Date | Clock | "system";
15
23
  }
16
24
  /** Default {@link DatabaseOptions.seed} when constructing a {@link Database}. */
17
25
  export declare const DEFAULT_DATABASE_SEED = 1;
@@ -27,5 +27,16 @@ export declare class Prng {
27
27
  /** Independent copy with the same engine state. */
28
28
  clone(): Prng;
29
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
+ }
30
41
  /** FNV-1a hash of `parts` as a signed 32-bit seed (for tests and custom PRNGs). */
31
42
  export declare function deriveSeed(...parts: Array<number | string | bigint>): number;
@@ -48,6 +48,8 @@ export declare class DatabaseState {
48
48
  changes: number;
49
49
  totalChanges: number;
50
50
  foreignKeysEnabled: boolean;
51
+ /** When true, LIKE / like() are case-sensitive (SQLite `PRAGMA case_sensitive_like`). */
52
+ caseSensitiveLike: boolean;
51
53
  schemaVersion: number;
52
54
  userVersion: number;
53
55
  databaseForSchema(schema: string | null, qualifiedForError?: string): DatabaseState;
@@ -52,6 +52,8 @@ export declare class Table {
52
52
  private scanCache;
53
53
  /** Lazy covering hashes: column nameLower → serializeIndexKey → rowids. */
54
54
  private equalityHashes;
55
+ /** Cached maximum rowid. `undefined` means recompute after deleting the maximum. */
56
+ private maximumRowid;
55
57
  frozen: boolean;
56
58
  constructor(name: string, columns: ColumnInfo[], options?: TableOptions);
57
59
  integerPkColumn(): ColumnInfo | undefined;
@@ -56,6 +56,11 @@ export declare function typeofSql(value: SqlValue): string;
56
56
  export declare function affinityFromTypeName(typeName: string | null | undefined): Affinity;
57
57
  /** Apply column {@link Affinity} conversion rules to `value`. */
58
58
  export declare function applyAffinity(value: SqlValue, affinity: Affinity): SqlValue;
59
+ /**
60
+ * Apply SQLite comparison affinity rules before comparing two values.
61
+ * A null affinity represents an expression with no affinity.
62
+ */
63
+ export declare function applyComparisonAffinity(left: SqlValue, right: SqlValue, leftAffinity: Affinity | null, rightAffinity: Affinity | null): [SqlValue, SqlValue];
59
64
  /** Parse `value` as a number, or `null` when it is not numeric. */
60
65
  export declare function coerceToNumber(value: SqlValue): number | null;
61
66
  /** Truncate `value` toward zero as an integer, or `null` when it is not numeric. */
@@ -12,9 +12,9 @@ export type { Expr } from "./ast/nodes.js";
12
12
  export type { EvalContext } from "./expressions/context.js";
13
13
  export { evalExpr } from "./expressions/eval.js";
14
14
  export { globMatch, likeMatch } from "./expressions/like.js";
15
- export { tokenize, type Token, type TokenKind } from "./lexer/tokenize.js";
16
- export { parse, type ParsedStatement } from "./parser/index.js";
17
- export { type Clock, DEFAULT_DATABASE_SEED, DEFAULT_NOW, deriveSeed, fixedClock, Prng, resolveClock, } from "./runtime/index.js";
15
+ export { type Token, type TokenKind, tokenize } from "./lexer/tokenize.js";
16
+ export { type ParsedStatement, parse } from "./parser/index.js";
17
+ export { type Clock, DEFAULT_DATABASE_SEED, DEFAULT_NOW, deriveSeed, fixedClock, OsEntropy, Prng, type RandomMode, resolveClock, systemClock, } from "./runtime/index.js";
18
18
  export { type DecodedSnapshot, decodeDatabaseState, encodeDatabaseState, type SnapshotRuntime, } from "./serialization/index.js";
19
19
  export type { DatabaseState } from "./storage/database-state.js";
20
20
  export { type Affinity, affinityFromTypeName, applyAffinity, asSqlJsonText, asSqlReal, canonicalizeNumber, cloneSqlValue, coerceToNumber, compareSql, isSqlJsonText, isSqlReal, isTruthySql, SqlJsonText, SqlReal, type SqlValue, type StorageClass, sqlValueEquals, storageClassOf, toInteger, typeofSql, utf8Decode, utf8Encode, } from "./types/value.js";