knex/src/dialects/mysql2/transaction.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

import Transaction from '../../transaction';
import inherits from 'inherits';
const debug = require('debug')('knex:tx');
2016-03-02 17:07:05 +01:00
import { assign, isUndefined } from 'lodash';
2016-03-02 17:07:05 +01:00
function Transaction_MySQL2() {
Transaction.apply(this, arguments);
2016-03-02 17:07:05 +01:00
}
inherits(Transaction_MySQL2, Transaction);
2016-03-02 17:07:05 +01:00
assign(Transaction_MySQL2.prototype, {
query(conn, sql, status, value) {
const t = this;
const q = this.trxClient
.query(conn, sql)
.catch(
(err) => err.code === 'ER_SP_DOES_NOT_EXIST',
() => {
this.trxClient.logger.warn(
'Transaction was implicitly committed, do not mix transactions and' +
'DDL with MySQL (#805)'
);
}
)
2016-03-02 17:07:05 +01:00
.catch(function(err) {
status = 2;
value = err;
t._completed = true;
debug('%s error running transaction query', t.txid);
2016-03-02 17:07:05 +01:00
})
.tap(function() {
if (status === 1) t._resolver(value);
if (status === 2) {
if (isUndefined(value)) {
value = new Error(`Transaction rejected with non-error: ${value}`);
}
t._rejecter(value);
}
});
2016-03-02 17:07:05 +01:00
if (status === 1 || status === 2) {
t._completed = true;
2016-03-02 17:07:05 +01:00
}
return q;
},
});
2016-03-02 17:07:05 +01:00
export default Transaction_MySQL2;