connection-callback.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 'use strict';
  2. const Connection = require('./connection');
  3. const util = require('util');
  4. const Errors = require('./misc/errors');
  5. const { Status } = require('./const/connection_status');
  6. function ConnectionCallback(options) {
  7. Connection.call(this, options);
  8. let connecting = 1;
  9. const connectPromise = this.connect.bind(this);
  10. const changeUserPromise = this.changeUser.bind(this);
  11. const queryPromise = this.query.bind(this);
  12. const endPromise = this.end.bind(this);
  13. const pingPromise = this.ping.bind(this);
  14. const resetPromise = this.reset.bind(this);
  15. const commitPromise = this.commit.bind(this);
  16. const rollbackPromise = this.rollback.bind(this);
  17. const emptySuccess = (rows) => {};
  18. const emptyError = (err) => {};
  19. //*****************************************************************
  20. // internal equivalent with callback of promised functions
  21. //*****************************************************************
  22. const _commitCallback = (callback) => {
  23. commitPromise()
  24. .then(() => {
  25. if (callback) callback(null, null, null);
  26. })
  27. .catch(callback || emptyError);
  28. };
  29. const _rollbackCallback = (callback) => {
  30. rollbackPromise()
  31. .then(() => {
  32. if (callback) callback(null, null, null);
  33. })
  34. .catch(callback || emptyError);
  35. };
  36. const _pingCallback = (timeout, callback) => {
  37. let _timeout, _cb;
  38. if (typeof timeout === 'function') {
  39. _cb = timeout;
  40. _timeout = undefined;
  41. } else {
  42. _timeout = timeout;
  43. _cb = callback;
  44. }
  45. pingPromise(_timeout)
  46. .then(_cb || emptySuccess)
  47. .catch(_cb || emptyError);
  48. };
  49. const _resetCallback = (callback) => {
  50. resetPromise()
  51. .then(callback || emptySuccess)
  52. .catch(callback || emptyError);
  53. };
  54. const _beginTransactionCallback = (callback) => {
  55. queryPromise('START TRANSACTION')
  56. .then(() => {
  57. if (callback) callback(null, null, null);
  58. })
  59. .catch(callback || emptyError);
  60. };
  61. const _endCallback = (callback) => {
  62. endPromise()
  63. .then(callback || emptySuccess)
  64. .catch(callback || emptyError);
  65. };
  66. const _connectCallback = function (callback) {
  67. if (!callback) {
  68. throw new Errors.createError(
  69. 'missing callback parameter',
  70. false,
  71. this.info,
  72. 'HY000',
  73. Errors.ER_MISSING_PARAMETER
  74. );
  75. }
  76. if (connecting === 1) {
  77. this.on('connect', callback);
  78. } else {
  79. switch (this._status()) {
  80. case Status.CLOSING:
  81. case Status.CLOSED:
  82. callback(
  83. Errors.createError(
  84. 'Connection closed',
  85. true,
  86. this.info,
  87. '08S01',
  88. Errors.ER_CONNECTION_ALREADY_CLOSED
  89. )
  90. );
  91. break;
  92. default:
  93. callback();
  94. }
  95. }
  96. };
  97. const _changeUserCallback = (options, callback) => {
  98. let _options, _cb;
  99. if (typeof options === 'function') {
  100. _cb = options;
  101. _options = undefined;
  102. } else {
  103. _options = options;
  104. _cb = callback;
  105. }
  106. changeUserPromise(_options)
  107. .then(() => {
  108. if (_cb) _cb(null, null, null);
  109. })
  110. .catch(_cb || emptyError);
  111. };
  112. //*****************************************************************
  113. // replacing public promise function with callback equivalent
  114. //*****************************************************************
  115. this.commit = _commitCallback;
  116. this.rollback = _rollbackCallback;
  117. this.ping = _pingCallback;
  118. this.reset = _resetCallback;
  119. this.end = _endCallback;
  120. this.connect = _connectCallback;
  121. this.changeUser = _changeUserCallback;
  122. this.query = this._queryCallback;
  123. this.batch = this._batchCallback;
  124. this.beginTransaction = _beginTransactionCallback;
  125. const self = this;
  126. connectPromise()
  127. .then(() => {
  128. connecting = 0;
  129. self.emit('connect');
  130. })
  131. .catch((err) => {
  132. connecting = 0;
  133. self.emit('connect', err);
  134. });
  135. }
  136. util.inherits(ConnectionCallback, Connection);
  137. module.exports = ConnectionCallback;