|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +common.skipIfSQLiteMissing(); |
| 4 | + |
| 5 | +if (!common.enoughTestMem) |
| 6 | + common.skip('intensive SQL text tests due to memory confinements'); |
| 7 | + |
| 8 | +const assert = require('node:assert'); |
| 9 | +const { constants } = require('node:buffer'); |
| 10 | +const dc = require('node:diagnostics_channel'); |
| 11 | +const { Database } = require('node:sqlite'); |
| 12 | + |
| 13 | +// 'é' is one byte per character in V8 and two bytes in UTF-8. |
| 14 | +const text = 'é'.repeat((constants.MAX_STRING_LENGTH >>> 1) + 1); |
| 15 | +const tooLong = { code: 'ERR_STRING_TOO_LONG', name: 'Error' }; |
| 16 | + |
| 17 | +{ |
| 18 | + using db = new Database(':memory:'); |
| 19 | + using stmt = db.prepare(`SELECT length('${text}') AS len`); |
| 20 | + assert.throws(() => stmt.sourceSQL, tooLong); |
| 21 | +} |
| 22 | + |
| 23 | +{ |
| 24 | + using db = new Database(':memory:'); |
| 25 | + using stmt = db.prepare(`SELECT length('${text}')`); |
| 26 | + assert.throws(() => stmt.get(), tooLong); |
| 27 | + assert.throws(() => stmt.all(), tooLong); |
| 28 | + assert.throws(() => stmt.iterate().next(), tooLong); |
| 29 | + assert.throws(() => stmt.columns(), tooLong); |
| 30 | +} |
| 31 | + |
| 32 | +{ |
| 33 | + const handler = common.mustNotCall(); |
| 34 | + dc.subscribe('sqlite.db.query', handler); |
| 35 | + const sql = `SELECT length('${text}') AS a, length(?) AS b`; |
| 36 | + using db = new Database(':memory:'); |
| 37 | + db.limits.length = Buffer.byteLength(text); |
| 38 | + using stmt = db.prepare(sql); |
| 39 | + assert.deepStrictEqual( |
| 40 | + stmt.get(Buffer.alloc(1)), |
| 41 | + { __proto__: null, a: text.length, b: 1 }, |
| 42 | + ); |
| 43 | + assert.throws(() => stmt.expandedSQL, { code: 'ERR_SQLITE_ERROR' }); |
| 44 | + dc.unsubscribe('sqlite.db.query', handler); |
| 45 | +} |
0 commit comments