Skip to content

Commit 9995606

Browse files
authored
Merge pull request #531 from nros/issue-495_promise_connection_lacks_functions
fix: PromiseConnection missing interface functions from Connection
2 parents 69317cf + a5ef45e commit 9995606

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

promise.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,104 @@ PromiseConnection.prototype.end = function () {
6868
});
6969
};
7070

71+
PromiseConnection.prototype.beginTransaction = function () {
72+
var c = this.connection;
73+
return new this.Promise(function (resolve, reject) {
74+
var done = makeDoneCb(resolve, reject);
75+
c.beginTransaction(done);
76+
});
77+
};
78+
79+
PromiseConnection.prototype.commit = function () {
80+
var c = this.connection;
81+
return new this.Promise(function (resolve, reject) {
82+
var done = makeDoneCb(resolve, reject);
83+
c.commit(done);
84+
});
85+
};
86+
87+
PromiseConnection.prototype.rollback = function () {
88+
var c = this.connection;
89+
return new this.Promise(function (resolve, reject) {
90+
var done = makeDoneCb(resolve, reject);
91+
c.rollback(done);
92+
});
93+
};
94+
95+
PromiseConnection.prototype.ping = function () {
96+
var c = this.connection;
97+
return new this.Promise(function (resolve, reject) {
98+
c.ping(resolve);
99+
});
100+
};
101+
102+
PromiseConnection.prototype.connect = function () {
103+
var c = this.connection;
104+
return new this.Promise(function (resolve, reject) {
105+
c.connect(function (error, param) {
106+
if (error) {
107+
reject(error);
108+
} else {
109+
resolve(param);
110+
}
111+
});
112+
});
113+
};
114+
115+
PromiseConnection.prototype.prepare = function () {
116+
var c = this.connection;
117+
return new this.Promise(function (resolve, reject) {
118+
c.prepare(function (error, statement) {
119+
if (error) {
120+
reject(error);
121+
} else {
122+
resolve(statement);
123+
}
124+
});
125+
});
126+
};
127+
128+
// note: the callback of "changeUser" is not called on success
129+
// hence there is no possibility to call "resolve"
130+
131+
// patching PromiseConnection
132+
// create facade functions for prototype functions on "Connection" that are not yet
133+
// implemented with PromiseConnection
134+
135+
// proxy synchronous functions only
136+
(function (functionsToWrap) {
137+
138+
for (var i = 0; functionsToWrap && i < functionsToWrap.length; i++) {
139+
var func = functionsToWrap[i];
140+
141+
if (
142+
typeof core.Connection.prototype[func] === 'function'
143+
&& PromiseConnection.prototype[func] === undefined
144+
) {
145+
PromiseConnection.prototype[func] = (function factory (funcName) {
146+
return function () {
147+
return core.Connection
148+
.prototype[funcName].apply(this.connection, arguments);
149+
};
150+
})(func);
151+
}
152+
}
153+
154+
})([
155+
// synchronous functions
156+
'close',
157+
'createBinlogStream',
158+
'destroy',
159+
'escape',
160+
'escapeId',
161+
'format',
162+
'pause',
163+
'pipe',
164+
'resume',
165+
'unprepare'
166+
]);
167+
168+
71169
function createPool (opts) {
72170
var corePool = core.createPool(opts);
73171
var Promise = opts.Promise || global.Promise || require('es6-promise');

0 commit comments

Comments
 (0)