connection.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. 'use strict';
  2. const EventEmitter = require('events').EventEmitter;
  3. const PromisePreparedStatementInfo = require('./prepared_statement_info.js');
  4. const makeDoneCb = require('./make_done_cb.js');
  5. const inheritEvents = require('./inherit_events.js');
  6. const BaseConnection = require('../base/connection.js');
  7. class PromiseConnection extends EventEmitter {
  8. constructor(connection, promiseImpl) {
  9. super();
  10. this.connection = connection;
  11. this.Promise = promiseImpl || Promise;
  12. inheritEvents(connection, this, [
  13. 'error',
  14. 'drain',
  15. 'connect',
  16. 'end',
  17. 'enqueue',
  18. ]);
  19. }
  20. release() {
  21. this.connection.release();
  22. }
  23. query(query, params) {
  24. const c = this.connection;
  25. const localErr = new Error();
  26. if (typeof params === 'function') {
  27. throw new Error(
  28. 'Callback function is not available with promise clients.',
  29. );
  30. }
  31. return new this.Promise((resolve, reject) => {
  32. const done = makeDoneCb(resolve, reject, localErr);
  33. if (params !== undefined) {
  34. c.query(query, params, done);
  35. } else {
  36. c.query(query, done);
  37. }
  38. });
  39. }
  40. execute(query, params) {
  41. const c = this.connection;
  42. const localErr = new Error();
  43. if (typeof params === 'function') {
  44. throw new Error(
  45. 'Callback function is not available with promise clients.',
  46. );
  47. }
  48. return new this.Promise((resolve, reject) => {
  49. const done = makeDoneCb(resolve, reject, localErr);
  50. if (params !== undefined) {
  51. c.execute(query, params, done);
  52. } else {
  53. c.execute(query, done);
  54. }
  55. });
  56. }
  57. end() {
  58. return new this.Promise((resolve) => {
  59. this.connection.end(resolve);
  60. });
  61. }
  62. beginTransaction() {
  63. const c = this.connection;
  64. const localErr = new Error();
  65. return new this.Promise((resolve, reject) => {
  66. const done = makeDoneCb(resolve, reject, localErr);
  67. c.beginTransaction(done);
  68. });
  69. }
  70. commit() {
  71. const c = this.connection;
  72. const localErr = new Error();
  73. return new this.Promise((resolve, reject) => {
  74. const done = makeDoneCb(resolve, reject, localErr);
  75. c.commit(done);
  76. });
  77. }
  78. rollback() {
  79. const c = this.connection;
  80. const localErr = new Error();
  81. return new this.Promise((resolve, reject) => {
  82. const done = makeDoneCb(resolve, reject, localErr);
  83. c.rollback(done);
  84. });
  85. }
  86. ping() {
  87. const c = this.connection;
  88. const localErr = new Error();
  89. return new this.Promise((resolve, reject) => {
  90. c.ping((err) => {
  91. if (err) {
  92. localErr.message = err.message;
  93. localErr.code = err.code;
  94. localErr.errno = err.errno;
  95. localErr.sqlState = err.sqlState;
  96. localErr.sqlMessage = err.sqlMessage;
  97. reject(localErr);
  98. } else {
  99. resolve(true);
  100. }
  101. });
  102. });
  103. }
  104. connect() {
  105. const c = this.connection;
  106. const localErr = new Error();
  107. return new this.Promise((resolve, reject) => {
  108. c.connect((err, param) => {
  109. if (err) {
  110. localErr.message = err.message;
  111. localErr.code = err.code;
  112. localErr.errno = err.errno;
  113. localErr.sqlState = err.sqlState;
  114. localErr.sqlMessage = err.sqlMessage;
  115. reject(localErr);
  116. } else {
  117. resolve(param);
  118. }
  119. });
  120. });
  121. }
  122. prepare(options) {
  123. const c = this.connection;
  124. const promiseImpl = this.Promise;
  125. const localErr = new Error();
  126. return new this.Promise((resolve, reject) => {
  127. c.prepare(options, (err, statement) => {
  128. if (err) {
  129. localErr.message = err.message;
  130. localErr.code = err.code;
  131. localErr.errno = err.errno;
  132. localErr.sqlState = err.sqlState;
  133. localErr.sqlMessage = err.sqlMessage;
  134. reject(localErr);
  135. } else {
  136. const wrappedStatement = new PromisePreparedStatementInfo(
  137. statement,
  138. promiseImpl,
  139. );
  140. resolve(wrappedStatement);
  141. }
  142. });
  143. });
  144. }
  145. changeUser(options) {
  146. const c = this.connection;
  147. const localErr = new Error();
  148. return new this.Promise((resolve, reject) => {
  149. c.changeUser(options, (err) => {
  150. if (err) {
  151. localErr.message = err.message;
  152. localErr.code = err.code;
  153. localErr.errno = err.errno;
  154. localErr.sqlState = err.sqlState;
  155. localErr.sqlMessage = err.sqlMessage;
  156. reject(localErr);
  157. } else {
  158. resolve();
  159. }
  160. });
  161. });
  162. }
  163. get config() {
  164. return this.connection.config;
  165. }
  166. get threadId() {
  167. return this.connection.threadId;
  168. }
  169. }
  170. // patching PromiseConnection
  171. // create facade functions for prototype functions on "Connection" that are not yet
  172. // implemented with PromiseConnection
  173. // proxy synchronous functions only
  174. (function (functionsToWrap) {
  175. for (let i = 0; functionsToWrap && i < functionsToWrap.length; i++) {
  176. const func = functionsToWrap[i];
  177. if (
  178. typeof BaseConnection.prototype[func] === 'function' &&
  179. PromiseConnection.prototype[func] === undefined
  180. ) {
  181. PromiseConnection.prototype[func] = (function factory(funcName) {
  182. return function () {
  183. return BaseConnection.prototype[funcName].apply(
  184. this.connection,
  185. arguments,
  186. );
  187. };
  188. })(func);
  189. }
  190. }
  191. })([
  192. // synchronous functions
  193. 'close',
  194. 'createBinlogStream',
  195. 'destroy',
  196. 'escape',
  197. 'escapeId',
  198. 'format',
  199. 'pause',
  200. 'pipe',
  201. 'resume',
  202. 'unprepare',
  203. ]);
  204. module.exports = PromiseConnection;