zero_postgres/sync/unnamed_portal.rs
1//! Unnamed portal for 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 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 fn exec<H: ExtendedHandler>(&mut self, max_rows: u32, handler: &mut H) -> Result<bool> {
22 self.conn.lowlevel_execute("", max_rows, handler)
23 }
24
25 /// Execute the portal and call a closure for each row.
26 ///
27 /// Returns `Ok(true)` if more rows available (PortalSuspended received).
28 /// Returns `Ok(false)` if all rows fetched (CommandComplete received).
29 pub fn exec_foreach<T: for<'b> FromRow<'b>, F: FnMut(T) -> Result<()>>(
30 &mut self,
31 max_rows: u32,
32 f: F,
33 ) -> Result<bool> {
34 let mut handler = ForEachHandler::<T, F>::new(f);
35 self.exec(max_rows, &mut handler)
36 }
37}