Skip to content

Commit 3cb4e29

Browse files
committed
Added information on how to use Vec<T> with Client::query_raw (fixes #567)
1 parent cc9b823 commit 3cb4e29

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

postgres/src/client.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,33 @@ impl Client {
255255
/// # Ok(())
256256
/// # }
257257
/// ```
258+
///
259+
/// If you have a type like `Vec<T>` where `T: ToSql` Rust will not know how to use it as params. To get around
260+
/// this the type must explicitly be converted to `&dyn ToSql`.
261+
///
262+
/// ```no_run
263+
/// # use postgres::{Client, NoTls};
264+
/// use postgres::types::ToSql;
265+
/// use fallible_iterator::FallibleIterator;
266+
/// # fn main() -> Result<(), postgres::Error> {
267+
/// # let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
268+
///
269+
/// let params: Vec<String> = vec![
270+
/// "first param".into(),
271+
/// "second param".into(),
272+
/// ];
273+
/// let mut it = client.query_raw(
274+
/// "SELECT foo FROM bar WHERE biz = $1 AND baz = $2",
275+
/// params.iter().map(|p| p as &dyn ToSql),
276+
/// )?;
277+
///
278+
/// while let Some(row) = it.next()? {
279+
/// let foo: i32 = row.get("foo");
280+
/// println!("foo: {}", foo);
281+
/// }
282+
/// # Ok(())
283+
/// # }
284+
/// ```
258285
pub fn query_raw<'a, T, I>(&mut self, query: &T, params: I) -> Result<RowIter<'_>, Error>
259286
where
260287
T: ?Sized + ToStatement,

tokio-postgres/src/client.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,36 @@ impl Client {
313313
/// Panics if the number of parameters provided does not match the number expected.
314314
///
315315
/// [`query`]: #method.query
316+
///
317+
/// # Examples
318+
///
319+
/// If you have a type like `Vec<T>` where `T: ToSql` Rust will not know how to use it as params. To get around
320+
/// this the type must explicitly be converted to `&dyn ToSql`.
321+
///
322+
/// ```no_run
323+
/// # { #![cfg(feature = "runtime")] // tokio_postgres::connect doesn't exist without "runtime"
324+
/// # use tokio_postgres::{Client, NoTls};
325+
/// # async fn async_main() -> Result<(), tokio_postgres::Error> {
326+
/// # let (client, _) = tokio_postgres::connect("host=localhost user=postgres", NoTls).await?;
327+
/// use tokio_postgres::types::ToSql;
328+
/// use futures::StreamExt;
329+
///
330+
/// let params: Vec<String> = vec![
331+
/// "first param".into(),
332+
/// "second param".into(),
333+
/// ];
334+
/// let mut it = Box::pin(client.query_raw(
335+
/// "SELECT foo FROM bar WHERE biz = $1 AND baz = $2",
336+
/// params.iter().map(|p| p as &dyn ToSql),
337+
/// ).await?);
338+
///
339+
/// while let Some(row) = it.next().await.transpose()? {
340+
/// let foo: i32 = row.get("foo");
341+
/// println!("foo: {}", foo);
342+
/// }
343+
/// # Ok(())
344+
/// # } }
345+
/// ```
316346
pub async fn query_raw<'a, T, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
317347
where
318348
T: ?Sized + ToStatement,

0 commit comments

Comments
 (0)