-
Notifications
You must be signed in to change notification settings - Fork 31
/
mssqlDriver.js
132 lines (106 loc) · 3.51 KB
/
mssqlDriver.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
var promisify = require('./promisify');
var optionalRequire = require("./optionalRequire");
var debug = require('debug')('sworm:mssql');
module.exports = function() {
var sql = optionalRequire("mssql");
return {
query: function(query, params, options) {
var request = new sql.Request(this.transaction || this.connection);
if (params) {
Object.keys(params).forEach(function (key) {
request.input(key, params[key]);
});
}
debug(query, params);
return request.query(query).then(function (result) {
if (options.statement || options.insert) {
var r = {}
if (options.statement) {
r.changes = result.rowsAffected[0]
}
if (options.insert) {
r.id = params.hasOwnProperty(options.id) ? params[options.id] : result.recordset[0][options.id]
}
return r
} else {
return result.recordset
}
})
},
connect: function(config) {
var self = this;
self.connection = new sql.ConnectionPool(config.config || config.url);
return promisify(function(cb) {
return self.connection.connect(cb);
});
},
begin: function() {
this.transaction = this.connection.transaction();
debug('begin transaction');
return this.transaction.begin();
},
commit: function() {
var self = this;
debug('commit');
return this.transaction.commit().then(function() {
self.transaction = undefined;
});
},
rollback: function() {
var self = this;
debug('rollback');
return this.transaction.rollback().then(function() {
self.transaction = undefined;
});
},
insertEmpty: function(meta) {
return findInsert(meta).insertEmpty(meta)
},
insertStatement: function(meta, fields, values) {
return findInsert(meta).insertStatement(meta, fields, values)
},
insert: function(query, params, options) {
return this.query(query, params, options)
},
close: function() {
this.connection.close();
return Promise.resolve();
}
};
};
function findInsert(meta) {
var generatedId = (meta.hasOwnProperty('generatedId')? meta.generatedId: undefined) || 'scope_identity';
if (meta.compoundKey) {
return insertNoIdentity
} else if (generatedId == 'scope_identity') {
return insertScopeIdentity
} else if (generatedId == 'output') {
return insertOutputIdentity
} else {
throw new Error('expected generatedId to be either scope_identity or output, found: ' + generatedId)
}
}
var insertScopeIdentity = {
insertEmpty: function(meta) {
return 'insert into ' + meta.table + ' default values; select scope_identity() as ' + meta.id;
},
insertStatement: function(meta, fields, values) {
return 'insert into ' + meta.table + ' (' + fields + ') values (' + values + '); select scope_identity() as ' + meta.id;
}
}
var insertNoIdentity = {
insertEmpty: function(meta) {
return 'insert into ' + meta.table + ' default values'
},
insertStatement: function(meta, fields, values) {
return 'insert into ' + meta.table + ' (' + fields + ') values (' + values + ')';
}
}
var insertOutputIdentity = {
insertEmpty: function(meta) {
return 'insert into ' + meta.table + ' output inserted.' + meta.id + ' default values'
},
insertStatement: function(meta, fields, values) {
return 'insert into ' + meta.table + ' (' + fields + ') output inserted.' + meta.id + ' values (' + values + ')';
}
}