Skip to content

Commit

Permalink
added support for prepared statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Aug 1, 2015
1 parent c6d1814 commit 8604ece
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,7 @@ If, however you normally exit your application by killing the NodeJS process, th
# History
* Version 1.8.2 added support for [Prepared Statements](https://github.com/brianc/node-postgres/wiki/Prepared-Statements). Released: August 01, 2015.
* Version 1.8.0 added support for Query Streaming via [node-pg-query-stream](https://github.com/brianc/node-pg-query-stream). Released: July 23, 2015.
* Version 1.7.2 significant code refactoring and optimizations; added support for super-massive transactions. Released: June 27, 2015.
* Version 1.6.0 major update for the test platform + adding coverage. Released: June 19, 2015.
Expand Down
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@ function $query(ctx, query, values, qrm) {
return $stream(ctx, query, values);
}
var errMsg,
isFunc = $isObject(query, ['funcName']), // function call;
isPS = $isObject(query, ['name', 'text']), // prepared statement;
options = ctx.options,
pgFormatting = (options && options.pgFormatting),
pgFormatting = (options && options.pgFormatting) || isPS,
params = pgFormatting ? values : undefined,
isResult = qrm === 'result',
isFunc = $isObject(query, ['funcName']);
isResult = qrm === 'result';

return $p(function (resolve, reject) {

Expand Down
64 changes: 64 additions & 0 deletions test/dbSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,70 @@ describe("Querying a function", function () {
});
});

describe("Prepared Statement", function () {

describe("valid, without parameters", function () {
var result;
beforeEach(function (done) {
db.many({
name: "get all users",
text: "select * from users"
})
.then(function (data) {
result = data;
})
.finally(function () {
done();
});
});
it("must return all users", function () {
expect(result instanceof Array).toBe(true);
expect(result.length > 0).toBe(true);
});
});

describe("valid, with parameters", function () {
var result;
beforeEach(function (done) {
db.one({
name: "find one user",
text: "select * from users where id=$1",
values: [1]
})
.then(function (data) {
result = data;
})
.finally(function () {
done();
});
});
it("must return all users", function () {
expect(result && typeof(result) === 'object').toBeTruthy();
});
});

describe("with invalid query", function () {
var result;
beforeEach(function (done) {
db.many({
name: "break it",
text: "select * from somewhere"
})
.then(nope, function (reason) {
result = reason;
})
.finally(function () {
done();
});
});
it("must return all users", function () {
expect(result instanceof Error).toBe(true);
expect(result.message).toBe('relation "somewhere" does not exist');
});
});

});

if (jasmine.Runner) {
var _finishCallback = jasmine.Runner.prototype.finishCallback;
jasmine.Runner.prototype.finishCallback = function () {
Expand Down

0 comments on commit 8604ece

Please sign in to comment.