zero_postgres/tokio/
unnamed_portal.rs

1//! Unnamed portal for async iterative row fetching.
2
3use crate::conversion::FromRow;
4use crate::error::Result;
5use crate::handler::{ExtendedHandler, ForEachHandler};
6
7use super::Conn;
8
9/// Handle to an unnamed portal for async iterative row fetching.
10///
11/// Created by [`Conn::exec_iter()`]. Use [`exec()`](Self::exec) to retrieve rows in batches.
12pub struct UnnamedPortal<'a> {
13    pub(crate) conn: &'a mut Conn,
14}
15
16impl<'a> UnnamedPortal<'a> {
17    /// Execute the portal to fetch up to `max_rows` rows using the provided handler.
18    ///
19    /// Returns `Ok(true)` if more rows available (PortalSuspended received).
20    /// Returns `Ok(false)` if all rows fetched (CommandComplete received).
21    pub async fn exec<H: ExtendedHandler>(
22        &mut self,
23        max_rows: u32,
24        handler: &mut H,
25    ) -> Result<bool> {
26        self.conn.lowlevel_execute("", max_rows, handler).await
27    }
28
29    /// Execute the portal and call a closure for each row.
30    ///
31    /// Returns `Ok(true)` if more rows available (PortalSuspended received).
32    /// Returns `Ok(false)` if all rows fetched (CommandComplete received).
33    pub async fn exec_foreach<T: for<'b> FromRow<'b>, F: FnMut(T) -> Result<()>>(
34        &mut self,
35        max_rows: u32,
36        f: F,
37    ) -> Result<bool> {
38        let mut handler = ForEachHandler::<T, F>::new(f);
39        self.exec(max_rows, &mut handler).await
40    }
41}