-
-
Notifications
You must be signed in to change notification settings - Fork 847
Expand file tree
/
Copy pathexpandedSql.test.js
More file actions
244 lines (214 loc) · 8.61 KB
/
expandedSql.test.js
File metadata and controls
244 lines (214 loc) · 8.61 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var sqlite3 = require('..');
var assert = require('assert');
describe('expandedSql', function() {
var db;
before(function(done) {
db = new sqlite3.Database(':memory:');
db.serialize(function() {
db.run("CREATE TABLE foo (id INT, txt TEXT)");
db.run("CREATE TABLE complex (a INT, b INT, c INT, d INT, e INT)", done);
});
});
after(function(done) {
db.wait(function() {
db.close(done);
});
});
it('should be accessible as a property on Statement', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)", function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(NULL,NULL)");
stmt.finalize(done);
});
});
it('should show bound parameters in callbacks', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
stmt.run(1, "test", function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(1,'test')");
stmt.finalize(done);
});
});
it('should work with get callback', function(done) {
var stmt = db.prepare("SELECT * FROM foo WHERE id = ?");
stmt.get(1, function(err, row) {
if (err) throw err;
assert.equal(stmt.expandedSql, "SELECT * FROM foo WHERE id = 1");
assert.equal(row.id, 1);
stmt.finalize(done);
});
});
it('should work with all callback', function(done) {
var stmt = db.prepare("SELECT * FROM foo WHERE id > ?");
stmt.all(0, function(err, rows) {
if (err) throw err;
assert.equal(stmt.expandedSql, "SELECT * FROM foo WHERE id > 0");
assert.ok(rows.length > 0);
stmt.finalize(done);
});
});
it('should work with each callback', function(done) {
var stmt = db.prepare("SELECT * FROM foo WHERE id < ?");
var count = 0;
stmt.each(5, function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "SELECT * FROM foo WHERE id < 5");
count++;
}, function(err) {
if (err) throw err;
assert.ok(count > 0);
stmt.finalize(done);
});
});
it('should work with bind callback', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
stmt.bind(10, "hello", function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(10,'hello')");
stmt.run(function(err) {
if (err) throw err;
stmt.finalize(done);
});
});
});
it('should handle string escaping correctly', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
stmt.run(20, "test'quote", function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(20,'test''quote')");
stmt.finalize(done);
});
});
it('should handle incomplete bindings', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
stmt.run(20, function(err) {
if (err) throw err;
// sqlite fills unbound parameters with NULL, not ?
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(20,NULL)");
stmt.finalize(done);
});
});
it('should work with named parameters', function(done) {
var stmt = db.prepare("SELECT * FROM foo WHERE id = $id");
stmt.get({ $id: 1 }, function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "SELECT * FROM foo WHERE id = 1");
stmt.finalize(done);
});
});
it('should handle NULL values', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
stmt.run(200, null, function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(200,NULL)");
stmt.finalize(done);
});
});
it('should handle empty strings', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
stmt.run(1, "", function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(1,'')");
stmt.finalize(done);
});
});
it('should handle large integers', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
var maxSafeInt = Number.MAX_SAFE_INTEGER;
stmt.run(maxSafeInt, "large", function(err) {
if (err) throw err;
// sqlite formats large numbers - accept various scientific notations
var expanded = stmt.expandedSql;
assert.ok(
expanded.match(/INSERT INTO foo VALUES\(9\.00719.*[eE]\+1[45],'large'\)/),
'Expected large number in scientific notation, got: ' + expanded
);
stmt.finalize(done);
});
});
it('should return undefined before statement is prepared', function(done) {
var stmt = db.prepare("SELECT 1");
assert.equal(stmt.expandedSql, undefined);
stmt.finalize(done);
});
it('should return undefined after finalize', function(done) {
var stmt = db.prepare("SELECT 1");
stmt.finalize(function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, undefined);
done();
});
});
it('should update after reset and rebind', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
stmt.bind(5, "first", function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(5,'first')");
stmt.run(function(err) {
if (err) throw err;
stmt.reset(function(err) {
if (err) throw err;
stmt.bind(6, "second", function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(6,'second')");
stmt.finalize(done);
});
});
});
});
});
it('should work with batch inserts', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
var inserted = 0;
var batchSize = 5;
for (var i = 300; i < 300 + batchSize; i++) {
(function(id) {
stmt.run(id, 'batch' + id, function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(" + id + ",'batch" + id + "')");
inserted++;
if (inserted === batchSize) {
stmt.finalize(done);
}
});
})(i);
}
});
it('should work with complex queries with many parameters', function(done) {
var stmt = db.prepare("INSERT INTO complex VALUES(?,?,?,?,?)");
stmt.run(1, 2, 3, 4, 5, function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO complex VALUES(1,2,3,4,5)");
stmt.finalize(done);
});
});
it('should work within serialized transactions', function(done) {
db.serialize(function() {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
stmt.run(500, "transaction", function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(500,'transaction')");
stmt.finalize(done);
});
});
});
it('should be accessible outside callbacks after operations complete', function(done) {
var stmt = db.prepare("INSERT INTO foo VALUES(?,?)");
stmt.run(600, "outside", function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "INSERT INTO foo VALUES(600,'outside')");
stmt.finalize(done);
});
});
it('demonstrates the async queue behavior', function(done) {
var stmt = db.prepare("SELECT * FROM foo WHERE id = ?");
stmt.bind(1);
// worker thread still hasn't completed, so it isn't available
assert.equal(stmt.expandedSql, undefined);
stmt.bind(1, function(err) {
if (err) throw err;
assert.equal(stmt.expandedSql, "SELECT * FROM foo WHERE id = 1");
stmt.finalize(done);
});
});
});