Skip to main content

cartel_sqlite/engine/api/exec/
query.rs

1use crate::Cursor;
2use crate::{api, catalogs, row};
3use api::{batches, exec};
4use dope_fiber::abi;
5use std::result;
6
7pub struct Query<
8    'query,
9    'connection: 'query,
10    'd: 'connection,
11    I: catalogs::Catalog = catalogs::Empty,
12> {
13    start: exec::Start<'query, 'connection, 'd, I>,
14    arena: &'query mut batches::Arena<'connection>,
15}
16
17impl<'query, 'connection: 'query, 'd: 'connection, I: catalogs::Catalog>
18    Query<'query, 'connection, 'd, I>
19{
20    pub(crate) const fn new(
21        start: exec::Start<'query, 'connection, 'd, I>,
22        arena: &'query mut batches::Arena<'connection>,
23    ) -> Self {
24        Self { start, arena }
25    }
26
27    #[doc(hidden)]
28    pub fn run_no_rows<Q: catalogs::TypedQuery, P: api::Bind>(
29        self,
30        stmt: catalogs::Statement<I, Q>,
31        params: P,
32    ) -> impl abi::Fiber<'d, Output = result::Result<(), crate::Error>>
33    + use<'query, 'connection, 'd, I, Q, P>
34    where
35        I: catalogs::HasQuery<Q>,
36    {
37        exec::Command::new(self.start.run(stmt, params))
38    }
39
40    #[doc(hidden)]
41    pub fn run_one<R: row::raw::Decode + 'query, P: api::Bind, Q: catalogs::TypedQuery>(
42        self,
43        stmt: catalogs::Statement<I, Q>,
44        params: P,
45    ) -> impl abi::Fiber<'d, Output = result::Result<R::Borrowed<'query>, crate::Error>>
46    + use<'query, 'connection, 'd, I, R, P, Q>
47    where
48        I: catalogs::HasQuery<Q>,
49    {
50        let machine = self.start.run(stmt, params);
51        let create = exec::Single::<R, exec::One>::new(self.arena);
52        exec::Rows::new(machine, create)
53    }
54
55    #[doc(hidden)]
56    pub fn run_first<R: row::raw::Decode + 'query, P: api::Bind, Q: catalogs::TypedQuery>(
57        self,
58        stmt: catalogs::Statement<I, Q>,
59        params: P,
60    ) -> impl abi::Fiber<'d, Output = result::Result<Option<R::Borrowed<'query>>, crate::Error>>
61    + use<'query, 'connection, 'd, I, R, P, Q>
62    where
63        I: catalogs::HasQuery<Q>,
64    {
65        let machine = self.start.run(stmt, params);
66        let create = exec::Single::<R, exec::First>::new(self.arena);
67        exec::Rows::new(machine, create)
68    }
69
70    #[doc(hidden)]
71    pub fn run_all<R: row::raw::Decode + 'query, P: api::Bind, Q: catalogs::TypedQuery>(
72        self,
73        stmt: catalogs::Statement<I, Q>,
74        params: P,
75    ) -> impl abi::Fiber<'d, Output = result::Result<batches::Rows<'query, R>, crate::Error>>
76    + use<'query, 'connection, 'd, I, R, P, Q>
77    where
78        I: catalogs::HasQuery<Q>,
79    {
80        let machine = self.start.run(stmt, params);
81        exec::Rows::new(machine, exec::Collect::<R>::new(self.arena))
82    }
83
84    #[doc(hidden)]
85    pub fn stream<R: row::raw::Decode, P: api::Bind, Q: catalogs::TypedQuery>(
86        self,
87        stmt: catalogs::Statement<I, Q>,
88        params: P,
89    ) -> Cursor<'query, 'connection, 'd, R, Q, P, I>
90    where
91        I: catalogs::HasQuery<Q>,
92    {
93        Cursor::new(self.start.run(stmt, params))
94    }
95}