- Notifications
You must be signed in to change notification settings - Fork514
Work with pools that don't support prepared statements#1147
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
f397668
3f8f5de
74eb4db
f00ed42
dbd4d02
0fa3247
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -364,6 +364,54 @@ impl Client { | ||
query::query(&self.inner, statement, params).await | ||
} | ||
/// Like `query`, but requires the types of query parameters to be explicitly specified. | ||
/// | ||
/// Compared to `query`, 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. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// # async fn async_main(client: &tokio_postgres::Client) -> Result<(), tokio_postgres::Error> { | ||
/// use tokio_postgres::types::ToSql; | ||
/// use tokio_postgres::types::Type; | ||
/// use futures_util::{pin_mut, TryStreamExt}; | ||
/// | ||
/// let rows = client.query_typed( | ||
/// "SELECT foo FROM bar WHERE biz = $1 AND baz = $2", | ||
/// &[(&"first param", Type::TEXT), (&2i32, Type::INT4)], | ||
/// ).await?; | ||
/// | ||
/// for row in rows { | ||
/// let foo: i32 = row.get("foo"); | ||
/// println!("foo: {}", foo); | ||
/// } | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub async fn query_typed( | ||
&self, | ||
statement: &str, | ||
params: &[(&(dyn ToSql + Sync), Type)], | ||
) -> Result<Vec<Row>, Error> { | ||
fn slice_iter<'a>( | ||
s: &'a [(&'a (dyn ToSql + Sync), Type)], | ||
) -> impl ExactSizeIterator<Item = (&'a dyn ToSql, Type)> + 'a { | ||
s.iter() | ||
.map(|(param, param_type)| (*param as _, param_type.clone())) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. There's no need to factor this into a separate function here since it's only being called one place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Fixed. Earlier thought was to allow access to the raw | ||
query::query_typed(&self.inner, statement, slice_iter(params)) | ||
.await? | ||
.try_collect() | ||
.await | ||
} | ||
/// Executes a statement, returning the number of rows modified. | ||
/// | ||
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list | ||