Skip to content

Commit 14252af

Browse files
authored
Merge pull request #568 from runfalk/master
Added information on how to use Vec<T> with Client::query_raw
2 parents cc9b823 + 778f278 commit 14252af

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,34 @@ 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+
/// # async fn async_main(client: &tokio_postgres::Client) -> Result<(), tokio_postgres::Error> {
324+
/// use tokio_postgres::types::ToSql;
325+
/// use futures::{pin_mut, StreamExt};
326+
///
327+
/// let params: Vec<String> = vec![
328+
/// "first param".into(),
329+
/// "second param".into(),
330+
/// ];
331+
/// let mut it = client.query_raw(
332+
/// "SELECT foo FROM bar WHERE biz = $1 AND baz = $2",
333+
/// params.iter().map(|p| p as &dyn ToSql),
334+
/// ).await?;
335+
///
336+
/// pin_mut!(it);
337+
/// while let Some(row) = it.next().await.transpose()? {
338+
/// let foo: i32 = row.get("foo");
339+
/// println!("foo: {}", foo);
340+
/// }
341+
/// # Ok(())
342+
/// # }
343+
/// ```
316344
pub async fn query_raw<'a, T, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
317345
where
318346
T: ?Sized + ToStatement,

0 commit comments

Comments
 (0)