forked from SohumB/knex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.js
276 lines (244 loc) · 9.21 KB
/
runner.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var _ = require('lodash');
var Promise = require('./promise');
var uuid = require('node-uuid');
// The "Runner" constructor takes a "builder" (query, schema, or raw)
// and runs through each of the query statements, calling any additional
// "output" method provided alongside the query and bindings.
function Runner(builder) {
this.builder = builder;
this.queries = [];
this.assumeGlobalTransaction = builder.client.pool.config.assumeGlobalTransaction;
this.transactionIsolationLevel = builder.transactionIsolationLevel || builder.client.connectionSettings.transactionIsolationLevel;
// The "connection" object is set on the runner when
// "run" is called.
this.connection = void 0;
}
Runner.prototype._beginTransaction = 'begin;';
Runner.prototype._commitTransaction = 'commit;';
Runner.prototype._rollbackTransaction = 'rollback;';
Runner.prototype._transactionIsolationLevels = {
"READ_UNCOMMITTED": "read uncommitted",
"READ_COMMITTED": "read committed",
"REPEATABLE_READ": "repeatable read",
"SERIALIZABLE": "serializable"
};
Runner.prototype._transactionIsolationLevelSQL = "SET TRANSACTION ISOLATION LEVEL ";
Runner.prototype._beginSavepoint = function(name) {
return 'savepoint ' + name + ';';
};
Runner.prototype._releaseSavepoint = function(name) {
return 'release savepoint ' + name + ';';
};
Runner.prototype._rollbackSavepoint = function(name) {
return 'rollback to savepoint ' + name + ';';
};
// "Run" the target, calling "toSQL" on the builder, returning
// an object or array of queries to run, each of which are run on
// a single connection.
Runner.prototype.run = Promise.method(function() {
if (this.builder._transacting) {
return this.transactionQuery();
}
return Promise.bind(this)
.then(this.ensureConnection)
.then(function(connection) {
this.connection = connection;
// Emit a "start" event on both the builder and the client,
// allowing us to listen in on any events. We fire on the "client"
// before building the SQL, and on the builder after building the SQL
// in case we want to determine at how long it actually
// took to build the query.
this.client.emit('start', this.builder);
var sql = this.builder.toSQL();
this.builder.emit('start', this.builder);
if (_.isArray(sql)) {
return this.queryArray(sql);
}
return this.query(sql);
})
// If there are any "error" listeners, we fire an error event
// and then re-throw the error to be eventually handled by
// the promise chain. Useful if you're wrapping in a custom `Promise`.
.catch(function(err) {
if (this.builder._events && this.builder._events.error) {
this.builder.emit('error', err);
}
throw err;
})
// Fire a single "end" event on the builder when
// all queries have successfully completed.
.tap(function() {
this.builder.emit('end');
})
.finally(this.cleanupConnection);
});
// Stream the result set, by passing through to the dialect's streaming
// capabilities. If the options are
var PassThrough;
Runner.prototype.stream = function(options, handler) {
// If we specify stream(handler).then(...
if (arguments.length === 1) {
if (_.isFunction(options)) {
handler = options;
options = {};
}
}
// Determines whether we emit an error or throw here.
var hasHandler = _.isFunction(handler);
// Lazy-load the "PassThrough" dependency.
PassThrough = PassThrough || require('readable-stream').PassThrough;
var stream = new PassThrough({objectMode: true});
var promise = Promise.bind(this)
.then(this.ensureConnection)
.then(function(connection) {
this.connection = connection;
var sql = this.builder.toSQL();
var err = new Error('The stream may only be used with a single query statement.');
if (_.isArray(sql)) {
if (hasHandler) throw err;
stream.emit('error', err);
}
return sql;
}).then(function(sql) {
return this._stream(sql, stream, options);
}).finally(this.cleanupConnection);
// If a function is passed to handle the stream, send the stream
// there and return the promise, otherwise just return the stream
// and the promise will take care of itsself.
if (hasHandler) {
handler(stream);
return promise;
}
return stream;
};
// Allow you to pipe the stream to a writable stream.
Runner.prototype.pipe = function(writable) {
return this.stream().pipe(writable);
};
// "Runs" a query, returning a promise. All queries specified by the builder are guaranteed
// to run in sequence, and on the same connection, especially helpful when schema building
// and dealing with foreign key constraints, etc.
Runner.prototype.query = Promise.method(function(obj) {
obj.__cid = this.connection.__cid;
this.builder.emit('query', obj);
this.client.emit('query', obj);
return this._query(obj).bind(this).then(this.processResponse);
});
// In the case of the "schema builder" we call `queryArray`, which runs each
// of the queries in sequence.
Runner.prototype.queryArray = Promise.method(function(queries) {
return queries.length === 1 ? this.query(queries[0]) : Promise.bind(this)
.thenReturn(queries)
.reduce(function(memo, query) {
return this.query(query).then(function(resp) {
memo.push(resp);
return memo;
});
}, []);
});
// Check whether there's a transaction flag, and that it has a connection.
Runner.prototype.ensureConnection = Promise.method(function() {
if (this.builder._connection) {
return this.builder._connection;
}
return this.client.acquireConnection();
});
// "Debug" the query being run.
Runner.prototype.debug = function(obj) {
console.dir(_.extend({__cid: this.connection.__cid}, obj));
};
// Check whether we're "debugging", based on either calling `debug` on the query.
Runner.prototype.isDebugging = function() {
return (this.client.isDebugging === true || this.builder._debug === true);
};
// Transaction Methods:
// -------
// Run the transaction on the correct "runner" instance.
Runner.prototype.transactionQuery = Promise.method(function() {
var runner = this.builder._transacting._runner;
if (!(runner instanceof Runner)) {
throw new Error('Invalid transaction object provided.');
}
var sql = this.builder.toSQL();
if (_.isArray(sql)) {
return runner.queryArray(sql);
}
return runner.query(sql);
});
// Begins a transaction statement on the instance,
// resolving with the connection of the current transaction.
Runner.prototype.startTransaction = Promise.method(function() {
return Promise.bind(this)
.then(this.ensureConnection)
.then(function(connection) {
this.connection = connection;
this.transaction = true;
var command = this._beginTransaction;
if (this.transactionIsolationLevel && !this.assumeGlobalTransaction) {
command += this._transactionIsolationLevelSQL + this._transactionIsolationLevels[this.transactionIsolationLevel] + ";";
}
if (this.assumeGlobalTransaction) {
this.savepointName = 'svp' + uuid.v1().replace(/[^a-z0-9]/g,'');
command = this._beginSavepoint(this.savepointName);
}
return this.query({sql: command});
}).thenReturn(this);
});
// Finishes the transaction statement and handles disposing of the connection,
// resolving / rejecting the transaction's promise, and ensuring the transaction object's
// `_runner` property is `null`'ed out so it cannot continue to be used.
Runner.prototype.finishTransaction = Promise.method(function(action, containerObject, msg) {
var query, dfd = containerObject.__dfd__;
// Run the query to commit / rollback the transaction.
switch (action) {
case 0:
query = this.commitTransaction();
break;
case 1:
query = this.rollbackTransaction();
break;
}
return query.then(function(resp) {
msg = (msg === void 0) ? resp : msg;
switch (action) {
case 0:
dfd.fulfill(msg);
break;
case 1:
dfd.reject(msg);
break;
}
// If there was a problem committing the transaction,
// reject the transaction block (to reject the entire transaction block),
// then re-throw the error for any promises chained off the commit.
}).catch(function(e) {
dfd.reject(e);
throw e;
}).bind(this).finally(function() {
// Kill the "_runner" object on the containerObject,
// so it's not possible to continue using the transaction object.
containerObject._runner = void 0;
return this.cleanupConnection();
});
});
Runner.prototype.commitTransaction = function() {
var command = this.assumeGlobalTransaction ?
this._releaseSavepoint(this.savepointName) :
this._commitTransaction;
return this.query({sql: command});
};
Runner.prototype.rollbackTransaction = function() {
var command = this.assumeGlobalTransaction ?
this._rollbackSavepoint(this.savepointName) :
this._rollbackTransaction;
return this.query({sql: command});
};
// Cleanup the connection as necessary, if the `_connection` was
// explicitly set on the query we don't need to do anything here,
// otherwise we
Runner.prototype.cleanupConnection = Promise.method(function() {
if (!this.builder._connection) {
return this.client.releaseConnection(this.connection);
}
});
module.exports = Runner;