Skip to content

Commit 55abbaa

Browse files
Tristan Daviesbrianc
Tristan Davies
authored andcommitted
don't mutate params when preparing statement (brianc#992)
1 parent 5d3b506 commit 55abbaa

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/query.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,8 @@ Query.prototype.prepare = function(connection) {
146146
}, true);
147147
}
148148

149-
//TODO is there some better way to prepare values for the database?
150149
if(self.values) {
151-
for(var i = 0, len = self.values.length; i < len; i++) {
152-
self.values[i] = utils.prepareValue(self.values[i]);
153-
}
150+
self.values = self.values.map(utils.prepareValue);
154151
}
155152

156153
//http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY

test/integration/client/simple-query-tests.js

+25
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@ test("simple query interface using addRow", function() {
5757
});
5858
});
5959

60+
test("prepared statements do not mutate params", function() {
61+
62+
var client = helper.client();
63+
64+
var params = [1]
65+
66+
var query = client.query("select name from person where $1 = 1 order by name", params);
67+
68+
assert.deepEqual(params, [1])
69+
70+
client.on('drain', client.end.bind(client));
71+
72+
query.on('row', function(row, result) {
73+
assert.ok(result);
74+
result.addRow(row);
75+
});
76+
77+
query.on('end', function(result) {
78+
assert.lengthIs(result.rows, 26, "result returned wrong number of rows");
79+
assert.lengthIs(result.rows, result.rowCount);
80+
assert.equal(result.rows[0].name, "Aaron");
81+
assert.equal(result.rows[25].name, "Zanzabar");
82+
});
83+
});
84+
6085
test("multiple simple queries", function() {
6186
var client = helper.client();
6287
client.query({ text: "create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');"})

0 commit comments

Comments
 (0)