Skip to content

Commit 3999786

Browse files
Renegade334targos
authored andcommitted
sqlite: allow returning ArrayBufferViews from user-defined functions
PR-URL: #56790 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e1f86c1 commit 3999786

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

doc/api/sqlite.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,13 @@ more data types than SQLite, only a subset of JavaScript types are supported.
500500
Attempting to write an unsupported data type to SQLite will result in an
501501
exception.
502502

503-
| SQLite | JavaScript |
504-
| --------- | -------------------- |
505-
| `NULL` | {null} |
506-
| `INTEGER` | {number} or {bigint} |
507-
| `REAL` | {number} |
508-
| `TEXT` | {string} |
509-
| `BLOB` | {Uint8Array} |
503+
| SQLite | JavaScript |
504+
| --------- | -------------------------- |
505+
| `NULL` | {null} |
506+
| `INTEGER` | {number} or {bigint} |
507+
| `REAL` | {number} |
508+
| `TEXT` | {string} |
509+
| `BLOB` | {TypedArray} or {DataView} |
510510

511511
## `sqlite.constants`
512512

src/node_sqlite.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class UserDefinedFunction {
213213
} else if (result->IsString()) {
214214
Utf8Value val(isolate, result.As<String>());
215215
sqlite3_result_text(ctx, *val, val.length(), SQLITE_TRANSIENT);
216-
} else if (result->IsUint8Array()) {
216+
} else if (result->IsArrayBufferView()) {
217217
ArrayBufferViewContents<uint8_t> buf(result);
218218
sqlite3_result_blob(ctx, buf.data(), buf.length(), SQLITE_TRANSIENT);
219219
} else if (result->IsBigInt()) {

test/parallel/test-sqlite-custom-functions.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,18 @@ suite('DatabaseSync.prototype.function()', () => {
274274
db.function('retString', () => { return 'foo'; });
275275
db.function('retBigInt', () => { return 5n; });
276276
db.function('retUint8Array', () => { return new Uint8Array([1, 2, 3]); });
277+
db.function('retArrayBufferView', () => {
278+
const arrayBuffer = new Uint8Array([1, 2, 3]).buffer;
279+
return new DataView(arrayBuffer);
280+
});
277281
const stmt = db.prepare(`SELECT
278282
retUndefined() AS retUndefined,
279283
retNull() AS retNull,
280284
retNumber() AS retNumber,
281285
retString() AS retString,
282286
retBigInt() AS retBigInt,
283-
retUint8Array() AS retUint8Array
287+
retUint8Array() AS retUint8Array,
288+
retArrayBufferView() AS retArrayBufferView
284289
`);
285290
assert.deepStrictEqual(stmt.get(), {
286291
__proto__: null,
@@ -290,6 +295,7 @@ suite('DatabaseSync.prototype.function()', () => {
290295
retString: 'foo',
291296
retBigInt: 5,
292297
retUint8Array: new Uint8Array([1, 2, 3]),
298+
retArrayBufferView: new Uint8Array([1, 2, 3]),
293299
});
294300
});
295301

0 commit comments

Comments
 (0)