Skip to content

Support dynamic sized IN clauses #167

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

Closed
oralordos opened this issue Dec 13, 2019 · 9 comments
Closed

Support dynamic sized IN clauses #167

oralordos opened this issue Dec 13, 2019 · 9 comments

Comments

@oralordos
Copy link

You should add support for a query with a dynamically sized list of values for an IN clause in a WHERE segment. This would be useful for when you are using a dataloader to get data from the database. This is most useful for graphql servers.

See the large example on this page for a use-case: https://gqlgen.com/reference/dataloaders/

I am thinking something like:

-- name: GetPeopleMany :many
SELECT * FROM people
WHERE id IN ($);

would generate a go function that takes in a slice of values for the input. This is distinguished from just wanting a single value by only having the $ instead of $1.

@kyleconroy
Copy link
Collaborator

You're in luck! sqlc already supports this using ANY. The following SQL:

-- name: GetPeopleMany :many
SELECT * FROM people
WHERE id = ANY($1::int[]);

generate this method signature

func (q *Queries) GetPeopleMany(ctx context.Context, ids []int) ([]People, error) {
  ...
}

@markschmid
Copy link

Any way for this to work with mysql as well? Tried ANY(?::int[]) but it reports a syntax error.

@SebastienMelki
Copy link

Any way for this to work with mysql as well? Tried ANY(?::int[]) but it reports a syntax error.

@kyleconroy I have the same issue, should this work for mysql or is it only available in postgres?

@josharian
Copy link
Contributor

Ditto, but sqlite.

@orisano
Copy link
Contributor

orisano commented Jul 29, 2023

please use sqlc.slice()
https://play.sqlc.dev/p/ed1b1d235f3a32c811a27dec68d826b4c0c36dedf0e614f0339fa02e11cc4ba4

@iceteahh
Copy link

-- name: GetPeopleMany :many
SELECT * FROM people
WHERE id = ANY($1::int[]);

This won't work with enum type

@mbesida
Copy link

mbesida commented Apr 4, 2024

-- name: GetPeopleMany :many SELECT * FROM people WHERE id = ANY($1::int[]);

This won't work with enum type

At the moment(Apr 2024) it works with enum types. You just need to specify the enum type (e.g. ANY($1::MyEnum[]))

@psnehanshu
Copy link

@mbesida This cause error:

failed to encode args[n]: unable to encode []CustomEnumType{\"v1\"} into text format for unknown type (OID 1234): cannot find encode plan

@BijaySharma
Copy link

I used it this way and it works like charm.

-- name: GetPeopleMany :many
SELECT * FROM people
WHERE id IN (sqlc.slice('ids'));

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

10 participants