-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (52 loc) · 1.35 KB
/
index.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
#!/usr/bin/env node
const program = require('commander');
const version = require('./package.json').version;
const sqlite3 = require('@journeyapps/sqlcipher').verbose();
const readline = require('readline');
program.version(version)
.usage('sqlcipher-cli -d test.db -s secretkey')
.requiredOption('-d, --database <database>', 'database file')
.requiredOption('-s, --secret <secret>', 'secret key for sqlcipher')
.parse(process.argv);
var lines = [];
const db = new sqlite3.Database(program.database);
db.run('PRAGMA key = "' + program.secret + '"');
const r = readline.createInterface({
input: process.stdin,
output: process.stdout
});
db.on('error', (e) => {
console.log('Cannot open database file');
process.exit();
});
db.on('open', () => {
console.clear();
console.log('Database connected');
r.prompt('>');
})
r.on('line', (line) => {
line = line.trim();
if (line === 'quit' && lines.length == 0) {
console.log('Bye');
r.close();
}
lines.push(line);
if (line.endsWith(';')) {
// join the lines with space character as a query string and execute it
db.all(lines.join(' '), [], (err, rows) => {
if (err) {
console.log(err);
} else {
console.log(rows);
}
lines = []
r.prompt('>');
})
} else {
r.prompt('>');
}
})
r.on('close', () => {
db.close();
process.exit();
})