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

Encode Format based on Type #937

Merged
merged 2 commits into from Aug 16, 2022
Merged

Conversation

NAlexPear
Copy link
Contributor

#836 has been helpful as a way of pairing parameter types with their encodings, but the encode_format method in the ToSql trait would be even more useful with the addition of a reference to Type. This would enable more granular encoding of parameters based on the types inferred by Postgres during the prepare phase, which would be useful when processing payloads from more general type systems like gRPC or JSON.

Since parameter types are already generated by the time bind() is called, it's easy to include them as part of the format-generating process. This PR simply adds a Type parameter to the encode_format method added to ToSql, which should not be a breaking change if it can be made before the next release.

let (param_formats, params): (Vec<_>, Vec<_>) = params
.into_iter()
.map(|p| (p.borrow_to_sql().encode_format() as i16, p))
.zip(param_types.iter())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A zipped iterator stops as soon as the shortest iterator ends, so the length check below won't work properly. You may need to manually iterate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better (or sufficient) to move the length check before the zip to guard against the early-ending case?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, we already require ExactSizeIterator so we can just move the assert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, updated in this commit.

@sfackler
Copy link
Owner

sfackler commented Aug 16, 2022

I think you just need to rebase to fix the clippy error. LGTM otherwise!

@sfackler sfackler merged commit 91b2187 into sfackler:master Aug 16, 2022
@NAlexPear NAlexPear deleted the encode_format_types branch August 16, 2022 13:32
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

Successfully merging this pull request may close these issues.

None yet

2 participants