better-sqlite3-multiple-ciphers 7.4.7-beta.1 → 7.5.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/README.md +1 -1
  2. package/deps/download.sh +111 -108
  3. package/deps/setup.ps1 +9 -11
  4. package/deps/sqlite3/sqlite3.c +272560 -0
  5. package/deps/sqlite3/sqlite3.h +12770 -0
  6. package/deps/sqlite3/sqlite3ext.h +675 -0
  7. package/deps/sqlite3.gyp +13 -7
  8. package/deps/symlink.js +7 -4
  9. package/lib/database.js +17 -6
  10. package/lib/sqlite-error.js +1 -2
  11. package/package.json +10 -4
  12. package/src/better_sqlite3.cpp +46 -35
  13. package/src/better_sqlite3.hpp +40 -38
  14. package/.gitattributes +0 -1
  15. package/.github/workflows/prebuild.yml +0 -49
  16. package/.github/workflows/test.yml +0 -59
  17. package/benchmark/benchmark.js +0 -31
  18. package/benchmark/drivers.js +0 -21
  19. package/benchmark/index.js +0 -83
  20. package/benchmark/seed.js +0 -47
  21. package/benchmark/trials.js +0 -65
  22. package/benchmark/types/insert.js +0 -16
  23. package/benchmark/types/select-all.js +0 -14
  24. package/benchmark/types/select-iterate.js +0 -23
  25. package/benchmark/types/select.js +0 -14
  26. package/benchmark/types/transaction.js +0 -40
  27. package/deps/extract.js +0 -16
  28. package/deps/sqlite3.tar.gz +0 -0
  29. package/docs/api.md +0 -645
  30. package/docs/benchmark.md +0 -38
  31. package/docs/compilation.md +0 -76
  32. package/docs/integer.md +0 -79
  33. package/docs/performance.md +0 -39
  34. package/docs/threads.md +0 -97
  35. package/docs/tips.md +0 -35
  36. package/docs/troubleshooting.md +0 -23
  37. package/docs/unsafe.md +0 -16
  38. package/src/better_sqlite3.lzz +0 -88
  39. package/src/objects/backup.lzz +0 -138
  40. package/src/objects/database.lzz +0 -468
  41. package/src/objects/statement-iterator.lzz +0 -138
  42. package/src/objects/statement.lzz +0 -323
  43. package/src/util/bind-map.lzz +0 -73
  44. package/src/util/binder.lzz +0 -190
  45. package/src/util/constants.lzz +0 -151
  46. package/src/util/custom-aggregate.lzz +0 -121
  47. package/src/util/custom-function.lzz +0 -59
  48. package/src/util/custom-table.lzz +0 -397
  49. package/src/util/data-converter.lzz +0 -17
  50. package/src/util/data.lzz +0 -145
  51. package/src/util/macros.lzz +0 -159
  52. package/src/util/query-macros.lzz +0 -71
  53. package/test/00.setup.js +0 -25
  54. package/test/01.sqlite-error.js +0 -27
  55. package/test/10.database.open.js +0 -159
  56. package/test/11.database.close.js +0 -68
  57. package/test/12.database.pragma.js +0 -65
  58. package/test/13.database.prepare.js +0 -60
  59. package/test/14.database.exec.js +0 -46
  60. package/test/20.statement.run.js +0 -170
  61. package/test/21.statement.get.js +0 -109
  62. package/test/22.statement.all.js +0 -129
  63. package/test/23.statement.iterate.js +0 -223
  64. package/test/24.statement.bind.js +0 -107
  65. package/test/25.statement.columns.js +0 -46
  66. package/test/30.database.transaction.js +0 -157
  67. package/test/31.database.checkpoint.js +0 -62
  68. package/test/32.database.function.js +0 -211
  69. package/test/33.database.aggregate.js +0 -603
  70. package/test/34.database.table.js +0 -671
  71. package/test/35.database.load-extension.js +0 -75
  72. package/test/36.database.backup.js +0 -240
  73. package/test/37.database.serialize.js +0 -81
  74. package/test/40.bigints.js +0 -145
  75. package/test/41.at-exit.js +0 -52
  76. package/test/42.integrity.js +0 -531
  77. package/test/43.verbose.js +0 -100
  78. package/test/44.worker-threads.js +0 -66
  79. package/test/45.unsafe-mode.js +0 -52
  80. package/test/46.encryption.js +0 -69
  81. package/test/50.misc.js +0 -44
@@ -1,671 +0,0 @@
1
- 'use strict';
2
- const Database = require('../.');
3
-
4
- describe('Database#table()', function () {
5
- beforeEach(function () {
6
- this.db = new Database(util.next());
7
- });
8
- afterEach(function () {
9
- this.db.close();
10
- });
11
-
12
- it('should throw an exception if the correct arguments are not provided', function () {
13
- expect(() => this.db.table()).to.throw(TypeError);
14
- expect(() => this.db.table(null)).to.throw(TypeError);
15
- expect(() => this.db.table('a')).to.throw(TypeError);
16
- expect(() => this.db.table({})).to.throw(TypeError);
17
- expect(() => this.db.table({ rows: function*(){}, columns: ['x'] })).to.throw(TypeError);
18
- expect(() => this.db.table({ name: 'b', rows: function*(){}, columns: ['x'] })).to.throw(TypeError);
19
- expect(() => this.db.table(() => {})).to.throw(TypeError);
20
- expect(() => this.db.table(function* c() {})).to.throw(TypeError);
21
- expect(() => this.db.table({}, function d() {})).to.throw(TypeError);
22
- expect(() => this.db.table({ name: 'e', rows: function* e() {}, columns: ['x'] }, function e() {})).to.throw(TypeError);
23
- expect(() => this.db.table('f')).to.throw(TypeError);
24
- expect(() => this.db.table('g', null)).to.throw(TypeError);
25
- expect(() => this.db.table('h', {})).to.throw(TypeError);
26
- expect(() => this.db.table('i', Object.create(Function.prototype))).to.throw(TypeError);
27
- expect(() => this.db.table('j', { columns: ['x'] }, function j() {})).to.throw(TypeError);
28
- expect(() => this.db.table('k', { name: 'k', columns: ['x'] }, function* k() {})).to.throw(TypeError);
29
- expect(() => this.db.table('l', { name: 'l', rows: function* l() {} })).to.throw(TypeError);
30
- expect(() => this.db.table(new String('m'), { columns: ['x'], rows: function* m() {} })).to.throw(TypeError);
31
- expect(() => this.db.table(new String('n'), () => {})).to.throw(TypeError);
32
- });
33
- it('should throw an exception if boolean options are provided as non-booleans', function () {
34
- expect(() => this.db.table('a', { columns: ['x'], rows: function*(){}, directOnly: undefined })).to.throw(TypeError);
35
- expect(() => this.db.table('b', { columns: ['x'], rows: function*(){}, safeIntegers: undefined })).to.throw(TypeError);
36
- });
37
- it('should throw an exception if the "columns" option is invalid', function () {
38
- expect(() => this.db.table('a', { rows: function*(){} })).to.throw(TypeError);
39
- expect(() => this.db.table('b', { columns: undefined, rows: function*(){} })).to.throw(TypeError);
40
- expect(() => this.db.table('c', { columns: 'x', rows: function*(){} })).to.throw(TypeError);
41
- expect(() => this.db.table('d', { columns: { length: 1, 0: 'x', [Symbol.iterator]: () => ['x'].values() }, rows: function*(){} })).to.throw(TypeError);
42
- expect(() => this.db.table('e', { columns: ['x',, 'y'], rows: function*(){} })).to.throw(TypeError);
43
- expect(() => this.db.table('f', { columns: ['x', new String('y')], rows: function*(){} })).to.throw(TypeError);
44
- expect(() => this.db.table('g', { columns: ['x', 'x'], rows: function*(){} })).to.throw(TypeError);
45
- expect(() => this.db.table('h', { columns: [], rows: function*(){} })).to.throw(RangeError);
46
- });
47
- it('should throw an exception if the "parameters" option is invalid', function () {
48
- expect(() => this.db.table('a', { parameters: undefined, columns: ['foo'], rows: function*(){} })).to.throw(TypeError);
49
- expect(() => this.db.table('b', { parameters: 'x', columns: ['foo'], rows: function*(){} })).to.throw(TypeError);
50
- expect(() => this.db.table('c', { parameters: { length: 1, 0: 'x', [Symbol.iterator]: () => ['x'].values() }, columns: ['foo'], rows: function*(){} })).to.throw(TypeError);
51
- expect(() => this.db.table('d', { parameters: ['x',, 'y'], columns: ['foo'], rows: function*(){} })).to.throw(TypeError);
52
- expect(() => this.db.table('e', { parameters: ['x', new String('y')], columns: ['foo'], rows: function*(){} })).to.throw(TypeError);
53
- expect(() => this.db.table('f', { parameters: ['x', 'x'], columns: ['foo'], rows: function*(){} })).to.throw(TypeError);
54
- expect(() => this.db.table('g', { parameters: ['x'], columns: ['x'], rows: function*(){} })).to.throw(TypeError);
55
- expect(() => this.db.table('h', { parameters: [...Array(33)].map((_, i) => `p${i}`), columns: ['foo'], rows: function*(){} })).to.throw(RangeError);
56
- });
57
- it('should throw an exception if the "rows" option is invalid', function () {
58
- expect(() => this.db.table('a', { columns: ['x'] })).to.throw(TypeError);
59
- expect(() => this.db.table('b', { columns: ['x'], rows: undefined })).to.throw(TypeError);
60
- expect(() => this.db.table('c', { columns: ['x'], rows: {} })).to.throw(TypeError);
61
- expect(() => this.db.table('d', { columns: ['x'], rows: () => {} })).to.throw(TypeError);
62
- expect(() => this.db.table('e', { columns: ['x'], rows: function () {} })).to.throw(TypeError);
63
- expect(() => this.db.table('f', { columns: ['x'], rows: Object.create(Function.prototype) })).to.throw(TypeError);
64
- expect(() => this.db.table('g', { columns: ['x'], rows: Object.create(Object.getPrototypeOf(function*(){})) })).to.throw(TypeError);
65
- expect(() => this.db.table('h', { columns: ['x'], rows: Object.setPrototypeOf(() => {}, Object.create(Object.getPrototypeOf(function*(){}))) })).to.throw(TypeError);
66
- });
67
- it('should throw an exception if the provided name is empty', function () {
68
- expect(() => this.db.table('', { columns: ['x'], rows: function* () {} })).to.throw(TypeError);
69
- expect(() => this.db.table('', { name: 'a', columns: ['x'], rows: function* () {} })).to.throw(TypeError);
70
- expect(() => this.db.table('', { name: 'b', columns: ['x'], rows: function* b() {} })).to.throw(TypeError);
71
- expect(() => this.db.table('', function c() {})).to.throw(TypeError);
72
- });
73
- it('should throw an exception if generator.length is invalid', function () {
74
- const length = x => Object.defineProperty(function*(){}, 'length', { value: x });
75
- expect(() => this.db.table('a', { columns: ['x'], rows: length(undefined) })).to.throw(TypeError);
76
- expect(() => this.db.table('b', { columns: ['x'], rows: length(null) })).to.throw(TypeError);
77
- expect(() => this.db.table('c', { columns: ['x'], rows: length('1') })).to.throw(TypeError);
78
- expect(() => this.db.table('d', { columns: ['x'], rows: length(NaN) })).to.throw(TypeError);
79
- expect(() => this.db.table('e', { columns: ['x'], rows: length(Infinity) })).to.throw(TypeError);
80
- expect(() => this.db.table('f', { columns: ['x'], rows: length(1.000000001) })).to.throw(TypeError);
81
- expect(() => this.db.table('g', { columns: ['x'], rows: length(-0.000000001) })).to.throw(TypeError);
82
- expect(() => this.db.table('h', { columns: ['x'], rows: length(-1) })).to.throw(TypeError);
83
- expect(() => this.db.table('i', { columns: ['x'], rows: length(32.000000001) })).to.throw(TypeError);
84
- expect(() => this.db.table('j', { columns: ['x'], rows: length(33) })).to.throw(RangeError);
85
- });
86
- it('should register a virtual table and return the database object', function () {
87
- const length = x => Object.defineProperty(function*(){}, 'length', { value: x });
88
- expect(this.db.table('a', { columns: ['x'], rows: function* () {} })).to.equal(this.db);
89
- expect(this.db.table('b', { columns: ['x'], rows: length(1) })).to.equal(this.db);
90
- expect(this.db.table('c', { columns: ['x'], rows: length(32) })).to.equal(this.db);
91
- });
92
- it('should enable the registered virtual table to be queried from SQL', function () {
93
- const rows = [
94
- { a: null, b: 123, c: 456.789, d: 'foo', e: Buffer.from('bar') },
95
- { a: null, b: 987, c: 654.321, d: 'oof', e: Buffer.from('rab') },
96
- ];
97
- this.db.table('vtab', {
98
- columns: ['a', 'b', 'c', 'd', 'e'],
99
- *rows() {
100
- for (const obj of rows) {
101
- yield Object.values(obj);
102
- }
103
- },
104
- });
105
- expect(this.db.prepare('SELECT * FROM vtab').all()).to.deep.equal(rows);
106
- expect(this.db.prepare('SELECT * FROM vtab WHERE b < 500').all()).to.deep.equal(rows.slice(0, 1));
107
- expect(this.db.prepare('SELECT * FROM vtab ORDER BY d DESC').all()).to.deep.equal(rows.slice().reverse());
108
- });
109
- it('should infer parameters for the virtual table', function () {
110
- this.db.table('vtab', {
111
- columns: ['a', 'b'],
112
- *rows(x, y) {
113
- yield [x, y];
114
- yield [x * 2, y * 3];
115
- },
116
- });
117
- expect(this.db.prepare('SELECT * FROM vtab(?, ?)').all(2, 3))
118
- .to.deep.equal([{ a: 2, b: 3 }, { a: 4, b: 9 }]);
119
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$1" = ? AND "$2" = ?').all(2, 3))
120
- .to.deep.equal([{ a: 2, b: 3 }, { a: 4, b: 9 }]);
121
- expect(() => this.db.prepare('SELECT * FROM vtab(?, ?, ?)'))
122
- .to.throw(Database.SqliteError);
123
- expect(() => this.db.prepare('SELECT * FROM vtab WHERE "$1" = ? AND "$2" = ? AND "$3" = ?'))
124
- .to.throw(Database.SqliteError);
125
- });
126
- it('should accept explicit parameters for the virtual table', function () {
127
- this.db.table('vtab', {
128
- columns: ['a', 'b'],
129
- parameters: ['x', 'y', 'z'],
130
- *rows(p1, p2, p3, p4) {
131
- yield [arguments[0], arguments[1] + arguments[2]];
132
- yield [arguments[0] * 2, (arguments[1] + arguments[2]) * 3];
133
- },
134
- });
135
- expect(this.db.prepare('SELECT * FROM vtab(?, ?, ?)').all(2, 3, 4))
136
- .to.deep.equal([{ a: 2, b: 7 }, { a: 4, b: 21 }]);
137
- expect(this.db.prepare('SELECT * FROM vtab WHERE x = ? AND y = ? AND z = ?').all(2, 3, 4))
138
- .to.deep.equal([{ a: 2, b: 7 }, { a: 4, b: 21 }]);
139
- expect(() => this.db.prepare('SELECT * FROM vtab(?, ?, ?, ?)'))
140
- .to.throw(Database.SqliteError);
141
- expect(() => this.db.prepare('SELECT * FROM vtab WHERE "$1" = ? AND "$2" = ? AND "$3" = ?'))
142
- .to.throw(Database.SqliteError);
143
- });
144
- it('should accept a large number of parameters for the virtual table', function () {
145
- const args = ['foo', 'bar', 1, -2, Buffer.from('hello'), 5, -10, 'baz', 99.9, -0.5];
146
- this.db.table('vtab', {
147
- columns: ['x'],
148
- *rows(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) {
149
- yield [p10];
150
- yield [p9];
151
- yield [p8];
152
- yield [p7];
153
- yield [p6];
154
- yield [p5];
155
- yield [p4];
156
- yield [p3];
157
- yield [p2];
158
- yield [p1];
159
- },
160
- });
161
- expect(this.db.prepare('SELECT * FROM vtab(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)').pluck().all(args))
162
- .to.deep.equal(args.slice().reverse());
163
- expect(this.db.prepare('SELECT * FROM vtab(?, ?, ?, ?, ?, ?, ?, ?, ?)').pluck().all(args.slice(0, -1)))
164
- .to.deep.equal([null].concat(args.slice(0, -1).reverse()));
165
- expect(() => this.db.prepare('SELECT * FROM vtab(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'))
166
- .to.throw(Database.SqliteError);
167
- });
168
- it('should correctly handle arguments even when used out of order', function () {
169
- const calls = [];
170
- this.db.table('vtab', {
171
- columns: ['x', 'y'],
172
- *rows(x, y) {
173
- calls.push([...arguments]);
174
- yield { x, y };
175
- },
176
- });
177
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$1" = ? AND "$2" = ?').get(10, 5))
178
- .to.deep.equal({ x: 10, y: 5 });
179
- expect(calls.splice(0)).to.deep.equal([[10, 5]]);
180
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$2" = ? AND "$1" = ?').get(5, 10))
181
- .to.deep.equal({ x: 10, y: 5 });
182
- expect(calls.splice(0)).to.deep.equal([[10, 5]]);
183
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$2" = ? AND "$2" = ? AND "$1" = ?').get(5, 5, 10))
184
- .to.deep.equal({ x: 10, y: 5 });
185
- expect(calls.splice(0)).to.deep.equal([[10, 5]]);
186
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$2" = ? AND "$2" = ? AND "$1" = ?').get(5, 9, 10))
187
- .to.be.undefined;
188
- expect(calls.splice(0)).to.deep.equal([]);
189
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$2" = ? AND "$2" = ? AND "$1" = ?').get(9, 5, 10))
190
- .to.be.undefined;
191
- expect(calls.splice(0)).to.deep.equal([]);
192
- });
193
- it('should correctly handle arguments that are constrained to other arguments', function () {
194
- const calls = [];
195
- this.db.table('vtab', {
196
- columns: ['x', 'y'],
197
- *rows(x, y) {
198
- calls.push([...arguments]);
199
- yield { x, y };
200
- },
201
- });
202
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$1" = ? AND "$2" = "$1"').get(10))
203
- .to.deep.equal({ x: 10, y: 10 });
204
- expect(calls.splice(0)).to.deep.equal([[10, 10]]);
205
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$2" = "$1" AND "$1" = ?').get(10))
206
- .to.deep.equal({ x: 10, y: 10 });
207
- expect(calls.splice(0)).to.deep.equal([[10, 10]]);
208
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$2" = ? AND "$2" = "$1" AND "$1" = ?').get(10, 10))
209
- .to.deep.equal({ x: 10, y: 10 });
210
- expect(calls.splice(0)).to.deep.equal([[10, 10]]);
211
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$2" = ? AND "$2" = "$1" AND "$1" = ?').get(5, 10))
212
- .to.be.undefined;
213
- expect(calls.splice(0)).to.deep.equal([]);
214
- expect(this.db.prepare('SELECT * FROM vtab WHERE "$2" = "$1" AND "$2" = ? AND "$1" = ?').get(5, 10))
215
- .to.be.undefined;
216
- expect(calls.splice(0)).to.deep.equal([]);
217
- });
218
- it('should throw an exception if the database is busy', function () {
219
- let ranOnce = false;
220
- for (const x of this.db.prepare('SELECT 2').pluck().iterate()) {
221
- expect(x).to.equal(2);
222
- ranOnce = true;
223
- expect(() => this.db.table('a', { columns: ['x'], rows: function* () {} })).to.throw(TypeError);
224
- }
225
- expect(ranOnce).to.be.true;
226
- this.db.table('b', { columns: ['x'], rows: function* () {} });
227
- });
228
- it('should cause the database to become busy when querying the virtual table', function () {
229
- let checkCount = 0;
230
- const expectBusy = function* () {
231
- for (let i = 0; i < 3; ++i) {
232
- expect(() => this.db.exec('SELECT * FROM a')).to.throw(TypeError);
233
- expect(() => this.db.prepare('SELECT 555')).to.throw(TypeError);
234
- expect(() => this.db.pragma('cache_size')).to.throw(TypeError);
235
- expect(() => this.db.function('x', () => {})).to.throw(TypeError);
236
- expect(() => this.db.table('y', { columns: ['x'], rows: function* () {} })).to.throw(TypeError);
237
- checkCount += 1;
238
- yield [i];
239
- }
240
- };
241
- this.db.table('a', { columns: ['x'], rows: function* () {} });
242
- this.db.table('b', { columns: ['x'], rows: expectBusy });
243
-
244
- expect(this.db.prepare('SELECT * FROM b').pluck().all()).to.deep.equal([0, 1, 2]);
245
- expect(checkCount).to.equal(3);
246
-
247
- this.db.exec('SELECT * FROM a');
248
- this.db.prepare('SELECT 555');
249
- this.db.pragma('cache_size');
250
- this.db.function('xx', () => {});
251
- this.db.table('yy', { columns: ['x'], rows: function* () {} })
252
- });
253
- it('should cause the virtual table to throw when yielding an invalid value', function () {
254
- this.db.table('a', {
255
- columns: ['x'],
256
- *rows() { yield [42]; }
257
- });
258
- this.db.table('b', {
259
- columns: ['x'],
260
- *rows() { yield 42; }
261
- });
262
- this.db.table('c', {
263
- columns: ['x'],
264
- *rows() { yield; }
265
- });
266
- this.db.table('d', {
267
- columns: ['x'],
268
- *rows() { yield null; }
269
- });
270
- expect(this.db.prepare('SELECT * FROM a').get()).to.deep.equal({ x: 42 });
271
- expect(() => this.db.prepare('SELECT * FROM b').get()).to.throw(TypeError);
272
- expect(() => this.db.prepare('SELECT * FROM c').get()).to.throw(TypeError);
273
- expect(() => this.db.prepare('SELECT * FROM d').get()).to.throw(TypeError);
274
- });
275
- it('should allow arrays to be yielded as rows', function () {
276
- const rows = [
277
- { a: null, b: 123, c: 456.789, d: 'foo', e: Buffer.from('bar') },
278
- { a: null, b: 987, c: 654.321, d: 'oof', e: Buffer.from('rab') },
279
- ];
280
- this.db.table('vtab', {
281
- columns: ['a', 'b', 'c', 'd', 'e'],
282
- *rows() {
283
- for (const obj of rows) {
284
- yield Object.values(obj);
285
- }
286
- },
287
- });
288
- expect(this.db.prepare('SELECT * FROM vtab').all()).to.deep.equal(rows);
289
- });
290
- it('should allow objects to be yielded as rows', function () {
291
- const rows = [
292
- { a: null, b: 123, c: 456.789, d: 'foo', e: Buffer.from('bar') },
293
- { a: null, b: 987, c: 654.321, d: 'oof', e: Buffer.from('rab') },
294
- { e: Buffer.from('hello'), d: 'world', c: 0.1, b: 10, a: null },
295
- { d: 'old friend', c: -0.1, e: Buffer.from('goodbye'), a: null, b: -10 },
296
- ];
297
- this.db.table('vtab', {
298
- columns: ['a', 'b', 'c', 'd', 'e'],
299
- *rows() {
300
- for (const obj of rows) {
301
- yield obj;
302
- }
303
- },
304
- });
305
- expect(this.db.prepare('SELECT * FROM vtab').all()).to.deep.equal(rows);
306
- });
307
- it('should throw an exception if an invalid array is yielded', function () {
308
- const tests = [
309
- [1, 2, 3, 4, 5],
310
- [1, 2, 3, 4, 5, 6],
311
- [1, 2, 3, 4],
312
- [],
313
- [1, 2, 3, 4, new Number(5)],
314
- [1, 2, 3, 4, [5]],
315
- [1, 2, 3, 4, new Date()],
316
- ];
317
- this.db.table('vtab', {
318
- columns: ['a', 'b', 'c', 'd', 'e'],
319
- *rows(n) {
320
- yield tests[n];
321
- },
322
- });
323
- expect(this.db.prepare('SELECT * FROM vtab(?)').raw().all(0)).to.deep.equal([tests[0]]);
324
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(1)).to.throw(TypeError);
325
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(2)).to.throw(TypeError);
326
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(3)).to.throw(TypeError);
327
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(4)).to.throw(TypeError);
328
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(5)).to.throw(TypeError);
329
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(6)).to.throw(TypeError);
330
- });
331
- it('should throw an exception if an invalid object is yielded', function () {
332
- const tests = [
333
- { a: 1, b: 2, c: 3, d: 4, e: 5 },
334
- { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 },
335
- { a: 1, b: 2, c: 3, d: 4 },
336
- {},
337
- { a: 1, b: 2, c: 3, d: 4, e: new Number(5) },
338
- { a: 1, b: 2, c: 3, d: 4, e: [5] },
339
- { a: 1, b: 2, c: 3, d: 4, e: new Date() },
340
- { a: 1, b: 2, c: 3, d: 4, f: 5 },
341
- ];
342
- this.db.table('vtab', {
343
- columns: ['a', 'b', 'c', 'd', 'e'],
344
- *rows(n) {
345
- yield tests[n];
346
- },
347
- });
348
- expect(this.db.prepare('SELECT * FROM vtab(?)').all(0)).to.deep.equal([tests[0]]);
349
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(1)).to.throw(TypeError);
350
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(2)).to.throw(TypeError);
351
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(3)).to.throw(TypeError);
352
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(4)).to.throw(TypeError);
353
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(5)).to.throw(TypeError);
354
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(6)).to.throw(TypeError);
355
- expect(() => this.db.prepare('SELECT * FROM vtab(?)').all(7)).to.throw(TypeError);
356
- });
357
- it('should automatically assign rowids without affecting yielded objects', function () {
358
- let rows = [{ x: 5 }, { x: 10 }];
359
- this.db.table('a', {
360
- columns: ['x'],
361
- *rows() { yield* rows; },
362
- });
363
- expect(this.db.prepare('SELECT rowid, * FROM a').all())
364
- .to.deep.equal([{ rowid: 1, x: 5 }, { rowid: 2, x: 10 }]);
365
- expect(rows).to.deep.equal([{ x: 5 }, { x: 10 }]);
366
-
367
- rows = [{ rowid: 5 }, { rowid: 10 }];
368
- this.db.table('b', {
369
- columns: ['rowid'],
370
- *rows() { yield* rows; },
371
- });
372
- expect(this.db.prepare('SELECT oid AS oid, * FROM b').all())
373
- .to.deep.equal([{ oid: 1, rowid: 5 }, { oid: 2, rowid: 10 }]);
374
- expect(rows).to.deep.equal([{ rowid: 5 }, { rowid: 10 }]);
375
- });
376
- it('should be driven by stmt.iterate() one row at a time', function () {
377
- let state = 0;
378
- this.db.table('vtab', {
379
- columns: ['x'],
380
- *rows() {
381
- state += 1;
382
- yield ['foo'];
383
- state += 1;
384
- yield ['bar'];
385
- state += 1;
386
- yield ['baz'];
387
- state += 1;
388
- yield ['qux'];
389
- state += 1;
390
- },
391
- });
392
- const values = [];
393
- for (const value of this.db.prepare('SELECT * FROM vtab').pluck().iterate()) {
394
- values.push(value);
395
- if (value === 'baz') break;
396
- }
397
- expect(values).to.deep.equal(['foo', 'bar', 'baz']);
398
- expect(state).to.equal(3);
399
- });
400
- it('should throw an exception if preparing a statement that uses an unsupported operator on a parameter', function () {
401
- this.db.table('vtab', {
402
- columns: ['a', 'b'],
403
- parameters: ['x', 'y', 'z'],
404
- *rows(x, y, z) {
405
- yield [x, y + z];
406
- yield [x * 2, (y + z) * 3];
407
- },
408
- });
409
- expect(this.db.prepare('SELECT * FROM vtab(?, ?, ?)').all(2, 3, 4))
410
- .to.deep.equal([{ a: 2, b: 7 }, { a: 4, b: 21 }]);
411
- expect(this.db.prepare('SELECT * FROM vtab WHERE x = ? AND y = ? AND z = ?').all(2, 3, 4))
412
- .to.deep.equal([{ a: 2, b: 7 }, { a: 4, b: 21 }]);
413
- expect(() => this.db.prepare('SELECT * FROM vtab WHERE x = ? AND y = ? AND z > ?'))
414
- .to.throw(Database.SqliteError);
415
- expect(() => this.db.prepare('SELECT * FROM vtab WHERE x = ? AND y < ? AND z = ?'))
416
- .to.throw(Database.SqliteError);
417
- expect(() => this.db.prepare('SELECT * FROM vtab WHERE x IS ? AND y = ? AND z = ?'))
418
- .to.throw(Database.SqliteError);
419
- });
420
- it('should properly escape column and parameter names', function () {
421
- this.db.table('vtab', {
422
- columns: ['foo);'],
423
- parameters: ['x"); SELECT "y', 'y'],
424
- *rows(x, y) {
425
- yield [x];
426
- yield [y];
427
- yield [x + y];
428
- },
429
- });
430
- expect(this.db.prepare('SELECT "foo);" FROM vtab WHERE "x""); SELECT ""y" = ? AND y = ?').all(5, 10))
431
- .to.deep.equal([{ 'foo);': 5 }, { 'foo);': 10 }, { 'foo);': 15 }]);
432
- });
433
- it('should not allow CREATE VIRTUAL TABLE statements by default', function () {
434
- this.db.table('mod', {
435
- columns: ['x'],
436
- *rows() {},
437
- });
438
- expect(() => this.db.exec('CREATE VIRTUAL TABLE a USING mod')).to.throw(Database.SqliteError);
439
- expect(() => this.db.exec('CREATE VIRTUAL TABLE b USING mod()')).to.throw(Database.SqliteError);
440
- expect(() => this.db.exec('CREATE VIRTUAL TABLE c USING mod(foo)')).to.throw(Database.SqliteError);
441
- });
442
- it('should support CREATE VIRTUAL TABLE statements by accepting a factory function', function () {
443
- let table = '';
444
- this.db.table('mod', function (...args) {
445
- expect(this).to.deep.equal({ module: 'mod', database: 'main', table });
446
- return {
447
- columns: ['x'],
448
- *rows() { yield* args.map(x => [x]); },
449
- };
450
- });
451
- expect(() => this.db.prepare('SELECT * FROM mod')).to.throw(Database.SqliteError);
452
- table = 'foo';
453
- this.db.exec(`CREATE VIRTUAL TABLE ${table} USING mod(hello world, how are you?)`);
454
- table = 'bar';
455
- this.db.exec(`CREATE VIRTUAL TABLE ${table} USING mod(1, 2, 3)`);
456
- expect(this.db.prepare('SELECT x FROM foo').pluck().all()).to.deep.equal(['hello world', 'how are you?']);
457
- expect(this.db.prepare('SELECT x FROM bar').pluck().all()).to.deep.equal(['1', '2', '3']);
458
- expect(() => this.db.prepare('SELECT * FROM mod')).to.throw(Database.SqliteError);
459
- });
460
- it('should correctly handle omitted arguments in any order', function () {
461
- this.db.table('vtab', {
462
- columns: ['value'],
463
- parameters: ['x', 'y', 'z'],
464
- *rows(x = 100, y = 10, z = 1) {
465
- expect(arguments.length).to.equal(3);
466
- yield [x + y + z];
467
- },
468
- });
469
- expect(this.db.prepare('SELECT * FROM vtab(?, ?, ?)').pluck().get(2.2, 3.3, 4.4)).to.equal(9.9);
470
- expect(this.db.prepare('SELECT * FROM vtab(?, ?)').pluck().get(2.2, 3.3)).to.equal(6.5);
471
- expect(this.db.prepare('SELECT * FROM vtab(?)').pluck().get(2.2)).to.equal(13.2);
472
- expect(this.db.prepare('SELECT * FROM vtab').pluck().get()).to.equal(111);
473
- expect(this.db.prepare('SELECT * FROM vtab WHERE x = ? AND y = ? AND z = ?').pluck().get(2.2, 3.3, 4.4)).to.equal(9.9);
474
- expect(this.db.prepare('SELECT * FROM vtab WHERE x = ? AND y = ?').pluck().get(2.2, 3.3)).to.equal(6.5);
475
- expect(this.db.prepare('SELECT * FROM vtab WHERE x = ? AND z = ?').pluck().get(2.2, 3.3)).to.equal(15.5);
476
- expect(this.db.prepare('SELECT * FROM vtab WHERE y = ? AND z = ?').pluck().get(2.2, 3.3)).to.equal(105.5);
477
- expect(this.db.prepare('SELECT * FROM vtab WHERE x = ?').pluck().get(2.2)).to.equal(13.2);
478
- expect(this.db.prepare('SELECT * FROM vtab WHERE y = ?').pluck().get(2.2)).to.equal(103.2);
479
- expect(this.db.prepare('SELECT * FROM vtab WHERE z = ?').pluck().get(2.2)).to.equal(112.2);
480
- });
481
- it('should not call the generator function if any arguments are NULL', function () {
482
- let calls = 0;
483
- this.db.table('vtab', {
484
- columns: ['val'],
485
- parameters: ['x', 'y', 'z'],
486
- *rows(x = 0, y = 0, z = 0) {
487
- calls += 1;
488
- yield [x + y + z];
489
- },
490
- });
491
- expect(this.db.prepare('SELECT val FROM vtab(?, ?, ?)').pluck().all(1, 10, 100)).to.deep.equal([111]);
492
- expect(this.db.prepare('SELECT val FROM vtab(?, ?)').pluck().all(1, 10)).to.deep.equal([11]);
493
- expect(this.db.prepare('SELECT val FROM vtab(?, ?, ?)').pluck().all(1, 10, null)).to.deep.equal([]);
494
- expect(this.db.prepare('SELECT val FROM vtab(?, ?, ?)').pluck().all(1, null, 100)).to.deep.equal([]);
495
- expect(this.db.prepare('SELECT val FROM vtab(?, ?, ?)').pluck().all(null, 10, 100)).to.deep.equal([]);
496
- expect(this.db.prepare('SELECT val FROM vtab(?, ?)').pluck().all(1, null)).to.deep.equal([]);
497
- expect(calls).to.equal(2);
498
- });
499
- it('should close a statement iterator that caused a virtual table to throw', function () {
500
- this.db.prepare('CREATE TABLE iterable (x INTEGER)').run();
501
- this.db.prepare('INSERT INTO iterable WITH RECURSIVE temp(x) AS (SELECT 1 UNION ALL SELECT x * 2 FROM temp LIMIT 10) SELECT * FROM temp').run();
502
-
503
- let i = 0;
504
- const err = new Error('foo');
505
- this.db.table('vtab', {
506
- columns: ['value'],
507
- parameters: ['x'],
508
- *rows(x) {
509
- if (++i >= 5) throw err;
510
- yield [x];
511
- },
512
- });
513
- const iterator = this.db.prepare('SELECT value FROM vtab JOIN iterable USING (x)').pluck().iterate();
514
-
515
- let total = 0;
516
- expect(() => {
517
- for (const value of iterator) {
518
- total += value;
519
- expect(() => this.db.exec('SELECT value FROM vtab JOIN iterable USING (x) LIMIT 4')).to.throw(TypeError);
520
- }
521
- }).to.throw(err);
522
-
523
- expect(total).to.equal(1 + 2 + 4 + 8);
524
- expect(iterator.next()).to.deep.equal({ value: undefined, done: true });
525
- expect(total).to.equal(1 + 2 + 4 + 8);
526
-
527
- i = 0;
528
- this.db.exec('SELECT value FROM vtab JOIN iterable USING (x) LIMIT 4');
529
- expect(i).to.equal(4);
530
- });
531
- it('should not be able to affect bound buffers mid-query', function () {
532
- const input = Buffer.alloc(1024 * 8).fill(0xbb);
533
- let called = false;
534
- this.db.table('vtab', {
535
- columns: ['x'],
536
- *rows(arg) {
537
- called = true;
538
- input[0] = 2;
539
- arg[0] = 2;
540
- yield [123];
541
- },
542
- });
543
- const [output, arg, num] = this.db.prepare('SELECT :input, "$1", x FROM vtab(:input)').raw().get({ input });
544
- expect(called).to.be.true;
545
- expect(output.equals(Buffer.alloc(1024 * 8).fill(0xbb))).to.be.true;
546
- expect(arg.equals(Buffer.alloc(1024 * 8).fill(0xbb))).to.be.true;
547
- expect(num).to.equal(123);
548
- });
549
- describe('should propagate exceptions', function () {
550
- const exceptions = [new TypeError('foobar'), new Error('baz'), { yup: 'ok' }, 'foobarbazqux', '', null, 123.4];
551
- const expectError = (exception, fn) => {
552
- try { fn(); } catch (ex) {
553
- expect(ex).to.equal(exception);
554
- return;
555
- }
556
- throw new TypeError('Expected table to throw an exception');
557
- };
558
-
559
- specify('thrown in the factory function', function () {
560
- exceptions.forEach((exception, index) => {
561
- const calls = [];
562
- this.db.table(`mod${index}`, () => {
563
- calls.push('a');
564
- throw exception;
565
- calls.push('b');
566
- return {
567
- columns: ['x'],
568
- *rows() {
569
- calls.push('c');
570
- yield [42];
571
- calls.push('d');
572
- },
573
- };
574
- });
575
- expect(calls.splice(0)).to.deep.equal([]);
576
- expectError(exception, () => this.db.exec(`CREATE VIRTUAL TABLE vtab${index} USING mod${index}()`));
577
- expect(calls.splice(0)).to.deep.equal(['a']);
578
- expect(() => this.db.prepare(`SELECT * FROM vtab${index}`)).to.throw(Database.SqliteError);
579
- expect(calls.splice(0)).to.deep.equal([]);
580
- });
581
- });
582
- specify('thrown in the rows() function', function () {
583
- exceptions.forEach((exception, index) => {
584
- const calls = [];
585
- this.db.table(`mod${index}`, () => {
586
- calls.push('a');
587
- return {
588
- columns: ['x'],
589
- *rows() {
590
- calls.push('b');
591
- yield [42];
592
- calls.push('c');
593
- throw exception;
594
- calls.push('d');
595
- },
596
- };
597
- });
598
- expect(calls.splice(0)).to.deep.equal([]);
599
- this.db.exec(`CREATE VIRTUAL TABLE vtab${index} USING mod${index}()`);
600
- expect(calls.splice(0)).to.deep.equal(['a']);
601
- expect(this.db.prepare(`SELECT * FROM vtab${index}`).pluck().get()).to.equal(42);
602
- expect(calls.splice(0)).to.deep.equal(['b']);
603
- expectError(exception, () => this.db.prepare(`SELECT * FROM vtab${index}`).pluck().all());
604
- expect(calls.splice(0)).to.deep.equal(['b', 'c']);
605
- });
606
- });
607
- specify('thrown due to yielding an invalid value', function () {
608
- const calls = [];
609
- this.db.table('mod', () => {
610
- calls.push('a');
611
- return {
612
- columns: ['x'],
613
- *rows() {
614
- calls.push('b');
615
- yield [42];
616
- calls.push('c');
617
- yield [new Number(42)];
618
- calls.push('d');
619
- },
620
- };
621
- });
622
- expect(calls.splice(0)).to.deep.equal([]);
623
- this.db.exec('CREATE VIRTUAL TABLE vtab USING mod()');
624
- expect(calls.splice(0)).to.deep.equal(['a']);
625
- expect(this.db.prepare('SELECT * FROM vtab').pluck().get()).to.equal(42);
626
- expect(calls.splice(0)).to.deep.equal(['b']);
627
- expect(() => this.db.prepare('SELECT * FROM vtab').pluck().all()).to.throw(TypeError);
628
- expect(calls.splice(0)).to.deep.equal(['b', 'c']);
629
- });
630
- });
631
- describe('should not affect external environment', function () {
632
- specify('busy state', function () {
633
- this.db.table('vtab', {
634
- columns: ['x'],
635
- *rows(arg) {
636
- expect(() => this.db.exec('SELECT 555')).to.throw(TypeError);
637
- yield [arg * 2];
638
- },
639
- });
640
- let ranOnce = false;
641
- for (const x of this.db.prepare('SELECT * FROM vtab(555)').pluck().iterate()) {
642
- ranOnce = true;
643
- expect(x).to.equal(1110);
644
- expect(() => this.db.exec('SELECT 555')).to.throw(TypeError);
645
- }
646
- expect(ranOnce).to.be.true;
647
- this.db.exec('SELECT 555');
648
- });
649
- specify('was_js_error state', function () {
650
- this.db.prepare('CREATE TABLE data (value INTEGER)').run();
651
- const stmt = this.db.prepare('SELECT value FROM data');
652
- this.db.prepare('DROP TABLE data').run();
653
-
654
- const err = new Error('foo');
655
- this.db.table('vtab', {
656
- columns: ['x'],
657
- *rows() { throw err; },
658
- });
659
-
660
- expect(() => this.db.prepare('SELECT * FROM vtab').get()).to.throw(err);
661
- try { stmt.get(); } catch (ex) {
662
- expect(ex).to.be.an.instanceof(Error);
663
- expect(ex).to.not.equal(err);
664
- expect(ex.message).to.not.equal(err.message);
665
- expect(ex).to.be.an.instanceof(Database.SqliteError);
666
- return;
667
- }
668
- throw new TypeError('Expected the statement to throw an exception');
669
- });
670
- });
671
- });