typed_statements.rs

 1use anyhow::{Context as _, Result};
 2
 3use crate::{
 4    bindable::{Bind, Column},
 5    connection::Connection,
 6    statement::Statement,
 7};
 8
 9impl Connection {
10    /// Prepare a statement which has no bindings and returns nothing.
11    ///
12    /// Note: If there are multiple statements that depend upon each other
13    /// (such as those which make schema changes), preparation will fail.
14    /// Use a true migration instead.
15    pub fn exec<'a>(&'a self, query: &str) -> Result<impl 'a + FnMut() -> Result<()>> {
16        let mut statement = Statement::prepare(self, query)?;
17        Ok(move || statement.exec())
18    }
19
20    /// Prepare a statement which takes a binding, but returns nothing.
21    /// The bindings for a given invocation should be passed to the returned
22    /// closure
23    ///
24    /// Note: If there are multiple statements that depend upon each other
25    /// (such as those which make schema changes), preparation will fail.
26    /// Use a true migration instead.
27    pub fn exec_bound<'a, B: Bind>(
28        &'a self,
29        query: &str,
30    ) -> Result<impl 'a + FnMut(B) -> Result<()>> {
31        let mut statement = Statement::prepare(self, query)?;
32        Ok(move |bindings| statement.with_bindings(&bindings)?.exec())
33    }
34
35    /// Prepare a statement which has no bindings and returns a `Vec<C>`.
36    ///
37    /// Note: If there are multiple statements that depend upon each other
38    /// (such as those which make schema changes), preparation will fail.
39    /// Use a true migration instead.
40    pub fn select<'a, C: Column>(
41        &'a self,
42        query: &str,
43    ) -> Result<impl 'a + FnMut() -> Result<Vec<C>>> {
44        let mut statement = Statement::prepare(self, query)?;
45        Ok(move || statement.rows::<C>())
46    }
47
48    /// Prepare a statement which takes a binding and returns a `Vec<C>`.
49    ///
50    /// Note: If there are multiple statements that depend upon each other
51    /// (such as those which make schema changes), preparation will fail.
52    /// Use a true migration instead.
53    pub fn select_bound<'a, B: Bind, C: Column>(
54        &'a self,
55        query: &str,
56    ) -> Result<impl 'a + FnMut(B) -> Result<Vec<C>>> {
57        let mut statement = Statement::prepare(self, query)?;
58        Ok(move |bindings| statement.with_bindings(&bindings)?.rows::<C>())
59    }
60
61    /// Prepare a statement that selects a single row from the database.
62    /// Will return none if no rows are returned and will error if more than
63    /// 1 row
64    ///
65    /// Note: If there are multiple statements that depend upon each other
66    /// (such as those which make schema changes), preparation will fail.
67    /// Use a true migration instead.
68    pub fn select_row<'a, C: Column>(
69        &'a self,
70        query: &str,
71    ) -> Result<impl 'a + FnMut() -> Result<Option<C>>> {
72        let mut statement = Statement::prepare(self, query)?;
73        Ok(move || statement.maybe_row::<C>())
74    }
75
76    /// Prepare a statement which takes a binding and selects a single row
77    /// from the database. Will return none if no rows are returned and will
78    /// error if more than 1 row is returned.
79    ///
80    /// Note: If there are multiple statements that depend upon each other
81    /// (such as those which make schema changes), preparation will fail.
82    /// Use a true migration instead.
83    pub fn select_row_bound<'a, B: Bind, C: Column>(
84        &'a self,
85        query: &str,
86    ) -> Result<impl 'a + FnMut(B) -> Result<Option<C>>> {
87        let mut statement = Statement::prepare(self, query)?;
88        Ok(move |bindings| {
89            statement
90                .with_bindings(&bindings)
91                .context("Bindings failed")?
92                .maybe_row::<C>()
93                .context("Maybe row failed")
94        })
95    }
96}