node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

client.query(sqlString, params[], function) alters the given params[]

Open ngervasi opened this issue 11 years ago • 7 comments

When supplying a parameters array to client.query the given array is modified (in my case it originally was an array of numbers and got transformed to an array of strings). This is not correct behaviour, I see the problem in the source code (query.js, line 143):

self.values[i] = utils.prepareValue(self.values[i]);

If transformation is needed it should be done on a clone of the passed array so that it doesn't break any logic in case the original array is reused somewhere else after the query.

I think a quick fix would be to change (query.js, line 14) from:

this.values = config.values;

to

this.values = config.values.slice();

ngervasi avatar Mar 18 '15 14:03 ngervasi

yeah that totally sucks! Sorry about that. Do you wanna submit a PR for that change w/ a little test? If not, I can bang one together.

brianc avatar Mar 28 '15 17:03 brianc

Hi Brian, maybe it's faster if you do the change directly, I've never done a Pull request... but I promise I will start learning and contribute :)

ngervasi avatar Mar 30 '15 14:03 ngervasi

@ngervasi Many people are on the fence like yourself, so was I, for a while, till I tried and then was laughing how easy it really was ;) And if you don't like GIT, use Source Tree - it is a huge help: http://www.sourcetreeapp.com/

vitaly-t avatar Mar 30 '15 22:03 vitaly-t

You're right Vitaly, it's time to actively contribute to the project, I sent my pull requests but I didn't create the tests, I'm unsure about how to do that properly. Can someone give me a hint about it? I'm not familiar with node tests... btw: thanks Brian for this impressive project!

ngervasi avatar Mar 30 '15 23:03 ngervasi

All tests are in \test sub-folder. Typically, you either amend an existing test or add a new one. Given the nature of the issue, I'd say you have to create a new test. The syntax is very easy, and for details just look at the test library ;)

vitaly-t avatar Mar 31 '15 00:03 vitaly-t

@brianc , @ngervasi Any progress here? I'm stuck with some tests here on parameters, they are altered, unexpectedly.

This is kind of a big issue.

vitaly-t avatar Jun 25 '15 19:06 vitaly-t

Unfortunately not, I'm very busy and I wasn't able to learn how to setup the test, if you have time have a look at my patch and feel free to submit it on your own. Sorry bout that but it's a tough time.

ngervasi avatar Jun 26 '15 18:06 ngervasi