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

Make query!() recognize Vec<Vec<u8>> as a type #873

Closed
augustocdias opened this issue Dec 3, 2020 · 2 comments · Fixed by #876
Closed

Make query!() recognize Vec<Vec<u8>> as a type #873

augustocdias opened this issue Dec 3, 2020 · 2 comments · Fixed by #876
Labels
db:postgres Related to PostgreSQL E-easy enhancement New feature or request

Comments

@augustocdias
Copy link

augustocdias commented Dec 3, 2020

I'm trying to batch a insert of multiple rows with the following query (PostgreSQL):

INSERT INTO instances (instance_hash, workflow_hash)
     SELECT i, w FROM unnest($1::BYTEA[], $2::BYTEA[]) as x(i,w)
ON CONFLICT (instance_hash) DO NOTHING

I'm getting the following error: unsupported type BYTEA[] for param #1

I'm using the query! macro to do it and check the query. Is there a way to do it without doing it unchecked?

@abonander abonander added E-medium enhancement New feature or request db:postgres Related to PostgreSQL E-easy and removed E-medium labels Dec 3, 2020
@abonander
Copy link
Collaborator

abonander commented Dec 3, 2020

@augustocdias it's not exactly optimal but you could pass hex encoded strings in and decode them in Postgres:

INSERT INTO instances (instance_hash, workflow_hash)
     SELECT decode(i, 'hex'), decode(w, 'hex') FROM unnest($1::TEXT[], $2::TEXT[]) as x(i,w)
ON CONFLICT (instance_hash) DO NOTHING

We already have trait implementations for Vec<Vec<u8>>, &[&[u8]], Vec<&[u8]> and &[Vec<u8>], it just needs to be enabled in the macros.

Unfortunately though it's only possible to specify one or two types for the macros to use, an "owned" type and a "borrowed" type. Which ones would be the most useful here? You might think Vec<Vec<u8>> and &[&[u8]] but the latter can only be created by constructing an array or vec of slices which is somewhat redundant.

Thus, I would argue that it should be Vec<Vec<u8>> | &[Vec<u8>] which should be added here (and also mirrors Vec<String> | &[String] above which has the same issue): https://github.com/launchbadge/sqlx/blob/master/sqlx-macros/src/database/postgres.rs#L78

When the const_panic feature is stabilized we can get rid of the owned/borrowed distinction and just specify a single type and have a const is_compatible() assertion for borrowed types, which would also significantly improve the typechecking code and the errors it gives.

@abonander abonander changed the title Unsupported type BYTEA[] Make query!() recognize Vec<Vec<u8>> as a type Dec 3, 2020
@augustocdias
Copy link
Author

It seems to be as simple as you described. I created the PR for that.

Tks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db:postgres Related to PostgreSQL E-easy enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants