@quereus/quereus 0.6.4 → 0.6.6
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 +19 -1
- package/dist/src/common/logger.d.ts +59 -0
- package/dist/src/common/logger.d.ts.map +1 -1
- package/dist/src/common/logger.js +68 -0
- package/dist/src/common/logger.js.map +1 -1
- package/dist/src/func/builtins/datetime.d.ts.map +1 -1
- package/dist/src/func/builtins/datetime.js +10 -5
- package/dist/src/func/builtins/datetime.js.map +1 -1
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/planner/building/constraint-builder.d.ts.map +1 -1
- package/dist/src/planner/building/constraint-builder.js +4 -0
- package/dist/src/planner/building/constraint-builder.js.map +1 -1
- package/dist/src/planner/building/delete.d.ts.map +1 -1
- package/dist/src/planner/building/delete.js +2 -1
- package/dist/src/planner/building/delete.js.map +1 -1
- package/dist/src/planner/building/insert.d.ts.map +1 -1
- package/dist/src/planner/building/insert.js +4 -1
- package/dist/src/planner/building/insert.js.map +1 -1
- package/dist/src/planner/building/update.d.ts.map +1 -1
- package/dist/src/planner/building/update.js +4 -2
- package/dist/src/planner/building/update.js.map +1 -1
- package/dist/src/planner/nodes/dml-executor-node.d.ts +8 -2
- package/dist/src/planner/nodes/dml-executor-node.d.ts.map +1 -1
- package/dist/src/planner/nodes/dml-executor-node.js +11 -2
- package/dist/src/planner/nodes/dml-executor-node.js.map +1 -1
- package/dist/src/planner/validation/determinism-validator.d.ts +47 -0
- package/dist/src/planner/validation/determinism-validator.d.ts.map +1 -0
- package/dist/src/planner/validation/determinism-validator.js +63 -0
- package/dist/src/planner/validation/determinism-validator.js.map +1 -0
- package/dist/src/runtime/async-util.d.ts.map +1 -1
- package/dist/src/runtime/async-util.js +4 -3
- package/dist/src/runtime/async-util.js.map +1 -1
- package/dist/src/runtime/emit/add-constraint.d.ts.map +1 -1
- package/dist/src/runtime/emit/add-constraint.js +3 -0
- package/dist/src/runtime/emit/add-constraint.js.map +1 -1
- package/dist/src/runtime/emit/dml-executor.d.ts.map +1 -1
- package/dist/src/runtime/emit/dml-executor.js +84 -8
- package/dist/src/runtime/emit/dml-executor.js.map +1 -1
- package/dist/src/runtime/emitters.d.ts.map +1 -1
- package/dist/src/runtime/emitters.js +2 -1
- package/dist/src/runtime/emitters.js.map +1 -1
- package/dist/src/runtime/types.d.ts +1 -0
- package/dist/src/runtime/types.d.ts.map +1 -1
- package/dist/src/runtime/types.js.map +1 -1
- package/dist/src/runtime/utils.d.ts +14 -0
- package/dist/src/runtime/utils.d.ts.map +1 -1
- package/dist/src/runtime/utils.js +40 -1
- package/dist/src/runtime/utils.js.map +1 -1
- package/dist/src/schema/manager.d.ts.map +1 -1
- package/dist/src/schema/manager.js +50 -0
- package/dist/src/schema/manager.js.map +1 -1
- package/dist/src/util/ast-stringify.d.ts.map +1 -1
- package/dist/src/util/ast-stringify.js +14 -1
- package/dist/src/util/ast-stringify.js.map +1 -1
- package/dist/src/util/mutation-statement.d.ts +16 -0
- package/dist/src/util/mutation-statement.d.ts.map +1 -0
- package/dist/src/util/mutation-statement.js +92 -0
- package/dist/src/util/mutation-statement.js.map +1 -0
- package/dist/src/util/sql-literal.d.ts +11 -0
- package/dist/src/util/sql-literal.d.ts.map +1 -0
- package/dist/src/util/sql-literal.js +18 -0
- package/dist/src/util/sql-literal.js.map +1 -0
- package/dist/src/vtab/memory/table.d.ts +1 -2
- package/dist/src/vtab/memory/table.d.ts.map +1 -1
- package/dist/src/vtab/memory/table.js +3 -3
- package/dist/src/vtab/memory/table.js.map +1 -1
- package/dist/src/vtab/table.d.ts +23 -5
- package/dist/src/vtab/table.d.ts.map +1 -1
- package/dist/src/vtab/table.js +6 -0
- package/dist/src/vtab/table.js.map +1 -1
- package/package.json +1 -1
- package/src/common/logger.ts +75 -1
- package/src/func/builtins/datetime.ts +10 -5
- package/src/index.ts +4 -1
- package/src/planner/building/constraint-builder.ts +178 -173
- package/src/planner/building/delete.ts +5 -1
- package/src/planner/building/insert.ts +8 -1
- package/src/planner/building/update.ts +10 -2
- package/src/planner/nodes/dml-executor-node.ts +8 -2
- package/src/planner/validation/determinism-validator.ts +104 -0
- package/src/runtime/async-util.ts +4 -3
- package/src/runtime/emit/add-constraint.ts +3 -0
- package/src/runtime/emit/dml-executor.ts +105 -9
- package/src/runtime/emitters.ts +2 -1
- package/src/runtime/types.ts +3 -0
- package/src/runtime/utils.ts +41 -1
- package/src/schema/manager.ts +800 -742
- package/src/util/ast-stringify.ts +24 -1
- package/src/util/hash.ts +90 -90
- package/src/util/mutation-statement.ts +129 -0
- package/src/util/plugin-helper.ts +110 -110
- package/src/util/sql-literal.ts +22 -0
- package/src/vtab/memory/table.ts +3 -8
- package/src/vtab/table.ts +25 -10
|
@@ -497,6 +497,13 @@ export function insertToString(stmt: AST.InsertStmt): string {
|
|
|
497
497
|
parts.push(`(${stmt.columns.map(quoteIdentifierIfNeeded).join(', ')})`);
|
|
498
498
|
}
|
|
499
499
|
|
|
500
|
+
if (stmt.contextValues && stmt.contextValues.length > 0) {
|
|
501
|
+
const contextAssignments = stmt.contextValues.map(assign =>
|
|
502
|
+
`${quoteIdentifierIfNeeded(assign.name)} = ${expressionToString(assign.value)}`
|
|
503
|
+
);
|
|
504
|
+
parts.push('with context', contextAssignments.join(', '));
|
|
505
|
+
}
|
|
506
|
+
|
|
500
507
|
if (stmt.values) {
|
|
501
508
|
const valueRows = stmt.values.map(row =>
|
|
502
509
|
`(${row.map(expressionToString).join(', ')})`
|
|
@@ -533,7 +540,16 @@ export function updateToString(stmt: AST.UpdateStmt): string {
|
|
|
533
540
|
parts.push(withClauseToString(stmt.withClause));
|
|
534
541
|
}
|
|
535
542
|
|
|
536
|
-
parts.push('update', expressionToString(stmt.table)
|
|
543
|
+
parts.push('update', expressionToString(stmt.table));
|
|
544
|
+
|
|
545
|
+
if (stmt.contextValues && stmt.contextValues.length > 0) {
|
|
546
|
+
const contextAssignments = stmt.contextValues.map(assign =>
|
|
547
|
+
`${quoteIdentifierIfNeeded(assign.name)} = ${expressionToString(assign.value)}`
|
|
548
|
+
);
|
|
549
|
+
parts.push('with context', contextAssignments.join(', '));
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
parts.push('set');
|
|
537
553
|
|
|
538
554
|
const assignments = stmt.assignments.map(assign =>
|
|
539
555
|
`${quoteIdentifierIfNeeded(assign.column)} = ${expressionToString(assign.value)}`
|
|
@@ -573,6 +589,13 @@ export function deleteToString(stmt: AST.DeleteStmt): string {
|
|
|
573
589
|
|
|
574
590
|
parts.push('delete from', expressionToString(stmt.table));
|
|
575
591
|
|
|
592
|
+
if (stmt.contextValues && stmt.contextValues.length > 0) {
|
|
593
|
+
const contextAssignments = stmt.contextValues.map(assign =>
|
|
594
|
+
`${quoteIdentifierIfNeeded(assign.name)} = ${expressionToString(assign.value)}`
|
|
595
|
+
);
|
|
596
|
+
parts.push('with context', contextAssignments.join(', '));
|
|
597
|
+
}
|
|
598
|
+
|
|
576
599
|
if (stmt.where) {
|
|
577
600
|
parts.push('where', expressionToString(stmt.where));
|
|
578
601
|
}
|
package/src/util/hash.ts
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Hash and encoding utilities
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Computes FNV-1a hash of a string (64-bit variant)
|
|
7
|
-
* Returns an 8-byte array representing the hash
|
|
8
|
-
*
|
|
9
|
-
* FNV-1a is a fast, non-cryptographic hash function with good distribution.
|
|
10
|
-
* See: http://www.isthe.com/chongo/tech/comp/fnv/
|
|
11
|
-
*/
|
|
12
|
-
export function fnv1aHash(str: string): Uint8Array {
|
|
13
|
-
// FNV-1a 64-bit parameters
|
|
14
|
-
// Using two 32-bit integers to represent 64-bit hash (JavaScript limitation)
|
|
15
|
-
let hashHigh = 0xcbf29ce4; // Upper 32 bits of FNV offset basis
|
|
16
|
-
let hashLow = 0x84222325; // Lower 32 bits of FNV offset basis
|
|
17
|
-
|
|
18
|
-
const fnvPrimeHigh = 0x00000100; // Upper 32 bits of FNV prime
|
|
19
|
-
const fnvPrimeLow = 0x000001b3; // Lower 32 bits of FNV prime
|
|
20
|
-
|
|
21
|
-
for (let i = 0; i < str.length; i++) {
|
|
22
|
-
const charCode = str.charCodeAt(i);
|
|
23
|
-
|
|
24
|
-
// XOR with byte (handle multi-byte characters)
|
|
25
|
-
hashLow ^= charCode & 0xff;
|
|
26
|
-
|
|
27
|
-
// Multiply by FNV prime (64-bit multiplication using 32-bit parts)
|
|
28
|
-
const aHigh = hashHigh;
|
|
29
|
-
const aLow = hashLow;
|
|
30
|
-
|
|
31
|
-
hashLow = (aLow * fnvPrimeLow) >>> 0;
|
|
32
|
-
hashHigh = (aHigh * fnvPrimeLow + aLow * fnvPrimeHigh + (hashLow / 0x100000000)) >>> 0;
|
|
33
|
-
hashLow = hashLow >>> 0;
|
|
34
|
-
|
|
35
|
-
// Handle high byte of character if present
|
|
36
|
-
if (charCode > 0xff) {
|
|
37
|
-
hashLow ^= (charCode >>> 8) & 0xff;
|
|
38
|
-
const bHigh = hashHigh;
|
|
39
|
-
const bLow = hashLow;
|
|
40
|
-
hashLow = (bLow * fnvPrimeLow) >>> 0;
|
|
41
|
-
hashHigh = (bHigh * fnvPrimeLow + bLow * fnvPrimeHigh + (hashLow / 0x100000000)) >>> 0;
|
|
42
|
-
hashLow = hashLow >>> 0;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Convert to 8-byte array
|
|
47
|
-
const bytes = new Uint8Array(8);
|
|
48
|
-
bytes[0] = (hashHigh >>> 24) & 0xff;
|
|
49
|
-
bytes[1] = (hashHigh >>> 16) & 0xff;
|
|
50
|
-
bytes[2] = (hashHigh >>> 8) & 0xff;
|
|
51
|
-
bytes[3] = hashHigh & 0xff;
|
|
52
|
-
bytes[4] = (hashLow >>> 24) & 0xff;
|
|
53
|
-
bytes[5] = (hashLow >>> 16) & 0xff;
|
|
54
|
-
bytes[6] = (hashLow >>> 8) & 0xff;
|
|
55
|
-
bytes[7] = hashLow & 0xff;
|
|
56
|
-
|
|
57
|
-
return bytes;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Converts a byte array to base64url encoding
|
|
62
|
-
* (URL-safe base64 without padding: uses - and _ instead of + and /, no = padding)
|
|
63
|
-
*
|
|
64
|
-
* Base64url is defined in RFC 4648 Section 5.
|
|
65
|
-
*/
|
|
66
|
-
export function toBase64Url(bytes: Uint8Array): string {
|
|
67
|
-
const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
68
|
-
let result = '';
|
|
69
|
-
|
|
70
|
-
for (let i = 0; i < bytes.length; i += 3) {
|
|
71
|
-
// Get 3 bytes (24 bits) and convert to 4 base64 characters (6 bits each)
|
|
72
|
-
const byte1 = bytes[i];
|
|
73
|
-
const byte2 = i + 1 < bytes.length ? bytes[i + 1] : 0;
|
|
74
|
-
const byte3 = i + 2 < bytes.length ? bytes[i + 2] : 0;
|
|
75
|
-
|
|
76
|
-
const triplet = (byte1 << 16) | (byte2 << 8) | byte3;
|
|
77
|
-
|
|
78
|
-
result += base64Chars[(triplet >>> 18) & 0x3f];
|
|
79
|
-
result += base64Chars[(triplet >>> 12) & 0x3f];
|
|
80
|
-
if (i + 1 < bytes.length) {
|
|
81
|
-
result += base64Chars[(triplet >>> 6) & 0x3f];
|
|
82
|
-
}
|
|
83
|
-
if (i + 2 < bytes.length) {
|
|
84
|
-
result += base64Chars[triplet & 0x3f];
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return result;
|
|
89
|
-
}
|
|
90
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Hash and encoding utilities
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Computes FNV-1a hash of a string (64-bit variant)
|
|
7
|
+
* Returns an 8-byte array representing the hash
|
|
8
|
+
*
|
|
9
|
+
* FNV-1a is a fast, non-cryptographic hash function with good distribution.
|
|
10
|
+
* See: http://www.isthe.com/chongo/tech/comp/fnv/
|
|
11
|
+
*/
|
|
12
|
+
export function fnv1aHash(str: string): Uint8Array {
|
|
13
|
+
// FNV-1a 64-bit parameters
|
|
14
|
+
// Using two 32-bit integers to represent 64-bit hash (JavaScript limitation)
|
|
15
|
+
let hashHigh = 0xcbf29ce4; // Upper 32 bits of FNV offset basis
|
|
16
|
+
let hashLow = 0x84222325; // Lower 32 bits of FNV offset basis
|
|
17
|
+
|
|
18
|
+
const fnvPrimeHigh = 0x00000100; // Upper 32 bits of FNV prime
|
|
19
|
+
const fnvPrimeLow = 0x000001b3; // Lower 32 bits of FNV prime
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < str.length; i++) {
|
|
22
|
+
const charCode = str.charCodeAt(i);
|
|
23
|
+
|
|
24
|
+
// XOR with byte (handle multi-byte characters)
|
|
25
|
+
hashLow ^= charCode & 0xff;
|
|
26
|
+
|
|
27
|
+
// Multiply by FNV prime (64-bit multiplication using 32-bit parts)
|
|
28
|
+
const aHigh = hashHigh;
|
|
29
|
+
const aLow = hashLow;
|
|
30
|
+
|
|
31
|
+
hashLow = (aLow * fnvPrimeLow) >>> 0;
|
|
32
|
+
hashHigh = (aHigh * fnvPrimeLow + aLow * fnvPrimeHigh + (hashLow / 0x100000000)) >>> 0;
|
|
33
|
+
hashLow = hashLow >>> 0;
|
|
34
|
+
|
|
35
|
+
// Handle high byte of character if present
|
|
36
|
+
if (charCode > 0xff) {
|
|
37
|
+
hashLow ^= (charCode >>> 8) & 0xff;
|
|
38
|
+
const bHigh = hashHigh;
|
|
39
|
+
const bLow = hashLow;
|
|
40
|
+
hashLow = (bLow * fnvPrimeLow) >>> 0;
|
|
41
|
+
hashHigh = (bHigh * fnvPrimeLow + bLow * fnvPrimeHigh + (hashLow / 0x100000000)) >>> 0;
|
|
42
|
+
hashLow = hashLow >>> 0;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Convert to 8-byte array
|
|
47
|
+
const bytes = new Uint8Array(8);
|
|
48
|
+
bytes[0] = (hashHigh >>> 24) & 0xff;
|
|
49
|
+
bytes[1] = (hashHigh >>> 16) & 0xff;
|
|
50
|
+
bytes[2] = (hashHigh >>> 8) & 0xff;
|
|
51
|
+
bytes[3] = hashHigh & 0xff;
|
|
52
|
+
bytes[4] = (hashLow >>> 24) & 0xff;
|
|
53
|
+
bytes[5] = (hashLow >>> 16) & 0xff;
|
|
54
|
+
bytes[6] = (hashLow >>> 8) & 0xff;
|
|
55
|
+
bytes[7] = hashLow & 0xff;
|
|
56
|
+
|
|
57
|
+
return bytes;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Converts a byte array to base64url encoding
|
|
62
|
+
* (URL-safe base64 without padding: uses - and _ instead of + and /, no = padding)
|
|
63
|
+
*
|
|
64
|
+
* Base64url is defined in RFC 4648 Section 5.
|
|
65
|
+
*/
|
|
66
|
+
export function toBase64Url(bytes: Uint8Array): string {
|
|
67
|
+
const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
68
|
+
let result = '';
|
|
69
|
+
|
|
70
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
71
|
+
// Get 3 bytes (24 bits) and convert to 4 base64 characters (6 bits each)
|
|
72
|
+
const byte1 = bytes[i];
|
|
73
|
+
const byte2 = i + 1 < bytes.length ? bytes[i + 1] : 0;
|
|
74
|
+
const byte3 = i + 2 < bytes.length ? bytes[i + 2] : 0;
|
|
75
|
+
|
|
76
|
+
const triplet = (byte1 << 16) | (byte2 << 8) | byte3;
|
|
77
|
+
|
|
78
|
+
result += base64Chars[(triplet >>> 18) & 0x3f];
|
|
79
|
+
result += base64Chars[(triplet >>> 12) & 0x3f];
|
|
80
|
+
if (i + 1 < bytes.length) {
|
|
81
|
+
result += base64Chars[(triplet >>> 6) & 0x3f];
|
|
82
|
+
}
|
|
83
|
+
if (i + 2 < bytes.length) {
|
|
84
|
+
result += base64Chars[triplet & 0x3f];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { Row, SqlValue } from '../common/types.js';
|
|
2
|
+
import type { TableSchema } from '../schema/table.js';
|
|
3
|
+
import type * as AST from '../parser/ast.js';
|
|
4
|
+
import { insertToString, updateToString, deleteToString } from './ast-stringify.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Builds a deterministic INSERT statement for mutation logging.
|
|
8
|
+
* Creates an AST and uses insertToString for consistency.
|
|
9
|
+
*/
|
|
10
|
+
export function buildInsertStatement(
|
|
11
|
+
tableSchema: TableSchema,
|
|
12
|
+
newRow: Row,
|
|
13
|
+
contextRow?: Row
|
|
14
|
+
): string {
|
|
15
|
+
// Build INSERT AST
|
|
16
|
+
const stmt: AST.InsertStmt = {
|
|
17
|
+
type: 'insert',
|
|
18
|
+
table: { type: 'identifier', name: tableSchema.name },
|
|
19
|
+
columns: tableSchema.columns.map(col => col.name),
|
|
20
|
+
values: [newRow.map(sqlValueToLiteralExpr)],
|
|
21
|
+
contextValues: buildContextAssignments(tableSchema, contextRow)
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return insertToString(stmt);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Builds a deterministic UPDATE statement for mutation logging.
|
|
29
|
+
*/
|
|
30
|
+
export function buildUpdateStatement(
|
|
31
|
+
tableSchema: TableSchema,
|
|
32
|
+
newRow: Row,
|
|
33
|
+
oldKeyValues: Row,
|
|
34
|
+
contextRow?: Row
|
|
35
|
+
): string {
|
|
36
|
+
const pkDef = tableSchema.primaryKeyDefinition;
|
|
37
|
+
|
|
38
|
+
// Build UPDATE AST
|
|
39
|
+
const stmt: AST.UpdateStmt = {
|
|
40
|
+
type: 'update',
|
|
41
|
+
table: { type: 'identifier', name: tableSchema.name },
|
|
42
|
+
contextValues: buildContextAssignments(tableSchema, contextRow),
|
|
43
|
+
assignments: tableSchema.columns.map((col, idx) => ({
|
|
44
|
+
column: col.name,
|
|
45
|
+
value: sqlValueToLiteralExpr(newRow[idx])
|
|
46
|
+
})),
|
|
47
|
+
where: buildWhereClause(pkDef, oldKeyValues, tableSchema)
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
return updateToString(stmt);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Builds a deterministic DELETE statement for mutation logging.
|
|
55
|
+
*/
|
|
56
|
+
export function buildDeleteStatement(
|
|
57
|
+
tableSchema: TableSchema,
|
|
58
|
+
oldKeyValues: Row,
|
|
59
|
+
contextRow?: Row
|
|
60
|
+
): string {
|
|
61
|
+
const pkDef = tableSchema.primaryKeyDefinition;
|
|
62
|
+
|
|
63
|
+
// Build DELETE AST
|
|
64
|
+
const stmt: AST.DeleteStmt = {
|
|
65
|
+
type: 'delete',
|
|
66
|
+
table: { type: 'identifier', name: tableSchema.name },
|
|
67
|
+
contextValues: buildContextAssignments(tableSchema, contextRow),
|
|
68
|
+
where: buildWhereClause(pkDef, oldKeyValues, tableSchema)
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return deleteToString(stmt);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Converts a SqlValue to a LiteralExpr AST node.
|
|
76
|
+
*/
|
|
77
|
+
function sqlValueToLiteralExpr(value: SqlValue): AST.LiteralExpr {
|
|
78
|
+
return {
|
|
79
|
+
type: 'literal',
|
|
80
|
+
value: value
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Builds context assignments from context row values.
|
|
86
|
+
*/
|
|
87
|
+
function buildContextAssignments(
|
|
88
|
+
tableSchema: TableSchema,
|
|
89
|
+
contextRow?: Row
|
|
90
|
+
): AST.ContextAssignment[] | undefined {
|
|
91
|
+
if (!contextRow || contextRow.length === 0 || !tableSchema.mutationContext) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return tableSchema.mutationContext.map((ctx, idx) => ({
|
|
96
|
+
name: ctx.name,
|
|
97
|
+
value: sqlValueToLiteralExpr(contextRow[idx])
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Builds a WHERE clause for primary key matching.
|
|
103
|
+
* Returns an AND expression of pk_col = value for each PK column.
|
|
104
|
+
*/
|
|
105
|
+
function buildWhereClause(
|
|
106
|
+
pkDef: TableSchema['primaryKeyDefinition'],
|
|
107
|
+
keyValues: Row,
|
|
108
|
+
tableSchema: TableSchema
|
|
109
|
+
): AST.Expression {
|
|
110
|
+
const conditions: AST.Expression[] = pkDef.map((pkCol, idx) => ({
|
|
111
|
+
type: 'binary' as const,
|
|
112
|
+
operator: '=' as const,
|
|
113
|
+
left: { type: 'column' as const, name: tableSchema.columns[pkCol.index].name },
|
|
114
|
+
right: sqlValueToLiteralExpr(keyValues[idx])
|
|
115
|
+
}));
|
|
116
|
+
|
|
117
|
+
if (conditions.length === 1) {
|
|
118
|
+
return conditions[0];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Build nested AND expressions: (a AND b) AND c AND ...
|
|
122
|
+
return conditions.reduce((acc, cond) => ({
|
|
123
|
+
type: 'binary' as const,
|
|
124
|
+
operator: 'and' as const,
|
|
125
|
+
left: acc,
|
|
126
|
+
right: cond
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
|
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Plugin registration helper for static plugin loading
|
|
3
|
-
*
|
|
4
|
-
* This module provides utilities for registering plugins without dynamic imports,
|
|
5
|
-
* which is useful for React Native and other environments where dynamic imports
|
|
6
|
-
* are not supported.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { Database } from '../core/database.js';
|
|
10
|
-
import type { PluginRegistrations } from '../vtab/manifest.js';
|
|
11
|
-
import type { SqlValue } from '../common/types.js';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Plugin function type - what a plugin exports as its default export
|
|
15
|
-
*/
|
|
16
|
-
export type PluginFunction = (
|
|
17
|
-
db: Database,
|
|
18
|
-
config?: Record<string, SqlValue>
|
|
19
|
-
) => Promise<PluginRegistrations> | PluginRegistrations;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Register a plugin's components with the database.
|
|
23
|
-
*
|
|
24
|
-
* This is a helper function for static plugin loading that handles calling
|
|
25
|
-
* the plugin function and registering all returned components (vtables,
|
|
26
|
-
* functions, collations, types) with the database.
|
|
27
|
-
*
|
|
28
|
-
* @param db Database instance to register with
|
|
29
|
-
* @param plugin Plugin function (the default export from a plugin module)
|
|
30
|
-
* @param config Optional configuration object to pass to the plugin
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* ```typescript
|
|
34
|
-
* import { Database } from '@quereus/quereus';
|
|
35
|
-
* import { registerPlugin } from '@quereus/quereus';
|
|
36
|
-
* import myPlugin from './plugins/my-plugin';
|
|
37
|
-
*
|
|
38
|
-
* const db = new Database();
|
|
39
|
-
* await registerPlugin(db, myPlugin, { apiKey: 'secret' });
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```typescript
|
|
44
|
-
* // Register multiple plugins
|
|
45
|
-
* await registerPlugin(db, stringFunctions);
|
|
46
|
-
* await registerPlugin(db, customCollations);
|
|
47
|
-
* await registerPlugin(db, jsonTable, { cacheSize: 100 });
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export async function registerPlugin(
|
|
51
|
-
db: Database,
|
|
52
|
-
plugin: PluginFunction,
|
|
53
|
-
config: Record<string, SqlValue> = {}
|
|
54
|
-
): Promise<void> {
|
|
55
|
-
// Call the plugin function to get registrations
|
|
56
|
-
const registrations = await plugin(db, config);
|
|
57
|
-
|
|
58
|
-
// Register virtual table modules
|
|
59
|
-
if (registrations.vtables) {
|
|
60
|
-
for (const vtable of registrations.vtables) {
|
|
61
|
-
try {
|
|
62
|
-
db.registerVtabModule(vtable.name, vtable.module, vtable.auxData);
|
|
63
|
-
} catch (error) {
|
|
64
|
-
throw new Error(
|
|
65
|
-
`Failed to register vtable module '${vtable.name}': ${error instanceof Error ? error.message : String(error)}`
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Register functions
|
|
72
|
-
if (registrations.functions) {
|
|
73
|
-
for (const func of registrations.functions) {
|
|
74
|
-
try {
|
|
75
|
-
db.registerFunction(func.schema);
|
|
76
|
-
} catch (error) {
|
|
77
|
-
throw new Error(
|
|
78
|
-
`Failed to register function '${func.schema.name}/${func.schema.numArgs}': ${error instanceof Error ? error.message : String(error)}`
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Register collations
|
|
85
|
-
if (registrations.collations) {
|
|
86
|
-
for (const collation of registrations.collations) {
|
|
87
|
-
try {
|
|
88
|
-
db.registerCollation(collation.name, collation.func);
|
|
89
|
-
} catch (error) {
|
|
90
|
-
throw new Error(
|
|
91
|
-
`Failed to register collation '${collation.name}': ${error instanceof Error ? error.message : String(error)}`
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Register types
|
|
98
|
-
if (registrations.types) {
|
|
99
|
-
for (const type of registrations.types) {
|
|
100
|
-
try {
|
|
101
|
-
db.registerType(type.name, type.definition);
|
|
102
|
-
} catch (error) {
|
|
103
|
-
throw new Error(
|
|
104
|
-
`Failed to register type '${type.name}': ${error instanceof Error ? error.message : String(error)}`
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Plugin registration helper for static plugin loading
|
|
3
|
+
*
|
|
4
|
+
* This module provides utilities for registering plugins without dynamic imports,
|
|
5
|
+
* which is useful for React Native and other environments where dynamic imports
|
|
6
|
+
* are not supported.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Database } from '../core/database.js';
|
|
10
|
+
import type { PluginRegistrations } from '../vtab/manifest.js';
|
|
11
|
+
import type { SqlValue } from '../common/types.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Plugin function type - what a plugin exports as its default export
|
|
15
|
+
*/
|
|
16
|
+
export type PluginFunction = (
|
|
17
|
+
db: Database,
|
|
18
|
+
config?: Record<string, SqlValue>
|
|
19
|
+
) => Promise<PluginRegistrations> | PluginRegistrations;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Register a plugin's components with the database.
|
|
23
|
+
*
|
|
24
|
+
* This is a helper function for static plugin loading that handles calling
|
|
25
|
+
* the plugin function and registering all returned components (vtables,
|
|
26
|
+
* functions, collations, types) with the database.
|
|
27
|
+
*
|
|
28
|
+
* @param db Database instance to register with
|
|
29
|
+
* @param plugin Plugin function (the default export from a plugin module)
|
|
30
|
+
* @param config Optional configuration object to pass to the plugin
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { Database } from '@quereus/quereus';
|
|
35
|
+
* import { registerPlugin } from '@quereus/quereus';
|
|
36
|
+
* import myPlugin from './plugins/my-plugin';
|
|
37
|
+
*
|
|
38
|
+
* const db = new Database();
|
|
39
|
+
* await registerPlugin(db, myPlugin, { apiKey: 'secret' });
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // Register multiple plugins
|
|
45
|
+
* await registerPlugin(db, stringFunctions);
|
|
46
|
+
* await registerPlugin(db, customCollations);
|
|
47
|
+
* await registerPlugin(db, jsonTable, { cacheSize: 100 });
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export async function registerPlugin(
|
|
51
|
+
db: Database,
|
|
52
|
+
plugin: PluginFunction,
|
|
53
|
+
config: Record<string, SqlValue> = {}
|
|
54
|
+
): Promise<void> {
|
|
55
|
+
// Call the plugin function to get registrations
|
|
56
|
+
const registrations = await plugin(db, config);
|
|
57
|
+
|
|
58
|
+
// Register virtual table modules
|
|
59
|
+
if (registrations.vtables) {
|
|
60
|
+
for (const vtable of registrations.vtables) {
|
|
61
|
+
try {
|
|
62
|
+
db.registerVtabModule(vtable.name, vtable.module, vtable.auxData);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Failed to register vtable module '${vtable.name}': ${error instanceof Error ? error.message : String(error)}`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Register functions
|
|
72
|
+
if (registrations.functions) {
|
|
73
|
+
for (const func of registrations.functions) {
|
|
74
|
+
try {
|
|
75
|
+
db.registerFunction(func.schema);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
`Failed to register function '${func.schema.name}/${func.schema.numArgs}': ${error instanceof Error ? error.message : String(error)}`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Register collations
|
|
85
|
+
if (registrations.collations) {
|
|
86
|
+
for (const collation of registrations.collations) {
|
|
87
|
+
try {
|
|
88
|
+
db.registerCollation(collation.name, collation.func);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
`Failed to register collation '${collation.name}': ${error instanceof Error ? error.message : String(error)}`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Register types
|
|
98
|
+
if (registrations.types) {
|
|
99
|
+
for (const type of registrations.types) {
|
|
100
|
+
try {
|
|
101
|
+
db.registerType(type.name, type.definition);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`Failed to register type '${type.name}': ${error instanceof Error ? error.message : String(error)}`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { SqlValue } from '../common/types.js';
|
|
2
|
+
import { expressionToString } from './ast-stringify.js';
|
|
3
|
+
import type * as AST from '../parser/ast.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Converts a SqlValue to its SQL literal string representation.
|
|
7
|
+
* This is used for mutation logging to create deterministic, replayable SQL statements.
|
|
8
|
+
* Uses the existing expressionToString function for consistency.
|
|
9
|
+
*
|
|
10
|
+
* @param value The value to convert to a SQL literal
|
|
11
|
+
* @returns SQL literal string representation
|
|
12
|
+
*/
|
|
13
|
+
export function sqlValueToLiteral(value: SqlValue): string {
|
|
14
|
+
// Create a literal expression AST node and use expressionToString
|
|
15
|
+
const literalExpr: AST.LiteralExpr = {
|
|
16
|
+
type: 'literal',
|
|
17
|
+
value: value
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return expressionToString(literalExpr);
|
|
21
|
+
}
|
|
22
|
+
|
package/src/vtab/memory/table.ts
CHANGED
|
@@ -123,16 +123,11 @@ export class MemoryTable extends VirtualTable {
|
|
|
123
123
|
// Note: getBestAccessPlan is handled by the MemoryTableModule, not the table instance.
|
|
124
124
|
|
|
125
125
|
/** Performs mutation through the connection's transaction layer */
|
|
126
|
-
async update(
|
|
127
|
-
operation: 'insert' | 'update' | 'delete',
|
|
128
|
-
values: Row | undefined,
|
|
129
|
-
oldKeyValues?: Row,
|
|
130
|
-
onConflict?: ConflictResolution
|
|
131
|
-
): Promise<Row | undefined> {
|
|
126
|
+
async update(args: import('../table.js').UpdateArgs): Promise<Row | undefined> {
|
|
132
127
|
const conn = await this.ensureConnection();
|
|
133
128
|
// Delegate mutation to the manager.
|
|
134
|
-
//
|
|
135
|
-
return this.manager.performMutation(conn, operation, values, oldKeyValues, onConflict);
|
|
129
|
+
// Note: mutationStatement is ignored by memory table (could be logged if needed)
|
|
130
|
+
return this.manager.performMutation(conn, args.operation, args.values, args.oldKeyValues, args.onConflict);
|
|
136
131
|
}
|
|
137
132
|
|
|
138
133
|
/** Begins a transaction for this connection */
|
package/src/vtab/table.ts
CHANGED
|
@@ -9,6 +9,22 @@ import type { ConflictResolution } from '../common/constants.js';
|
|
|
9
9
|
import type { VirtualTableConnection } from './connection.js';
|
|
10
10
|
import type { PlanNode } from '../planner/nodes/plan-node.js';
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Arguments passed to VirtualTable.update() method.
|
|
14
|
+
*/
|
|
15
|
+
export interface UpdateArgs {
|
|
16
|
+
/** The operation to perform (insert, update, delete) */
|
|
17
|
+
operation: RowOp;
|
|
18
|
+
/** For INSERT/UPDATE, the values to insert/update. For DELETE, undefined */
|
|
19
|
+
values: Row | undefined;
|
|
20
|
+
/** For UPDATE/DELETE, the old key values of the row to modify. Undefined for INSERT */
|
|
21
|
+
oldKeyValues?: Row;
|
|
22
|
+
/** Conflict resolution mode (defaults to ABORT if unspecified) */
|
|
23
|
+
onConflict?: ConflictResolution;
|
|
24
|
+
/** Optional: Deterministic SQL statement that reproduces this mutation (if logMutations is enabled) */
|
|
25
|
+
mutationStatement?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
12
28
|
/**
|
|
13
29
|
* Base class representing a virtual table instance.
|
|
14
30
|
* Module implementations should subclass this to provide specific table behavior.
|
|
@@ -21,6 +37,13 @@ export abstract class VirtualTable {
|
|
|
21
37
|
public errorMessage?: string;
|
|
22
38
|
public tableSchema?: TableSchema;
|
|
23
39
|
|
|
40
|
+
/**
|
|
41
|
+
* When true, the update() method will receive a mutationStatement parameter
|
|
42
|
+
* containing a deterministic SQL statement that reproduces the mutation.
|
|
43
|
+
* This enables replication, audit logging, and change data capture.
|
|
44
|
+
*/
|
|
45
|
+
public wantStatements?: boolean;
|
|
46
|
+
|
|
24
47
|
constructor(db: Database, module: AnyVirtualTableModule, schemaName: string, tableName: string) {
|
|
25
48
|
this.db = db;
|
|
26
49
|
this.module = module;
|
|
@@ -69,19 +92,11 @@ export abstract class VirtualTable {
|
|
|
69
92
|
|
|
70
93
|
/**
|
|
71
94
|
* Performs an INSERT, UPDATE, or DELETE operation
|
|
72
|
-
* @param
|
|
73
|
-
* @param values For INSERT/UPDATE, the values to insert/update. For DELETE, undefined
|
|
74
|
-
* @param oldKeyValues For UPDATE/DELETE, the old key values of the row to modify. Undefined for INSERT
|
|
75
|
-
* @param onConflict Conflict resolution mode (defaults to ABORT if unspecified)
|
|
95
|
+
* @param args Arguments object containing operation details and optional mutation statement
|
|
76
96
|
* @returns new row for INSERT/UPDATE, undefined for DELETE
|
|
77
97
|
* @throws QuereusError or ConstraintError on failure
|
|
78
98
|
*/
|
|
79
|
-
abstract update(
|
|
80
|
-
operation: RowOp,
|
|
81
|
-
values: Row | undefined,
|
|
82
|
-
oldKeyValues?: Row,
|
|
83
|
-
onConflict?: ConflictResolution
|
|
84
|
-
): Promise<Row | undefined>;
|
|
99
|
+
abstract update(args: UpdateArgs): Promise<Row | undefined>;
|
|
85
100
|
|
|
86
101
|
/**
|
|
87
102
|
* (Optional) Creates a new connection for transaction support.
|