Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Added toParam() for use with e.g. node-postgres' parameterized queries #34

Merged
merged 4 commits into from
Dec 16, 2013

Conversation

mtsr
Copy link
Contributor

@mtsr mtsr commented Aug 23, 2013

I've done some initial work on adding a toParam() function that produces a { text: '', values: [] } object for use with for example node-postgres' parameterized queries.

I've included tests for my changes, although I'm not familiar enough with the project to ensure complete coverage. I also changed the WhereBlock tests slightly because neither parameterized where's nor IN was being tested.

Documentation still needs to be done, but I didn't feel that was useful before discussing the functionality itself.

I still have to actually test this with node-postgres, but I'd love some feedback on this, that's why the pull-request already.

I haven't programmed CoffeeScript before, so there might be some javascript-isms in there, please feel free to point those out as well.

@mtsr
Copy link
Contributor Author

mtsr commented Aug 24, 2013

What this pull request allows you to do is instead of

var query = squel.update()
  .table("students")
  .set("name", "$1")
  .set("gender", "$2")
  .toString();
// 'UPDATE students SET name = $1, gender = $2

// node-postgres client
client.query({ text: query, values: ['Thomas', 'M'] });

you can do

var query = squel.update()
  .table("students")
  .set("name", "Thomas")
  .set("gender", "M")
  .toParam();
// { text: 'UPDATE students SET name = $1, gender = $2', values: ['Thomas', 'M'] }

// node-postgres client
client.query(query);

which is much easier to read and maintain.

@hiddentao
Copy link
Owner

Very nice. Perhaps I ought to deprecate the the old way of doing parameterised queries.

@mtsr
Copy link
Contributor Author

mtsr commented Sep 20, 2013

Hey, I made a minor mistake in the changes. I fixed it in my fork and could merge it into the pull request if you're considering implementing it.

Conflicts:
	docs/squel.html
	squel.min.js
Conflicts:
	squel.min.js
@mtsr
Copy link
Contributor Author

mtsr commented Sep 20, 2013

There. That should fix my mistake (and the one I made while fixing it).

@hiddentao
Copy link
Owner

Thank. I'm going to push out version 1.2 now with some other changes, including the ability to load in db engine-specific blocks and builders (Postgres already supported). I'm wondering whether this work should go into that or into the main Squel code.

@hiddentao hiddentao merged commit 780659b into hiddentao:master Dec 16, 2013
@hiddentao hiddentao mentioned this pull request Dec 16, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants