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,46 +0,0 @@
1
- 'use strict';
2
- const Database = require('../.');
3
-
4
- describe('Database#exec()', 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 a string is not provided', function () {
13
- expect(() => this.db.exec(123)).to.throw(TypeError);
14
- expect(() => this.db.exec(0)).to.throw(TypeError);
15
- expect(() => this.db.exec(null)).to.throw(TypeError);
16
- expect(() => this.db.exec()).to.throw(TypeError);
17
- expect(() => this.db.exec(new String('CREATE TABLE entries (a TEXT, b INTEGER)'))).to.throw(TypeError);
18
- });
19
- it('should throw an exception if invalid SQL is provided', function () {
20
- expect(() => this.db.exec('CREATE TABLE entries (a TEXT, b INTEGER')).to.throw(Database.SqliteError).with.property('code', 'SQLITE_ERROR');
21
- });
22
- it('should obey the restrictions of readonly mode', function () {
23
- this.db.close();
24
- this.db = new Database(util.current(), { readonly: true });
25
- expect(() => this.db.exec('CREATE TABLE people (name TEXT)')).to.throw(Database.SqliteError).with.property('code', 'SQLITE_READONLY');
26
- this.db.exec('SELECT 555');
27
- });
28
- it('should execute the SQL, returning the database object itself', function () {
29
- const returnValues = [];
30
-
31
- const r1 = this.db.exec('CREATE TABLE entries (a TEXT, b INTEGER)');
32
- const r2 = this.db.exec("INSERT INTO entries VALUES ('foobar', 44); INSERT INTO entries VALUES ('baz', NULL);");
33
- const r3 = this.db.exec('SELECT * FROM entries');
34
-
35
- expect(r1).to.equal(this.db);
36
- expect(r2).to.equal(this.db);
37
- expect(r3).to.equal(this.db);
38
-
39
- const rows = this.db.prepare('SELECT * FROM entries ORDER BY rowid').all();
40
- expect(rows.length).to.equal(2);
41
- expect(rows[0].a).to.equal('foobar');
42
- expect(rows[0].b).to.equal(44);
43
- expect(rows[1].a).to.equal('baz');
44
- expect(rows[1].b).to.equal(null);
45
- });
46
- });
@@ -1,170 +0,0 @@
1
- 'use strict';
2
- const Database = require('../.');
3
-
4
- describe('Statement#run()', function () {
5
- beforeEach(function () {
6
- this.db = new Database(util.next());
7
- this.db.init = (data = false) => {
8
- this.db.info = this.db.prepare('CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB)').run();
9
- if (data) {
10
- this.db.prepare('CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT)').run();
11
- this.db.prepare('CREATE TABLE ages (age INTEGER, person INTEGER NOT NULL REFERENCES people(id) ON DELETE CASCADE ON UPDATE CASCADE)').run();
12
- this.db.prepare("INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff'), ('foo', 25, 3.14, x'1133ddff'), ('foo', 25, 3.14, x'1133ddff')").run();
13
- this.db.prepare("INSERT INTO people VALUES (1, 'bob'), (2, 'sarah')").run();
14
- }
15
- return this.db;
16
- };
17
- });
18
- afterEach(function () {
19
- this.db.close();
20
- });
21
-
22
- it('should work with CREATE TABLE', function () {
23
- const { info } = this.db.init();
24
- expect(info.changes).to.equal(0);
25
- expect(info.lastInsertRowid).to.equal(0);
26
- });
27
- it('should work with CREATE TABLE IF NOT EXISTS', function () {
28
- const stmt = this.db.init().prepare('CREATE TABLE IF NOT EXISTS entries (a TEXT, b INTEGER, c REAL, d BLOB)');
29
- const info = stmt.run();
30
- expect(info.changes).to.equal(0);
31
- expect(info.lastInsertRowid).to.equal(0);
32
- });
33
- it('should work with SELECT', function () {
34
- const stmt = this.db.prepare('SELECT 555');
35
- const info = stmt.run();
36
- expect(info.changes).to.equal(0);
37
- expect(info.lastInsertRowid).to.equal(0);
38
- });
39
- it('should work with INSERT INTO', function () {
40
- let stmt = this.db.init().prepare("INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff')");
41
- let info = stmt.run();
42
- expect(info.changes).to.equal(1);
43
- expect(info.lastInsertRowid).to.equal(1);
44
-
45
- info = stmt.run();
46
- expect(info.changes).to.equal(1);
47
- expect(info.lastInsertRowid).to.equal(2);
48
-
49
- stmt = this.db.prepare("INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff'), ('foo', 25, 3.14, x'1133ddff')");
50
- info = stmt.run();
51
- expect(info.changes).to.equal(2);
52
- expect(info.lastInsertRowid).to.equal(4);
53
- });
54
- it('should work with UPDATE', function () {
55
- const stmt = this.db.init(true).prepare("UPDATE entries SET a='bar' WHERE rowid=1");
56
- expect(stmt.run().changes).to.equal(1);
57
- });
58
- it('should work with DELETE FROM', function () {
59
- let stmt = this.db.init(true).prepare("DELETE FROM entries WHERE a='foo'");
60
- expect(stmt.run().changes).to.equal(3);
61
-
62
- stmt = this.db.prepare("INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff')");
63
- stmt.run();
64
- const info = stmt.run();
65
- expect(info.changes).to.equal(1);
66
- expect(info.lastInsertRowid).to.equal(2);
67
- });
68
- it('should work with BEGIN and COMMIT', function () {
69
- expect(this.db.init(true).inTransaction).to.equal(false);
70
- expect(this.db.prepare("BEGIN TRANSACTION").run().changes).to.equal(0);
71
- expect(this.db.inTransaction).to.equal(true);
72
- const info = this.db.prepare("INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff')").run();
73
- expect(info.changes).to.equal(1);
74
- expect(info.lastInsertRowid).to.equal(4);
75
- expect(this.db.inTransaction).to.equal(true);
76
- expect(this.db.prepare("COMMIT TRANSACTION").run().changes).to.equal(0);
77
- expect(this.db.inTransaction).to.equal(false);
78
- });
79
- it('should work with DROP TABLE', function () {
80
- const stmt = this.db.init(true).prepare("DROP TABLE entries");
81
- expect(stmt.run().changes).to.equal(0);
82
- });
83
- it('should throw an exception for failed constraints', function () {
84
- this.db.init(true).prepare("INSERT INTO ages VALUES (25, 1)").run();
85
- this.db.prepare("INSERT INTO ages VALUES (30, 2)").run();
86
- this.db.prepare("INSERT INTO ages VALUES (35, 2)").run();
87
- let stmt = this.db.prepare("INSERT INTO ages VALUES (30, 3)");
88
- expect(() => stmt.run()).to.throw(Database.SqliteError).with.property('code', 'SQLITE_CONSTRAINT_FOREIGNKEY');
89
- stmt = this.db.prepare("INSERT INTO ages VALUES (30, NULL)");
90
- expect(() => stmt.run()).to.throw(Database.SqliteError).with.property('code', 'SQLITE_CONSTRAINT_NOTNULL');
91
- });
92
- it('should allow ad-hoc transactions', function () {
93
- expect(this.db.init(true).prepare("BEGIN TRANSACTION").run().changes).to.equal(0);
94
- expect(this.db.prepare("INSERT INTO ages VALUES (45, 2)").run().changes).to.equal(1);
95
- const stmt = this.db.prepare("INSERT INTO ages VALUES (30, 3)");
96
- expect(() => stmt.run()).to.throw(Database.SqliteError).with.property('code', 'SQLITE_CONSTRAINT_FOREIGNKEY');
97
- expect(this.db.prepare("ROLLBACK TRANSACTION").run().changes).to.equal(0);
98
- });
99
- it('should not count changes from indirect mechanisms', function () {
100
- const stmt = this.db.init(true).prepare("UPDATE people SET id=55 WHERE id=2");
101
- expect(stmt.run().changes).to.equal(1);
102
- });
103
- it('should count accurate DELETE changes when a dropped table has side effects', function () {
104
- const stmt = this.db.init(true).prepare("DROP TABLE people");
105
- expect(stmt.run().changes).to.equal(2);
106
- });
107
- it('should obey the restrictions of readonly mode', function () {
108
- this.db.close();
109
- this.db = new Database(util.current(), { readonly: true });
110
- const stmt = this.db.prepare('CREATE TABLE people (name TEXT)');
111
- expect(() => stmt.run()).to.throw(Database.SqliteError).with.property('code', 'SQLITE_READONLY');
112
- });
113
- it('should accept bind parameters', function () {
114
- this.db.prepare("CREATE TABLE entries (a TEXT CHECK(typeof(a)=='text'), b INTEGER CHECK(typeof(b)=='integer' OR typeof(b)=='real'), c REAL CHECK(typeof(c)=='real' OR typeof(c)=='integer'), d BLOB CHECK(typeof(d)=='blob'))").run();
115
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, 25, Buffer.alloc(8).fill(0xdd));
116
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run(['foo', 25, 25, Buffer.alloc(8).fill(0xdd)]);
117
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run(['foo', 25], [25], Buffer.alloc(8).fill(0xdd));
118
- this.db.prepare('INSERT INTO entries VALUES (@a, @b, @c, @d)').run({ a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd) });
119
- this.db.prepare('INSERT INTO entries VALUES ($a, $b, $c, $d)').run({ a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd) });
120
- this.db.prepare('INSERT INTO entries VALUES (:a, :b, :c, :d)').run({ a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd) });
121
- this.db.prepare('INSERT INTO entries VALUES (?, @a, @a, ?)').run({ a: 25 }, ['foo'], Buffer.alloc(8).fill(0xdd));
122
- expect(() =>
123
- this.db.prepare('INSERT INTO entries VALUES (?, @a, @a, ?)').run({ a: 25 }, ['foo'], Buffer.alloc(8).fill(0xdd), Buffer.alloc(8).fill(0xdd))
124
- ).to.throw(RangeError);
125
- expect(() =>
126
- this.db.prepare('INSERT INTO entries VALUES (?, @a, @a, ?)').run({ a: 25 }, ['foo'])
127
- ).to.throw(RangeError);
128
- this.db.prepare('INSERT INTO entries VALUES (?, @a, @a, ?)').run({ a: 25, c: 25 }, ['foo'], Buffer.alloc(8).fill(0xdd));
129
- expect(() =>
130
- this.db.prepare('INSERT INTO entries VALUES (?, @a, @a, ?)').run({}, ['foo'], Buffer.alloc(8).fill(0xdd))
131
- ).to.throw(RangeError);
132
- expect(() =>
133
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run(25, 'foo', 25, Buffer.alloc(8).fill(0xdd))
134
- ).to.throw(Database.SqliteError).with.property('code', 'SQLITE_CONSTRAINT_CHECK');
135
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, 25, Buffer.alloc(8).fill(0xdd), {});
136
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, 25, Buffer.alloc(8).fill(0xdd), { foo: 'foo' });
137
- expect(() =>
138
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, 25, { 4: Buffer.alloc(8).fill(0xdd) })
139
- ).to.throw(RangeError);
140
- expect(() =>
141
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run()
142
- ).to.throw(RangeError);
143
- expect(() =>
144
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run({ length: 4, 0: 'foo', 1: 25, 2: 25, 3: Buffer.alloc(8).fill(0xdd) })
145
- ).to.throw(RangeError);
146
- expect(() =>
147
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, new Number(25), Buffer.alloc(8).fill(0xdd))
148
- ).to.throw(TypeError);
149
- expect(() =>
150
- this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', { low: 25, high: 25 }, 25, Buffer.alloc(8).fill(0xdd))
151
- ).to.throw(RangeError);
152
- function Foo() {
153
- this.a = 'foo';
154
- this.b = 25;
155
- this.c = 25;
156
- this.d = Buffer.alloc(8).fill(0xdd);
157
- }
158
- expect(() =>
159
- this.db.prepare('INSERT INTO entries VALUES (@a, @b, @c, @d)').run(new Foo)
160
- ).to.throw(TypeError);
161
-
162
- // This part of the test may fail is Statement#get() does not work.
163
- let i = 0;
164
- let row;
165
- while (row = this.db.prepare(`SELECT * FROM entries WHERE rowid=${++i}`).get()) {
166
- expect(row).to.deep.equal({ a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd) });
167
- }
168
- expect(i).to.equal(11);
169
- });
170
- });
@@ -1,109 +0,0 @@
1
- 'use strict';
2
- const Database = require('../.');
3
-
4
- describe('Statement#get()', function () {
5
- beforeEach(function () {
6
- this.db = new Database(util.next());
7
- this.db.prepare('CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)').run();
8
- this.db.prepare("INSERT INTO entries WITH RECURSIVE temp(a, b, c, d, e) AS (SELECT 'foo', 1, 3.14, x'dddddddd', NULL UNION ALL SELECT a, b + 1, c, d, e FROM temp LIMIT 10) SELECT * FROM temp").run();
9
- });
10
- afterEach(function () {
11
- this.db.close();
12
- });
13
-
14
- it('should throw an exception when used on a statement that returns no data', function () {
15
- let stmt = this.db.prepare("INSERT INTO entries VALUES ('foo', 1, 3.14, x'dddddddd', NULL)");
16
- expect(stmt.reader).to.be.false;
17
- expect(() => stmt.get()).to.throw(TypeError);
18
-
19
- stmt = this.db.prepare("CREATE TABLE IF NOT EXISTS entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)");
20
- expect(stmt.reader).to.be.false;
21
- expect(() => stmt.get()).to.throw(TypeError);
22
-
23
- stmt = this.db.prepare("BEGIN TRANSACTION");
24
- expect(stmt.reader).to.be.false;
25
- expect(() => stmt.get()).to.throw(TypeError);
26
- });
27
- it('should return the first matching row', function () {
28
- let stmt = this.db.prepare("SELECT * FROM entries ORDER BY rowid");
29
- expect(stmt.reader).to.be.true;
30
- expect(stmt.get()).to.deep.equal({ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null });
31
-
32
- stmt = this.db.prepare("SELECT * FROM entries WHERE b > 5 ORDER BY rowid");
33
- expect(stmt.get()).to.deep.equal({ a: 'foo', b: 6, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null });
34
- });
35
- it('should work with RETURNING clause', function () {
36
- let stmt = this.db.prepare("INSERT INTO entries (a, b) VALUES ('bar', 888), ('baz', 999) RETURNING *");
37
- expect(stmt.reader).to.be.true;
38
- expect(stmt.get()).to.deep.equal({ a: 'bar', b: 888, c: null, d: null, e: null });
39
-
40
- stmt = this.db.prepare("SELECT * FROM entries WHERE b > 900 ORDER BY rowid");
41
- expect(stmt.get()).to.deep.equal({ a: 'baz', b: 999, c: null, d: null, e: null });
42
- });
43
- it('should obey the current pluck and expand settings', function () {
44
- const stmt = this.db.prepare("SELECT *, 2 + 3.5 AS c FROM entries ORDER BY rowid");
45
- const expanded = { entries: { a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null }, $: { c: 5.5 } };
46
- const row = Object.assign({}, expanded.entries, expanded.$);
47
- const plucked = expanded.entries.a;
48
- const raw = Object.values(expanded.entries).concat(expanded.$.c);
49
- expect(stmt.get()).to.deep.equal(row);
50
- expect(stmt.pluck(true).get()).to.deep.equal(plucked);
51
- expect(stmt.get()).to.deep.equal(plucked);
52
- expect(stmt.pluck(false).get()).to.deep.equal(row);
53
- expect(stmt.get()).to.deep.equal(row);
54
- expect(stmt.pluck().get()).to.deep.equal(plucked);
55
- expect(stmt.get()).to.deep.equal(plucked);
56
- expect(stmt.expand().get()).to.deep.equal(expanded);
57
- expect(stmt.get()).to.deep.equal(expanded);
58
- expect(stmt.expand(false).get()).to.deep.equal(row);
59
- expect(stmt.get()).to.deep.equal(row);
60
- expect(stmt.expand(true).get()).to.deep.equal(expanded);
61
- expect(stmt.get()).to.deep.equal(expanded);
62
- expect(stmt.pluck(true).get()).to.deep.equal(plucked);
63
- expect(stmt.get()).to.deep.equal(plucked);
64
- expect(stmt.raw().get()).to.deep.equal(raw);
65
- expect(stmt.get()).to.deep.equal(raw);
66
- expect(stmt.raw(false).get()).to.deep.equal(row);
67
- expect(stmt.get()).to.deep.equal(row);
68
- expect(stmt.raw(true).get()).to.deep.equal(raw);
69
- expect(stmt.get()).to.deep.equal(raw);
70
- expect(stmt.expand(true).get()).to.deep.equal(expanded);
71
- expect(stmt.get()).to.deep.equal(expanded);
72
- });
73
- it('should return undefined when no rows were found', function () {
74
- const stmt = this.db.prepare("SELECT * FROM entries WHERE b == 999");
75
- expect(stmt.get()).to.be.undefined;
76
- expect(stmt.pluck().get()).to.be.undefined;
77
- });
78
- it('should accept bind parameters', function () {
79
- const row = { a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null };
80
- const SQL1 = 'SELECT * FROM entries WHERE a=? AND b=? AND c=? AND d=? AND e IS ?';
81
- const SQL2 = 'SELECT * FROM entries WHERE a=@a AND b=@b AND c=@c AND d=@d AND e IS @e';
82
- let result = this.db.prepare(SQL1).get('foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null);
83
- expect(result).to.deep.equal(row);
84
-
85
- result = this.db.prepare(SQL1).get(['foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null]);
86
- expect(result).to.deep.equal(row);
87
-
88
- result = this.db.prepare(SQL1).get(['foo', 1], [3.14], Buffer.alloc(4).fill(0xdd), [,]);
89
- expect(result).to.deep.equal(row);
90
-
91
- result = this.db.prepare(SQL2).get({ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: undefined });
92
- expect(result).to.deep.equal(row);
93
-
94
- result = this.db.prepare(SQL2).get({ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xaa), e: undefined });
95
- expect(result).to.be.undefined;
96
-
97
- expect(() =>
98
- this.db.prepare(SQL2).get({ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd) })
99
- ).to.throw(RangeError);
100
-
101
- expect(() =>
102
- this.db.prepare(SQL1).get()
103
- ).to.throw(RangeError);
104
-
105
- expect(() =>
106
- this.db.prepare(SQL2).get({})
107
- ).to.throw(RangeError);
108
- });
109
- });
@@ -1,129 +0,0 @@
1
- 'use strict';
2
- const Database = require('../.');
3
-
4
- describe('Statement#all()', function () {
5
- beforeEach(function () {
6
- this.db = new Database(util.next());
7
- this.db.prepare('CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)').run();
8
- this.db.prepare("INSERT INTO entries WITH RECURSIVE temp(a, b, c, d, e) AS (SELECT 'foo', 1, 3.14, x'dddddddd', NULL UNION ALL SELECT a, b + 1, c, d, e FROM temp LIMIT 10) SELECT * FROM temp").run();
9
- });
10
- afterEach(function () {
11
- this.db.close();
12
- });
13
-
14
- it('should throw an exception when used on a statement that returns no data', function () {
15
- let stmt = this.db.prepare("INSERT INTO entries VALUES ('foo', 1, 3.14, x'dddddddd', NULL)");
16
- expect(stmt.reader).to.be.false;
17
- expect(() => stmt.all()).to.throw(TypeError);
18
-
19
- stmt = this.db.prepare("CREATE TABLE IF NOT EXISTS entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)");
20
- expect(stmt.reader).to.be.false;
21
- expect(() => stmt.all()).to.throw(TypeError);
22
-
23
- stmt = this.db.prepare("BEGIN TRANSACTION");
24
- expect(stmt.reader).to.be.false;
25
- expect(() => stmt.all()).to.throw(TypeError);
26
- });
27
- it('should return an array of every matching row', function () {
28
- const row = { a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null };
29
-
30
- let stmt = this.db.prepare("SELECT * FROM entries ORDER BY rowid");
31
- expect(stmt.reader).to.be.true;
32
- matchesFrom(stmt.all(), 1);
33
-
34
- stmt = this.db.prepare("SELECT * FROM entries WHERE b > 5 ORDER BY rowid");
35
- matchesFrom(stmt.all(), 6);
36
-
37
- function matchesFrom(rows, i) {
38
- let index = 0;
39
- for (; i <= 10; ++i, ++index) {
40
- row.b = i;
41
- expect(rows[index]).to.deep.equal(row);
42
- }
43
- expect(index).to.equal(rows.length);
44
- }
45
- });
46
- it('should work with RETURNING clause', function () {
47
- let stmt = this.db.prepare("INSERT INTO entries (a, b) VALUES ('bar', 888), ('baz', 999) RETURNING *");
48
- expect(stmt.reader).to.be.true;
49
- expect(stmt.all()).to.deep.equal([
50
- { a: 'bar', b: 888, c: null, d: null, e: null },
51
- { a: 'baz', b: 999, c: null, d: null, e: null },
52
- ]);
53
-
54
- stmt = this.db.prepare("SELECT * FROM entries WHERE b > 800 ORDER BY rowid");
55
- expect(stmt.all()).to.deep.equal([
56
- { a: 'bar', b: 888, c: null, d: null, e: null },
57
- { a: 'baz', b: 999, c: null, d: null, e: null },
58
- ]);
59
- });
60
- it('should obey the current pluck and expand settings', function () {
61
- const stmt = this.db.prepare("SELECT *, 2 + 3.5 AS c FROM entries ORDER BY rowid");
62
- const expanded = new Array(10).fill().map((_, i) => ({
63
- entries: { a: 'foo', b: i + 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null },
64
- $: { c: 5.5 },
65
- }));
66
- const rows = expanded.map(x => Object.assign({}, x.entries, x.$));
67
- const plucked = expanded.map(x => x.entries.a);
68
- const raw = expanded.map(x => Object.values(x.entries).concat(x.$.c))
69
- expect(stmt.all()).to.deep.equal(rows);
70
- expect(stmt.pluck(true).all()).to.deep.equal(plucked);
71
- expect(stmt.all()).to.deep.equal(plucked);
72
- expect(stmt.pluck(false).all()).to.deep.equal(rows);
73
- expect(stmt.all()).to.deep.equal(rows);
74
- expect(stmt.pluck().all()).to.deep.equal(plucked);
75
- expect(stmt.all()).to.deep.equal(plucked);
76
- expect(stmt.expand().all()).to.deep.equal(expanded);
77
- expect(stmt.all()).to.deep.equal(expanded);
78
- expect(stmt.expand(false).all()).to.deep.equal(rows);
79
- expect(stmt.all()).to.deep.equal(rows);
80
- expect(stmt.expand(true).all()).to.deep.equal(expanded);
81
- expect(stmt.all()).to.deep.equal(expanded);
82
- expect(stmt.pluck(true).all()).to.deep.equal(plucked);
83
- expect(stmt.all()).to.deep.equal(plucked);
84
- expect(stmt.raw().all()).to.deep.equal(raw);
85
- expect(stmt.all()).to.deep.equal(raw);
86
- expect(stmt.raw(false).all()).to.deep.equal(rows);
87
- expect(stmt.all()).to.deep.equal(rows);
88
- expect(stmt.raw(true).all()).to.deep.equal(raw);
89
- expect(stmt.all()).to.deep.equal(raw);
90
- expect(stmt.expand(true).all()).to.deep.equal(expanded);
91
- expect(stmt.all()).to.deep.equal(expanded);
92
- });
93
- it('should return an empty array when no rows were found', function () {
94
- const stmt = this.db.prepare("SELECT * FROM entries WHERE b == 999");
95
- expect(stmt.all()).to.deep.equal([]);
96
- expect(stmt.pluck().all()).to.deep.equal([]);
97
- });
98
- it('should accept bind parameters', function () {
99
- const rows = [{ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null }];
100
- const SQL1 = 'SELECT * FROM entries WHERE a=? AND b=? AND c=? AND d=? AND e IS ?';
101
- const SQL2 = 'SELECT * FROM entries WHERE a=@a AND b=@b AND c=@c AND d=@d AND e IS @e';
102
- let result = this.db.prepare(SQL1).all('foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null);
103
- expect(result).to.deep.equal(rows);
104
-
105
- result = this.db.prepare(SQL1).all(['foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null]);
106
- expect(result).to.deep.equal(rows);
107
-
108
- result = this.db.prepare(SQL1).all(['foo', 1], [3.14], Buffer.alloc(4).fill(0xdd), [,]);
109
- expect(result).to.deep.equal(rows);
110
-
111
- result = this.db.prepare(SQL2).all({ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: undefined });
112
- expect(result).to.deep.equal(rows);
113
-
114
- result = this.db.prepare(SQL2).all({ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xaa), e: undefined });
115
- expect(result).to.deep.equal([]);
116
-
117
- expect(() =>
118
- this.db.prepare(SQL2).all({ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd) })
119
- ).to.throw(RangeError);
120
-
121
- expect(() =>
122
- this.db.prepare(SQL1).all()
123
- ).to.throw(RangeError);
124
-
125
- expect(() =>
126
- this.db.prepare(SQL2).all({})
127
- ).to.throw(RangeError);
128
- });
129
- });
@@ -1,223 +0,0 @@
1
- 'use strict';
2
- const Database = require('../.');
3
-
4
- describe('Statement#iterate()', function () {
5
- beforeEach(function () {
6
- this.db = new Database(util.next());
7
- this.db.prepare("CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)").run();
8
- this.db.prepare("INSERT INTO entries WITH RECURSIVE temp(a, b, c, d, e) AS (SELECT 'foo', 1, 3.14, x'dddddddd', NULL UNION ALL SELECT a, b + 1, c, d, e FROM temp LIMIT 10) SELECT * FROM temp").run();
9
- });
10
- afterEach(function () {
11
- this.db.close();
12
- });
13
-
14
- it('should throw an exception when used on a statement that returns no data', function () {
15
- let stmt = this.db.prepare("INSERT INTO entries VALUES ('foo', 1, 3.14, x'dddddddd', NULL)");
16
- expect(stmt.reader).to.be.false;
17
- expect(() => stmt.iterate()).to.throw(TypeError);
18
-
19
- stmt = this.db.prepare("CREATE TABLE IF NOT EXISTS entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)");
20
- expect(stmt.reader).to.be.false;
21
- expect(() => stmt.iterate()).to.throw(TypeError);
22
-
23
- stmt = this.db.prepare("BEGIN TRANSACTION");
24
- expect(stmt.reader).to.be.false;
25
- expect(() => stmt.iterate()).to.throw(TypeError);
26
-
27
- this.db.prepare("INSERT INTO entries WITH RECURSIVE temp(a, b, c, d, e) AS (SELECT 'foo', 1, 3.14, x'dddddddd', NULL UNION ALL SELECT a, b + 1, c, d, e FROM temp LIMIT 10) SELECT * FROM temp").run();
28
- });
29
- it('should return an iterator over each matching row', function () {
30
- const row = { a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null };
31
-
32
- let count = 0;
33
- let stmt = this.db.prepare("SELECT * FROM entries ORDER BY rowid");
34
- expect(stmt.reader).to.be.true;
35
-
36
- const iterator = stmt.iterate();
37
- expect(iterator).to.not.be.null;
38
- expect(typeof iterator).to.equal('object');
39
- expect(iterator.next).to.be.a('function');
40
- expect(iterator.return).to.be.a('function');
41
- expect(iterator.throw).to.not.be.a('function');
42
- expect(iterator[Symbol.iterator]).to.be.a('function');
43
- expect(iterator[Symbol.iterator]()).to.equal(iterator);
44
-
45
- for (const data of iterator) {
46
- row.b = ++count;
47
- expect(data).to.deep.equal(row);
48
- }
49
- expect(count).to.equal(10);
50
-
51
- count = 0;
52
- stmt = this.db.prepare("SELECT * FROM entries WHERE b > 5 ORDER BY rowid");
53
- const iterator2 = stmt.iterate();
54
- expect(iterator).to.not.equal(iterator2);
55
- for (const data of iterator2) {
56
- row.b = ++count + 5;
57
- expect(data).to.deep.equal(row);
58
- }
59
- expect(count).to.equal(5);
60
- });
61
- it('should work with RETURNING clause', function () {
62
- let stmt = this.db.prepare("INSERT INTO entries (a, b) VALUES ('bar', 888), ('baz', 999) RETURNING *");
63
- expect(stmt.reader).to.be.true;
64
- expect([...stmt.iterate()]).to.deep.equal([
65
- { a: 'bar', b: 888, c: null, d: null, e: null },
66
- { a: 'baz', b: 999, c: null, d: null, e: null },
67
- ]);
68
-
69
- stmt = this.db.prepare("SELECT * FROM entries WHERE b > 800 ORDER BY rowid");
70
- expect([...stmt.iterate()]).to.deep.equal([
71
- { a: 'bar', b: 888, c: null, d: null, e: null },
72
- { a: 'baz', b: 999, c: null, d: null, e: null },
73
- ]);
74
- });
75
- it('should obey the current pluck and expand settings', function () {
76
- const shouldHave = (desiredData) => {
77
- let i = 0;
78
- for (const data of stmt.iterate()) {
79
- i += 1;
80
- if (typeof desiredData === 'object' && desiredData !== null) {
81
- if (Array.isArray(desiredData)) {
82
- desiredData[1] = i;
83
- } else if (typeof desiredData.entries === 'object' && desiredData.entries !== null) {
84
- desiredData.entries.b = i;
85
- } else {
86
- desiredData.b = i;
87
- }
88
- }
89
- expect(data).to.deep.equal(desiredData);
90
- }
91
- expect(i).to.equal(10);
92
- };
93
- const expanded = {
94
- entries: { a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null },
95
- $: { c: 5.5 },
96
- };
97
- const row = Object.assign({}, expanded.entries, expanded.$);
98
- const plucked = expanded.entries.a;
99
- const raw = Object.values(expanded.entries).concat(expanded.$.c);
100
- const stmt = this.db.prepare("SELECT *, 2 + 3.5 AS c FROM entries ORDER BY rowid");
101
- shouldHave(row);
102
- stmt.pluck(true);
103
- shouldHave(plucked);
104
- shouldHave(plucked);
105
- stmt.pluck(false);
106
- shouldHave(row);
107
- shouldHave(row);
108
- stmt.pluck();
109
- shouldHave(plucked);
110
- shouldHave(plucked);
111
- stmt.expand();
112
- shouldHave(expanded);
113
- shouldHave(expanded);
114
- stmt.expand(false);
115
- shouldHave(row);
116
- shouldHave(row);
117
- stmt.expand(true);
118
- shouldHave(expanded);
119
- shouldHave(expanded);
120
- stmt.pluck(true);
121
- shouldHave(plucked);
122
- shouldHave(plucked);
123
- stmt.raw();
124
- shouldHave(raw);
125
- shouldHave(raw);
126
- stmt.raw(false);
127
- shouldHave(row);
128
- shouldHave(row);
129
- stmt.raw(true);
130
- shouldHave(raw);
131
- shouldHave(raw);
132
- stmt.expand(true);
133
- shouldHave(expanded);
134
- shouldHave(expanded);
135
- });
136
- it('should close the iterator when throwing in a for-of loop', function () {
137
- const err = new Error('foobar');
138
- const stmt = this.db.prepare("SELECT * FROM entries ORDER BY rowid");
139
- const iterator = stmt.iterate();
140
- let count = 0;
141
- expect(() => {
142
- for (const row of iterator) { ++count; throw err; }
143
- }).to.throw(err);
144
- expect(count).to.equal(1);
145
- expect(iterator.next()).to.deep.equal({ value: undefined, done: true });
146
- for (const row of iterator) ++count;
147
- expect(count).to.equal(1);
148
- for (const row of stmt.iterate()) ++count;
149
- expect(count).to.equal(11);
150
- });
151
- it('should close the iterator when using break in a for-of loop', function () {
152
- const stmt = this.db.prepare("SELECT * FROM entries ORDER BY rowid");
153
- const iterator = stmt.iterate();
154
- let count = 0;
155
- for (const row of iterator) { ++count; break; }
156
- expect(count).to.equal(1);
157
- expect(iterator.next()).to.deep.equal({ value: undefined, done: true });
158
- for (const row of iterator) ++count;
159
- expect(count).to.equal(1);
160
- for (const row of stmt.iterate()) ++count;
161
- expect(count).to.equal(11);
162
- });
163
- it('should return an empty iterator when no rows were found', function () {
164
- const stmt = this.db.prepare("SELECT * FROM entries WHERE b == 999");
165
- expect(stmt.iterate().next()).to.deep.equal({ value: undefined, done: true });
166
- for (const data of stmt.pluck().iterate()) {
167
- throw new Error('This callback should not have been invoked');
168
- }
169
- });
170
- it('should accept bind parameters', function () {
171
- const shouldHave = (SQL, desiredData, args) => {
172
- let i = 0;
173
- const stmt = this.db.prepare(SQL);
174
- for (const data of stmt.iterate(...args)) {
175
- desiredData.b = ++i;
176
- expect(data).to.deep.equal(desiredData);
177
- }
178
- expect(i).to.equal(1);
179
- };
180
-
181
- const row = { a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null };
182
- const SQL1 = 'SELECT * FROM entries WHERE a=? AND b=? AND c=? AND d=? AND e IS ?';
183
- const SQL2 = 'SELECT * FROM entries WHERE a=@a AND b=@b AND c=@c AND d=@d AND e IS @e';
184
-
185
- shouldHave(SQL1, row, ['foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null]);
186
- shouldHave(SQL1, row, [['foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null]]);
187
- shouldHave(SQL1, row, [['foo', 1], [3.14], Buffer.alloc(4).fill(0xdd), [,]]);
188
- shouldHave(SQL2, row, [{ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: undefined }]);
189
-
190
- for (const data of this.db.prepare(SQL2).iterate({ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xaa), e: undefined })) {
191
- throw new Error('This callback should not have been invoked');
192
- }
193
-
194
- expect(() =>
195
- this.db.prepare(SQL2).iterate(row, () => {})
196
- ).to.throw(TypeError);
197
-
198
- expect(() =>
199
- this.db.prepare(SQL2).iterate({ a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd) })
200
- ).to.throw(RangeError);
201
-
202
- expect(() =>
203
- this.db.prepare(SQL1).iterate()
204
- ).to.throw(RangeError);
205
-
206
- expect(() =>
207
- this.db.prepare(SQL2).iterate()
208
- ).to.throw(TypeError);
209
-
210
- expect(() =>
211
- this.db.prepare(SQL2).iterate(row, {})
212
- ).to.throw(TypeError);
213
-
214
- expect(() =>
215
- this.db.prepare(SQL2).iterate({})
216
- ).to.throw(RangeError);
217
-
218
- this.db.prepare(SQL1).iterate('foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null).return();
219
- expect(() =>
220
- this.db.prepare(SQL1).iterate('foo', 1, new (function(){})(), Buffer.alloc(4).fill(0xdd), null)
221
- ).to.throw(TypeError);
222
- });
223
- });