forked from juzeon/dd-signal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbm.js
53 lines (52 loc) · 2.05 KB
/
dbm.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
const $=require('./includes');
const path=require('path');
const fs=require('fs');
const Database = require('better-sqlite3');
const db = new Database(path.join(__dirname,'dd-signal.db'), { verbose: console.log });
let tables=['vtbs','watch'];
for(let table of tables){
let result=db.prepare('SELECT count(*) as exist FROM sqlite_master WHERE type=\'table\' AND name = ?').get(table);
if(!result.exist){
db.exec(fs.readFileSync(path.join(__dirname,'sql/'+table+'.sql'),'utf8'));
}
}
module.exports={
addVtbToWatch(chatid,mid,roomid,username,liveStatus,title){
const vtb=this.getVtbByMid(mid);
if(!vtb) {
db.prepare('insert into vtbs (mid,roomid,username,liveStatus,title) values' +
'(?,?,?,?,?)').run(mid, roomid, username, liveStatus, title);
$.emitter.emit('updateVtbs');
}
db.prepare('insert into watch (chatid,mid) values(?,?)').run(chatid,mid);
},
getVtbByMid(mid){
return db.prepare('select * from vtbs where mid=?').get(mid);
},
getWatchByChatid(chatid){
return db.prepare('select w.*,v.* from watch w inner join vtbs v on w.mid=v.mid where w.chatid=?').all(chatid);
},
existsWatch(chatid,mid){
return db.prepare('select rowid from watch where chatid=? and mid=?').get(chatid,mid)?true:false;
},
delWatch(chatid,mid){
db.prepare('delete from watch where chatid=? and mid=?').run(chatid,mid);
let other=db.prepare('select * from watch where mid=?').get(mid);
if(!other){
db.prepare('delete from vtbs where mid=?').run(mid);
$.emitter.emit('updateVtbs');
}
},
getVtbByUsername(username){
return db.prepare('select * from vtbs where username=?').get(username);
},
getVtbs(){
return db.prepare('select * from vtbs').all();
},
updateVtbColumn(column,value,mid){
db.prepare('update vtbs set '+column+'=? where mid=?').run(value,mid);
},
getWatchByMid(mid){
return db.prepare('select * from watch where mid=?').all(mid);
}
};