forked from m4heshd/better-sqlite3-multiple-ciphers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40.bigints.js
145 lines (132 loc) · 7.39 KB
/
40.bigints.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
const Database = require('../.');
describe('BigInts', function () {
beforeEach(function () {
this.db = new Database(util.next());
this.db.prepare('CREATE TABLE entries (a INTEGER, b REAL, c TEXT)').run();
});
afterEach(function () {
this.db.close();
});
it('should bind to prepared statements', function () {
const int = BigInt('1006028374637854687');
this.db.prepare('INSERT INTO entries VALUES (?, ?, ?)').run(int, int, int);
this.db.prepare('INSERT INTO entries VALUES (?, ?, ?)').bind(int, int, int).run();
const db2 = new Database(util.next());
try {
db2.prepare('CREATE TABLE entries (a INTEGER, b REAL, c TEXT)').run();
db2.prepare('INSERT INTO entries VALUES (?, ?, ?)').run(int, int, int);
db2.prepare('INSERT INTO entries VALUES (?, ?, ?)').bind(int, int, int).run();
} finally {
db2.close();
}
});
it('should be allowed as a return value in user-defined functions', function () {
this.db.function('returnsInteger', a => BigInt(a + a));
expect(this.db.prepare('SELECT returnsInteger(?)').pluck().get(42)).to.equal(84);
});
it('should get returned by operations after setting .safeIntegers()', function () {
const int = BigInt('1006028374637854687');
this.db.prepare('INSERT INTO entries VALUES (?, ?, ?)').run(int, int, int);
this.db.prepare('INSERT INTO entries VALUES (?, ?, ?)').run(int, int, int);
let stmt = this.db.prepare('SELECT a FROM entries').pluck();
expect(stmt.get()).to.equal(1006028374637854700);
expect(stmt.safeIntegers().get()).to.deep.equal(int);
expect(stmt.get()).to.deep.equal(int);
expect(stmt.safeIntegers(false).get()).to.equal(1006028374637854700);
expect(stmt.get()).to.equal(1006028374637854700);
expect(stmt.safeIntegers(true).get()).to.deep.equal(int);
expect(stmt.get()).to.deep.equal(int);
stmt = this.db.prepare('SELECT b FROM entries').pluck();
expect(stmt.get()).to.equal(1006028374637854700);
expect(stmt.safeIntegers().get()).to.equal(1006028374637854700);
stmt = this.db.prepare('SELECT c FROM entries').pluck();
expect(stmt.get()).to.equal('1006028374637854687');
expect(stmt.safeIntegers().get()).to.equal('1006028374637854687');
let lastRowid = this.db.prepare('SELECT rowid FROM entries ORDER BY rowid DESC').pluck().get();
stmt = this.db.prepare('INSERT INTO entries VALUES (?, ?, ?)');
expect(stmt.run(int, int, int).lastInsertRowid).to.equal(++lastRowid);
expect(stmt.safeIntegers().run(int, int, int).lastInsertRowid).to.deep.equal(BigInt(++lastRowid));
expect(stmt.run(int, int, int).lastInsertRowid).to.deep.equal(BigInt(++lastRowid));
expect(stmt.safeIntegers(false).run(int, int, int).lastInsertRowid).to.equal(++lastRowid);
});
it('should get passed to functions defined with the "safeIntegers" option', function () {
this.db.function('customfunc', { safeIntegers: true }, (a) => { return (typeof a) + a; });
expect(this.db.prepare('SELECT customfunc(?)').pluck().get(2)).to.equal('number2');
expect(this.db.prepare('SELECT customfunc(?)').pluck().get(BigInt(2))).to.equal('bigint2');
});
it('should get passed to aggregates defined with the "safeIntegers" option', function () {
this.db.aggregate('customagg', { safeIntegers: true, step: (_, a) => { return (typeof a) + a; } });
expect(this.db.prepare('SELECT customagg(?)').pluck().get(2)).to.equal('number2');
expect(this.db.prepare('SELECT customagg(?)').pluck().get(BigInt(2))).to.equal('bigint2');
});
it('should get passed to virtual tables defined with the "safeIntegers" option', function () {
this.db.table('customvtab', { safeIntegers: true, columns: ['x'], *rows(a) { yield [(typeof a) + a]; } });
expect(this.db.prepare('SELECT * FROM customvtab(?)').pluck().get(2)).to.equal('number2');
expect(this.db.prepare('SELECT * FROM customvtab(?)').pluck().get(BigInt(2))).to.equal('bigint2');
});
it('should respect the default setting on the database', function () {
let arg;
const int = BigInt('1006028374637854687');
const customFunctionArg = (name, options, dontDefine) => {
dontDefine || this.db.function(name, options, (a) => { arg = a; });
this.db.prepare(`SELECT ${name}(?)`).get(int);
return arg;
};
const customAggregateArg = (name, options, dontDefine) => {
dontDefine || this.db.aggregate(name, { ...options, step: (_, a) => { arg = a; } });
this.db.prepare(`SELECT ${name}(?)`).get(int);
return arg;
};
const customTableArg = (name, options, dontDefine) => {
dontDefine || this.db.table(name, { ...options, columns: ['x'], *rows(a) { arg = a; } });
this.db.prepare(`SELECT * FROM ${name}(?)`).get(int);
return arg;
};
this.db.prepare('INSERT INTO entries VALUES (?, ?, ?)').run(int, int, int);
this.db.defaultSafeIntegers(true);
const stmt = this.db.prepare('SELECT a FROM entries').pluck();
expect(stmt.get()).to.deep.equal(int);
expect(stmt.safeIntegers(false).get()).to.equal(1006028374637854700);
expect(customFunctionArg('a1')).to.deep.equal(int);
expect(customFunctionArg('a2', { safeIntegers: false })).to.equal(1006028374637854700);
expect(customAggregateArg('a1')).to.deep.equal(int);
expect(customAggregateArg('a2', { safeIntegers: false })).to.equal(1006028374637854700);
expect(customTableArg('a1')).to.deep.equal(int);
expect(customTableArg('a2', { safeIntegers: false })).to.equal(1006028374637854700);
this.db.defaultSafeIntegers(false);
const stmt2 = this.db.prepare('SELECT a FROM entries').pluck();
expect(stmt2.get()).to.equal(1006028374637854700);
expect(stmt2.safeIntegers().get()).to.deep.equal(int);
expect(customFunctionArg('a3')).to.equal(1006028374637854700);
expect(customFunctionArg('a4', { safeIntegers: true })).to.deep.equal(int);
expect(customAggregateArg('a3')).to.equal(1006028374637854700);
expect(customAggregateArg('a4', { safeIntegers: true })).to.deep.equal(int);
expect(customTableArg('a3')).to.equal(1006028374637854700);
expect(customTableArg('a4', { safeIntegers: true })).to.deep.equal(int);
this.db.defaultSafeIntegers();
expect(stmt.get()).to.equal(1006028374637854700);
expect(stmt2.get()).to.deep.equal(int);
expect(customFunctionArg('a1', {}, true)).to.deep.equal(int);
expect(customFunctionArg('a2', {}, true)).to.equal(1006028374637854700);
expect(customFunctionArg('a3', {}, true)).to.equal(1006028374637854700);
expect(customFunctionArg('a4', {}, true)).to.deep.equal(int);
expect(customAggregateArg('a1', {}, true)).to.deep.equal(int);
expect(customAggregateArg('a2', {}, true)).to.equal(1006028374637854700);
expect(customAggregateArg('a3', {}, true)).to.equal(1006028374637854700);
expect(customAggregateArg('a4', {}, true)).to.deep.equal(int);
expect(customTableArg('a1', {}, true)).to.deep.equal(int);
expect(customTableArg('a2', {}, true)).to.equal(1006028374637854700);
expect(customTableArg('a3', {}, true)).to.equal(1006028374637854700);
expect(customTableArg('a4', {}, true)).to.deep.equal(int);
const stmt3 = this.db.prepare('SELECT a FROM entries').pluck();
expect(stmt3.get()).to.deep.equal(int);
expect(stmt3.safeIntegers(false).get()).to.equal(1006028374637854700);
expect(customFunctionArg('a5')).to.deep.equal(int);
expect(customFunctionArg('a6', { safeIntegers: false })).to.equal(1006028374637854700);
expect(customAggregateArg('a5')).to.deep.equal(int);
expect(customAggregateArg('a6', { safeIntegers: false })).to.equal(1006028374637854700);
expect(customTableArg('a5')).to.deep.equal(int);
expect(customTableArg('a6', { safeIntegers: false })).to.equal(1006028374637854700);
});
});