Movatterモバイル変換


[0]ホーム

URL:


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

Add parameter and column formatting to query_raw()#747

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

NAlexPear
Copy link
Contributor

@NAlexPearNAlexPear commentedMar 14, 2021
edited
Loading

Summary

This PR wouldclose#746by adding aformat(&self) method toToSql, defaulting toFormat::Binary for backwards compatibility and ease-of-use by addingparam_formats andcolumn_formats to thequery_raw() method.

Notes

This implementation allocatestwo new vectors, which might not be acceptable. Running benchmarks showed no consistent real-world performance differences on my machine, but YMMV. It would be possible to avoid those allocations ifencode_bind could take anIntoIterator<Item = &P> instead ofP, I think, but that would certainly be a lot more work to get changed.

) ->Result<IsNull,Box<dynError +Sync +Send>>;

/// Specifies the message format of the value
fnformat(&self) ->Format{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't know how I feel about exposing this on ToSql, since the same can't really be done for FromSql.

An alternative I've been thinking about is to have a separate query method that operates entirely in "string mode", both for parameters and the resulting rows.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

That's a really good point about the lack of equivalent handle on the returned types. It would be nice to let both parameters and returned types have independently-configurable formats like Postgres itself, but a text-only query variant (query_text, perhaps?) would probably cover those cases well-enough.

If we wanted to keep the configurability of each input and output's format, what about adding that configuration toStatement instead? Or tracking the configuration of those formats in aStatement, generated throughprepare_typed or some otherprepare_* method?

Choose a reason for hiding this comment

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

An alternative I've been thinking about is to have a separate query method that operates entirely in "string mode", both for parameters and the resulting rows.

@sfackler This would be wonderful; is it anything you've thought more about? Passing arbitrary types as strings is particularly useful, since I could pass any number as a string without knowing the exact intended type and Postgres itself can figure it out. Being able to then load those numbers as strings would also be wonderful. My use case might not be typical, though, so I might be the only person wanting this.

@NAlexPearNAlexPear changed the titleAdd format() method to ToSql for parameter format specificationAdd parameter and column formatting to query_raw()Mar 15, 2021
@NAlexPear
Copy link
ContributorAuthor

@sfackler as a possible implementation of a "string mode" query method that lets users specify their own formats for inputs and outputs, I've addedparam_formats andcolumn_formats toquery_raw.

I decided against a separate method sincequery_raw is supposed to be the "maximally flexible version ofquery", andquery_raw_er didn't seem like a good road to go down. But if this change breaks your backwards-compatibility goals, then I can pull that work out into a separatequery method.

Also not sure which check failed on this latest commit, since I don't have access to CircleCI, but let me know what it is and I'll fix it.

@sfackler
Copy link
Collaborator

error[E0061]: this function takes 4 arguments but 2 arguments were supplied   --> postgres/src/client.rs:276:35    |276 |             .block_on(self.client.query_raw(query, params))?;    |                                   ^^^^^^^^^ -----  ------ supplied 2 arguments    |                                   |    |                                   expected 4 argumentserror[E0061]: this function takes 4 arguments but 2 arguments were supplied   --> postgres/src/client.rs:276:35    |276 |             .block_on(self.client.query_raw(query, params))?;    |                                   ^^^^^^^^^ -----  ------ supplied 2 arguments    |                                   |    |                                   expected 4 argumentserror[E0061]: this function takes 4 arguments but 2 arguments were supplied   --> postgres/src/transaction.rs:114:58    |114 |             .block_on(self.transaction.as_ref().unwrap().query_raw(query, params))?;    |                                                          ^^^^^^^^^ -----  ------ supplied 2 arguments    |                                                          |    |                                                          expected 4 argumentserror[E0061]: this function takes 4 arguments but 2 arguments were supplied   --> postgres/src/transaction.rs:114:58    |114 |             .block_on(self.transaction.as_ref().unwrap().query_raw(query, params))?;    |                                                          ^^^^^^^^^ -----  ------ supplied 2 arguments    |                                                          |    |                                                          expected 4 argumentserror: aborting due to 2 previous errors

@sfackler
Copy link
Collaborator

In addition to the fact that it breaks backwards compatibility, I don't think that query_raw is the right thing to modify, since it still works with ToSql/FromSql which are defined to work with binary formats.

@NAlexPear
Copy link
ContributorAuthor

gah, sorry, forgot about the sync client. Updating!

@NAlexPear
Copy link
ContributorAuthor

I don't think that query_raw is the right thing to modify, since it still works with ToSql/FromSql which are defined to work with binary formats.

If one wants to mix-and-match both input and output formats (which I would like to), I don't think this requirement is avoidable. Nor is it a bad thing, I don't think... we can leverageto_sql_checked to make sure that only thoseToSql implementations that can handleTEXT are valid forFormat::Text params, and probably ditto forFromSql (although I didn't implement that check).ToSql andFromSql still seem valid to me in this context, since the important part is the handling ofType::TEXT.

@NAlexPear
Copy link
ContributorAuthor

Closing in favor of#836

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@sfacklersfacklersfackler left review comments

+1 more reviewer

@kevlarrkevlarrkevlarr left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Provide mechanism for letting Postgres itself handle type inference

3 participants

@NAlexPear@sfackler@kevlarr

[8]ページ先頭

©2009-2025 Movatter.jp