-
Notifications
You must be signed in to change notification settings - Fork 7
/
model.js
44 lines (41 loc) · 1.29 KB
/
model.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
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root', // change line to fit your requirements
password : '', // change line to fit your requirements
database : 'exploitdb'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
exports.getExploits = function(id, options, callback) {
var sql = 'SELECT * from exploits';
var tmp = '';
for (var key in options) {
if (key == 'before' || key == 'after') {
tmp += ' AND _date';
if (key == 'before') {
tmp += ' <= ';
} else {
tmp += ' >= ';
}
tmp += connection.escape(options[key]);
} else {
tmp += " AND " + key + " LIKE " + connection.escape('%' +options[key] + '%');
}
}
if (id != undefined) {
sql += ' WHERE id = ' + connection.escape(id) + tmp;
} else {
// replace the first occurence of 'AND' with a 'WHERE'
sql += tmp.replace('AND', 'WHERE');
}
connection.query(sql, function(err, results) {
if (err) throw err;
callback(results);
});
}