forked from RiiConnect24/RiiTag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbdriver.js
153 lines (126 loc) · 4.4 KB
/
dbdriver.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Forked from Matthew Elswick's DB driver
// used in the Starie Tech Bot Framework
//
// Modified to fit the needs of this project.
const sqlite = require("sqlite3");
class DatabaseDriver {
constructor(filename) {
this.dbPromise = new sqlite.Database(filename);
}
async create(table, fields) {
var db = await this.dbPromise;
if (!db) {
console.error("Cannot access a non-existent database.");
}
await db.run(`CREATE TABLE IF NOT EXISTS \`${table}\`(${fields.join(", ")})`);
}
async insert(table, fields, values) {
var db = await this.dbPromise;
if (!db) {
console.error("Cannot access a non-existent database.");
}
await db.run(`INSERT INTO \`${table}\`(\`${fields.join("`, `")}\`) VALUES('${values.join("', '")}')`);
}
async delete(table, key, value) {
var db = await this.dbPromise;
if (!db) {
console.error("Cannot access a non-existent database.");
}
await db.run(`DELETE FROM \`${table}\` WHERE \`${key}\`="${value}"`);
}
async get(table, key, value) {
var db = await this.dbPromise;
// var results;
if (!db) {
console.error("Cannot access a non-existent database.");
}
var query = await new Promise(function(resolve, reject) {
db.get(`SELECT * FROM \`${table}\` WHERE \`${key}\` = "${value}"`, function(err, row) {
if (err) {
reject(err);
} else {
resolve(row);
}
});
});
return query;
}
async exists(table, key, value) {
var db = await this.dbPromise;
var driver = this;
if (!db) {
console.error("Cannot access a non-existent database.");
}
var query = await new Promise(function(resolve, reject) {
driver.get(table, key, value).then(function(res) {
if (res == undefined) {
resolve(false);
} else {
resolve(true);
}
}).catch(function(err) {
reject(err);
});
});
return query;
}
async getTableSorted(table, key, desc) {
var db = await this.dbPromise;
if (!db) {
console.error("Cannot access a non-existent database.");
}
if (!desc) {
return await this.all(`SELECT * FROM \`${table}\` ORDER BY \`${key}\``);
}
else {
return await this.all(`SELECT * FROM \`${table}\` ORDER BY \`${key}\` DESC`);
}
}
async updateTable(table, key, value, userKey, userValue) {
var db = await this.dbPromise;
if (!db) {
console.error("Cannot access a non-existent database.");
}
await db.run(`UPDATE \`${table}\` SET \`${key}\`="${value}" WHERE \`${userKey}\` = "${userValue}"`);
}
async run(sql) {
var db = await this.dbPromise;
if (!db) {
console.error("Cannot access a non-existent database.");
}
await db.run(sql);
}
async all(sql) {
var db = await this.dbPromise;
if (!db) {
console.error("Cannot access a non-existent database.");
}
return await new Promise(function(resolve, reject) {
db.all(sql, function(err, rows) {
if (err) {
reject(err);
}
else {
resolve(rows);
}
});
});
}
async increment(table, key, value, incKey) {
var db = await this.dbPromise;
if (!db) {
console.error("Cannot access a non-existent database.");
}
return await new Promise(function(resolve, reject) {
console.log(`UPDATE \`${table}\` SET \`${incKey}\` = \`${incKey}\' + 1 WHERE \`${key}\` = "${value}"`);
db.run(`UPDATE \`${table}\` SET \`${incKey}\` = \`${incKey}\` + 1 WHERE \`${key}\` = "${value}"`, function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
}
module.exports = DatabaseDriver;