|
| 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 | +]; |
| 29 | + |
| 30 | +suite('StatementSync with TypedArray', () => { |
| 31 | + for (const [displayName, TypedArray] of TypedArrays) { |
| 32 | + test(displayName, (t) => { |
| 33 | + const db = new DatabaseSync(nextDb()); |
| 34 | + t.after(() => { db.close(); }); |
| 35 | + db.exec('CREATE TABLE test (data BLOB)'); |
| 36 | + // insert |
| 37 | + { |
| 38 | + const stmt = db.prepare('INSERT INTO test VALUES (?)'); |
| 39 | + stmt.run(new TypedArray(arrayBuffer)); |
| 40 | + } |
| 41 | + // select all |
| 42 | + { |
| 43 | + const stmt = db.prepare('SELECT * FROM test'); |
| 44 | + const row = stmt.get(); |
| 45 | + t.assert.ok(row.data instanceof Uint8Array); |
| 46 | + t.assert.strictEqual(row.data.length, 8); |
| 47 | + t.assert.deepStrictEqual(row.data, new Uint8Array(arrayBuffer)); |
| 48 | + } |
| 49 | + // query |
| 50 | + { |
| 51 | + const stmt = db.prepare('SELECT * FROM test WHERE data = ?'); |
| 52 | + const rows = stmt.all(new TypedArray(arrayBuffer)); |
| 53 | + t.assert.strictEqual(rows.length, 1); |
| 54 | + t.assert.ok(rows[0].data instanceof Uint8Array); |
| 55 | + t.assert.strictEqual(rows[0].data.length, 8); |
| 56 | + t.assert.deepStrictEqual(rows[0].data, new Uint8Array(arrayBuffer)); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | +}); |
0 commit comments