Skip to main content

cartel_sqlite/engine/
cursor.rs

1use crate::{api, catalogs, row};
2use api::exec;
3use dope_fiber::abi;
4use std::{marker, mem, ops, result};
5
6/// A bounded row scan tied to the connection or transaction that issued it.
7/// Borrowed rows are valid only for one visitor invocation and cannot be
8/// retained by a later invocation.
9pub struct Cursor<
10    'query,
11    'connection: 'query,
12    'd: 'connection,
13    R,
14    Q: catalogs::TypedQuery,
15    P = (),
16    I: catalogs::Catalog = catalogs::Empty,
17> where
18    I: catalogs::HasQuery<Q>,
19{
20    machine: exec::Machine<'query, 'connection, 'd, I, P, catalogs::Statement<I, Q>>,
21    marker: marker::PhantomData<fn() -> R>,
22}
23
24impl<'query, 'connection: 'query, 'd: 'connection, R, Q, P, I>
25    Cursor<'query, 'connection, 'd, R, Q, P, I>
26where
27    Q: catalogs::TypedQuery,
28    I: catalogs::Catalog + catalogs::HasQuery<Q>,
29{
30    const LAYOUT: () = assert!(
31        mem::size_of::<Self>()
32            == mem::size_of::<
33                exec::Machine<'query, 'connection, 'd, I, P, catalogs::Statement<I, Q>>,
34            >()
35    );
36
37    pub(super) const fn new(
38        machine: exec::Machine<'query, 'connection, 'd, I, P, catalogs::Statement<I, Q>>,
39    ) -> Self {
40        let () = Self::LAYOUT;
41        Self {
42            machine,
43            marker: marker::PhantomData,
44        }
45    }
46}
47
48impl<
49    'query,
50    'connection: 'query,
51    'd: 'connection,
52    R: row::raw::Decode,
53    Q: catalogs::TypedQuery,
54    P: api::Bind,
55    I: catalogs::Catalog + catalogs::HasQuery<Q>,
56> Cursor<'query, 'connection, 'd, R, Q, P, I>
57{
58    pub fn visit<V>(
59        self,
60        visitor: V,
61    ) -> impl abi::Fiber<'d, Output = result::Result<(), crate::Error>>
62    + use<'query, 'connection, 'd, R, Q, P, I, V>
63    where
64        V: for<'row> FnMut(R::Borrowed<'row>) -> ops::ControlFlow<()>,
65    {
66        exec::Rows::<I, Q, R, P, _>::new(self.machine, exec::Visit::new(visitor))
67    }
68}