tokio_postgres_cursor/cursor/ext.rs
1use tokio_postgres::{Error, Transaction};
2
3use crate::cursor::stream::CursorStream;
4
5/// Extension trait for
6/// [`Transaction`](https://docs.rs/tokio-postgres/latest/tokio_postgres/struct.Transaction.html)
7/// to add cursor support.
8pub trait TransactionExt {
9 /// Method to create a new [`CursorStream`] for the given query.
10 ///
11 /// Parameters:
12 /// - `query`: The SQL query for which the cursor will be declared.
13 /// - `batch_size`: The number of rows to fetch in each batch.
14 fn query_cursor<'a>(
15 &'a self,
16 query: &str,
17 batch_size: usize,
18 ) -> impl std::future::Future<Output = Result<CursorStream<'a>, Error>> + Send
19 where
20 Self: 'a;
21}
22
23/// Implementation of [`TransactionExt`] for
24/// [`Transaction`](https://docs.rs/tokio-postgres/latest/tokio_postgres/struct.Transaction.html)
25impl<'t> TransactionExt for Transaction<'t> {
26 /// Method to create a new [`CursorStream`] for the given query.
27 ///
28 /// Parameters:
29 /// - `query`: The SQL query for which the cursor will be declared.
30 /// - `batch_size`: The number of rows to fetch in each batch.
31 ///
32 /// Errors:
33 /// - Propagates
34 /// [`tokio_postgres::Error`](https://docs.rs/tokio-postgres/latest/tokio_postgres/error/struct.Error.html)
35 /// if the cursor declaration fails
36 async fn query_cursor<'a>(
37 &'a self,
38 query: &str,
39 batch_size: usize,
40 ) -> Result<CursorStream<'a>, Error>
41 where
42 Self: 'a,
43 {
44 CursorStream::new(self, query, batch_size).await
45 }
46}