knex/lib/dialects/mysql/transaction.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict';
2015-05-09 13:58:18 -04:00
var Transaction = require('../../transaction');
var assign = require('lodash/object/assign');
var inherits = require('inherits');
var debug = require('debug')('knex:tx');
var helpers = require('../../helpers');
function Transaction_MySQL() {
2015-05-09 13:58:18 -04:00
Transaction.apply(this, arguments);
}
2015-05-09 13:58:18 -04:00
inherits(Transaction_MySQL, Transaction);
assign(Transaction_MySQL.prototype, {
2015-05-09 13:58:18 -04:00
query: function query(conn, sql, status, value) {
var t = this;
var q = this.trxClient.query(conn, sql)['catch'](function (err) {
return err.errno === 1305;
}, function () {
helpers.warn('Transaction was implicitly committed, do not mix transactions and DDL with MySQL (#805)');
})['catch'](function (err) {
status = 2;
value = err;
t._completed = true;
debug('%s error running transaction query', t.txid);
}).tap(function () {
if (status === 1) t._resolver(value);
if (status === 2) t._rejecter(value);
});
if (status === 1 || status === 2) {
2015-05-09 13:58:18 -04:00
t._completed = true;
}
return q;
}
2015-05-09 13:58:18 -04:00
});
2015-05-09 13:58:18 -04:00
module.exports = Transaction_MySQL;