Skip to content

Commit 2bd5694

Browse files
authored
sqlite: allow returning ArrayBufferViews from user-defined functions
PR-URL: nodejs#56790 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ed52ab9 commit 2bd5694

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
@@ -518,13 +518,13 @@ more data types than SQLite, only a subset of JavaScript types are supported.
518518
Attempting to write an unsupported data type to SQLite will result in an
519519
exception.
520520

521-
| SQLite | JavaScript |
522-
| --------- | -------------------- |
523-
| `NULL` | {null} |
524-
| `INTEGER` | {number} or {bigint} |
525-
| `REAL` | {number} |
526-
| `TEXT` | {string} |
527-
| `BLOB` | {Uint8Array} |
521+
| SQLite | JavaScript |
522+
| --------- | -------------------------- |
523+
| `NULL` | {null} |
524+
| `INTEGER` | {number} or {bigint} |
525+
| `REAL` | {number} |
526+
| `TEXT` | {string} |
527+
| `BLOB` | {TypedArray} or {DataView} |
528528

529529
## `sqlite.constants`
530530

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)