Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite: allow returning ArrayBufferViews from user-defined functions #56790

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,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
Loading