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 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

Open
ash-hashtag wants to merge1 commit intorust-postgres:master
base:master
Choose a base branch
Loading
fromash-hashtag:query_typed_helpers

Conversation

ash-hashtag
Copy link

resolves#1232

Copy link
Contributor

@ramnivasramnivas left a 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.

Copy link
Contributor

@ramnivasramnivas left a 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

Copy link
Member

@paolobarbolinipaolobarbolini left a 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.

Comment on lines 367 to 368
let stream =self
.query_typed_raw(statement, params.iter().map(|(v, t)|(*v, t.clone())))
.await?;
pin_mut!(stream);

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?

Suggested change
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?
);

self.client()
}

asyncfnquery_typed_one(

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

Comment on lines 213 to 228
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
}

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

Comment on lines +73 to +85
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>;

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

Comment on lines 326 to 385
/// 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)

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

Comment on lines 251 to 277
/// 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),
)
}

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

Comment on lines +309 to +336
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)
}

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

Comment on lines 195 to 211
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)
}

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

Comment on lines 625 to 666
/// 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))
}

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

@paolobarbolinipaolobarbolini changed the titleAdded methods query_typed_opt and query_typed_oneAdd methodsquery_typed_opt,query_typed_one andexecute_typedOct 5, 2025
@ash-hashtagash-hashtag changed the titleAdd methodsquery_typed_opt,query_typed_one andexecute_typedAdd methodsquery_typed_opt,query_typed_oneOct 5, 2025
@ash-hashtag
Copy link
Author

ash-hashtag commentedOct 5, 2025
edited
Loading

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.

yeah I think I need some help, I tried to do on my own, and made a bunch of mess

@ash-hashtagash-hashtag changed the titleAdd methodsquery_typed_opt,query_typed_oneAdd methodsquery_typed_opt,query_typed_one andexecute_typedOct 5, 2025
add 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
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@paolobarbolinipaolobarboliniAwaiting requested review from paolobarbolini

1 more reviewer

@ramnivasramnivasramnivas approved these changes

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.

Additional helper methods forquery_typed
3 participants
@ash-hashtag@ramnivas@paolobarbolini

[8]ページ先頭

©2009-2025 Movatter.jp