Skip to content

Commit 70cfc05

Browse files
committed
simplified .all and added tests for it
1 parent 5a019b7 commit 70cfc05

File tree

12 files changed

+148
-37
lines changed

12 files changed

+148
-37
lines changed

src/objects/statement/all.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ NAN_METHOD(Statement::All) {
2828
v8::Local<v8::Array> returnedArray = Nan::New<v8::Array>(row_count);
2929

3030
if (row_count > 0) {
31-
unsigned int i = 0;
32-
rows.Flush([&returnedArray, &i] (v8::Local<v8::Value> value) {
33-
Nan::Set(returnedArray, i++, value);
31+
rows.Flush([&returnedArray, &row_count] (v8::Local<v8::Value> value) {
32+
Nan::Set(returnedArray, --row_count, value);
3433
});
3534
}
3635
QUERY_RETURN(stmt, STATEMENT_CLEAR_BINDINGS, returnedArray);

src/util/list.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ class List {
99
Node* next;
1010
} Node;
1111
Node* first;
12-
Node* last;
1312

1413
public:
15-
explicit List() : first(NULL), last(NULL) {}
14+
explicit List() : first(NULL) {}
1615

1716
~List() {
1817
while (first != NULL) {
@@ -22,30 +21,24 @@ class List {
2221
}
2322
}
2423

25-
// Pushes an item onto the list.
24+
// Unshifts an item onto the list.
2625
void Add(T item) {
2726
Node* new_node = new Node;
2827
new_node->item = item;
29-
new_node->next = NULL;
30-
if (last == NULL) {
31-
first = last = new_node;
32-
} else {
33-
last->next = new_node;
34-
last = new_node;
35-
}
28+
new_node->next = first;
29+
first = new_node;
3630
}
3731

3832
// Executes a function for each item in the list, and removes them all
3933
// from the list. The passed function must not modify the list.
40-
// The execution order goes from first-added to last-added.
34+
// The execution order goes from last-added to first-added.
4135
template <class F> void Flush(F fn) {
4236
while (first != NULL) {
4337
fn(first->item);
4438
Node* next = first->next;
4539
delete first;
4640
first = next;
4741
}
48-
last = NULL;
4942
}
5043
};
5144

test/10.database.open.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
var expect = require('chai').expect;
22
var fs = require('fs');
33
var Database = require('../.');
4-
var util = require('../tools/test-util.js');
4+
var util = (function () {
5+
var path = require('path');
6+
var dbId = 0;
7+
var obj;
8+
return obj = {
9+
current: function () {
10+
return 'temp/' + path.basename(__filename).split('.')[0] + '.' + dbId + '.db';
11+
},
12+
next: function () {++dbId; return obj.current();}
13+
};
14+
}());
515

616
describe('new Database()', function () {
717
it('should throw an exception when file path is not a string', function () {

test/11.database.close.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
var expect = require('chai').expect;
22
var Database = require('../.');
3-
var util = require('../tools/test-util.js');
3+
var util = (function () {
4+
var path = require('path');
5+
var dbId = 0;
6+
var obj;
7+
return obj = {
8+
current: function () {
9+
return 'temp/' + path.basename(__filename).split('.')[0] + '.' + dbId + '.db';
10+
},
11+
next: function () {++dbId; return obj.current();}
12+
};
13+
}());
414

515
describe('Database#close()', function () {
616
describe('before opening it', function () {

test/12.database.pragma.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
var expect = require('chai').expect;
22
var Database = require('../.');
3-
var util = require('../tools/test-util.js');
3+
var util = (function () {
4+
var path = require('path');
5+
var dbId = 0;
6+
var obj;
7+
return obj = {
8+
current: function () {
9+
return 'temp/' + path.basename(__filename).split('.')[0] + '.' + dbId + '.db';
10+
},
11+
next: function () {++dbId; return obj.current();}
12+
};
13+
}());
414

515
describe('Database#pragma()', function () {
616
it('should throw an exception if a string is not provided', function (done) {

test/13.database.prepare.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
var expect = require('chai').expect;
22
var Database = require('../.');
3-
var util = require('../tools/test-util.js');
3+
var util = (function () {
4+
var path = require('path');
5+
var dbId = 0;
6+
var obj;
7+
return obj = {
8+
current: function () {
9+
return 'temp/' + path.basename(__filename).split('.')[0] + '.' + dbId + '.db';
10+
},
11+
next: function () {++dbId; return obj.current();}
12+
};
13+
}());
414

515
describe('Database#prepare()', function () {
616
it('should throw an exception if a string is not provided', function (done) {

test/14.database.transaction.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
var expect = require('chai').expect;
22
var Database = require('../.');
3-
var util = require('../tools/test-util.js');
3+
var util = (function () {
4+
var path = require('path');
5+
var dbId = 0;
6+
var obj;
7+
return obj = {
8+
current: function () {
9+
return 'temp/' + path.basename(__filename).split('.')[0] + '.' + dbId + '.db';
10+
},
11+
next: function () {++dbId; return obj.current();}
12+
};
13+
}());
414

515
describe('Database#transaction()', function () {
616
it('should throw an exception if an array of strings is not provided', function (done) {

test/20.statement.run.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict';
22
var expect = require('chai').expect;
33
var Database = require('../.');
4-
var util = require('../tools/test-util.js');
54
var db;
65

76
before(function (done) {
8-
db = new Database(util.next());
7+
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db');
98
db.on('open', done);
109
});
1110

test/21.statement.get.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict';
22
var expect = require('chai').expect;
33
var Database = require('../.');
4-
var util = require('../tools/test-util.js');
54
var db;
65

76
before(function (done) {
8-
db = new Database(util.next());
7+
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db');
98
db.on('open', done);
109
});
1110

test/22.statement.all.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict';
2+
var expect = require('chai').expect;
3+
var Database = require('../.');
4+
var db;
5+
6+
before(function (done) {
7+
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db');
8+
db.on('open', done);
9+
});
10+
11+
describe('Statement#all()', function () {
12+
it('should throw an exception when used on a write statement', function () {
13+
db.prepare('CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)').run();
14+
var stmt = db.prepare("INSERT INTO entries VALUES ('foo', 1, 3.14, x'dddddddd', NULL)");
15+
expect(stmt.readonly).to.be.false;
16+
expect(function () {stmt.all();}).to.throw(TypeError);
17+
});
18+
it('should return the first matching row', function () {
19+
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();
20+
var row = {a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null};
21+
22+
var stmt = db.prepare("SELECT * FROM entries");
23+
expect(stmt.readonly).to.be.true;
24+
matchesFrom(stmt.all(), 1);
25+
26+
stmt = db.prepare("SELECT * FROM entries WHERE b > 5");
27+
matchesFrom(stmt.all(), 6);
28+
29+
function matchesFrom(rows, i) {
30+
for (var index = 0; i<=10; ++i, ++index) {
31+
row.b = i;
32+
expect(rows[index]).to.deep.equal(row);
33+
}
34+
expect(index).to.equal(rows.length);
35+
}
36+
});
37+
it('should obey the current pluck setting', function () {
38+
var stmt = db.prepare("SELECT * FROM entries");
39+
var plucked = new Array(10).fill('foo');
40+
expect(stmt.all()).to.not.deep.equal(plucked);
41+
expect(stmt.pluck(true).all()).to.deep.equal(plucked);
42+
expect(stmt.all()).to.deep.equal(plucked);
43+
expect(stmt.pluck(false).all()).to.not.deep.equal(plucked);
44+
expect(stmt.all()).to.not.deep.equal(plucked);
45+
expect(stmt.pluck().all()).to.deep.equal(plucked);
46+
expect(stmt.all()).to.deep.equal(plucked);
47+
});
48+
it('should return an empty array when no rows were found', function () {
49+
var stmt = db.prepare("SELECT * FROM entries WHERE b == 999");
50+
expect(stmt.all()).to.deep.equal([]);
51+
expect(stmt.pluck().all()).to.deep.equal([]);
52+
});
53+
it('should accept bind parameters', function () {
54+
var rows = [{a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null}];
55+
var SQL1 = 'SELECT * FROM entries WHERE a=? AND b=? AND c=? AND d=? AND e IS ?';
56+
var SQL2 = 'SELECT * FROM entries WHERE a=@a AND b=@b AND c=@c AND d=@d AND e IS @e';
57+
var result = db.prepare(SQL1).all('foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null);
58+
expect(result).to.deep.equal(rows);
59+
60+
result = db.prepare(SQL1).all(['foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null]);
61+
expect(result).to.deep.equal(rows);
62+
63+
result = db.prepare(SQL1).all(['foo', 1], [3.14], Buffer.alloc(4).fill(0xdd), [,]);
64+
expect(result).to.deep.equal(rows);
65+
66+
result = db.prepare(SQL2).all({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: undefined});
67+
expect(result).to.deep.equal(rows);
68+
69+
result = db.prepare(SQL2).all({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xaa), e: undefined});
70+
expect(result).to.deep.equal([]);
71+
72+
expect(function () {
73+
db.prepare(SQL2).all({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd)});
74+
}).to.throw(Error);
75+
76+
expect(function () {
77+
db.prepare(SQL1).all();
78+
}).to.throw(Error);
79+
80+
expect(function () {
81+
db.prepare(SQL2).all({});
82+
}).to.throw(Error);
83+
});
84+
});

0 commit comments

Comments
 (0)