forked from featurist/sworm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracleDriver.js
205 lines (165 loc) · 5.1 KB
/
oracleDriver.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var optionalRequire = require('./optionalRequire');
var promisify = require('./promisify');
var debug = require('debug')('sworm:oracle');
var swormDebug = require('debug')('sworm');
var _ = require('underscore');
var urlUtils = require('url');
var redactConfig = require('./redactConfig');
var outstandingQueries = require('./outstandingQueries');
var randomstring = require('randomstring');
module.exports = function () {
var oracledb = optionalRequire('oracledb');
return {
outstandingQueries: outstandingQueries(),
query: function (query, params, options) {
var results = this.execute(replaceParameters(query), params, _.extend({outFormat: oracledb.ARRAY}, options));
if (options && (options.statement || options.insert || options.formatRows == false)) {
return results;
} else {
return results.then(function (r) {
return formatRows(r);
});
}
},
execute: function (query, params, options) {
var self = this;
debug(query, params);
return this.outstandingQueries.execute(promisify(function (cb) {
self.connection.execute(query, params || {}, options, cb)
}));
},
insert: function(query, params, options) {
var id = options.id;
return this.query(query + ' returning ' + id + ' into :returning_into_id', params, options).then(function (rows) {
return rows.outBinds.returning_into_id[0];
});
},
generateTransactionName: function() {
return 't' + randomstring.generate();
},
begin: function(options) {
var transactionName = options && options.name || this.generateTransactionName();
return this.query("set transaction name '" + transactionName + "'", undefined, {statement: true});
},
connect: function (swormConfig) {
var self = this;
var config = swormConfig.url? parseUrl(swormConfig.url): swormConfig.config;
oracledb.autoCommit = true;
if (config.options) {
Object.keys(config.options).forEach(function (key) {
oracledb[key] = config.options[key];
});
}
function makeConnection() {
if (config.pool === true) {
return connectionPool(oracledb, config, swormConfig).then(function (pool) {
return promisify(function (cb) { pool.getConnection(cb); });
});
} else if (config.pool) {
return promisify(function (cb) { config.pool.getConnection(cb); });
} else {
return promisify(function (cb) { oracledb.getConnection(config, cb); });
}
}
return makeConnection().then(function (connection) {
self.connection = connection;
});
},
close: function () {
var self = this;
if (self.connection) {
return this.outstandingQueries.whenNotExecuting(function () {
return promisify(function (cb) {
self.connection.release(cb);
});
});
} else {
return Promise.resolve();
}
},
insertEmpty: function(table, id) {
return 'insert into ' + table + ' (' + id + ') values (default)';
},
outputIdKeys: function (idType) {
return {
returning_into_id: { type: idType || oracledb.NUMBER, dir: oracledb.BIND_OUT }
};
}
};
};
function formatRows(resultSet) {
var rows = resultSet.rows;
if (!rows) {
return rows;
}
var fields = resultSet.metaData.map(function (field) {
if (/[a-z]/.test(field.name)) {
return field.name;
} else {
return field.name.toLowerCase();
}
});
if (fields.length > 0) {
var length = rows.length;
var results = new Array(length);
for (var r = 0; r < length; r++) {
var row = {};
results[r] = row;
for (var f = 0; f < fields.length; f++) {
row[fields[f]] = rows[r][f];
}
}
return results;
} else {
return rows;
}
}
function replaceParameters(query) {
return query.replace(/@([a-z_0-9]+)\b/gi, function (_, paramName) {
return ':' + paramName;
});
}
function parseValue(value) {
var number = Number(value);
if (!isNaN(number)) {
return number;
}
if (value == 'true' || value == 'false') {
return value == 'true';
}
return value;
}
function parseOptions(options) {
var result = {};
Object.keys(options).forEach(function (key) {
result[key] = parseValue(options[key]);
});
return result;
}
function parseUrl(url) {
var u = urlUtils.parse(url, true);
var auth = u.auth? u.auth.split(':'): [];
var options = parseOptions(u.query);
var pool = options.pool;
delete options.pool;
return {
user: auth[0],
password: auth[1],
connectString: u.host + u.pathname,
pool: pool,
options: options
};
}
var connectionPoolCache = {};
module.exports.connectionPoolCache = connectionPoolCache;
function connectionPool(oracledb, config, swormConfig) {
var key = JSON.stringify(config);
var value = connectionPoolCache[key];
if (!value) {
value = connectionPoolCache[key] = promisify(function (cb) {
swormDebug('creating connection pool', redactConfig(swormConfig));
oracledb.createPool(config, cb);
});
}
return value;
}