-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.js
145 lines (138 loc) · 5.55 KB
/
database.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
// Establishing connection to the database
var logger = require("./logger");
var mysql = require('mysql');
var util= require('util');
class Database {
constructor() {
this.connection = mysql.createPool({
connectionLimit: config.connectionLimit,
host: config.host,
user: config.user,
password: config.password,
database: config.database,
multipleStatements: config.multipleStatements,
timezone: config.timezone
});
}
mysqlSecElapsed(timestr) {
if (timestr===null)
return(1000);
var date = new Date();
var date_sec = date.getTime();
var timestr_sec = timestr;
console.log(date_sec);
console.log(timestr_sec);
console.log((timestr_sec-date_sec)/1000);
return((timestr_sec-date_sec)/1000);
}
mysqlNow() {
var date;
date = new Date();
date = date.getUTCFullYear() + '-' +
('00' + (date.getUTCMonth() + 1)).slice(-2) + '-' +
('00' + date.getUTCDate()).slice(-2) + ' ' +
('00' + date.getUTCHours()).slice(-2) + ':' +
('00' + date.getUTCMinutes()).slice(-2) + ':' +
('00' + date.getUTCSeconds()).slice(-2);
return(date);
}
query( sql, args ) {
logger.info('#server.database.query: SQL: %s',sql);
return new Promise( ( resolve, reject ) => {
this.connection.query( sql, args, ( err, rows ) => {
if ( err )
return reject( err );
resolve( rows );
} );
} );
}
queryuser( userId, cb) {
return new Promise((resolve, reject) => {
var sql = "SELECT *, TIMESTAMPDIFF(SECOND, blockchaintransact, UTC_TIMESTAMP()) AS blockchaintransactsecs FROM user WHERE id=" + userId;
this.query(sql)
.then(async function (rows) {
if (rows.length == 0) {
logger.error('#server.database.queryuser: Could not find user');
reject("Coudn't find userid " + userId);
}
if (rows.length > 1) {
logger.error('#server.database.queryuser: Multiple userids found');
reject("Multiple IDs found for: " + userId);
}
resolve(rows);
})
.catch(function (error) {
logger.error('#server.database.queryuser: Issue with query: ', sql);
reject(err);
})
})
}
queryaddr( userId, coin ,cb) {
return new Promise((resolve, reject) => {
var sql = "SELECT * FROM user WHERE id=" + userId;
this.query(sql)
.then(function (rows) {
if (rows.length == 0) {
logger.error('#server.database.queryaddr: Could not find userid: %s', userId);
reject("Coudn't find userid " + userId);
}
if (rows.length > 1) {
logger.error('#server.database.queryaddr: Severe problem with userid: %s, %s', userId, rows.length);
reject("Too many userids for " + userId);
}
// Check if the ath address is valid
if (rows[0].athaddr === "") {
athGetAddress(coin)
.then(function (address) {
var vsql = "UPDATE user SET athaddr='" + address + "' WHERE id=" + userId;
this.query(vsql)
.then(function(rows1) {
return(address);
})
.catch(function(error) {
return(error);
})
});
} else {
switch (coin) {
case "ATH":
resolve(rows[0].athaddr);
break;
case "ETHO":
resolve(rows[0].ethoaddr);
break;
}
}
})
.catch(function (error) {
return(error);
})
});
}
logging(userid, str) {
return new Promise( ( resolve, reject ) => {
var date;
date = new Date();
date = date.getUTCFullYear() + '-' +
('00' + (date.getUTCMonth() + 1)).slice(-2) + '-' +
('00' + date.getUTCDate()).slice(-2) + ' ' +
('00' + date.getUTCHours()).slice(-2) + ':' +
('00' + date.getUTCMinutes()).slice(-2) + ':' +
('00' + date.getUTCSeconds()).slice(-2);
if (str.length>255) {
if (debugon)
console.log(" >>> DEBUG (fn=logging) String is to long for logging: ", str);
} else {
this.connection.query("INSERT INTO logs (userid, log, date) VALUES (" + userid + ", '" + str + "','" + date + "')", (err, rows) => {
if (err)
return reject(err);
resolve(rows);
});
}
});
}
escape(arg) {
return(this.connection.escape(arg));
}
}
module.exports = Database;