2019-06-04 00:37:17 +02:00
|
|
|
const Transaction = require('../../transaction');
|
|
|
|
const Debug = require('debug');
|
2019-07-10 22:38:20 +02:00
|
|
|
const { isUndefined } = require('lodash');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const debug = Debug('knex:tx');
|
|
|
|
|
2018-07-09 15:58:22 -04:00
|
|
|
class Transaction_MySQL extends Transaction {}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-07-10 22:38:20 +02:00
|
|
|
Object.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
|
|
|
})
|
2019-06-17 07:00:31 -04:00
|
|
|
.then(function(res) {
|
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)) {
|
2019-08-17 16:31:03 +05:30
|
|
|
if (t.doNotRejectOnRollback && /^ROLLBACK\b/i.test(sql)) {
|
2019-06-21 12:56:00 +02:00
|
|
|
t._resolver();
|
|
|
|
return;
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
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
|
|
|
}
|
2019-06-17 07:00:31 -04:00
|
|
|
return res;
|
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
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = Transaction_MySQL;
|