This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
forked from bigyunicorn/security_lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
90 lines (57 loc) · 1.92 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
/********************************************************************************/
/* */
/* database.js */
/* */
/* Database query methods */
/* */
/********************************************************************************/
var adb = require("any-db");
var config = require("./config.js");
/********************************************************************************/
/* */
/* Initializations */
/* */
/********************************************************************************/
var pool = adb.createPool(config.DB_CONNECT,{ min : 1, max : 4 });
/********************************************************************************/
/* */
/* Query function */
/* */
/********************************************************************************/
function query(q,prms,next)
{
if (prms instanceof Function) {
next = prms;
prms = undefined;
}
console.log("DATABASE:",q);
q = fixQuery(q);
return pool.query(q,prms,callback(next));
}
function callback(next)
{
return function(err,data) {
if (err) console.log("DATABASE ERROR",err);
else console.log("DATABASE RETURN",data.rows.length);
if (next != null) next(err,data);
}
}
/********************************************************************************/
/* */
/* Handle mysql - postgresql differences on parameters */
/* */
/********************************************************************************/
function fixQuery(q)
{
if (config.DB_CONNECT.substring(0,5) == "mysql") {
q = q.replace(/\$\d+/g,"?");
}
return q;
}
/********************************************************************************/
/* */
/* Exports */
/* */
/********************************************************************************/
exports.query = query;
/* end of database.js */