Skip to content

Added information on how to use Vec<T> with Client::query_raw #568

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

Merged
merged 1 commit into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions postgres/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,33 @@ impl Client {
/// # Ok(())
/// # }
/// ```
///
/// If you have a type like `Vec<T>` where `T: ToSql` Rust will not know how to use it as params. To get around
/// this the type must explicitly be converted to `&dyn ToSql`.
///
/// ```no_run
/// # use postgres::{Client, NoTls};
/// use postgres::types::ToSql;
/// use fallible_iterator::FallibleIterator;
/// # fn main() -> Result<(), postgres::Error> {
/// # let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
///
/// let params: Vec<String> = vec![
/// "first param".into(),
/// "second param".into(),
/// ];
/// let mut it = client.query_raw(
/// "SELECT foo FROM bar WHERE biz = $1 AND baz = $2",
/// params.iter().map(|p| p as &dyn ToSql),
/// )?;
///
/// while let Some(row) = it.next()? {
/// let foo: i32 = row.get("foo");
/// println!("foo: {}", foo);
/// }
/// # Ok(())
/// # }
/// ```
pub fn query_raw<'a, T, I>(&mut self, query: &T, params: I) -> Result<RowIter<'_>, Error>
where
T: ?Sized + ToStatement,
Expand Down
28 changes: 28 additions & 0 deletions tokio-postgres/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,34 @@ impl Client {
/// Panics if the number of parameters provided does not match the number expected.
///
/// [`query`]: #method.query
///
/// # Examples
///
/// If you have a type like `Vec<T>` where `T: ToSql` Rust will not know how to use it as params. To get around
/// this the type must explicitly be converted to `&dyn ToSql`.
///
/// ```no_run
/// # async fn async_main(client: &tokio_postgres::Client) -> Result<(), tokio_postgres::Error> {
/// use tokio_postgres::types::ToSql;
/// use futures::{pin_mut, StreamExt};
///
/// let params: Vec<String> = vec![
/// "first param".into(),
/// "second param".into(),
/// ];
/// let mut it = client.query_raw(
/// "SELECT foo FROM bar WHERE biz = $1 AND baz = $2",
/// params.iter().map(|p| p as &dyn ToSql),
/// ).await?;
///
/// pin_mut!(it);
/// while let Some(row) = it.next().await.transpose()? {
/// let foo: i32 = row.get("foo");
/// println!("foo: {}", foo);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn query_raw<'a, T, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
where
T: ?Sized + ToStatement,
Expand Down