Skip to content

Commit e6eba78

Browse files
authored
Merge pull request #575 from faazshift/master
Fixing promisified prepare implementation and promisifying returned statement
2 parents 52aaeb4 + 3ca641e commit e6eba78

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

promise.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,46 @@ PromiseConnection.prototype.connect = function () {
129129
});
130130
};
131131

132-
PromiseConnection.prototype.prepare = function () {
132+
PromiseConnection.prototype.prepare = function (options) {
133133
var c = this.connection;
134+
var promiseImpl = this.Promise;
134135
return new this.Promise(function (resolve, reject) {
135-
c.prepare(function (error, statement) {
136+
c.prepare(options, function (error, statement) {
136137
if (error) {
137138
reject(error);
138139
} else {
139-
resolve(statement);
140+
var wrappedStatement = new PromisePreparedStatementInfo(statement, promiseImpl);
141+
resolve(wrappedStatement);
140142
}
141143
});
142144
});
143145
};
144146

147+
function PromisePreparedStatementInfo (statement, promiseImpl) {
148+
this.statement = statement;
149+
this.Promise = promiseImpl;
150+
}
151+
152+
PromisePreparedStatementInfo.prototype.execute = function (parameters) {
153+
var s = this.statement;
154+
return new this.Promise(function (resolve, reject) {
155+
var done = makeDoneCb(resolve, reject);
156+
if (parameters) {
157+
s.execute(parameters, done);
158+
} else {
159+
s.execute(done);
160+
}
161+
});
162+
};
163+
164+
PromisePreparedStatementInfo.prototype.close = function () {
165+
var s = this.statement;
166+
return new this.Promise(function (resolve, reject) {
167+
s.close();
168+
resolve();
169+
});
170+
};
171+
145172
// note: the callback of "changeUser" is not called on success
146173
// hence there is no possibility to call "resolve"
147174

test/integration/test-promise-wrappers.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,31 @@ function testObjParams() {
104104
});
105105
}
106106

107+
function testPrepared() {
108+
var connResolved;
109+
var connPromise = createConnection(config)
110+
.then(function(conn) {
111+
connResolved = conn;
112+
return conn.prepare('select ?-? as ttt, ? as uuu');
113+
})
114+
.then(function(statement) {
115+
return statement.execute([11, 3, 'test']);
116+
})
117+
.then(function(result) {
118+
assert.equal(result[0][0].ttt, 8);
119+
assert.equal(result[0][0].uuu, 'test');
120+
return connResolved.end();
121+
})
122+
.catch(function(err) {
123+
console.log(err);
124+
if (connResolved) {
125+
connResolved.end();
126+
} else {
127+
console.log('Warning: promise rejected before executing prepared statement');
128+
}
129+
});
130+
}
131+
107132
function testEventsConnect() {
108133
var connPromise = createConnection(config).then(function(conn) {
109134
var events = 0;
@@ -226,6 +251,7 @@ function testEventsPool() {
226251
testBasic();
227252
testErrors();
228253
testObjParams();
254+
testPrepared();
229255
testEventsConnect();
230256
testBasicPool();
231257
testErrorsPool();

0 commit comments

Comments
 (0)