Skip to content

Commit

Permalink
sqlite: allow returning ArrayBufferViews from user-defined functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Renegade334 committed Jan 27, 2025
1 parent 50d405a commit 9fb613e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
14 changes: 7 additions & 7 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,13 @@ more data types than SQLite, only a subset of JavaScript types are supported.
Attempting to write an unsupported data type to SQLite will result in an
exception.

| SQLite | JavaScript |
| --------- | -------------------- |
| `NULL` | {null} |
| `INTEGER` | {number} or {bigint} |
| `REAL` | {number} |
| `TEXT` | {string} |
| `BLOB` | {Uint8Array} |
| SQLite | JavaScript |
| --------- | -------------------------- |
| `NULL` | {null} |
| `INTEGER` | {number} or {bigint} |
| `REAL` | {number} |
| `TEXT` | {string} |
| `BLOB` | {TypedArray} or {DataView} |

## `sqlite.constants`

Expand Down
2 changes: 1 addition & 1 deletion src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class UserDefinedFunction {
} else if (result->IsString()) {
Utf8Value val(isolate, result.As<String>());
sqlite3_result_text(ctx, *val, val.length(), SQLITE_TRANSIENT);
} else if (result->IsUint8Array()) {
} else if (result->IsArrayBufferView()) {
ArrayBufferViewContents<uint8_t> buf(result);
sqlite3_result_blob(ctx, buf.data(), buf.length(), SQLITE_TRANSIENT);
} else if (result->IsBigInt()) {
Expand Down
8 changes: 7 additions & 1 deletion test/parallel/test-sqlite-custom-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,18 @@ suite('DatabaseSync.prototype.function()', () => {
db.function('retString', () => { return 'foo'; });
db.function('retBigInt', () => { return 5n; });
db.function('retUint8Array', () => { return new Uint8Array([1, 2, 3]); });
db.function('retArrayBufferView', () => {
const arrayBuffer = new Uint8Array([1, 2, 3]).buffer;
return new DataView(arrayBuffer);
});
const stmt = db.prepare(`SELECT
retUndefined() AS retUndefined,
retNull() AS retNull,
retNumber() AS retNumber,
retString() AS retString,
retBigInt() AS retBigInt,
retUint8Array() AS retUint8Array
retUint8Array() AS retUint8Array,
retArrayBufferView() AS retArrayBufferView
`);
assert.deepStrictEqual(stmt.get(), {
__proto__: null,
Expand All @@ -290,6 +295,7 @@ suite('DatabaseSync.prototype.function()', () => {
retString: 'foo',
retBigInt: 5,
retUint8Array: new Uint8Array([1, 2, 3]),
retArrayBufferView: new Uint8Array([1, 2, 3]),
});
});

Expand Down

0 comments on commit 9fb613e

Please sign in to comment.