Skip to content

Commit e285668

Browse files
committed
exclude AB from named param binding, move tests
1 parent 1f2afe9 commit e285668

File tree

3 files changed

+73
-12
lines changed

3 files changed

+73
-12
lines changed

src/node_sqlite.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,8 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) {
21612161
int anon_idx = 1;
21622162
int anon_start = 0;
21632163

2164-
if (args[0]->IsObject() && !args[0]->IsArrayBufferView()) {
2164+
if (args[0]->IsObject() && !args[0]->IsArrayBufferView() &&
2165+
!args[0]->IsArrayBuffer() && !args[0]->IsSharedArrayBuffer()) {
21652166
Local<Object> obj = args[0].As<Object>();
21662167
Local<Context> context = Isolate::GetCurrent()->GetCurrentContext();
21672168
Local<Array> keys;

test/parallel/test-sqlite-data-types.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,10 @@ suite('data binding and mapping', () => {
8080
text: '',
8181
buf: new Uint8Array(),
8282
});
83-
84-
const uint8Array = new Uint8Array([ 49, 50, 51, 52 ]);
85-
const arrayBuffer = uint8Array.buffer;
86-
t.assert.deepStrictEqual(
87-
stmt.run(5, null, null, null, arrayBuffer),
88-
{ changes: 1, lastInsertRowid: 5 }
89-
);
90-
t.assert.deepStrictEqual(
91-
query.get(5),
92-
{ __proto__: null, key: 5, int: null, double: null, text: null, buf: uint8Array }
93-
);
9483
});
9584

85+
86+
9687
test('large strings are bound correctly', (t) => {
9788
const db = new DatabaseSync(nextDb());
9889
t.after(() => { db.close(); });

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)