1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::{Binding, CheckedSql, ToSql};
use std::marker::PhantomData;

pub struct Prepare<'a, B, S> {
    name: &'a str,
    binding: PhantomData<B>,
    stmt: S,
}

impl<'a, B, S> Prepare<'a, B, S> {
    pub(crate) fn new(name: &'a str, stmt: S) -> Self {
        Prepare {
            name,
            binding: PhantomData,
            stmt,
        }
    }
}

impl<B: Binding, S: ToSql> Prepare<'_, B, S> {
    pub fn execute(&self, binding: B) -> Execute<B> {
        Execute {
            name: self.name,
            binding,
        }
    }
}

impl<B: Binding, S: ToSql> ToSql for Prepare<'_, B, S> {
    fn write_sql_unchecked(&self, sql: &mut String) {
        sql.push_str("PREPARE ");
        sql.push_str(self.name);

        B::write_types(sql);

        sql.push_str(" AS ");
        self.stmt.write_sql_unchecked(sql);
    }
}

impl<B, S: CheckedSql> CheckedSql for Prepare<'_, B, S> {}

impl<B, S: Copy> Clone for Prepare<'_, B, S> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<B, S: Copy> Copy for Prepare<'_, B, S> {}

#[derive(Debug, Clone, Copy)]
pub struct Execute<'a, B> {
    name: &'a str,
    binding: B,
}

impl<B: Binding> ToSql for Execute<'_, B> {
    fn write_sql_unchecked(&self, sql: &mut String) {
        sql.push_str("EXECUTE ");
        sql.push_str(self.name);
        sql.push('(');
        self.binding.write_values(sql);
        sql.push(')');
    }
}

impl<B> CheckedSql for Execute<'_, B> {}