Skip to content

Commit 73193c5

Browse files
committed
Implement Database.pragma()
1 parent 6b19d4b commit 73193c5

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ class Database {
134134
}
135135

136136
pragma(source, options) {
137-
throw new Error("not implemented");
137+
if (options == null) options = {};
138+
if (typeof source !== 'string') throw new TypeError('Expected first argument to be a string');
139+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
140+
const simple = options['simple'];
141+
const stmt = this.prepare(`PRAGMA ${source}`, this, true);
142+
return simple ? stmt.pluck().get() : stmt.all();
138143
}
139144

140145
backup(filename, options) {

integration-tests/tests/async.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ test.serial("Database.transaction()", async (t) => {
236236
t.is(stmt.get(5).name, "Junior");
237237
});
238238

239+
test.serial("Database.pragma()", async (t) => {
240+
const db = t.context.db;
241+
await db.pragma("cache_size = 2000");
242+
t.deepEqual(await db.pragma("cache_size"), [{ "cache_size": 2000 }]);
243+
});
244+
239245
test.serial("errors", async (t) => {
240246
const db = t.context.db;
241247

integration-tests/tests/sync.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ test.serial("values", async (t) => {
251251
t.deepEqual(stmt.get(9007199254740991n), [9007199254740991]);
252252
});
253253

254+
test.serial("Database.pragma()", async (t) => {
255+
const db = t.context.db;
256+
db.pragma("cache_size = 2000");
257+
t.deepEqual(db.pragma("cache_size"), [{ "cache_size": 2000 }]);
258+
});
259+
254260
test.serial("errors", async (t) => {
255261
const db = t.context.db;
256262

promise.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ class Database {
132132
}
133133

134134
pragma(source, options) {
135-
throw new Error("not implemented");
135+
if (options == null) options = {};
136+
if (typeof source !== 'string') throw new TypeError('Expected first argument to be a string');
137+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
138+
const simple = options['simple'];
139+
return this.prepare(`PRAGMA ${source}`, this, true).then(async (stmt) => {
140+
return simple ? await stmt.pluck().get() : await stmt.all();
141+
});
136142
}
137143

138144
backup(filename, options) {

0 commit comments

Comments
 (0)