@mistriwork/engine 1.0.0-rc.33 → 1.0.0-rc.35
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/dist/adapters/adapter.d.ts +44 -0
- package/dist/adapters/adapter.js +162 -0
- package/dist/adapters/postgres.d.ts +8 -0
- package/dist/adapters/postgres.js +41 -13
- package/dist/adapters/postgresBatchWrites.d.ts +39 -0
- package/dist/adapters/postgresBatchWrites.js +113 -0
- package/dist/adapters/scoped/scopedCommandOperations.d.ts +4 -1
- package/dist/adapters/scoped/scopedCommandOperations.js +4 -1
- package/dist/adapters/scoped/sqliteScopeCatalog.js +13 -18
- package/dist/adapters/sqlite.d.ts +19 -0
- package/dist/adapters/sqlite.js +265 -55
- package/dist/adapters/sqliteHandles.d.ts +10 -0
- package/dist/adapters/sqliteHandles.js +28 -0
- package/dist/adapters/telemetry/drivers.js +5 -0
- package/dist/adapters/telemetry/trace.d.ts +2 -0
- package/dist/adapters/telemetry/trace.js +2 -0
- package/dist/server/columnGrants.d.ts +6 -4
- package/dist/server/columnGrants.js +45 -36
- package/dist/shared/scopeCatalog.js +35 -1
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ANSI_STYLE, Refusal } from '../adapter.js';
|
|
2
|
+
import { cachedStatement as prepared } from '../sqliteHandles.js';
|
|
2
3
|
import { chooseColumnReference, deleteRuleOf, } from '../../shared/schema.js';
|
|
3
4
|
import { mapScopeCatalog } from '../../shared/scopeCatalog.js';
|
|
4
5
|
import { referenceKey } from './scopedTransaction.js';
|
|
@@ -127,7 +128,7 @@ export function sqliteKeyFacts(db) {
|
|
|
127
128
|
const definitions = new Map();
|
|
128
129
|
const definitionOf = (table) => {
|
|
129
130
|
if (!definitions.has(table.id)) {
|
|
130
|
-
const row = db
|
|
131
|
+
const row = prepared(db, "SELECT sql FROM main.sqlite_schema WHERE type = 'table' AND name = ?").get(table.name);
|
|
131
132
|
definitions.set(table.id, typeof row?.sql === 'string' ? row.sql : undefined);
|
|
132
133
|
}
|
|
133
134
|
return definitions.get(table.id);
|
|
@@ -170,14 +171,12 @@ export function sqliteKeyFacts(db) {
|
|
|
170
171
|
// A rowid alias holds integers alone, compared by no collation; it has no index of its own.
|
|
171
172
|
if (primary.length === 1 && table.columns.find((column) => column.name === primary[0])?.identity)
|
|
172
173
|
return [primary];
|
|
173
|
-
const index = db
|
|
174
|
-
.prepare(`PRAGMA main.index_list(${ANSI_STYLE.quote(table.name)})`)
|
|
174
|
+
const index = prepared(db, `PRAGMA main.index_list(${ANSI_STYLE.quote(table.name)})`)
|
|
175
175
|
.all()
|
|
176
176
|
.find((row) => row.origin === 'pk');
|
|
177
177
|
if (typeof index?.name !== 'string')
|
|
178
178
|
return [];
|
|
179
|
-
const indexed = db
|
|
180
|
-
.prepare(`PRAGMA main.index_xinfo(${ANSI_STYLE.quote(index.name)})`)
|
|
179
|
+
const indexed = prepared(db, `PRAGMA main.index_xinfo(${ANSI_STYLE.quote(index.name)})`)
|
|
181
180
|
.all()
|
|
182
181
|
.filter((row) => row.key === 1);
|
|
183
182
|
const same = primary.every((name) => {
|
|
@@ -196,7 +195,7 @@ export function sqliteKeyFacts(db) {
|
|
|
196
195
|
*/
|
|
197
196
|
export function sqliteBlockedTables(db) {
|
|
198
197
|
const blocked = new Map();
|
|
199
|
-
for (const table of db
|
|
198
|
+
for (const table of prepared(db, 'PRAGMA main.table_list').all()) {
|
|
200
199
|
if (table.schema !== 'main' || typeof table.name !== 'string')
|
|
201
200
|
continue;
|
|
202
201
|
if (table.type === 'virtual' || table.type === 'shadow')
|
|
@@ -216,12 +215,10 @@ export function readSqliteReadCatalog(db) {
|
|
|
216
215
|
}
|
|
217
216
|
function scanSqliteScopeCatalog(db) {
|
|
218
217
|
try {
|
|
219
|
-
if (db
|
|
218
|
+
if (prepared(db, 'PRAGMA foreign_keys').get()?.foreign_keys !== 1)
|
|
220
219
|
return refuse();
|
|
221
|
-
const entries = db
|
|
222
|
-
|
|
223
|
-
.all();
|
|
224
|
-
const listed = db.prepare('PRAGMA main.table_list').all();
|
|
220
|
+
const entries = prepared(db, "SELECT name, type FROM main.sqlite_schema WHERE type IN ('table', 'view') ORDER BY name").all();
|
|
221
|
+
const listed = prepared(db, 'PRAGMA main.table_list').all();
|
|
225
222
|
// Unknown PRAGMAs can return nothing without throwing. Never interpret
|
|
226
223
|
// unavailable table-kind metadata as proof that a table is ordinary.
|
|
227
224
|
if (!listed.some((table) => table.schema === 'main' && table.name === 'sqlite_schema' && table.type === 'table'))
|
|
@@ -231,9 +228,7 @@ function scanSqliteScopeCatalog(db) {
|
|
|
231
228
|
it only validates its row, is read from that text per table below (sqliteTriggers.ts, scopeTriggers.ts). */
|
|
232
229
|
const definitions = new Map();
|
|
233
230
|
for (const namespace of ['main', 'temp']) {
|
|
234
|
-
const triggers = db
|
|
235
|
-
.prepare(`SELECT tbl_name AS name, name AS trigger, sql FROM ${namespace}.sqlite_schema WHERE type = 'trigger' ORDER BY name`)
|
|
236
|
-
.all();
|
|
231
|
+
const triggers = prepared(db, `SELECT tbl_name AS name, name AS trigger, sql FROM ${namespace}.sqlite_schema WHERE type = 'trigger' ORDER BY name`).all();
|
|
237
232
|
for (const trigger of triggers) {
|
|
238
233
|
if (typeof trigger.name !== 'string' || typeof trigger.trigger !== 'string')
|
|
239
234
|
return refuse();
|
|
@@ -245,7 +240,7 @@ function scanSqliteScopeCatalog(db) {
|
|
|
245
240
|
}
|
|
246
241
|
const triggered = new Map();
|
|
247
242
|
const shadowed = new Set();
|
|
248
|
-
for (const table of db
|
|
243
|
+
for (const table of prepared(db, "SELECT name FROM temp.sqlite_schema WHERE type IN ('table', 'view')").all()) {
|
|
249
244
|
if (typeof table.name !== 'string')
|
|
250
245
|
return refuse();
|
|
251
246
|
shadowed.add(folded(table.name));
|
|
@@ -262,7 +257,7 @@ function scanSqliteScopeCatalog(db) {
|
|
|
262
257
|
!['table', 'view', 'virtual', 'shadow'].includes(kind.type) ||
|
|
263
258
|
(entry.type === 'view') !== (kind.type === 'view'))
|
|
264
259
|
return refuse();
|
|
265
|
-
const columns = db
|
|
260
|
+
const columns = prepared(db, `PRAGMA main.table_xinfo(${ANSI_STYLE.quote(entry.name)})`).all();
|
|
266
261
|
identities(columns);
|
|
267
262
|
if (!columns.length || columns.length !== kind.ncol)
|
|
268
263
|
return refuse();
|
|
@@ -279,7 +274,7 @@ function scanSqliteScopeCatalog(db) {
|
|
|
279
274
|
const primary = columns.filter((column) => column.pk > 0).sort((a, b) => a.pk - b.pk);
|
|
280
275
|
if (primary.some((column, i) => column.pk !== i + 1))
|
|
281
276
|
return refuse();
|
|
282
|
-
const indexes = db
|
|
277
|
+
const indexes = prepared(db, `PRAGMA main.index_list(${ANSI_STYLE.quote(entry.name)})`).all();
|
|
283
278
|
// INTEGER PRIMARY KEY DESC has a PK index and is not a rowid alias.
|
|
284
279
|
const identity = kind.type === 'table' &&
|
|
285
280
|
kind.wr === 0 &&
|
|
@@ -329,7 +324,7 @@ function scanSqliteScopeCatalog(db) {
|
|
|
329
324
|
const byName = identities(tables);
|
|
330
325
|
const foreignKeys = [];
|
|
331
326
|
for (const table of tables) {
|
|
332
|
-
const rows = db
|
|
327
|
+
const rows = prepared(db, `PRAGMA main.foreign_key_list(${ANSI_STYLE.quote(table.name)})`).all();
|
|
333
328
|
const groups = new Map();
|
|
334
329
|
for (const row of rows) {
|
|
335
330
|
if (!Number.isInteger(row.id) ||
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { cachedStatement, schemaFingerprint } from './sqliteHandles.js';
|
|
1
2
|
import { DatabaseSync, type SQLInputValue } from 'node:sqlite';
|
|
2
3
|
import { type Adapter, type ScopedReads, type Filter, type QueryRequest, type QueryResult, type SqlRequest, type SqlResult, type DeleteRequest, type DeleteResult, type UpdateRequest, type UpdateResult, type ViewResult, type BreakdownResult, TransactionOperations, type WriteTransaction, type InsertRequest, type InsertResult, type ExplainRequest, type ExplainResult, type ProfileLimits, type ProfileResult, type CountItem, type AggregateRequest, type AggregateResult } from './adapter.js';
|
|
3
4
|
import type { TriggerAcceptance } from './scoped/triggerProof.js';
|
|
@@ -144,6 +145,15 @@ export declare class SqliteAdapter implements Adapter {
|
|
|
144
145
|
*/
|
|
145
146
|
update(request: UpdateRequest): Promise<UpdateResult>;
|
|
146
147
|
private updateNow;
|
|
148
|
+
/**
|
|
149
|
+
* Several rows of one table changed by one statement inside a transaction, each as `updateNow` changes one: the
|
|
150
|
+
* same refusals before it, each row's own values and `expected`, one row a key. A `CASE` a column picks each
|
|
151
|
+
* row's value, the WHERE matches each key with its own guard, and RETURNING says which requests matched, so a
|
|
152
|
+
* row whose key or guard missed refuses the batch as it would have refused alone.
|
|
153
|
+
*/
|
|
154
|
+
private updateManyNow;
|
|
155
|
+
/** What `update` refuses before it writes, and the values it writes, coerced to their columns. */
|
|
156
|
+
private updatePlan;
|
|
147
157
|
/**
|
|
148
158
|
* Add one row.
|
|
149
159
|
*
|
|
@@ -161,6 +171,15 @@ export declare class SqliteAdapter implements Adapter {
|
|
|
161
171
|
*/
|
|
162
172
|
insert(request: InsertRequest): Promise<InsertResult>;
|
|
163
173
|
private insertNow;
|
|
174
|
+
/** What `insert` refuses before it writes, and the values it writes, coerced to their columns. */
|
|
175
|
+
private insertPlan;
|
|
176
|
+
/**
|
|
177
|
+
* Several rows of one table added by one statement inside a transaction, each as `insertNow` adds one: one
|
|
178
|
+
* multi-row `INSERT OR ABORT … RETURNING *` for each run of rows naming the same columns. SQLite does not promise
|
|
179
|
+
* the order RETURNING answers in, so each row is matched back to the request whose values it holds (batchedRows).
|
|
180
|
+
*/
|
|
181
|
+
private insertManyNow;
|
|
182
|
+
private insertOne;
|
|
164
183
|
/**
|
|
165
184
|
* Run a statement the user wrote.
|
|
166
185
|
*
|
package/dist/adapters/sqlite.js
CHANGED
|
@@ -8,14 +8,16 @@
|
|
|
8
8
|
* works for both is unlikely to be accidentally Postgres-shaped.
|
|
9
9
|
*/
|
|
10
10
|
import { statSync } from 'node:fs';
|
|
11
|
-
import { waitForLocks, assertIndependentWrite, writeToFile, prepareGuarded, transactionEpoch, storeEpoch } from './sqliteHandles.js';
|
|
11
|
+
import { waitForLocks, assertIndependentWrite, writeToFile, prepareGuarded, transactionEpoch, storeEpoch, schemaFingerprint, } from './sqliteHandles.js';
|
|
12
|
+
/* The tenant sources keep their catalog and store-layout checks by these (tenantReadSource.ts, tenantStore.ts). */
|
|
13
|
+
export { cachedStatement, schemaFingerprint } from './sqliteHandles.js';
|
|
12
14
|
import { captureScopeCatalog, mapScopeCatalog } from '../shared/scopeCatalog.js';
|
|
13
15
|
import { inlineRows } from '../shared/anyRows.js';
|
|
14
16
|
import { asksAboutRows } from '../shared/filter.js';
|
|
15
17
|
import { qualifiedTable } from '../shared/sqlRelation.js';
|
|
16
18
|
import { DatabaseSync } from 'node:sqlite';
|
|
17
19
|
import { isPromise, isProxy } from 'node:util/types';
|
|
18
|
-
import { ANSI_STYLE, buildWhere, clampLimit, clampOffset, boundStatement, looksWrappable, dedupe, refuseStacked, trimStatement, Refusal, WriteConflict, TransactionOutcomeUnknown, Serial, TransactionOperations, queueRecordLock, recordLockFilter, refuseAnalyze, countStatement, countEachStatement, countEachRows, countsTogether, orderClause, lanesStatement, withoutLaneRow, } from './adapter.js';
|
|
20
|
+
import { ANSI_STYLE, buildWhere, clampLimit, clampOffset, boundStatement, looksWrappable, dedupe, refuseStacked, trimStatement, Refusal, WriteConflict, TransactionOutcomeUnknown, Serial, TransactionOperations, queueRecordLock, recordLockFilter, refuseAnalyze, countStatement, countEachStatement, countEachRows, countsTogether, orderClause, lanesStatement, withoutLaneRow, batchedRows, chunked, gathered, refuseUpdateMatches, runsOfColumns, sameTable, } from './adapter.js';
|
|
19
21
|
import { compileAggregate } from '../shared/aggregate.js';
|
|
20
22
|
import { readExactly, summed } from '../shared/decimals.js';
|
|
21
23
|
import { buildUpdateGuard } from './scoped/updateGuard.js';
|
|
@@ -41,6 +43,49 @@ import { flagsFromDDL, vocabularyFromDDL } from '../shared/vocabulary.js';
|
|
|
41
43
|
import { coerceForColumn, columnKind, deleteRuleOf, } from '../shared/schema.js';
|
|
42
44
|
/** A generated column, virtual (2) or stored (3): the database writes it from its expression. */
|
|
43
45
|
const generated = (column) => column.hidden === 2 || column.hidden === 3;
|
|
46
|
+
/* The catalog answers a write transaction trusts outlive the adapter that read them: a writer reopened, or a second
|
|
47
|
+
one on the file, starts warm. Kept per file, by the identity of the file the handle opened (device, inode and birth
|
|
48
|
+
time, so a new file at a reused inode is a new key) and never across files; a file with no birth time to tell it
|
|
49
|
+
from its inode's next tenant, and an in-memory database, keep their own. Every entry is still served only under the
|
|
50
|
+
epoch the reader holds (epochCache.ts), and frozen, since another adapter reads the same value. */
|
|
51
|
+
const sharedCatalogs = new Map();
|
|
52
|
+
function catalogCacheOf(identity) {
|
|
53
|
+
if (identity === undefined)
|
|
54
|
+
return new EpochCache();
|
|
55
|
+
const cache = sharedCatalogs.get(identity) ?? new EpochCache();
|
|
56
|
+
sharedCatalogs.delete(identity);
|
|
57
|
+
sharedCatalogs.set(identity, cache);
|
|
58
|
+
while (sharedCatalogs.size > 32)
|
|
59
|
+
sharedCatalogs.delete(sharedCatalogs.keys().next().value);
|
|
60
|
+
return cache;
|
|
61
|
+
}
|
|
62
|
+
function frozen(value) {
|
|
63
|
+
if (!value || typeof value !== 'object' || Object.isFrozen(value))
|
|
64
|
+
return value;
|
|
65
|
+
if (value instanceof Map)
|
|
66
|
+
for (const [key, each] of value) {
|
|
67
|
+
frozen(key);
|
|
68
|
+
frozen(each);
|
|
69
|
+
}
|
|
70
|
+
else if (value instanceof Set)
|
|
71
|
+
for (const each of value)
|
|
72
|
+
frozen(each);
|
|
73
|
+
else
|
|
74
|
+
for (const each of Object.values(value))
|
|
75
|
+
frozen(each);
|
|
76
|
+
return Object.freeze(value);
|
|
77
|
+
}
|
|
78
|
+
/* What the scope catalog was resolved with is part of its key: a caller judging trigger effects reads another
|
|
79
|
+
catalog than one that does not. */
|
|
80
|
+
const resolverIds = new WeakMap();
|
|
81
|
+
let resolvers = 0;
|
|
82
|
+
const resolverKey = (resolve) => {
|
|
83
|
+
if (!resolve)
|
|
84
|
+
return 'scope';
|
|
85
|
+
if (!resolverIds.has(resolve))
|
|
86
|
+
resolverIds.set(resolve, ++resolvers);
|
|
87
|
+
return `scope:${resolverIds.get(resolve)}`;
|
|
88
|
+
};
|
|
44
89
|
const verifiedCommandAdapters = new WeakSet();
|
|
45
90
|
/** Identity-only native provenance; no caller can register a lookalike. */
|
|
46
91
|
export const isSqliteVerifiedCommandSource = (value) => !!value && typeof value === 'object' && !isProxy(value) && verifiedCommandAdapters.has(value);
|
|
@@ -59,8 +104,8 @@ export class SqliteAdapter {
|
|
|
59
104
|
/* Catalog answers a write transaction trusts — the journal's layout, the
|
|
60
105
|
scope catalog, a table's existence and its columns — kept under the
|
|
61
106
|
schema epoch the transaction read (transactionEpoch), so a warm command
|
|
62
|
-
asks the catalog nothing (epochCache.ts). */
|
|
63
|
-
catalogCache
|
|
107
|
+
asks the catalog nothing (epochCache.ts). Shared by every adapter on the file (catalogCacheOf). */
|
|
108
|
+
catalogCache;
|
|
64
109
|
/** The epoch the open write transaction read, on the handle it read it on. */
|
|
65
110
|
txEpoch;
|
|
66
111
|
constructor(file, label = file) {
|
|
@@ -76,9 +121,12 @@ export class SqliteAdapter {
|
|
|
76
121
|
if (typeof filename === 'string' && filename) {
|
|
77
122
|
const info = statSync(filename, { bigint: true });
|
|
78
123
|
this.writeIdentity = `${info.dev}:${info.ino}`;
|
|
124
|
+
this.catalogCache = catalogCacheOf(info.birthtimeNs > 0n ? `${info.dev}:${info.ino}:${info.birthtimeNs}` : undefined);
|
|
79
125
|
}
|
|
80
|
-
else
|
|
126
|
+
else {
|
|
81
127
|
this.writeIdentity = Symbol('sqlite-memory');
|
|
128
|
+
this.catalogCache = catalogCacheOf(undefined);
|
|
129
|
+
}
|
|
82
130
|
waitForLocks(this.db);
|
|
83
131
|
}
|
|
84
132
|
catch (error) {
|
|
@@ -767,6 +815,97 @@ export class SqliteAdapter {
|
|
|
767
815
|
return this.queueWrite(() => this.updateNow(request));
|
|
768
816
|
}
|
|
769
817
|
async updateNow(request, read = this.db, scoped = false) {
|
|
818
|
+
const { table, meta, pk, applied } = this.updatePlan(request, read);
|
|
819
|
+
const filter = {
|
|
820
|
+
groups: [pk.map((column) => ({ column, op: '=', value: request.key[column] }))],
|
|
821
|
+
};
|
|
822
|
+
/* Outside a transaction the count runs first, on the read handle, so the
|
|
823
|
+
check that decides whether to write cannot itself write. Inside one it
|
|
824
|
+
is the UPDATE's own count: the key is the whole primary key, so it
|
|
825
|
+
matches one row or none, and a refusal rolls the transaction back. */
|
|
826
|
+
const transactional = read !== this.db;
|
|
827
|
+
const matches = transactional ? 1 : await this.count(table, filter);
|
|
828
|
+
if (matches !== 1)
|
|
829
|
+
refuseUpdateMatches(request, matches);
|
|
830
|
+
const where = buildWhere(filter, ANSI_STYLE);
|
|
831
|
+
const assignments = Object.keys(applied)
|
|
832
|
+
.map((c) => `${ANSI_STYLE.quote(c)} = ?`)
|
|
833
|
+
.join(', ');
|
|
834
|
+
const guard = buildUpdateGuard(request, Object.fromEntries(meta.map((column) => [column.name, column.type])), ANSI_STYLE, 'sqlite', 1);
|
|
835
|
+
const text = `UPDATE ${scoped ? 'OR ABORT ' : ''}${ANSI_STYLE.quote(table)} SET ${assignments}${where.text}${guard.text}`;
|
|
836
|
+
const params = [...Object.values(applied), ...where.params, ...guard.params];
|
|
837
|
+
const write = read === this.db ? this.writeHandle() : read;
|
|
838
|
+
const result = write.prepare(text).run(...sqliteParams(params));
|
|
839
|
+
if (request.expected !== undefined && Number(result.changes) === 0)
|
|
840
|
+
throw new WriteConflict();
|
|
841
|
+
if (transactional && Number(result.changes) !== 1)
|
|
842
|
+
refuseUpdateMatches(request, Number(result.changes));
|
|
843
|
+
return { affected: Number(result.changes), applied, sql: { text, params } };
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* Several rows of one table changed by one statement inside a transaction, each as `updateNow` changes one: the
|
|
847
|
+
* same refusals before it, each row's own values and `expected`, one row a key. A `CASE` a column picks each
|
|
848
|
+
* row's value, the WHERE matches each key with its own guard, and RETURNING says which requests matched, so a
|
|
849
|
+
* row whose key or guard missed refuses the batch as it would have refused alone.
|
|
850
|
+
*/
|
|
851
|
+
updateManyNow(requests, handle) {
|
|
852
|
+
const plans = requests.map((request) => ({ request, ...this.updatePlan(request, handle) }));
|
|
853
|
+
const table = sameTable(plans.map((plan) => plan.table));
|
|
854
|
+
const pk = plans[0].pk;
|
|
855
|
+
const types = Object.fromEntries(plans[0].meta.map((column) => [column.name, column.type]));
|
|
856
|
+
const keys = new Set();
|
|
857
|
+
for (const { request } of plans) {
|
|
858
|
+
const key = JSON.stringify(pk.map((column) => String(request.key[column])));
|
|
859
|
+
if (keys.has(key))
|
|
860
|
+
throw new Refusal(`A batch changes each row of ${table} once; that key appears twice.`);
|
|
861
|
+
keys.add(key);
|
|
862
|
+
}
|
|
863
|
+
const results = [];
|
|
864
|
+
for (const chunk of chunked(plans, (plan) => pk.length * (Object.keys(plan.applied).length + 3) + 2 * Object.keys(plan.applied).length)) {
|
|
865
|
+
const match = (request) => {
|
|
866
|
+
const where = buildWhere({ groups: [pk.map((column) => ({ column, op: '=', value: request.key[column] }))] }, ANSI_STYLE);
|
|
867
|
+
return { text: where.text.replace(/^ WHERE /, ''), params: where.params };
|
|
868
|
+
};
|
|
869
|
+
const params = [];
|
|
870
|
+
const columns = [...new Set(chunk.flatMap((plan) => Object.keys(plan.applied)))];
|
|
871
|
+
const assignments = columns.map((column) => {
|
|
872
|
+
const cases = chunk
|
|
873
|
+
.filter((plan) => Object.hasOwn(plan.applied, column))
|
|
874
|
+
.map((plan) => {
|
|
875
|
+
const key = match(plan.request);
|
|
876
|
+
params.push(...key.params, plan.applied[column]);
|
|
877
|
+
return `WHEN ${key.text} THEN ?`;
|
|
878
|
+
});
|
|
879
|
+
const quoted = ANSI_STYLE.quote(column);
|
|
880
|
+
return `${quoted} = CASE ${cases.join(' ')} ELSE ${quoted} END`;
|
|
881
|
+
});
|
|
882
|
+
const wheres = chunk.map((plan) => {
|
|
883
|
+
const key = match(plan.request);
|
|
884
|
+
const guard = buildUpdateGuard(plan.request, types, ANSI_STYLE, 'sqlite', 1);
|
|
885
|
+
params.push(...key.params, ...guard.params);
|
|
886
|
+
return `(${key.text}${guard.text})`;
|
|
887
|
+
});
|
|
888
|
+
const which = chunk.map((plan, index) => {
|
|
889
|
+
const key = match(plan.request);
|
|
890
|
+
params.push(...key.params);
|
|
891
|
+
return `WHEN ${key.text} THEN ${index}`;
|
|
892
|
+
});
|
|
893
|
+
const text = `UPDATE ${ANSI_STYLE.quote(table)} SET ${assignments.join(', ')} WHERE ${wheres.join(' OR ')}` +
|
|
894
|
+
` RETURNING CASE ${which.join(' ')} END AS ${ANSI_STYLE.quote('mistri_batch_row')}`;
|
|
895
|
+
const matched = new Set(handle.prepare(text).all(...sqliteParams(params)).map((row) => Number(row.mistri_batch_row)));
|
|
896
|
+
chunk.forEach((plan, index) => {
|
|
897
|
+
if (!matched.has(index)) {
|
|
898
|
+
if (plan.request.expected !== undefined)
|
|
899
|
+
throw new WriteConflict();
|
|
900
|
+
refuseUpdateMatches(plan.request, 0);
|
|
901
|
+
}
|
|
902
|
+
results.push({ affected: 1, applied: plan.applied, sql: { text, params } });
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
return results;
|
|
906
|
+
}
|
|
907
|
+
/** What `update` refuses before it writes, and the values it writes, coerced to their columns. */
|
|
908
|
+
updatePlan(request, read) {
|
|
770
909
|
const table = this.assertTable(request.table, read);
|
|
771
910
|
/* A view with an INSTEAD OF trigger takes an UPDATE, and the trigger
|
|
772
911
|
decides what changes. Refused as the other dialects refuse it: what the
|
|
@@ -808,36 +947,7 @@ export class SqliteAdapter {
|
|
|
808
947
|
throw new Refusal(`"${column}" is NOT NULL and cannot be emptied.`);
|
|
809
948
|
}
|
|
810
949
|
}
|
|
811
|
-
|
|
812
|
-
groups: [pk.map((column) => ({ column, op: '=', value: request.key[column] }))],
|
|
813
|
-
};
|
|
814
|
-
/* Outside a transaction the count runs first, on the read handle, so the
|
|
815
|
-
check that decides whether to write cannot itself write. Inside one it
|
|
816
|
-
is the UPDATE's own count: the key is the whole primary key, so it
|
|
817
|
-
matches one row or none, and a refusal rolls the transaction back. */
|
|
818
|
-
const transactional = read !== this.db;
|
|
819
|
-
const matches = transactional ? 1 : await this.count(table, filter);
|
|
820
|
-
const refuseMatches = (count) => {
|
|
821
|
-
if (request.expected !== undefined && count === 0)
|
|
822
|
-
throw new WriteConflict();
|
|
823
|
-
throw new Refusal(`Refused: that key matches ${count} rows, not 1. An update is only allowed when it is certain which row it changes.`);
|
|
824
|
-
};
|
|
825
|
-
if (matches !== 1)
|
|
826
|
-
refuseMatches(matches);
|
|
827
|
-
const where = buildWhere(filter, ANSI_STYLE);
|
|
828
|
-
const assignments = Object.keys(applied)
|
|
829
|
-
.map((c) => `${ANSI_STYLE.quote(c)} = ?`)
|
|
830
|
-
.join(', ');
|
|
831
|
-
const guard = buildUpdateGuard(request, Object.fromEntries(meta.map((column) => [column.name, column.type])), ANSI_STYLE, 'sqlite', 1);
|
|
832
|
-
const text = `UPDATE ${scoped ? 'OR ABORT ' : ''}${ANSI_STYLE.quote(table)} SET ${assignments}${where.text}${guard.text}`;
|
|
833
|
-
const params = [...Object.values(applied), ...where.params, ...guard.params];
|
|
834
|
-
const write = read === this.db ? this.writeHandle() : read;
|
|
835
|
-
const result = write.prepare(text).run(...sqliteParams(params));
|
|
836
|
-
if (request.expected !== undefined && Number(result.changes) === 0)
|
|
837
|
-
throw new WriteConflict();
|
|
838
|
-
if (transactional && Number(result.changes) !== 1)
|
|
839
|
-
refuseMatches(Number(result.changes));
|
|
840
|
-
return { affected: Number(result.changes), applied, sql: { text, params } };
|
|
950
|
+
return { table, meta, pk, applied };
|
|
841
951
|
}
|
|
842
952
|
/**
|
|
843
953
|
* Add one row.
|
|
@@ -860,21 +970,7 @@ export class SqliteAdapter {
|
|
|
860
970
|
return this.queueWrite(() => this.insertNow(request));
|
|
861
971
|
}
|
|
862
972
|
async insertNow(request, read = this.db, scoped = false) {
|
|
863
|
-
const table = this.
|
|
864
|
-
const meta = this.tableInfo(table, read);
|
|
865
|
-
const entries = Object.entries(request.values ?? {});
|
|
866
|
-
const applied = {};
|
|
867
|
-
for (const [column, raw] of entries) {
|
|
868
|
-
const declared = meta.find((c) => c.name === column);
|
|
869
|
-
if (!declared)
|
|
870
|
-
throw new Refusal(`${table} has no column called "${column}".`);
|
|
871
|
-
if (generated(declared))
|
|
872
|
-
throw new Refusal(`"${column}" is generated by the database and cannot be written.`);
|
|
873
|
-
applied[column] = coerceForColumn(declared.type || '', raw);
|
|
874
|
-
if (applied[column] === null && declared.notnull === 1) {
|
|
875
|
-
throw new Refusal(`"${column}" is NOT NULL and cannot be null. Leave it out to use its default, if it has one.`);
|
|
876
|
-
}
|
|
877
|
-
}
|
|
973
|
+
const { table, meta, applied } = this.insertPlan(request, read);
|
|
878
974
|
const columns = Object.keys(applied);
|
|
879
975
|
// A schema-level ON CONFLICT REPLACE must never turn creation/restoration
|
|
880
976
|
// into deletion of an existing row. This statement overrides that policy.
|
|
@@ -908,6 +1004,72 @@ export class SqliteAdapter {
|
|
|
908
1004
|
const key = Object.fromEntries(pk.map((c) => [c, (scoped ? raw : safe)[c]]));
|
|
909
1005
|
return { key, row: safe, applied, sql: { text, params } };
|
|
910
1006
|
}
|
|
1007
|
+
/** What `insert` refuses before it writes, and the values it writes, coerced to their columns. */
|
|
1008
|
+
insertPlan(request, read) {
|
|
1009
|
+
const table = this.assertTable(request.table, read);
|
|
1010
|
+
const meta = this.tableInfo(table, read);
|
|
1011
|
+
const applied = {};
|
|
1012
|
+
for (const [column, raw] of Object.entries(request.values ?? {})) {
|
|
1013
|
+
const declared = meta.find((c) => c.name === column);
|
|
1014
|
+
if (!declared)
|
|
1015
|
+
throw new Refusal(`${table} has no column called "${column}".`);
|
|
1016
|
+
if (generated(declared))
|
|
1017
|
+
throw new Refusal(`"${column}" is generated by the database and cannot be written.`);
|
|
1018
|
+
applied[column] = coerceForColumn(declared.type || '', raw);
|
|
1019
|
+
if (applied[column] === null && declared.notnull === 1) {
|
|
1020
|
+
throw new Refusal(`"${column}" is NOT NULL and cannot be null. Leave it out to use its default, if it has one.`);
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
return { table, meta, applied };
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Several rows of one table added by one statement inside a transaction, each as `insertNow` adds one: one
|
|
1027
|
+
* multi-row `INSERT OR ABORT … RETURNING *` for each run of rows naming the same columns. SQLite does not promise
|
|
1028
|
+
* the order RETURNING answers in, so each row is matched back to the request whose values it holds (batchedRows).
|
|
1029
|
+
*/
|
|
1030
|
+
insertManyNow(requests, handle) {
|
|
1031
|
+
const plans = requests.map((request) => this.insertPlan(request, handle));
|
|
1032
|
+
const table = sameTable(plans.map((plan) => plan.table));
|
|
1033
|
+
const pk = plans[0].meta.filter((c) => c.pk > 0).map((c) => c.name);
|
|
1034
|
+
const results = [];
|
|
1035
|
+
for (const run of runsOfColumns(plans)) {
|
|
1036
|
+
const columns = Object.keys(run[0].applied);
|
|
1037
|
+
for (const chunk of chunked(run, () => Math.max(1, columns.length))) {
|
|
1038
|
+
const params = chunk.flatMap((plan) => Object.values(plan.applied));
|
|
1039
|
+
const text = `INSERT OR ABORT INTO ${ANSI_STYLE.quote(table)}` +
|
|
1040
|
+
(columns.length
|
|
1041
|
+
? ` (${columns.map((c) => ANSI_STYLE.quote(c)).join(', ')}) VALUES ${chunk.map(() => `(${columns.map(() => '?').join(', ')})`).join(', ')}`
|
|
1042
|
+
: ` DEFAULT VALUES`) +
|
|
1043
|
+
' RETURNING *';
|
|
1044
|
+
if (!columns.length && chunk.length > 1) {
|
|
1045
|
+
/* DEFAULT VALUES is one row; rows that name no column are added one statement each. */
|
|
1046
|
+
for (const plan of chunk)
|
|
1047
|
+
results.push(this.insertOne(pk, text, [], plan.applied, handle));
|
|
1048
|
+
continue;
|
|
1049
|
+
}
|
|
1050
|
+
const inserted = handle.prepare(text);
|
|
1051
|
+
inserted.setReadBigInts(true);
|
|
1052
|
+
const described = inserted.columns();
|
|
1053
|
+
const stored = inserted.all(...sqliteParams(params));
|
|
1054
|
+
const rows = batchedRows(chunk.map((plan) => plan.applied), stored);
|
|
1055
|
+
rows.forEach((row, index) => {
|
|
1056
|
+
const safe = toJsonSafe(scaleRows([{ ...row }], described.map((c) => c.name), scalesOf(described))[0]);
|
|
1057
|
+
const key = Object.fromEntries(pk.map((c) => [c, safe[c]]));
|
|
1058
|
+
results.push({ key, row: safe, applied: chunk[index].applied, sql: { text, params } });
|
|
1059
|
+
});
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
return results;
|
|
1063
|
+
}
|
|
1064
|
+
insertOne(pk, text, params, applied, handle) {
|
|
1065
|
+
const inserted = handle.prepare(text);
|
|
1066
|
+
inserted.setReadBigInts(true);
|
|
1067
|
+
const described = inserted.columns();
|
|
1068
|
+
const stored = inserted.get(...sqliteParams(params));
|
|
1069
|
+
const safe = toJsonSafe(scaleRows([{ ...stored }], described.map((c) => c.name), scalesOf(described))[0]);
|
|
1070
|
+
const key = Object.fromEntries(pk.map((c) => [c, safe[c]]));
|
|
1071
|
+
return { key, row: safe, applied, sql: { text, params } };
|
|
1072
|
+
}
|
|
911
1073
|
/**
|
|
912
1074
|
* Run a statement the user wrote.
|
|
913
1075
|
*
|
|
@@ -1279,22 +1441,64 @@ export class SqliteAdapter {
|
|
|
1279
1441
|
insert: history ? (r) => history.insert(r, insert) : insert,
|
|
1280
1442
|
remove: history ? (r) => history.remove(r, remove) : remove,
|
|
1281
1443
|
update: history ? (r) => history.update(r, update) : update,
|
|
1444
|
+
/* A batch only beneath the plain facade: a row scope proves each row's statement on its own. History still
|
|
1445
|
+
records each row, around the one statement (gathered). */
|
|
1446
|
+
...(scope === undefined
|
|
1447
|
+
? {
|
|
1448
|
+
insertMany: (rs) => {
|
|
1449
|
+
const many = async (rs) => this.insertManyNow(rs, handle);
|
|
1450
|
+
return history ? gathered(rs, (r, write) => history.insert(r, write), many) : many(rs);
|
|
1451
|
+
},
|
|
1452
|
+
updateMany: (rs) => {
|
|
1453
|
+
const many = async (rs) => this.updateManyNow(rs, handle);
|
|
1454
|
+
return history ? gathered(rs, (r, write) => history.update(r, write), many) : many(rs);
|
|
1455
|
+
},
|
|
1456
|
+
}
|
|
1457
|
+
: {}),
|
|
1282
1458
|
};
|
|
1283
1459
|
let evidence;
|
|
1284
1460
|
let physicalCatalog;
|
|
1461
|
+
/* The schema the last whole verification saw (schemaFingerprint). Unset until one passes, and again while one
|
|
1462
|
+
runs, so a verification that threw is never taken as passed. */
|
|
1463
|
+
let settled;
|
|
1464
|
+
const fingerprint = verify ? schemaFingerprint(handle) : undefined;
|
|
1285
1465
|
const verified = () => {
|
|
1286
1466
|
if (!verify)
|
|
1287
1467
|
return;
|
|
1288
1468
|
if (!handle.isTransaction || !physicalCatalog)
|
|
1289
1469
|
throw new Refusal('The verified transaction is no longer active.');
|
|
1470
|
+
settled = undefined;
|
|
1290
1471
|
const result = verify(handle, physicalCatalog);
|
|
1291
1472
|
if (isPromise(result))
|
|
1292
1473
|
void Promise.prototype.then.call(result, undefined, () => { });
|
|
1293
1474
|
if (result !== undefined || !handle.isTransaction)
|
|
1294
1475
|
throw new Refusal('Transaction verification must be synchronous.');
|
|
1476
|
+
settled = fingerprint();
|
|
1477
|
+
};
|
|
1478
|
+
/* Around each operation, the whole verification only when the schema moved since the last one passed. It runs
|
|
1479
|
+
whole when the transaction opens and again before COMMIT, which is what decides; between, nothing a handler can
|
|
1480
|
+
ask for changes what it attests. BEGIN IMMEDIATE holds the write lock, so no other connection changes the main
|
|
1481
|
+
schema; an operation runs no DDL; and the attested rows (a store's meta row, a membership) are runtime tables a
|
|
1482
|
+
handler cannot write. A schema change on this handle all the same (a TEMP shadow, an index, a trigger) moves the
|
|
1483
|
+
fingerprint, and the next operation verifies whole and refuses before anything reaches COMMIT. Verifying whole
|
|
1484
|
+
around every operation read the catalog twice a call: with forty tables, 100 to 150 ms of a command's every
|
|
1485
|
+
database call (a field report). */
|
|
1486
|
+
const verifiedStep = () => {
|
|
1487
|
+
if (!verify)
|
|
1488
|
+
return;
|
|
1489
|
+
if (!handle.isTransaction || !physicalCatalog)
|
|
1490
|
+
throw new Refusal('The verified transaction is no longer active.');
|
|
1491
|
+
if (settled !== undefined && fingerprint() === settled)
|
|
1492
|
+
return;
|
|
1493
|
+
verified();
|
|
1295
1494
|
};
|
|
1296
1495
|
if (scope !== undefined) {
|
|
1297
|
-
|
|
1496
|
+
/* Its schema captured once here, so the evidence mapping below is the same object every write and is
|
|
1497
|
+
made once per schema rather than copied and checked twice a command (mapScopeCatalog). */
|
|
1498
|
+
const catalog = this.catalogCache.throughSync(epoch, resolverKey(effects?.resolveTriggerEffects), () => {
|
|
1499
|
+
const read = readSqliteScopeCatalog(handle, effects?.resolveTriggerEffects);
|
|
1500
|
+
return frozen({ ...read, schema: captureScopeCatalog(read.schema) });
|
|
1501
|
+
});
|
|
1298
1502
|
physicalCatalog = catalog;
|
|
1299
1503
|
journal?.admit?.(handle, catalog, scope);
|
|
1300
1504
|
// Keep the complete physical graph for row/FK enforcement. Runtime
|
|
@@ -1393,7 +1597,7 @@ export class SqliteAdapter {
|
|
|
1393
1597
|
}
|
|
1394
1598
|
verified();
|
|
1395
1599
|
verifyOrdinary();
|
|
1396
|
-
const invoke = scopedCommandInvoker(operations, scope !== undefined, journal !== undefined, isRuntimeTable,
|
|
1600
|
+
const invoke = scopedCommandInvoker(operations, scope !== undefined, journal !== undefined, isRuntimeTable, verifiedStep);
|
|
1397
1601
|
const out = maintenance
|
|
1398
1602
|
? await maintenance(handle, operations, verified)
|
|
1399
1603
|
: await run(Object.freeze({
|
|
@@ -1407,9 +1611,15 @@ export class SqliteAdapter {
|
|
|
1407
1611
|
insert: (r) => invoke(r, (r) => records.insert(r)),
|
|
1408
1612
|
remove: (r) => invoke(r, (r) => records.remove(r)),
|
|
1409
1613
|
update: (r) => invoke(r, (r) => records.update(r)),
|
|
1614
|
+
...(scope === undefined && records.insertMany && records.updateMany
|
|
1615
|
+
? {
|
|
1616
|
+
insertMany: (rs) => invoke(rs, (rs) => records.insertMany(rs)),
|
|
1617
|
+
updateMany: (rs) => invoke(rs, (rs) => records.updateMany(rs)),
|
|
1618
|
+
}
|
|
1619
|
+
: {}),
|
|
1410
1620
|
// Beneath the row scope: the flows' command tells only of its own target, which it read through the scope.
|
|
1411
1621
|
...(history ? { tell: (r) => invoke(r, (r) => history.tell(r)) } : {}),
|
|
1412
|
-
}), journal?.open(handle, operations, verify ?
|
|
1622
|
+
}), journal?.open(handle, operations, verify ? verifiedStep : undefined), evidence);
|
|
1413
1623
|
await operations.finish();
|
|
1414
1624
|
witness?.settle();
|
|
1415
1625
|
await finishWriteCapture(history);
|
|
@@ -1498,7 +1708,7 @@ export class SqliteAdapter {
|
|
|
1498
1708
|
the first write of an epoch reads the list, and no later one asks. */
|
|
1499
1709
|
const epoch = this.epochOf(db);
|
|
1500
1710
|
if (epoch !== undefined) {
|
|
1501
|
-
const names = this.catalogCache.throughSync(epoch, 'tables', () => new Set(db.prepare(`SELECT name FROM sqlite_master WHERE type IN ('table','view')`).all().map((row) => row.name)));
|
|
1711
|
+
const names = this.catalogCache.throughSync(epoch, 'tables', () => frozen(new Set(db.prepare(`SELECT name FROM sqlite_master WHERE type IN ('table','view')`).all().map((row) => row.name))));
|
|
1502
1712
|
if (!names.has(name))
|
|
1503
1713
|
throw new Error(`No table or view called "${name}" in this database.`);
|
|
1504
1714
|
return name;
|
|
@@ -1511,7 +1721,7 @@ export class SqliteAdapter {
|
|
|
1511
1721
|
/** A table's columns, as a write reads them: from the catalog, or inside a
|
|
1512
1722
|
* write transaction from what was read under the epoch it holds. */
|
|
1513
1723
|
tableInfo(table, db) {
|
|
1514
|
-
return this.catalogCache.throughSync(this.epochOf(db), `columns:${table}`, () => db.prepare(`PRAGMA main.table_xinfo(${ANSI_STYLE.quote(table)})`).all().filter((c) => c.hidden !== 1));
|
|
1724
|
+
return this.catalogCache.throughSync(this.epochOf(db), `columns:${table}`, () => frozen(db.prepare(`PRAGMA main.table_xinfo(${ANSI_STYLE.quote(table)})`).all().filter((c) => c.hidden !== 1)));
|
|
1515
1725
|
}
|
|
1516
1726
|
/** Whether a relation is a view, as a write reads it: kept under the epoch, as its columns are. */
|
|
1517
1727
|
isView(table, db) {
|
|
@@ -36,6 +36,16 @@ export declare function transactionEpoch(db: DatabaseSync, journal?: {
|
|
|
36
36
|
epoch?: string;
|
|
37
37
|
journal: boolean;
|
|
38
38
|
};
|
|
39
|
+
/**
|
|
40
|
+
* The schema a write handle sees, cheaply, for asking between a verified transaction's operations whether it moved:
|
|
41
|
+
* main's `schema_version` and the handle's own TEMP `schema_version`, each bumped by any schema change. The statement
|
|
42
|
+
* form `PRAGMA temp.schema_version` reads the TEMP schema's own counter, where the table-valued
|
|
43
|
+
* `temp.pragma_schema_version` (SCHEMA_EPOCH) answers main's. A manual `PRAGMA schema_version = …` reset is outside
|
|
44
|
+
* this contract, as it is for captureSchemaLifetime: no business operation can send one.
|
|
45
|
+
*/
|
|
46
|
+
export declare function schemaFingerprint(db: DatabaseSync): () => string;
|
|
47
|
+
/** `db.prepare(sql)`, kept per handle: for the catalog's and a store layout's questions, asked again and again. */
|
|
48
|
+
export declare function cachedStatement(db: DatabaseSync, sql: string): StatementSync;
|
|
39
49
|
/** The schema and whether a store's meta row says its version, in one statement; undefined when the store is not there. */
|
|
40
50
|
export declare function storeEpoch(db: DatabaseSync, meta: {
|
|
41
51
|
table: string;
|
|
@@ -128,6 +128,34 @@ export function transactionEpoch(db, journal) {
|
|
|
128
128
|
return { journal: false };
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* The schema a write handle sees, cheaply, for asking between a verified transaction's operations whether it moved:
|
|
133
|
+
* main's `schema_version` and the handle's own TEMP `schema_version`, each bumped by any schema change. The statement
|
|
134
|
+
* form `PRAGMA temp.schema_version` reads the TEMP schema's own counter, where the table-valued
|
|
135
|
+
* `temp.pragma_schema_version` (SCHEMA_EPOCH) answers main's. A manual `PRAGMA schema_version = …` reset is outside
|
|
136
|
+
* this contract, as it is for captureSchemaLifetime: no business operation can send one.
|
|
137
|
+
*/
|
|
138
|
+
export function schemaFingerprint(db) {
|
|
139
|
+
const main = cachedStatement(db, 'PRAGMA main.schema_version'), temp = cachedStatement(db, 'PRAGMA temp.schema_version');
|
|
140
|
+
return () => `${main.get()?.schema_version}.${temp.get()?.schema_version}`;
|
|
141
|
+
}
|
|
142
|
+
/* A catalog scan asks the same few hundred questions every time it runs, and preparing them was most of its cost (a
|
|
143
|
+
field report); a store's layout check asks its few on every command. Each handle keeps its own, by text: a statement is bound to its connection, and SQLite prepares one
|
|
144
|
+
again by itself when the schema under it has moved. A catalog of more tables than the bound starts the map over. */
|
|
145
|
+
const statements = new WeakMap();
|
|
146
|
+
/** `db.prepare(sql)`, kept per handle: for the catalog's and a store layout's questions, asked again and again. */
|
|
147
|
+
export function cachedStatement(db, sql) {
|
|
148
|
+
let kept = statements.get(db);
|
|
149
|
+
if (!kept)
|
|
150
|
+
statements.set(db, (kept = new Map()));
|
|
151
|
+
let statement = kept.get(sql);
|
|
152
|
+
if (!statement) {
|
|
153
|
+
if (kept.size >= 2048)
|
|
154
|
+
kept.clear();
|
|
155
|
+
kept.set(sql, (statement = db.prepare(sql)));
|
|
156
|
+
}
|
|
157
|
+
return statement;
|
|
158
|
+
}
|
|
131
159
|
/** The schema and whether a store's meta row says its version, in one statement; undefined when the store is not there. */
|
|
132
160
|
export function storeEpoch(db, meta) {
|
|
133
161
|
try {
|
|
@@ -38,12 +38,17 @@ export async function explainStatement(spanId) {
|
|
|
38
38
|
return explain ? explain() : undefined;
|
|
39
39
|
}
|
|
40
40
|
const EXPLAINABLE = new Set(['SELECT', 'WITH']);
|
|
41
|
+
/* A catalog read or a PRAGMA: the engine's own checks, which run by the dozen
|
|
42
|
+
around every command, and each span cost two random ids even in a trace the
|
|
43
|
+
exporter drops (a field report). They are spans only in a sampled trace. */
|
|
44
|
+
const INTERNAL = /^\s*PRAGMA\b|\b(?:sqlite_schema|sqlite_master|sqlite_temp_schema|sqlite_temp_master|pg_catalog|information_schema)\b/i;
|
|
41
45
|
/** A statement's span, begun but not made current: a statement has no children. The caller ends it. */
|
|
42
46
|
function beginStatement(dialect, text, params, explain) {
|
|
43
47
|
const { operation, table, summary } = summarizeStatement(text);
|
|
44
48
|
const span = startSpan(summary, {
|
|
45
49
|
kind: 'client',
|
|
46
50
|
childOnly: true,
|
|
51
|
+
sampledOnly: INTERNAL.test(text),
|
|
47
52
|
attributes: {
|
|
48
53
|
'db.system.name': dialect,
|
|
49
54
|
'db.operation.name': operation,
|