Skip to content

Commit f2b7800

Browse files
committed
sqlite: bind ArrayBuffer
1 parent c9acf34 commit f2b7800

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

src/node_sqlite.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ void JSValueToSQLiteResult(Isolate* isolate,
198198
} else if (value->IsString()) {
199199
Utf8Value val(isolate, value.As<String>());
200200
sqlite3_result_text(ctx, *val, val.length(), SQLITE_TRANSIENT);
201-
} else if (value->IsArrayBufferView()) {
201+
} else if (value->IsArrayBufferView() || value->IsArrayBuffer() ||
202+
value->IsSharedArrayBuffer()) {
202203
ArrayBufferViewContents<uint8_t> buf(value);
203204
sqlite3_result_blob(ctx, buf.data(), buf.length(), SQLITE_TRANSIENT);
204205
} else if (value->IsBigInt()) {
@@ -2160,7 +2161,8 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) {
21602161
int anon_idx = 1;
21612162
int anon_start = 0;
21622163

2163-
if (args[0]->IsObject() && !args[0]->IsArrayBufferView()) {
2164+
if (args[0]->IsObject() && !args[0]->IsArrayBufferView() &&
2165+
!args[0]->IsArrayBuffer() && !args[0]->IsSharedArrayBuffer()) {
21642166
Local<Object> obj = args[0].As<Object>();
21652167
Local<Context> context = Isolate::GetCurrent()->GetCurrentContext();
21662168
Local<Array> keys;
@@ -2286,7 +2288,8 @@ bool StatementSync::BindValue(const Local<Value>& value, const int index) {
22862288
}
22872289
} else if (value->IsNull()) {
22882290
r = sqlite3_bind_null(statement_, index);
2289-
} else if (value->IsArrayBufferView()) {
2291+
} else if (value->IsArrayBufferView() || value->IsArrayBuffer() ||
2292+
value->IsSharedArrayBuffer()) {
22902293
ArrayBufferViewContents<uint8_t> buf(value);
22912294
r = sqlite3_bind_blob64(statement_,
22922295
index,

test/parallel/test-sqlite-typed-array-and-data-view.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ function nextDb() {
1414
}
1515

1616
const arrayBuffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]).buffer;
17+
const sharedArrayBuffer = new SharedArrayBuffer(8);
18+
const typedArrayOnSharedArrayBuffer = new Uint8Array(sharedArrayBuffer);
19+
typedArrayOnSharedArrayBuffer.set([1, 2, 3, 4, 5, 6, 7, 8]);
20+
1721
const TypedArrays = [
1822
['Int8Array', Int8Array],
1923
['Uint8Array', Uint8Array],
@@ -60,3 +64,68 @@ suite('StatementSync with TypedArray/DataView', () => {
6064
});
6165
}
6266
});
67+
68+
suite('StatementSync with ArrayBuffer and SharedArrayBuffer', () => {
69+
const buffers = [
70+
['ArrayBuffer', arrayBuffer],
71+
['SharedArrayBuffer', sharedArrayBuffer],
72+
];
73+
74+
for (const [displayName, buffer] of buffers) {
75+
test(`${displayName} - anonymous binding`, (t) => {
76+
const db = new DatabaseSync(nextDb());
77+
t.after(() => { db.close(); });
78+
db.exec('CREATE TABLE test (data BLOB)');
79+
// insert
80+
{
81+
const stmt = db.prepare('INSERT INTO test VALUES (?)');
82+
stmt.run(buffer);
83+
}
84+
// select all
85+
{
86+
const stmt = db.prepare('SELECT * FROM test');
87+
const row = stmt.get();
88+
t.assert.ok(row.data instanceof Uint8Array);
89+
t.assert.strictEqual(row.data.length, 8);
90+
t.assert.deepStrictEqual(row.data, new Uint8Array(arrayBuffer));
91+
}
92+
// query
93+
{
94+
const stmt = db.prepare('SELECT * FROM test WHERE data = ?');
95+
const rows = stmt.all(buffer);
96+
t.assert.strictEqual(rows.length, 1);
97+
t.assert.ok(rows[0].data instanceof Uint8Array);
98+
t.assert.strictEqual(rows[0].data.length, 8);
99+
t.assert.deepStrictEqual(rows[0].data, new Uint8Array(arrayBuffer));
100+
}
101+
});
102+
103+
test(`${displayName} - named binding (object)`, (t) => {
104+
const db = new DatabaseSync(nextDb());
105+
t.after(() => { db.close(); });
106+
db.exec('CREATE TABLE test (data BLOB)');
107+
// insert
108+
{
109+
const stmt = db.prepare('INSERT INTO test VALUES ($data)');
110+
stmt.run({ '$data': buffer });
111+
}
112+
// select all
113+
{
114+
const stmt = db.prepare('SELECT * FROM test');
115+
const row = stmt.get();
116+
t.assert.ok(row.data instanceof Uint8Array);
117+
t.assert.strictEqual(row.data.length, 8);
118+
t.assert.deepStrictEqual(row.data, new Uint8Array(arrayBuffer));
119+
}
120+
// query
121+
{
122+
const stmt = db.prepare('SELECT * FROM test WHERE data = $data');
123+
const rows = stmt.all({ '$data': buffer });
124+
t.assert.strictEqual(rows.length, 1);
125+
t.assert.ok(rows[0].data instanceof Uint8Array);
126+
t.assert.strictEqual(rows[0].data.length, 8);
127+
t.assert.deepStrictEqual(rows[0].data, new Uint8Array(arrayBuffer));
128+
}
129+
});
130+
}
131+
});

0 commit comments

Comments
 (0)