Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update query builder #143

Closed
kevinburke1 opened this issue Dec 9, 2019 · 1 comment
Closed

Update query builder #143

kevinburke1 opened this issue Dec 9, 2019 · 1 comment

Comments

@kevinburke1
Copy link
Contributor

A common-ish pattern is to allow three or four fields to be updated, and then issue an UPDATE query that only modifies the ones the user actually wants to set. Generally people do this by manually building a string - keeping track of $1, $2, etc. and then executing it.

I'm wondering if sqlc could help automate this process or at least make it less error prone.

@kyleconroy
Copy link
Collaborator

There are two ways to accomplish this today with sqlc.

The first is to have individual UPDATE queries and execute them in a transaction.

The other option is to use a CASE statement per field

UPDATE SET
  field1 = CASE update_field1 THEN value1 ELSE field1

Since sqlc will not generate SQL for you at runtime, these are the only two real options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants