Skip to content

Commit a8c34ea

Browse files
authored
Merge pull request TryGhost#1078 from gms1/add-open-flags
fix TryGhost#1032: add constants for file open
2 parents 5bb0dc0 + 802afaf commit a8c34ea

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/node_sqlite3.cc

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ NAN_MODULE_INIT(RegisterModule) {
2222
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_READWRITE, OPEN_READWRITE);
2323
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_CREATE, OPEN_CREATE);
2424
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_FULLMUTEX, OPEN_FULLMUTEX);
25+
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_URI, OPEN_URI);
26+
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_SHAREDCACHE, OPEN_SHAREDCACHE);
27+
DEFINE_CONSTANT_INTEGER(target, SQLITE_OPEN_PRIVATECACHE, OPEN_PRIVATECACHE);
2528
DEFINE_CONSTANT_STRING(target, SQLITE_VERSION, VERSION);
2629
#ifdef SQLITE_SOURCE_ID
2730
DEFINE_CONSTANT_STRING(target, SQLITE_SOURCE_ID, SOURCE_ID);

test/constants.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ describe('constants', function() {
66
assert.ok(sqlite3.OPEN_READONLY === 1);
77
assert.ok(sqlite3.OPEN_READWRITE === 2);
88
assert.ok(sqlite3.OPEN_CREATE === 4);
9+
assert.ok(sqlite3.OPEN_URI === 0x00000040);
910
assert.ok(sqlite3.OPEN_FULLMUTEX === 0x00010000);
11+
assert.ok(sqlite3.OPEN_SHAREDCACHE === 0x00020000);
12+
assert.ok(sqlite3.OPEN_PRIVATECACHE === 0x00040000);
1013
});
1114

1215
it('should have the right error flags', function() {

test/open_close.test.js

+58
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,64 @@ describe('open/close', function() {
3030
helper.deleteFile('test/tmp/test_create.db');
3131
});
3232
});
33+
34+
describe('open and close non-existant shared database', function() {
35+
before(function() {
36+
helper.deleteFile('test/tmp/test_create_shared.db');
37+
});
38+
39+
var db;
40+
it('should open the database', function(done) {
41+
db = new sqlite3.Database('file:./test/tmp/test_create_shared.db', sqlite3.OPEN_URI | sqlite3.OPEN_SHAREDCACHE | sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, done);
42+
});
43+
44+
it('should close the database', function(done) {
45+
db.close(done);
46+
});
47+
48+
it('should have created the file', function() {
49+
assert.fileExists('test/tmp/test_create_shared.db');
50+
});
51+
52+
after(function() {
53+
helper.deleteFile('test/tmp/test_create_shared.db');
54+
});
55+
});
56+
57+
58+
(sqlite3.VERSION_NUMBER < 3008000 ? describe.skip : describe)('open and close shared memory database', function() {
59+
60+
var db1;
61+
var db2;
62+
63+
it('should open the first database', function(done) {
64+
db1 = new sqlite3.Database('file:./test/tmp/test_memory.db?mode=memory', sqlite3.OPEN_URI | sqlite3.OPEN_SHAREDCACHE | sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, done);
65+
});
66+
67+
it('should open the second database', function(done) {
68+
db2 = new sqlite3.Database('file:./test/tmp/test_memory.db?mode=memory', sqlite3.OPEN_URI | sqlite3.OPEN_SHAREDCACHE | sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, done);
69+
});
70+
71+
it('first database should set the user_version', function(done) {
72+
db1.exec('PRAGMA user_version=42', done);
73+
});
74+
75+
it('second database should get the user_version', function(done) {
76+
db2.get('PRAGMA user_version', function(err, row) {
77+
if (err) throw err;
78+
assert.equal(row.user_version, 42);
79+
done();
80+
});
81+
});
82+
83+
it('should close the first database', function(done) {
84+
db1.close(done);
85+
});
86+
87+
it('should close the second database', function(done) {
88+
db2.close(done);
89+
});
90+
});
3391

3492
it('should not be unable to open an inaccessible database', function(done) {
3593
// NOTE: test assumes that the user is not allowed to create new files

0 commit comments

Comments
 (0)