-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
132 lines (117 loc) · 3.57 KB
/
db.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
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
const bookDbFilePath = require('./config.json')['bookPath'];
const noteDbFilePath = require('./config.json')['notePath'];
const path = require('path');
const homedir = require('os').homedir();
const sqlite3 = require('sqlite3');
const util = require('util');
const fs = require('fs');
const NOTE_QUERY = `SELECT
ZANNOTATIONSELECTEDTEXT as SelectedText,
ZANNOTATIONNOTE as Note,
ZFUTUREPROOFING5 as Chapter,
ZANNOTATIONCREATIONDATE as Created,
ZANNOTATIONMODIFICATIONDATE as Modified,
ZANNOTATIONASSETID as AssetId,
ZANNOTATIONISUNDERLINE as Underlined,
case ZANNOTATIONSTYLE
WHEN '5' then '#f1c1f7'
WHEN '4' then '#f7c1cb'
WHEN '3' then '#fffbc2'
WHEN '2' then '#def1ff'
WHEN '1' then '#cef5e6'
END as color
FROM ZAEANNOTATION
WHERE ZANNOTATIONASSETID = "%s"
AND ZANNOTATIONSELECTEDTEXT IS NOT NULL
ORDER BY ZANNOTATIONASSETID ASC,Created ASC`;
const BOOK_QUERY = `SELECT
ZASSETID,
ZTITLE AS Title,
ZAUTHOR AS Author
FROM ZBKLIBRARYASSET
WHERE ZTITLE IS NOT NULL`;
const BOOK_QUERY_PARAM = `SELECT
ZASSETID,
ZTITLE AS Title,
ZAUTHOR AS Author
FROM ZBKLIBRARYASSET
WHERE ZASSETID = "%s"
AND ZTITLE IS NOT NULL`;
function fromDir(startPath,filter){
if (!fs.existsSync(startPath)){
console.log("no dir ",startPath);
return;
}
const foundFiles = [];
var files=fs.readdirSync(startPath);
for(var i=0;i<files.length;i++){
var filename=path.join(startPath,files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()){
fromDir(filename,filter); //recurse
}
else if (filename.endsWith(filter)) {
foundFiles.push(filename);
};
};
return foundFiles;
};
async function getConnection(filePath) {
const basePath = path.join(homedir, filePath);
const resolvedFilePath = fromDir(basePath, '.sqlite');
let db;
return new Promise((resolve, reject) => {
db = new sqlite3.Database(resolvedFilePath[0], (err) => {
if (err) {
return reject(err);
}
return resolve(db);
});
});
}
async function getAllNotes(bookId) {
const dbNote = await getConnection(noteDbFilePath);
return new Promise((resolve, reject) => {
const queries = [];
dbNote.each(util.format(NOTE_QUERY, bookId), (err, row) => {
if (err) {
reject(err); // optional: you might choose to swallow errors.
} else {
queries.push(row); // accumulate the data
}
}, (err, n) => {
if (err) {
reject(err); // optional: again, you might choose to swallow this error.
} else {
resolve(queries); // resolve the promise
}
});
});
}
async function getBooks(bookId) {
let query;
if (bookId){
query = util.format(BOOK_QUERY_PARAM, bookId);
}else{
query = BOOK_QUERY;
}
const dbBook = await getConnection(bookDbFilePath);
return new Promise((resolve, reject) => {
const queries = [];
dbBook.each(query, (err, row) => {
if (err) {
reject(err); // optional: you might choose to swallow errors.
} else {
queries.push(row); // accumulate the data
}
}, (err, n) => {
if (err) {
reject(err); // optional: again, you might choose to swallow this error.
} else {
resolve(queries); // resolve the promise
}
});
});
}
module.exports.getBooks = getBooks;
module.exports.getAllNotes = getAllNotes;