- Notifications
You must be signed in to change notification settings - Fork514
Add methodsquery_typed_opt
,query_typed_one
andexecute_typed
#1265
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
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks for adding tests with parameters. Added a few more minor comments to make these tests even better.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Except for a nitpick in a comment, LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks@ash-hashtag for the PR and@ramnivas for the review.
I've left some nitpicks.@ash-hashtag I'd also like the the history to be squashed into a single commit. Let me know if you need help with that.
tokio-postgres/src/client.rs Outdated
let stream =self | ||
.query_typed_raw(statement, params.iter().map(|(v, t)|(*v, t.clone()))) | ||
.await?; | ||
pin_mut!(stream); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
NIT: could you usestd::pin::pin!
instead?
let stream =self | |
.query_typed_raw(statement, params.iter().map(|(v, t)|(*v, t.clone()))) | |
.await?; | |
pin_mut!(stream); | |
letmutstream =pin!( | |
self.query_typed_raw(statement, params.iter().map(|(v, t)|(*v, t.clone()))) | |
.await? | |
); |
tokio-postgres/src/generic_client.rs Outdated
self.client() | ||
} | ||
asyncfnquery_typed_one( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
NIT: this should be moved up, just afterquery_typed
tokio-postgres/src/generic_client.rs Outdated
asyncfnquery_typed_one( | ||
&self, | ||
statement:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Row,Error>{ | ||
self.query_typed_one(statement, params).await | ||
} | ||
/// Like [`Client::query_opt_typed`]. | ||
asyncfnquery_typed_opt( | ||
&self, | ||
statement:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Option<Row>,Error>{ | ||
self.query_typed_opt(statement, params).await | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
NIT: this should be moved up, just afterquery_typed
asyncfnquery_typed_one( | ||
&self, | ||
statement:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Row,Error>; | ||
/// Like [`Client::query_opt_typed`]. | ||
asyncfnquery_typed_opt( | ||
&self, | ||
statement:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Option<Row>,Error>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This should be beforequery_typed_raw
tokio-postgres/src/client.rs Outdated
/// Like `query_one`, but requires the types of query parameters to be explicitly specified. | ||
/// | ||
/// Compared to `query_one`, this method allows performing queries without three round trips (for | ||
/// prepare, execute, and close) by requiring the caller to specify parameter values along with | ||
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't | ||
/// supported (such as Cloudflare Workers with Hyperdrive). | ||
/// | ||
/// Executes a statement which returns a single row, returning it. | ||
/// | ||
/// Returns an error if the query does not return exactly one row. | ||
/// | ||
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list | ||
/// provided, 1-indexed. | ||
/// | ||
pubasyncfnquery_typed_one( | ||
&self, | ||
statement:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Row,Error>{ | ||
self.query_typed_opt(statement, params) | ||
.await | ||
.and_then(|res| res.ok_or_else(Error::row_count)) | ||
} | ||
/// Like `query_one`, but requires the types of query parameters to be explicitly specified. | ||
/// | ||
/// Compared to `query_one`, this method allows performing queries without three round trips (for | ||
/// prepare, execute, and close) by requiring the caller to specify parameter values along with | ||
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't | ||
/// supported (such as Cloudflare Workers with Hyperdrive). | ||
/// | ||
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the | ||
/// parameter of the list provided, 1-indexed. | ||
/// Executes a statements which returns zero or one rows, returning it. | ||
/// | ||
/// Returns an error if the query returns more than one row. | ||
pubasyncfnquery_typed_opt( | ||
&self, | ||
statement:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Option<Row>,Error>{ | ||
let stream =self | ||
.query_typed_raw(statement, params.iter().map(|(v, t)|(*v, t.clone()))) | ||
.await?; | ||
pin_mut!(stream); | ||
letmut first =None; | ||
// Originally this was two calls to `try_next().await?`, | ||
// once for the first element, and second to error if more than one. | ||
// | ||
// However, this new form with only one .await in a loop generates | ||
// slightly smaller codegen/stack usage for the resulting future. | ||
whileletSome(row) = stream.try_next().await?{ | ||
if first.is_some(){ | ||
returnErr(Error::row_count()); | ||
} | ||
first =Some(row); | ||
} | ||
Ok(first) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
NIT: This should be afterquery_typed
postgres/src/transaction.rs Outdated
/// Like `Client::query_typed_one`. | ||
pubfnquery_typed_one( | ||
&mutself, | ||
query:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Row,Error>{ | ||
self.connection.block_on( | ||
self.transaction | ||
.as_ref() | ||
.unwrap() | ||
.query_typed_one(query, params), | ||
) | ||
} | ||
/// Like `Client::query_typed_opt`. | ||
pubfnquery_typed_opt( | ||
&mutself, | ||
query:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Option<Row>,Error>{ | ||
self.connection.block_on( | ||
self.transaction | ||
.as_ref() | ||
.unwrap() | ||
.query_typed_opt(query, params), | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
NIT: this should be moved up, just afterquery_typed
fnquery_typed_one( | ||
&mutself, | ||
query:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Row,Error>{ | ||
self.query_typed_one(query, params) | ||
} | ||
fnquery_typed_opt( | ||
&mutself, | ||
query:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Option<Row>,Error>{ | ||
self.query_typed_opt(query, params) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
NIT: this should be moved up, just afterquery_typed
postgres/src/generic_client.rs Outdated
fnquery_typed_one( | ||
&mutself, | ||
query:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Row,Error>{ | ||
self.query_typed_one(query, params) | ||
} | ||
fnquery_typed_opt( | ||
&mutself, | ||
query:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Option<Row>,Error>{ | ||
self.query_typed_opt(query, params) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
NIT: this should be moved up, just afterquery_typed
Uh oh!
There was an error while loading.Please reload this page.
postgres/src/client.rs Outdated
/// Like `query_one`, but requires the types of query parameters to be explicitly specified. | ||
/// | ||
/// Compared to `query_one`, this method allows performing queries without three round trips (for | ||
/// prepare, execute, and close) by requiring the caller to specify parameter values along with | ||
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't | ||
/// supported (such as Cloudflare Workers with Hyperdrive). | ||
/// | ||
/// Executes a statement which returns a single row, returning it. | ||
/// | ||
/// Returns an error if the query does not return exactly one row. | ||
/// | ||
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list | ||
/// provided, 1-indexed. | ||
pubfnquery_typed_one( | ||
&mutself, | ||
query:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Row,Error>{ | ||
self.connection | ||
.block_on(self.client.query_typed_one(query, params)) | ||
} | ||
/// Like `query_opt`, but requires the types of query parameters to be explicitly specified. | ||
/// | ||
/// Compared to `query_opt`, this method allows performing queries without three round trips (for | ||
/// prepare, execute, and close) by requiring the caller to specify parameter values along with | ||
/// their Postgres type. Thus, this is suitable in environments where prepared statements aren't | ||
/// supported (such as Cloudflare Workers with Hyperdrive). | ||
/// Executes a statement which returns zero or one rows, returning it. | ||
/// | ||
/// Returns an error if the query returns more than one row. | ||
/// | ||
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list | ||
/// provided, 1-indexed. | ||
pubfnquery_typed_opt( | ||
&mutself, | ||
query:&str, | ||
params:&[(&(dynToSql +Sync),Type)], | ||
) ->Result<Option<Row>,Error>{ | ||
self.connection | ||
.block_on(self.client.query_typed_opt(query, params)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
NIT: this should be moved up, just afterquery_typed
query_typed_opt
,query_typed_one
andexecute_typed
9200080
to90d63a7
Comparequery_typed_opt
,query_typed_one
andexecute_typed
query_typed_opt
,query_typed_one
b1f1753
to9200080
Compareash-hashtag commentedOct 5, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
yeah I think I need some help, I tried to do on my own, and made a bunch of mess |
query_typed_opt
,query_typed_one
query_typed_opt
,query_typed_one
andexecute_typed
9711f3a
tod594b1c
Compareadd the same methods to sync version and add testsfix tests with appropriate placeholdersfix tests and compilation errorsadded comments to tests and more casesfix commentremove empty space for fmtrefactoringexecute_typed testsfix comment
resolves#1232