-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlpromise.js
97 lines (88 loc) · 2.49 KB
/
sqlpromise.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
var Q = require('q');
var sql = require('mssql');
var readFileWithPromise = Q.denodeify(require('fs').readFile);
//funcion para conseguir config
var getConfig = function()
{
console.log("Reading config file...");
var defer = Q.defer();
readFileWithPromise(__dirname+'/config.json').then (function(config){
config = config.toString('utf8');
config = JSON.parse(config);
defer.resolve(config);
}).fail(function(err){
defer.reject(err);
});
return defer.promise;
};
exports.doQuery = function (query)
{
console.log("doing query...");
var defer = Q.defer();
var sql = require('mssql');
getConfig().then(function(config){
var con = new sql.Connection(config,function conCB(err){
console.log(err);
var req =new sql.Request(con);
req.query(query,function queryCB(err,recordset) {
if (err) {
console.log(err);
defer.reject('Query failed:' + err);
con.close();
return defer.promise;
}
console.dir("Recordset is: ",recordset);
defer.resolve(recordset);
con.close();
});
});
});
return defer.promise;
};
exports.doInputProcedure = function (Option, StoreProcedure)
{
console.log("fetching store procedure...");
var defer = Q.defer();
var sql = require('mssql');
getConfig().then(function(config){
var con = new sql.Connection(config, function conDB(err){
console.log(err);
var req = new sql.Request(con);
req.input(Option.Param, Option.Type, Option.Value);
req.execute('StoreProcedure', function(err, recordsets, returnValue){
if (err) {
console.log(err);
defer.reject('Query failed:' + err);
con.close();
return defer.promise;
}
storeProcedure = JSON.stringify(recordsets);
console.dir("StoreProcedure is: "+storeProcedure);
defer.resolve(storeProcedure);
con.close();
});
})
});
};
exports.doUpdateQuery = function (query)
{
console.log("doing query...");
var defer = Q.defer();
var sql = require('mssql');
getConfig().then(function(config){
var con = new sql.Connection(config,function conCB(err){
console.log(err);
var req =new sql.Request(con);
req.query(query,function queryCB(err,recordset) {
if (err) {
defer.reject('Query failed:' + err);
con.close();
return defer.promise;
}
defer.resolve("success");
con.close();
});
});
});
return defer.promise;
};