Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Generic queries with optional params #762

Unanswered
rossb83 asked this question in Feature Requests & Ideas
Discussion options

cc @alecbz

I was thinking it would be neat if there were a way to make a query like this generic with optional params. In the notation below I'm using double question mark ?? to denote an optional param.

/* name: List :many */
SELECT * FROM some_table WHERE col_1 = ?? AND col_2 = ?? AND col_3 = ??

where the generated go struct took in pointers

type ListParams struct {
 Col_1 *string
 Col_2 *string
 Col_3 *string
}

in the above query, 8 queries would actually be generated

/* name: List_0 :many */
SELECT * FROM some_table WHERE col_1 = ? AND col_2 = ? AND col_3 = ?
/* name: List_1 :many */
SELECT * FROM some_table WHERE col_1 = ? AND col_2 = ?
/* name: List_2 :many */
SELECT * FROM some_table WHERE col_1 = ?
/* name: List_3 :many */
SELECT * FROM some_table WHERE col_2 = ? AND col_3 = ?
/* name: List_4 :many */
SELECT * FROM some_table WHERE col_2 = ?
/* name: List_5 :many */
SELECT * FROM some_table WHERE col_1 = ? AND col_3 = ?
/* name: List_6 :many */
SELECT * FROM some_table WHERE col_3 = ?
/* name: List_7 :many */
SELECT * FROM some_table

and the generated go method would re-route to the proper query based on the combination of nil values.

Does this fall in line with the goals of sqlc and how complex would this be to implement and work with the current functionality?

You must be logged in to vote

Replies: 0 comments

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

AltStyle によって変換されたページ (->オリジナル) /