123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- 'use strict';
- const EventEmitter = require('events').EventEmitter;
- const PromisePreparedStatementInfo = require('./prepared_statement_info.js');
- const makeDoneCb = require('./make_done_cb.js');
- const inheritEvents = require('./inherit_events.js');
- const BaseConnection = require('../base/connection.js');
- class PromiseConnection extends EventEmitter {
- constructor(connection, promiseImpl) {
- super();
- this.connection = connection;
- this.Promise = promiseImpl || Promise;
- inheritEvents(connection, this, [
- 'error',
- 'drain',
- 'connect',
- 'end',
- 'enqueue',
- ]);
- }
- release() {
- this.connection.release();
- }
- query(query, params) {
- const c = this.connection;
- const localErr = new Error();
- if (typeof params === 'function') {
- throw new Error(
- 'Callback function is not available with promise clients.',
- );
- }
- return new this.Promise((resolve, reject) => {
- const done = makeDoneCb(resolve, reject, localErr);
- if (params !== undefined) {
- c.query(query, params, done);
- } else {
- c.query(query, done);
- }
- });
- }
- execute(query, params) {
- const c = this.connection;
- const localErr = new Error();
- if (typeof params === 'function') {
- throw new Error(
- 'Callback function is not available with promise clients.',
- );
- }
- return new this.Promise((resolve, reject) => {
- const done = makeDoneCb(resolve, reject, localErr);
- if (params !== undefined) {
- c.execute(query, params, done);
- } else {
- c.execute(query, done);
- }
- });
- }
- end() {
- return new this.Promise((resolve) => {
- this.connection.end(resolve);
- });
- }
- beginTransaction() {
- const c = this.connection;
- const localErr = new Error();
- return new this.Promise((resolve, reject) => {
- const done = makeDoneCb(resolve, reject, localErr);
- c.beginTransaction(done);
- });
- }
- commit() {
- const c = this.connection;
- const localErr = new Error();
- return new this.Promise((resolve, reject) => {
- const done = makeDoneCb(resolve, reject, localErr);
- c.commit(done);
- });
- }
- rollback() {
- const c = this.connection;
- const localErr = new Error();
- return new this.Promise((resolve, reject) => {
- const done = makeDoneCb(resolve, reject, localErr);
- c.rollback(done);
- });
- }
- ping() {
- const c = this.connection;
- const localErr = new Error();
- return new this.Promise((resolve, reject) => {
- c.ping((err) => {
- if (err) {
- localErr.message = err.message;
- localErr.code = err.code;
- localErr.errno = err.errno;
- localErr.sqlState = err.sqlState;
- localErr.sqlMessage = err.sqlMessage;
- reject(localErr);
- } else {
- resolve(true);
- }
- });
- });
- }
- connect() {
- const c = this.connection;
- const localErr = new Error();
- return new this.Promise((resolve, reject) => {
- c.connect((err, param) => {
- if (err) {
- localErr.message = err.message;
- localErr.code = err.code;
- localErr.errno = err.errno;
- localErr.sqlState = err.sqlState;
- localErr.sqlMessage = err.sqlMessage;
- reject(localErr);
- } else {
- resolve(param);
- }
- });
- });
- }
- prepare(options) {
- const c = this.connection;
- const promiseImpl = this.Promise;
- const localErr = new Error();
- return new this.Promise((resolve, reject) => {
- c.prepare(options, (err, statement) => {
- if (err) {
- localErr.message = err.message;
- localErr.code = err.code;
- localErr.errno = err.errno;
- localErr.sqlState = err.sqlState;
- localErr.sqlMessage = err.sqlMessage;
- reject(localErr);
- } else {
- const wrappedStatement = new PromisePreparedStatementInfo(
- statement,
- promiseImpl,
- );
- resolve(wrappedStatement);
- }
- });
- });
- }
- changeUser(options) {
- const c = this.connection;
- const localErr = new Error();
- return new this.Promise((resolve, reject) => {
- c.changeUser(options, (err) => {
- if (err) {
- localErr.message = err.message;
- localErr.code = err.code;
- localErr.errno = err.errno;
- localErr.sqlState = err.sqlState;
- localErr.sqlMessage = err.sqlMessage;
- reject(localErr);
- } else {
- resolve();
- }
- });
- });
- }
- get config() {
- return this.connection.config;
- }
- get threadId() {
- return this.connection.threadId;
- }
- }
- // patching PromiseConnection
- // create facade functions for prototype functions on "Connection" that are not yet
- // implemented with PromiseConnection
- // proxy synchronous functions only
- (function (functionsToWrap) {
- for (let i = 0; functionsToWrap && i < functionsToWrap.length; i++) {
- const func = functionsToWrap[i];
- if (
- typeof BaseConnection.prototype[func] === 'function' &&
- PromiseConnection.prototype[func] === undefined
- ) {
- PromiseConnection.prototype[func] = (function factory(funcName) {
- return function () {
- return BaseConnection.prototype[funcName].apply(
- this.connection,
- arguments,
- );
- };
- })(func);
- }
- }
- })([
- // synchronous functions
- 'close',
- 'createBinlogStream',
- 'destroy',
- 'escape',
- 'escapeId',
- 'format',
- 'pause',
- 'pipe',
- 'resume',
- 'unprepare',
- ]);
- module.exports = PromiseConnection;
|