123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 'use strict';
- const Connection = require('./connection');
- const util = require('util');
- const Errors = require('./misc/errors');
- const { Status } = require('./const/connection_status');
- function ConnectionCallback(options) {
- Connection.call(this, options);
- let connecting = 1;
- const connectPromise = this.connect.bind(this);
- const changeUserPromise = this.changeUser.bind(this);
- const queryPromise = this.query.bind(this);
- const endPromise = this.end.bind(this);
- const pingPromise = this.ping.bind(this);
- const resetPromise = this.reset.bind(this);
- const commitPromise = this.commit.bind(this);
- const rollbackPromise = this.rollback.bind(this);
- const emptySuccess = (rows) => {};
- const emptyError = (err) => {};
- //*****************************************************************
- // internal equivalent with callback of promised functions
- //*****************************************************************
- const _commitCallback = (callback) => {
- commitPromise()
- .then(() => {
- if (callback) callback(null, null, null);
- })
- .catch(callback || emptyError);
- };
- const _rollbackCallback = (callback) => {
- rollbackPromise()
- .then(() => {
- if (callback) callback(null, null, null);
- })
- .catch(callback || emptyError);
- };
- const _pingCallback = (timeout, callback) => {
- let _timeout, _cb;
- if (typeof timeout === 'function') {
- _cb = timeout;
- _timeout = undefined;
- } else {
- _timeout = timeout;
- _cb = callback;
- }
- pingPromise(_timeout)
- .then(_cb || emptySuccess)
- .catch(_cb || emptyError);
- };
- const _resetCallback = (callback) => {
- resetPromise()
- .then(callback || emptySuccess)
- .catch(callback || emptyError);
- };
- const _beginTransactionCallback = (callback) => {
- queryPromise('START TRANSACTION')
- .then(() => {
- if (callback) callback(null, null, null);
- })
- .catch(callback || emptyError);
- };
- const _endCallback = (callback) => {
- endPromise()
- .then(callback || emptySuccess)
- .catch(callback || emptyError);
- };
- const _connectCallback = function (callback) {
- if (!callback) {
- throw new Errors.createError(
- 'missing callback parameter',
- false,
- this.info,
- 'HY000',
- Errors.ER_MISSING_PARAMETER
- );
- }
- if (connecting === 1) {
- this.on('connect', callback);
- } else {
- switch (this._status()) {
- case Status.CLOSING:
- case Status.CLOSED:
- callback(
- Errors.createError(
- 'Connection closed',
- true,
- this.info,
- '08S01',
- Errors.ER_CONNECTION_ALREADY_CLOSED
- )
- );
- break;
- default:
- callback();
- }
- }
- };
- const _changeUserCallback = (options, callback) => {
- let _options, _cb;
- if (typeof options === 'function') {
- _cb = options;
- _options = undefined;
- } else {
- _options = options;
- _cb = callback;
- }
- changeUserPromise(_options)
- .then(() => {
- if (_cb) _cb(null, null, null);
- })
- .catch(_cb || emptyError);
- };
- //*****************************************************************
- // replacing public promise function with callback equivalent
- //*****************************************************************
- this.commit = _commitCallback;
- this.rollback = _rollbackCallback;
- this.ping = _pingCallback;
- this.reset = _resetCallback;
- this.end = _endCallback;
- this.connect = _connectCallback;
- this.changeUser = _changeUserCallback;
- this.query = this._queryCallback;
- this.batch = this._batchCallback;
- this.beginTransaction = _beginTransactionCallback;
- const self = this;
- connectPromise()
- .then(() => {
- connecting = 0;
- self.emit('connect');
- })
- .catch((err) => {
- connecting = 0;
- self.emit('connect', err);
- });
- }
- util.inherits(ConnectionCallback, Connection);
- module.exports = ConnectionCallback;
|