-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_sqlite.js
201 lines (200 loc) · 5.81 KB
/
db_sqlite.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
function DB(){
var sq=require('sqlite3').verbose();
var db=new sq.Database('./'+global.config.db.data.database+'.sqlite3');
db.serialize(function(){
console.log('DB CONNECTED');
// db.run("DELETE FROM trades");
db.run("CREATE TABLE IF NOT EXISTS users ("
+"_id integer primary key autoincrement"
+", username integer not null UNIQUE"
+", date integer not null"
+", password text not null"
+")");
db.run("CREATE TABLE IF NOT EXISTS apis ("
+"_id integer primary key autoincrement"
+", user integer not null"
+", name text not null"
+", market text not null"
+", date integer not null"
+", data text null"
+", active integer default 1"
+")");
db.run("CREATE INDEX IF NOT EXISTS apis_user ON apis(user)");
db.run("CREATE TABLE IF NOT EXISTS tracks ("
+"_id integer primary key autoincrement"
+", user integer not null"
+", api integer not null"
+", market text not null"
+", pair text not null"
+", date integer not null"
+", amount real not null"
+", track text not null"
+", action text not null"
+", action_param text null"
+", status integer default 0"
+", active integer default 1"
+")");
db.run("CREATE INDEX IF NOT EXISTS tracks_user ON tracks(user,active)");
db.run("CREATE TABLE IF NOT EXISTS trades ("
+"_id integer primary key autoincrement"
+", user integer not null"
+", symbol text not null"
+", market text not null"
+", market_id text null"
+", type text not null"
+", amount real not null"
+", price real null"
+", date integer not null"
+")");
});
this.db=db;
};
DB.prototype.newUser=function(username,password,callback){
var stmt=this.db.prepare("INSERT INTO users(username,date,password) VALUES (?,?,?)");
stmt.run(username.toLowerCase(),new Date().getTime(),password,function(err){
if(err){
if(err.errno==19){
callback('Username already registered');
}else
callback(err);
console.log(err);
}else
callback(false,this.lastID);
});
stmt.finalize();
};
DB.prototype.saveApi=function(user,market,name,data,callback){
var stmt=this.db.prepare("INSERT INTO apis(user,name,market,date,data) VALUES (?,?,?,?,?)");
stmt.run(user,name,market,new Date().getTime(),JSON.stringify(data),function(err){
if(err && callback){
callback(err);
}else if(callback)
callback(false,this.lastID);
});
stmt.finalize();
};
DB.prototype.saveTrack=function(data,callback){
var stmt=this.db.prepare("INSERT INTO tracks(user,api,market,pair,date,amount,track,action,action_param) VALUES (?,?,?,?,?,?,?,?,?)");
stmt.run(data.user,data.api,data.market,data.pair,new Date().getTime(),data.amount,JSON.stringify(data.track),data.action,data.action_price,function(err){
if(err){
callback(err);
}else
callback(false,this.lastID);
});
stmt.finalize();
};
DB.prototype.saveTrade=function(data,callback){
var stmt=this.db.prepare("INSERT INTO trades(user,symbol,market,market_id,type,amount,price,date) VALUES (?,?,?,?,?,?,?,?)");
stmt.run(data.user,data.symbol,data.market,data.market_id
,data.type,data.amount,data.price
,data.date,function(err){
if(err && callback){
callback(err);
}else if(callback)
callback(false,this.lastID);
});
stmt.finalize();
};
DB.prototype.setTradeID=function(id,market_id){
this.db.run("UPDATE trades SET market_id=? WHERE _id=?",[market_id,id]);
};
DB.prototype.deleteApi=function(id,user){
this.db.run("DELETE from apis WHERE _id=? and user=?",[id,user]);
};
DB.prototype.deleteTrack=function(id,user){
this.db.run("DELETE from tracks WHERE _id=? and user=?",[id,user]);
};
DB.prototype.setTrackStatus=function(id,status,finish){
var active=(finish?0:1);
this.db.run("UPDATE tracks SET status=?,active=? WHERE _id=?",[status,active,id]);
};
DB.prototype.getUser=function(username,callback){
this.db.all("SELECT * FROM users where username=?",[username],function(error,rows){
if(error){
callback(error);
// console.log("DBgetOrders-E:"+error);
return;
}
callback(false,rows[0]);
});
};
DB.prototype.getTrack=function(id,callback){
this.db.all("SELECT tracks.*,apis.data as api_data,apis.active as api_active FROM tracks INNER JOIN apis ON tracks.api=apis._id where tracks._id=?",[id],function(error,rows){
if(error){
callback(error);
// console.log("DBgetOrders-E:"+error);
return;
}
var o=rows[0];
try{
o.track=JSON.parse(o.track);
o.api_data=JSON.parse(o.api_data);
}catch(e){
o.track=[];
o.api_data={};
}
callback(false,o);
});
};
DB.prototype.getApis=function(user,callback){
this.db.all("SELECT * FROM apis where user=?",[user],function(error,rows){
if(error){
callback(error);
// console.log("DBgetOrders-E:"+error);
return;
}
for(var i in rows){
try{
rows[i].data=JSON.parse(rows[i].data);
}catch(e){
rows[i].data=[];
}
}
callback(false,rows);
});
};
DB.prototype.getTracks=function(user,callback){
this.db.all("SELECT * FROM tracks where user=? and active=1",[user],function(error,rows){
if(error){
callback(error);
// console.log("DBgetOrders-E:"+error);
return;
}
for(var i in rows){
try{
rows[i].track=JSON.parse(rows[i].track);
}catch(e){
rows[i].track=[];
}
}
callback(false,rows);
});
};
DB.prototype.getAllTracks=function(callback){
this.db.all("SELECT * FROM tracks where active=1",function(error,rows){
if(error){
callback(error);
// console.log("DBgetOrders-E:"+error);
return;
}
for(var i in rows){
try{
rows[i].track=JSON.parse(rows[i].track);
}catch(e){
rows[i].track=[];
}
}
callback(false,rows);
});
};
DB.prototype.getOrders=function(user,callback){
this.db.all("SELECT * FROM trades WHERE user=? order by date desc",[user],function(error,rows){
if(error){
callback(error);
// console.log("DBgetOrders-E:"+error);
return;
}
callback(false,rows);
});
};
module.exports=DB;