use crate::{ExecResult, PhysicalProjectRows, PhysicalRow};
use uqa_sql::SQLError;
type StartCall<'a> = Box<dyn FnOnce() -> Result<PhysicalProjectRows, SQLError> + Send + 'a>;
pub struct DeferredTableFunctionRows<'a> {
start: Option<StartCall<'a>>,
rows: Option<PhysicalProjectRows>,
}
impl<'a> DeferredTableFunctionRows<'a> {
pub fn new(start: impl FnOnce() -> Result<PhysicalProjectRows, SQLError> + Send + 'a) -> Self {
Self {
start: Some(Box::new(start)),
rows: None,
}
}
}
impl Iterator for DeferredTableFunctionRows<'_> {
type Item = ExecResult<PhysicalRow>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(start) = self.start.take() {
match start() {
Ok(rows) => self.rows = Some(rows),
Err(error) => return Some(Err(error.into())),
}
}
self.rows.as_mut()?.next()
}
}
#[cfg(test)]
mod tests;