2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
var _ = require('lodash');
|
|
|
|
var Promise = require('./promise');
|
|
|
|
var assign = require('lodash/object/assign');
|
2015-04-22 10:34:14 -04:00
|
|
|
|
|
|
|
var PassThrough;
|
2014-04-08 16:25:57 -04:00
|
|
|
|
|
|
|
// 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.
|
2015-04-22 10:34:14 -04:00
|
|
|
function Runner(client, builder) {
|
2015-05-09 13:58:18 -04:00
|
|
|
this.client = client;
|
|
|
|
this.builder = builder;
|
|
|
|
this.queries = [];
|
2015-04-22 10:34:14 -04:00
|
|
|
|
|
|
|
// The "connection" object is set on the runner when
|
|
|
|
// "run" is called.
|
2015-05-09 13:58:18 -04:00
|
|
|
this.connection = void 0;
|
2015-04-19 16:31:52 -04:00
|
|
|
}
|
2014-06-22 15:11:01 -04:00
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
assign(Runner.prototype, {
|
|
|
|
|
|
|
|
// "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.
|
2015-05-09 13:58:18 -04:00
|
|
|
run: function run() {
|
|
|
|
var runner = this;
|
2014-05-06 16:18:39 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
return Promise.using(this.ensureConnection(), function (connection) {
|
2015-04-22 10:34:14 -04:00
|
|
|
runner.connection = connection;
|
2014-06-25 02:42:22 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
runner.client.emit('start', runner.builder);
|
|
|
|
runner.builder.emit('start', runner.builder);
|
2015-04-22 10:34:14 -04:00
|
|
|
var sql = runner.builder.toSQL();
|
2015-04-28 19:34:11 -04:00
|
|
|
|
|
|
|
if (runner.builder._debug) {
|
2015-05-09 13:58:18 -04:00
|
|
|
console.log(sql);
|
2015-04-28 19:34:11 -04:00
|
|
|
}
|
2015-04-22 10:34:14 -04:00
|
|
|
|
|
|
|
if (_.isArray(sql)) {
|
|
|
|
return runner.queryArray(sql);
|
|
|
|
}
|
|
|
|
return runner.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`.
|
2015-05-09 13:58:18 -04:00
|
|
|
['catch'](function (err) {
|
2015-04-22 10:34:14 -04:00
|
|
|
if (runner.builder._events && runner.builder._events.error) {
|
|
|
|
runner.builder.emit('error', err);
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
})
|
|
|
|
|
|
|
|
// Fire a single "end" event on the builder when
|
|
|
|
// all queries have successfully completed.
|
2015-05-09 13:58:18 -04:00
|
|
|
.tap(function () {
|
2015-04-22 10:34:14 -04:00
|
|
|
runner.builder.emit('end');
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Stream the result set, by passing through to the dialect's streaming
|
|
|
|
// capabilities. If the options are
|
2015-05-09 13:58:18 -04:00
|
|
|
stream: function stream(options, handler) {
|
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
// If we specify stream(handler).then(...
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
handler = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determines whether we emit an error or throw here.
|
|
|
|
var hasHandler = typeof handler === 'function';
|
|
|
|
|
|
|
|
// Lazy-load the "PassThrough" dependency.
|
|
|
|
PassThrough = PassThrough || require('readable-stream').PassThrough;
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
var runner = this;
|
2015-05-09 13:58:18 -04:00
|
|
|
var stream = new PassThrough({ objectMode: true });
|
|
|
|
var promise = Promise.using(this.ensureConnection(), function (connection) {
|
2015-04-22 10:34:14 -04:00
|
|
|
runner.connection = connection;
|
2015-05-09 13:58:18 -04:00
|
|
|
var sql = runner.builder.toSQL();
|
2014-05-05 19:48:12 -04:00
|
|
|
var err = new Error('The stream may only be used with a single query statement.');
|
|
|
|
if (_.isArray(sql)) {
|
2014-06-25 02:42:22 -04:00
|
|
|
if (hasHandler) throw err;
|
2014-05-05 22:00:29 -04:00
|
|
|
stream.emit('error', err);
|
2014-05-05 19:48:12 -04:00
|
|
|
}
|
|
|
|
return sql;
|
2015-05-09 13:58:18 -04:00
|
|
|
}).then(function (sql) {
|
2015-04-22 10:34:14 -04:00
|
|
|
return runner.client.stream(runner.connection, sql, stream, options);
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
2015-04-22 10:34:14 -04:00
|
|
|
|
|
|
|
// 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.
|
2015-05-09 13:58:18 -04:00
|
|
|
pipe: function pipe(writable, options) {
|
2015-04-28 19:34:11 -04:00
|
|
|
return this.stream().pipe(writable, options);
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// "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.
|
2015-05-09 13:58:18 -04:00
|
|
|
query: Promise.method(function (obj) {
|
|
|
|
this.builder.emit('query', assign({ __knexUid: this.connection.__knexUid }, obj));
|
|
|
|
var runner = this;
|
|
|
|
return this.client.query(this.connection, obj).then(function (resp) {
|
|
|
|
return runner.client.processResponse(resp, runner);
|
|
|
|
});
|
2015-04-22 10:34:14 -04:00
|
|
|
}),
|
|
|
|
|
|
|
|
// In the case of the "schema builder" we call `queryArray`, which runs each
|
|
|
|
// of the queries in sequence.
|
2015-05-09 13:58:18 -04:00
|
|
|
queryArray: function queryArray(queries) {
|
|
|
|
return queries.length === 1 ? this.query(queries[0]) : Promise.bind(this)['return'](queries).reduce(function (memo, query) {
|
|
|
|
return this.query(query).then(function (resp) {
|
|
|
|
memo.push(resp);
|
|
|
|
return memo;
|
|
|
|
});
|
|
|
|
}, []);
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Check whether there's a transaction flag, and that it has a connection.
|
2015-05-09 13:58:18 -04:00
|
|
|
ensureConnection: function ensureConnection() {
|
|
|
|
var runner = this;
|
|
|
|
return Promise['try'](function () {
|
|
|
|
return runner.connection || runner.client.acquireConnection();
|
|
|
|
}).disposer(function () {
|
|
|
|
runner.client.releaseConnection(runner.connection);
|
|
|
|
});
|
2014-05-06 16:18:39 -04:00
|
|
|
}
|
2014-04-15 11:43:47 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
module.exports = Runner;
|