Skip to content

Commit 632effa

Browse files
author
kai.zhu
committed
- allow "exec" commmand to pass dictionary-params to webworker
- add test passing dictionary-params to webworker
1 parent 72b5dcd commit 632effa

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

src/api.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
685685
* @param {string} sql a string containing some SQL text to execute
686686
* @return {Database.QueryExecResult[]} The results of each statement
687687
*/
688-
Database.prototype["exec"] = function exec(sql) {
688+
Database.prototype["exec"] = function exec(sql, params) {
689689
var curresult;
690690
var stmt;
691691
if (!this.db) {
@@ -713,6 +713,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
713713
if (pStmt !== NULL) {
714714
curresult = null;
715715
stmt = new Statement(pStmt, this);
716+
if (params != null) {
717+
stmt.bind(params);
718+
}
716719
while (stmt["step"]()) {
717720
if (curresult === null) {
718721
curresult = {

src/worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function onModuleReady(SQL) {
3131
}
3232
return postMessage({
3333
id: data["id"],
34-
results: db.exec(data["sql"])
34+
results: db.exec(data["sql"], data["params"])
3535
});
3636
case "each":
3737
if (db === null) {

test/test_worker.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// TODO: Instead of using tiny-worker, we could use the new Node 11 workers via
2-
// node --experimental-worker test/all.js
1+
// TODO: Instead of using puppeteer, we could use the new Node 11 workers via
2+
// node --experimental-worker test/all.js
33
// Then we could do this:
44
//const { Worker } = require('worker_threads');
5-
// But it turns out that the worker_threads interface is just different enough not to work.
5+
// But it turns out that the worker_threads interface is just different enough not to work.
66
var puppeteer = require("puppeteer");
77
var path = require("path");
88
var fs = require("fs");
@@ -40,7 +40,7 @@ exports.test = async function test(SQL, assert) {
4040
console.error("Skipping worker test for " + file + ". Not implemented yet");
4141
return;
4242
};
43-
// If we use tiny-worker, we need to pass in this new cwd as the root of the file being loaded:
43+
// If we use puppeteer, we need to pass in this new cwd as the root of the file being loaded:
4444
const filename = "../dist/worker." + file + ".js";
4545
var worker = await Worker.fromFile(path.join(__dirname, filename));
4646
var data = await worker.postMessage({ id: 1, action: 'open' });
@@ -50,20 +50,41 @@ exports.test = async function test(SQL, assert) {
5050
data = await worker.postMessage({
5151
id: 2,
5252
action: 'exec',
53+
params: {
54+
":num2": 2,
55+
"@str2": 'b',
56+
// test_worker.js has issue message-passing Uint8Array
57+
// but it works fine in real-world browser-usage
58+
// "$hex2": new Uint8Array([0x00, 0x42]),
59+
":num3": 3,
60+
"@str3": 'c'
61+
// "$hex3": new Uint8Array([0x00, 0x44])
62+
},
5363
sql: "CREATE TABLE test (num, str, hex);" +
5464
"INSERT INTO test VALUES (1, 'a', x'0042');" +
65+
"INSERT INTO test VALUES (:num2, @str2, x'0043');" +
66+
// test passing params split across multi-statement "exec"
67+
"INSERT INTO test VALUES (:num3, @str3, x'0044');" +
5568
"SELECT * FROM test;"
5669
});
5770
assert.strictEqual(data.id, 2, "Correct id");
71+
// debug error
72+
assert.strictEqual(data.error, undefined, data.error);
5873
var results = data.results;
5974
assert.ok(Array.isArray(results), 'Correct result type');
60-
assert.strictEqual(results.length, 1, 'Expected exactly 1 row');
61-
var row = results[0];
62-
assert.strictEqual(typeof row, 'object', 'Type of the returned row');
63-
assert.deepEqual(row.columns, ['num', 'str', 'hex'], 'Reading column names');
64-
assert.strictEqual(row.values[0][0], 1, 'Reading number');
65-
assert.strictEqual(row.values[0][1], 'a', 'Reading string');
66-
assert.deepEqual(obj2array(row.values[0][2]), [0x00, 0x42], 'Reading BLOB byte');
75+
assert.strictEqual(results.length, 1, 'Expected exactly 1 table');
76+
var table = results[0];
77+
assert.strictEqual(typeof table, 'object', 'Type of the returned table');
78+
assert.deepEqual(table.columns, ['num', 'str', 'hex'], 'Reading column names');
79+
assert.strictEqual(table.values[0][0], 1, 'Reading number');
80+
assert.strictEqual(table.values[0][1], 'a', 'Reading string');
81+
assert.deepEqual(obj2array(table.values[0][2]), [0x00, 0x42], 'Reading BLOB byte');
82+
assert.strictEqual(table.values[1][0], 2, 'Reading number');
83+
assert.strictEqual(table.values[1][1], 'b', 'Reading string');
84+
assert.deepEqual(obj2array(table.values[1][2]), [0x00, 0x43], 'Reading BLOB byte');
85+
assert.strictEqual(table.values[2][0], 3, 'Reading number');
86+
assert.strictEqual(table.values[2][1], 'c', 'Reading string');
87+
assert.deepEqual(obj2array(table.values[2][2]), [0x00, 0x44], 'Reading BLOB byte');
6788

6889
data = await worker.postMessage({ action: 'export' });
6990
var header = "SQLite format 3\0";

0 commit comments

Comments
 (0)