typed_sql/query/
prepare.rs

1use crate::{Binding, CheckedSql, ToSql};
2use std::marker::PhantomData;
3
4pub struct Prepare<'a, B, S> {
5    name: &'a str,
6    binding: PhantomData<B>,
7    stmt: S,
8}
9
10impl<'a, B, S> Prepare<'a, B, S> {
11    pub(crate) fn new(name: &'a str, stmt: S) -> Self {
12        Prepare {
13            name,
14            binding: PhantomData,
15            stmt,
16        }
17    }
18}
19
20impl<B: Binding, S: ToSql> Prepare<'_, B, S> {
21    pub fn execute(&self, binding: B) -> Execute<B> {
22        Execute {
23            name: self.name,
24            binding,
25        }
26    }
27}
28
29impl<B: Binding, S: ToSql> ToSql for Prepare<'_, B, S> {
30    fn write_sql_unchecked(&self, sql: &mut String) {
31        sql.push_str("PREPARE ");
32        sql.push_str(self.name);
33
34        B::write_types(sql);
35
36        sql.push_str(" AS ");
37        self.stmt.write_sql_unchecked(sql);
38    }
39}
40
41impl<B, S: CheckedSql> CheckedSql for Prepare<'_, B, S> {}
42
43impl<B, S: Copy> Clone for Prepare<'_, B, S> {
44    fn clone(&self) -> Self {
45        *self
46    }
47}
48
49impl<B, S: Copy> Copy for Prepare<'_, B, S> {}
50
51#[derive(Debug, Clone, Copy)]
52pub struct Execute<'a, B> {
53    name: &'a str,
54    binding: B,
55}
56
57impl<B: Binding> ToSql for Execute<'_, B> {
58    fn write_sql_unchecked(&self, sql: &mut String) {
59        sql.push_str("EXECUTE ");
60        sql.push_str(self.name);
61        sql.push('(');
62        self.binding.write_values(sql);
63        sql.push(')');
64    }
65}
66
67impl<B> CheckedSql for Execute<'_, B> {}