GeezerVison is showing github.com — sqlite: bind empty ArrayBuffer as empty blob · gabylb/node@81bcbbc
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: src/node_sqlite.cc +16-6Lines changed: 16 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -337,7 +337,12 @@ void JSValueToSQLiteResult(Isolate* isolate, | 337 | 337 | } else if (value->IsArrayBufferView() || value->IsArrayBuffer() || | 338 | 338 | value->IsSharedArrayBuffer()) { | 339 | 339 | ArrayBufferViewContents<uint8_t> buf(value); | 340 | | - sqlite3_result_blob64(ctx, buf.data(), buf.length(), SQLITE_TRANSIENT); | | 340 | + if (buf.length() == 0) { | | 341 | + // Empty buffers may lack a data pointer, which SQLite treats as NULL. | | 342 | + sqlite3_result_zeroblob(ctx, 0); | | 343 | + } else { | | 344 | + sqlite3_result_blob64(ctx, buf.data(), buf.length(), SQLITE_TRANSIENT); | | 345 | + } | 341 | 346 | } else if (value->IsBigInt()) { | 342 | 347 | bool lossless; | 343 | 348 | int64_t as_int = value.As<BigInt>()->Int64Value(&lossless); | @@ -3632,11 +3637,16 @@ bool Statement::BindValue(const Local<Value>& value, const int index) { | 3632 | 3637 | } else if (value->IsArrayBufferView() || value->IsArrayBuffer() || | 3633 | 3638 | value->IsSharedArrayBuffer()) { | 3634 | 3639 | ArrayBufferViewContents<uint8_t> buf(value); | 3635 | | - r = sqlite3_bind_blob64(statement_.get(), | 3636 | | - index, | 3637 | | - buf.data(), | 3638 | | - static_cast<sqlite3_uint64>(buf.length()), | 3639 | | - SQLITE_TRANSIENT); | | 3640 | + if (buf.length() == 0) { | | 3641 | + // Empty buffers may lack a data pointer, which SQLite treats as NULL. | | 3642 | + r = sqlite3_bind_zeroblob(statement_.get(), index, 0); | | 3643 | + } else { | | 3644 | + r = sqlite3_bind_blob64(statement_.get(), | | 3645 | + index, | | 3646 | + buf.data(), | | 3647 | + static_cast<sqlite3_uint64>(buf.length()), | | 3648 | + SQLITE_TRANSIENT); | | 3649 | + } | 3640 | 3650 | } else if (value->IsBoolean()) { | 3641 | 3651 | r = sqlite3_bind_int(statement_.get(), index, value->IsTrue() ? 1 : 0); | 3642 | 3652 | } else if (value->IsBigInt()) { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-custom-functions.js +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -322,6 +322,30 @@ suite('Database.prototype.function()', () => { | 322 | 322 | }); | 323 | 323 | }); | 324 | 324 |
| | 325 | + test('empty buffers are returned as empty blobs, not NULL', () => { | | 326 | + using db = new Database(':memory:'); | | 327 | + db.function('retEmptyArrayBuffer', () => new ArrayBuffer(0)); | | 328 | + db.function('retEmptySharedArrayBuffer', () => new SharedArrayBuffer(0)); | | 329 | + db.function('retEmptyUint8Array', () => { | | 330 | + return new Uint8Array(new ArrayBuffer(0)); | | 331 | + }); | | 332 | + using stmt = db.prepare(`SELECT | | 333 | + typeof(retEmptyArrayBuffer()) AS arrayBufferType, | | 334 | + typeof(retEmptySharedArrayBuffer()) AS sharedArrayBufferType, | | 335 | + typeof(retEmptyUint8Array()) AS uint8ArrayType, | | 336 | + retEmptyArrayBuffer() AS arrayBuffer, | | 337 | + retEmptySharedArrayBuffer() AS sharedArrayBuffer | | 338 | + `); | | 339 | + assert.deepStrictEqual(stmt.get(), { | | 340 | + __proto__: null, | | 341 | + arrayBufferType: 'blob', | | 342 | + sharedArrayBufferType: 'blob', | | 343 | + uint8ArrayType: 'blob', | | 344 | + arrayBuffer: new Uint8Array(), | | 345 | + sharedArrayBuffer: new Uint8Array(), | | 346 | + }); | | 347 | + }); | | 348 | + | 325 | 349 | test('throws if returned BigInt is too large for SQLite', () => { | 326 | 350 | const db = new Database(':memory:'); | 327 | 351 | db.function('retBigInt', () => { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-typed-array-and-data-view.js +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -120,3 +120,38 @@ suite('Statement with ArrayBuffer and SharedArrayBuffer', () => { | 120 | 120 | }); | 121 | 121 | } | 122 | 122 | }); | | 123 | + | | 124 | +suite('Statement with empty buffers', () => { | | 125 | + const emptyBuffers = [ | | 126 | + ['ArrayBuffer', () => new ArrayBuffer(0)], | | 127 | + ['SharedArrayBuffer', () => new SharedArrayBuffer(0)], | | 128 | + ['Uint8Array', () => new Uint8Array()], | | 129 | + ['Uint8Array on empty ArrayBuffer', () => new Uint8Array(new ArrayBuffer(0))], | | 130 | + ['DataView on empty ArrayBuffer', () => new DataView(new ArrayBuffer(0))], | | 131 | + ]; | | 132 | + | | 133 | + for (const [displayName, createBuffer] of emptyBuffers) { | | 134 | + test(`${displayName} binds as an empty blob, not NULL`, (t) => { | | 135 | + using db = new Database(':memory:'); | | 136 | + db.exec('CREATE TABLE test (data BLOB NOT NULL)'); | | 137 | + const buffer = createBuffer(); | | 138 | + // Anonymous binding | | 139 | + { | | 140 | + using stmt = db.prepare('SELECT typeof(?) AS type'); | | 141 | + t.assert.strictEqual(stmt.get(buffer).type, 'blob'); | | 142 | + } | | 143 | + // Named binding (object) | | 144 | + { | | 145 | + using stmt = db.prepare('SELECT typeof($data) AS type'); | | 146 | + t.assert.strictEqual(stmt.get({ '$data': buffer }).type, 'blob'); | | 147 | + } | | 148 | + // Insert into a NOT NULL column and read it back | | 149 | + { | | 150 | + using insert = db.prepare('INSERT INTO test VALUES (?)'); | | 151 | + insert.run(buffer); | | 152 | + using select = db.prepare('SELECT data FROM test'); | | 153 | + t.assert.deepStrictEqual(select.get().data, new Uint8Array()); | | 154 | + } | | 155 | + }); | | 156 | + } | | 157 | +}); |
|