Skip to content

Commit fb2db08

Browse files
committed
Merge with origin/master
2 parents a8a3f3c + 03978b6 commit fb2db08

File tree

7 files changed

+140
-4
lines changed

7 files changed

+140
-4
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ node_js:
66
before_script:
77
- npm install npm --global
88
- node script/create-test-tables.js pg://postgres@127.0.0.1:5432/postgres
9+
env:
10+
- PGUSER=postgres PGDATABASE=postgres

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ __I love contributions.__
9595
You are welcome contribute via pull requests. If you need help getting the tests running locally feel free to email me or gchat me.
9696

9797
I will __happily__ accept your pull request if it:
98-
- _)has tests__
98+
- __has tests__
9999
- looks reasonable
100100
- does not break backwards compatibility
101101
- satisfies jshint
@@ -135,6 +135,8 @@ node-postgres is by design _low level_ with the bare minimum of abstraction. Th
135135
- [brianc/node-sql](https://github.com/brianc/node-sql) - SQL generation for node.js
136136
- [hiddentao/suqel](https://hiddentao.github.io/squel/) - SQL query string builder for Javascript
137137
- [CSNW/sql-bricks](https://github.com/CSNW/sql-bricks) - Transparent, Schemaless SQL Generation
138+
- [datalanche/node-pg-format](https://github.com/datalanche/node-pg-format) - Safely and easily create dynamic SQL queries with this Node implementation of [PostgreSQL format()](http://www.postgresql.org/docs/9.3/static/functions-string.html#FUNCTIONS-STRING-FORMAT).
139+
- [iceddev/pg-transact](https://github.com/iceddev/pg-transact) - A nicer API on node-postgres transactions
138140

139141

140142
### Windows

lib/client.js

+9
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ Client.prototype.connect = function(callback) {
115115
self.activeQuery.handleCommandComplete(msg, con);
116116
});
117117

118+
//if a prepared statement has a name and properly parses
119+
//we track that its already been executed so we don't parse
120+
//it again on the same client
121+
con.on('parseComplete', function(msg) {
122+
if(self.activeQuery.name) {
123+
con.parsedStatements[self.activeQuery.name] = true;
124+
}
125+
});
126+
118127
con.on('copyInResponse', function(msg) {
119128
self.activeQuery.handleCopyInResponse(self.connection);
120129
});

lib/query.js

-3
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,6 @@ Query.prototype.prepare = function(connection) {
135135
name: self.name,
136136
types: self.types
137137
}, true);
138-
if(this.name) {
139-
connection.parsedStatements[this.name] = true;
140-
}
141138
}
142139

143140
//TODO is there some better way to prepare values for the database?
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var helper = require('../test-helper');
2+
var client = helper.client();
3+
4+
client.query('CREATE TEMP TABLE arrtest (n integer, s varchar)');
5+
client.query("INSERT INTO arrtest VALUES (4, 'foo'), (5, 'bar'), (6, 'baz');");
6+
7+
var qText = "SELECT \
8+
ARRAY[1, 2, 3] AS b,\
9+
ARRAY['xx', 'yy', 'zz'] AS c,\
10+
ARRAY(SELECT n FROM arrtest) AS d,\
11+
ARRAY(SELECT s FROM arrtest) AS e;";
12+
13+
client.query(qText, function(err, result) {
14+
if(err) throw err;
15+
var row = result.rows[0];
16+
for(var key in row) {
17+
assert.equal(typeof row[key], 'object');
18+
assert.equal(row[key].length, 3);
19+
}
20+
client.end();
21+
});
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var async = require('async');
2+
var helper = require('../test-helper');
3+
4+
var db = helper.client();
5+
6+
function createTableFoo(callback){
7+
db.query("create temp table foo(column1 int, column2 int)", callback);
8+
}
9+
10+
function createTableBar(callback){
11+
db.query("create temp table bar(column1 text, column2 text)", callback);
12+
}
13+
14+
function insertDataFoo(callback){
15+
db.query({
16+
name: 'insertFoo',
17+
text: 'insert into foo values($1,$2)',
18+
values:['one','two']
19+
}, callback );
20+
}
21+
22+
function insertDataBar(callback){
23+
db.query({
24+
name: 'insertBar',
25+
text: 'insert into bar values($1,$2)',
26+
values:['one','two']
27+
}, callback );
28+
}
29+
30+
function startTransaction(callback) {
31+
db.query('BEGIN', callback);
32+
}
33+
function endTransaction(callback) {
34+
db.query('COMMIT', callback);
35+
}
36+
37+
function doTransaction(callback) {
38+
// The transaction runs startTransaction, then all queries, then endTransaction,
39+
// no matter if there has been an error in a query in the middle.
40+
startTransaction(function() {
41+
insertDataFoo(function() {
42+
insertDataBar(function() {
43+
endTransaction( callback );
44+
});
45+
});
46+
});
47+
}
48+
49+
var steps = [
50+
createTableFoo,
51+
createTableBar,
52+
doTransaction,
53+
insertDataBar
54+
]
55+
56+
test('test if query fails', function() {
57+
async.series(steps, assert.success(function() {
58+
db.end()
59+
}))
60+
})
61+
62+
test('test if prepare works but bind fails', function() {
63+
var client = helper.client();
64+
var q = {
65+
text: 'SELECT $1::int as name',
66+
values: ['brian'],
67+
name: 'test'
68+
};
69+
client.query(q, assert.calls(function(err, res) {
70+
q.values = [1];
71+
client.query(q, assert.calls(function(err, res) {
72+
assert.ifError(err);
73+
client.end();
74+
}));
75+
}));
76+
});
77+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var helper = require('../test-helper');
2+
var assert = require('assert');
3+
4+
helper.pg.connect(function(err, client, done) {
5+
if (err) throw err;
6+
7+
var c = 'CREATE TEMP TABLE posts (body TEXT)';
8+
9+
client.query(c, function(err) {
10+
if (err) throw err;
11+
12+
c = 'INSERT INTO posts (body) VALUES ($1) RETURNING *';
13+
14+
var body = new Buffer('foo');
15+
client.query(c, [body], function(err) {
16+
if (err) throw err;
17+
18+
body = new Buffer([]);
19+
client.query(c, [body], function(err, res) {
20+
done();
21+
22+
if (err) throw err;
23+
assert.equal(res.rows[0].body, '')
24+
helper.pg.end();
25+
});
26+
});
27+
});
28+
});

0 commit comments

Comments
 (0)