2016-05-17 01:01:34 +10:00
|
|
|
import Transaction from '../../transaction';
|
|
|
|
import inherits from 'inherits';
|
|
|
|
import Debug from 'debug';
|
2018-07-09 08:10:34 -04:00
|
|
|
import { assign, isUndefined } from 'lodash';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const debug = Debug('knex:tx');
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
function Transaction_MySQL() {
|
2018-07-09 08:10:34 -04:00
|
|
|
Transaction.apply(this, arguments);
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
inherits(Transaction_MySQL, Transaction);
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
assign(Transaction_MySQL.prototype, {
|
2016-05-17 01:01:34 +10:00
|
|
|
query(conn, sql, status, value) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const t = this;
|
|
|
|
const q = this.trxClient
|
|
|
|
.query(conn, sql)
|
|
|
|
.catch(
|
|
|
|
(err) => err.errno === 1305,
|
|
|
|
() => {
|
|
|
|
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) {
|
2018-07-09 08:10:34 -04:00
|
|
|
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() {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (status === 1) t._resolver(value);
|
2017-03-26 17:43:17 +02:00
|
|
|
if (status === 2) {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (isUndefined(value)) {
|
|
|
|
value = new Error(`Transaction rejected with non-error: ${value}`);
|
2017-03-26 17:43:17 +02:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
t._rejecter(value);
|
2017-03-26 17:43:17 +02:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
if (status === 1 || status === 2) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t._completed = true;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
return q;
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
export default Transaction_MySQL;
|