GeezerVison is showing github.com — sqlite: support TypedArray and DataView in `StatementSync` · nodejs/node@1e201fd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/sqlite.md +20-4Lines changed: 20 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -329,11 +329,15 @@ over hand-crafted SQL strings when handling user input. | 329 | 329 |
| 330 | 330 | <!-- YAML | 331 | 331 | added: v22.5.0 | | 332 | +changes: | | 333 | + - version: REPLACEME | | 334 | + pr-url: https://github.com/nodejs/node/pull/56385 | | 335 | + description: Add support for `DataView` and typed array objects for `anonymousParameters`. | 332 | 336 | --> | 333 | 337 |
| 334 | 338 | * `namedParameters` {Object} An optional object used to bind named parameters. | 335 | 339 | The keys of this object are used to configure the mapping. | 336 | | -* `...anonymousParameters` {null|number|bigint|string|Buffer|Uint8Array} Zero or | | 340 | +* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or | 337 | 341 | more values to bind to anonymous parameters. | 338 | 342 | * Returns: {Array} An array of objects. Each object corresponds to a row | 339 | 343 | returned by executing the prepared statement. The keys and values of each | @@ -361,11 +365,15 @@ execution of this prepared statement. This property is a wrapper around | 361 | 365 |
| 362 | 366 | <!-- YAML | 363 | 367 | added: v22.5.0 | | 368 | +changes: | | 369 | + - version: REPLACEME | | 370 | + pr-url: https://github.com/nodejs/node/pull/56385 | | 371 | + description: Add support for `DataView` and typed array objects for `anonymousParameters`. | 364 | 372 | --> | 365 | 373 |
| 366 | 374 | * `namedParameters` {Object} An optional object used to bind named parameters. | 367 | 375 | The keys of this object are used to configure the mapping. | 368 | | -* `...anonymousParameters` {null|number|bigint|string|Buffer|Uint8Array} Zero or | | 376 | +* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or | 369 | 377 | more values to bind to anonymous parameters. | 370 | 378 | * Returns: {Object|undefined} An object corresponding to the first row returned | 371 | 379 | by executing the prepared statement. The keys and values of the object | @@ -381,11 +389,15 @@ values in `namedParameters` and `anonymousParameters`. | 381 | 389 |
| 382 | 390 | <!-- YAML | 383 | 391 | added: v23.4.0 | | 392 | +changes: | | 393 | + - version: REPLACEME | | 394 | + pr-url: https://github.com/nodejs/node/pull/56385 | | 395 | + description: Add support for `DataView` and typed array objects for `anonymousParameters`. | 384 | 396 | --> | 385 | 397 |
| 386 | 398 | * `namedParameters` {Object} An optional object used to bind named parameters. | 387 | 399 | The keys of this object are used to configure the mapping. | 388 | | -* `...anonymousParameters` {null|number|bigint|string|Buffer|Uint8Array} Zero or | | 400 | +* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or | 389 | 401 | more values to bind to anonymous parameters. | 390 | 402 | * Returns: {Iterator} An iterable iterator of objects. Each object corresponds to a row | 391 | 403 | returned by executing the prepared statement. The keys and values of each | @@ -400,11 +412,15 @@ the values in `namedParameters` and `anonymousParameters`. | 400 | 412 |
| 401 | 413 | <!-- YAML | 402 | 414 | added: v22.5.0 | | 415 | +changes: | | 416 | + - version: REPLACEME | | 417 | + pr-url: https://github.com/nodejs/node/pull/56385 | | 418 | + description: Add support for `DataView` and typed array objects for `anonymousParameters`. | 403 | 419 | --> | 404 | 420 |
| 405 | 421 | * `namedParameters` {Object} An optional object used to bind named parameters. | 406 | 422 | The keys of this object are used to configure the mapping. | 407 | | -* `...anonymousParameters` {null|number|bigint|string|Buffer|Uint8Array} Zero or | | 423 | +* `...anonymousParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or | 408 | 424 | more values to bind to anonymous parameters. | 409 | 425 | * Returns: {Object} | 410 | 426 | * `changes`: {number|bigint} The number of rows modified, inserted, or deleted | |
Collapse file Copy file name to clipboardExpand all lines: src/node_sqlite.cc +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -960,7 +960,7 @@ bool StatementSync::BindParams(const FunctionCallbackInfo<Value>& args) { | 960 | 960 | int anon_idx = 1; | 961 | 961 | int anon_start = 0; | 962 | 962 |
| 963 | | - if (args[0]->IsObject() && !args[0]->IsUint8Array()) { | | 963 | + if (args[0]->IsObject() && !args[0]->IsArrayBufferView()) { | 964 | 964 | Local<Object> obj = args[0].As<Object>(); | 965 | 965 | Local<Context> context = obj->GetIsolate()->GetCurrentContext(); | 966 | 966 | Local<Array> keys; | @@ -1065,7 +1065,7 @@ bool StatementSync::BindValue(const Local<Value>& value, const int index) { | 1065 | 1065 | statement_, index, *val, val.length(), SQLITE_TRANSIENT); | 1066 | 1066 | } else if (value->IsNull()) { | 1067 | 1067 | r = sqlite3_bind_null(statement_, index); | 1068 | | - } else if (value->IsUint8Array()) { | | 1068 | + } else if (value->IsArrayBufferView()) { | 1069 | 1069 | ArrayBufferViewContents<uint8_t> buf(value); | 1070 | 1070 | r = sqlite3_bind_blob( | 1071 | 1071 | statement_, index, buf.data(), buf.length(), SQLITE_TRANSIENT); | |
Collapse file Copy file name to clipboard +61Lines changed: 61 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +require('../common'); | | 3 | +const tmpdir = require('../common/tmpdir'); | | 4 | +const { join } = require('node:path'); | | 5 | +const { DatabaseSync } = require('node:sqlite'); | | 6 | +const { suite, test } = require('node:test'); | | 7 | +let cnt = 0; | | 8 | + | | 9 | +tmpdir.refresh(); | | 10 | + | | 11 | +function nextDb() { | | 12 | + return join(tmpdir.path, `database-${cnt++}.db`); | | 13 | +} | | 14 | + | | 15 | +const arrayBuffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]).buffer; | | 16 | +const TypedArrays = [ | | 17 | + ['Int8Array', Int8Array], | | 18 | + ['Uint8Array', Uint8Array], | | 19 | + ['Uint8ClampedArray', Uint8ClampedArray], | | 20 | + ['Int16Array', Int16Array], | | 21 | + ['Uint16Array', Uint16Array], | | 22 | + ['Int32Array', Int32Array], | | 23 | + ['Uint32Array', Uint32Array], | | 24 | + ['Float32Array', Float32Array], | | 25 | + ['Float64Array', Float64Array], | | 26 | + ['BigInt64Array', BigInt64Array], | | 27 | + ['BigUint64Array', BigUint64Array], | | 28 | + ['DataView', DataView], | | 29 | +]; | | 30 | + | | 31 | +suite('StatementSync with TypedArray/DataView', () => { | | 32 | + for (const [displayName, TypedArray] of TypedArrays) { | | 33 | + test(displayName, (t) => { | | 34 | + const db = new DatabaseSync(nextDb()); | | 35 | + t.after(() => { db.close(); }); | | 36 | + db.exec('CREATE TABLE test (data BLOB)'); | | 37 | + // insert | | 38 | + { | | 39 | + const stmt = db.prepare('INSERT INTO test VALUES (?)'); | | 40 | + stmt.run(new TypedArray(arrayBuffer)); | | 41 | + } | | 42 | + // select all | | 43 | + { | | 44 | + const stmt = db.prepare('SELECT * FROM test'); | | 45 | + const row = stmt.get(); | | 46 | + t.assert.ok(row.data instanceof Uint8Array); | | 47 | + t.assert.strictEqual(row.data.length, 8); | | 48 | + t.assert.deepStrictEqual(row.data, new Uint8Array(arrayBuffer)); | | 49 | + } | | 50 | + // query | | 51 | + { | | 52 | + const stmt = db.prepare('SELECT * FROM test WHERE data = ?'); | | 53 | + const rows = stmt.all(new TypedArray(arrayBuffer)); | | 54 | + t.assert.strictEqual(rows.length, 1); | | 55 | + t.assert.ok(rows[0].data instanceof Uint8Array); | | 56 | + t.assert.strictEqual(rows[0].data.length, 8); | | 57 | + t.assert.deepStrictEqual(rows[0].data, new Uint8Array(arrayBuffer)); | | 58 | + } | | 59 | + }); | | 60 | + } | | 61 | +}); |
|