-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.js
84 lines (68 loc) · 1.54 KB
/
db.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
'use strict';
var util = require('util');
var async = require('async');
var pool = require('./pool');
var fields = ['SCHEMA_NAME || \'.\' || TABLE_NAME as TABLE'];
var sql = util.format('select top 50 %s from TABLES', fields.join(','));
var finalCb;
var param;
function insp(obj) {
console.log(util.inspect(obj, {
colors : true
}));
}
function connect(cb) {
//client.connect(cb);
pool.acquire(function (err, client) {
if (err) {
// handle error - this is generally the err from your
// factory.create function
console.error('Pool acquire error: ' + err);
cb(err, null);
} else {
cb(null, client);
}
});
}
function executeAndfetchRows(client, cb) {
client.execute(sql, function(err, rows){
pool.release(client);
cb(err, rows);
});
}
function prepare(client, cb) {
client.prepare(sql, function (err, statement) {
cb(err, statement, client);
});
}
function executeAndfetchRowsWithParam(statement, client, cb) {
statement.execute(param, function (err, rows) {
cb(err, client, rows);
});
}
function release(client, rows, cb) {
pool.release(client);
cb(null, rows);
}
function done(err, rows) {
if (err) {
console.error(err);
}
finalCb(err, rows);
}
module.exports = {
execute : function (data) {
sql = data.sql;
finalCb = data.cb;
param = data.param;
console.log(util.inspect(data, {
colors : true
}));
console.log('\n');
if (param) {
async.waterfall([connect, prepare, executeAndfetchRowsWithParam, release], done);
} else {
async.waterfall([connect, executeAndfetchRows], done);
}
}
};