don't mutate params when preparing statement (#992)

This commit is contained in:
Tristan Davies 2016-04-28 15:46:33 -04:00 committed by Brian C
parent 5d3b506c70
commit 55abbaa844
2 changed files with 26 additions and 4 deletions

View File

@ -146,11 +146,8 @@ Query.prototype.prepare = function(connection) {
}, true);
}
//TODO is there some better way to prepare values for the database?
if(self.values) {
for(var i = 0, len = self.values.length; i < len; i++) {
self.values[i] = utils.prepareValue(self.values[i]);
}
self.values = self.values.map(utils.prepareValue);
}
//http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY

View File

@ -57,6 +57,31 @@ test("simple query interface using addRow", function() {
});
});
test("prepared statements do not mutate params", function() {
var client = helper.client();
var params = [1]
var query = client.query("select name from person where $1 = 1 order by name", params);
assert.deepEqual(params, [1])
client.on('drain', client.end.bind(client));
query.on('row', function(row, result) {
assert.ok(result);
result.addRow(row);
});
query.on('end', function(result) {
assert.lengthIs(result.rows, 26, "result returned wrong number of rows");
assert.lengthIs(result.rows, result.rowCount);
assert.equal(result.rows[0].name, "Aaron");
assert.equal(result.rows[25].name, "Zanzabar");
});
});
test("multiple simple queries", function() {
var client = helper.client();
client.query({ text: "create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');"})