diff --git a/postgres/src/client.rs b/postgres/src/client.rs index fb76f0f3..0a3a51e1 100644 --- a/postgres/src/client.rs +++ b/postgres/src/client.rs @@ -255,6 +255,33 @@ impl Client { /// # Ok(()) /// # } /// ``` + /// + /// If you have a type like `Vec` 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 = 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, Error> where T: ?Sized + ToStatement, diff --git a/tokio-postgres/src/client.rs b/tokio-postgres/src/client.rs index 4cd0b31c..7f622c56 100644 --- a/tokio-postgres/src/client.rs +++ b/tokio-postgres/src/client.rs @@ -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` 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 = 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 where T: ?Sized + ToStatement,