Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose sqlite3_update_hook #210

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ CFILES = \
main.c \
libauthorizer.c \
libfunction.c \
libhook.c \
libprogress.c \
libvfs.c \
$(CFILES_EXTRA)

JSFILES = \
src/libauthorizer.js \
src/libfunction.js \
src/libhook.js \
src/libprogress.js \
src/libvfs.js

Expand Down Expand Up @@ -72,6 +74,7 @@ EMFLAGS_LIBRARIES = \
--js-library src/libadapters.js \
--post-js src/libauthorizer.js \
--post-js src/libfunction.js \
--post-js src/libhook.js \
--post-js src/libprogress.js \
--post-js src/libvfs.js

Expand Down
10 changes: 5 additions & 5 deletions dist/wa-sqlite-async.mjs

Large diffs are not rendered by default.

Binary file modified dist/wa-sqlite-async.wasm
Binary file not shown.
10 changes: 5 additions & 5 deletions dist/wa-sqlite-jspi.mjs

Large diffs are not rendered by default.

Binary file modified dist/wa-sqlite-jspi.wasm
Binary file not shown.
10 changes: 5 additions & 5 deletions dist/wa-sqlite.mjs

Large diffs are not rendered by default.

Binary file modified dist/wa-sqlite.wasm
Binary file not shown.
4 changes: 3 additions & 1 deletion src/asyncify_imports.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@
"ipppppip",
"ipppppip_async",
"ipppiiip",
"ipppiiip_async"
"ipppiiip_async",
"vppippii",
"vppippii_async"
]
1 change: 1 addition & 0 deletions src/libadapters.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ DECLARE(I, ippppip, P, P, P, P, I, P);
DECLARE(I, ippipppp, P, P, I, P, P, P, P);
DECLARE(I, ipppppip, P, P, P, P, P, I, P);
DECLARE(I, ipppiiip, P, P, P, I, I, I, P);
DECLARE(void, vppippii, P, P, I, P, P, I, I);
#undef DECLARE
#undef P
#undef I
Expand Down
1 change: 1 addition & 0 deletions src/libadapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const SIGNATURES = [
'ippipppp', // xAuthorize
'ipppppip', // xOpen
'ipppiiip', // xShmMap
'vppippii', // xUpdateHook
];

// This object will define the methods callable from WebAssembly.
Expand Down
27 changes: 27 additions & 0 deletions src/libhook.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.
#include <stdio.h>
#include <emscripten.h>
#include <sqlite3.h>

#include "libadapters.h"

#define CALL_JS(SIGNATURE, KEY, ...) \
(asyncFlags ? \
SIGNATURE##_async(KEY, __VA_ARGS__) : \
SIGNATURE(KEY, __VA_ARGS__))

static void libhook_xUpdateHook(
void* pApp,
int iUpdateType,
const char* dbName,
const char* tblName,
sqlite3_int64 rowid) {
int hi32 = ((rowid & 0xFFFFFFFF00000000LL) >> 32);
int lo32 = (rowid & 0xFFFFFFFFLL);
const int asyncFlags = pApp ? *(int *)pApp : 0;
CALL_JS(vppippii, pApp, pApp, iUpdateType, dbName, tblName, lo32, hi32);
}

void EMSCRIPTEN_KEEPALIVE libhook_update_hook(sqlite3* db, int xUpdateHook, void* pApp) {
sqlite3_update_hook(db, xUpdateHook ? &libhook_xUpdateHook : NULL, pApp);
}
29 changes: 29 additions & 0 deletions src/libhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.
// This file should be included in the build with --post-js.

(function() {
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
let pAsyncFlags = 0;

Module['update_hook'] = function(db, xUpdateHook) {
if (pAsyncFlags) {
Module['deleteCallback'](pAsyncFlags);
Module['_sqlite3_free'](pAsyncFlags);
pAsyncFlags = 0;
}

pAsyncFlags = Module['_sqlite3_malloc'](4);
setValue(pAsyncFlags, xUpdateHook instanceof AsyncFunction ? 1 : 0, 'i32');

ccall(
'libhook_update_hook',
'void',
['number', 'number', 'number'],
[db, xUpdateHook ? 1 : 0, pAsyncFlags]);
if (xUpdateHook) {
Module['setCallback'](pAsyncFlags, (_, iUpdateType, dbName, tblName, lo32, hi32) => {
return xUpdateHook(iUpdateType, dbName, tblName, lo32, hi32);
});
}
};
})();
21 changes: 21 additions & 0 deletions src/sqlite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,27 @@ export function Factory(Module) {
};
})();

sqlite3.update_hook = function(db, xUpdateHook) {
verifyDatabase(db);

// Convert SQLite callback arguments to JavaScript-friendly arguments.
function cvtArgs(iUpdateType, dbName, tblName, lo32, hi32) {
return [
iUpdateType,
Module.UTF8ToString(dbName),
Module.UTF8ToString(tblName),
cvt32x2ToBigInt(lo32, hi32)
];
};
function adapt(f) {
return f instanceof AsyncFunction ?
(async (iUpdateType, dbName, tblName, lo32, hi32) => f(...cvtArgs(iUpdateType, dbName, tblName, lo32, hi32))) :
((iUpdateType, dbName, tblName, lo32, hi32) => f(...cvtArgs(iUpdateType, dbName, tblName, lo32, hi32)));
}

Module.update_hook(db, adapt(xUpdateHook));
};;

sqlite3.value = function(pValue) {
const type = sqlite3.value_type(pValue);
switch (type) {
Expand Down
20 changes: 20 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,26 @@ declare interface SQLiteAPI {
*/
step(stmt: number): Promise<number>;

/**
* Register an update hook
*
* The callback is invoked whenever a row is updated, inserted, or deleted
* in a rowid table on this connection.
* @see https://www.sqlite.org/c3ref/update_hook.html
*
* updateType is one of:
* - SQLITE_DELETE: 9
* - SQLITE_INSERT: 18
* - SQLITE_UPDATE: 23
* @see https://www.sqlite.org/c3ref/c_alter_table.html
*
* @param db database pointer
* @param callback
*/
update_hook(
db: number,
callback: (updateType: number, dbName: string|null, tblName: string|null, rowid: bigint) => void): void;

/**
* Extract a value from `sqlite3_value`
*
Expand Down
42 changes: 41 additions & 1 deletion test/callbacks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,5 +369,45 @@ for (const [key, factory] of FACTORIES) {
expect(authorizations.length).toBeGreaterThan(0);
});
});
}

describe(`${key} update_hook`, function() {
let db;
beforeEach(async function() {
db = await sqlite3.open_v2(':memory:');
});

afterEach(async function() {
await sqlite3.close(db);
});

it('should call update hook', async function() {
let rc;

let calls = [];
sqlite3.update_hook(db, (updateType, dbName, tblName, rowid) => {
calls.push([updateType, dbName, tblName, rowid]);
});

rc = await sqlite3.exec(db, `
CREATE TABLE t(i integer primary key, x);
INSERT INTO t VALUES (1, 'foo'), (2, 'bar'), (12345678987654321, 'baz');
`);
expect(rc).toEqual(SQLite.SQLITE_OK);
expect(calls).toEqual([
[18, "main", "t", 1n],
[18, "main", "t", 2n],
[18, "main", "t", 12345678987654321n],
]);

calls.splice(0, calls.length);

await sqlite3.exec(db, `DELETE FROM t WHERE i = 2`);
expect(calls).toEqual([[9, "main", "t", 2n]]);

calls.splice(0, calls.length);

await sqlite3.exec(db, `UPDATE t SET x = 'bar' WHERE i = 1`);
expect(calls).toEqual([[23, "main", "t", 1n]]);
});
});
}
Loading