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
68
69
70
pub use tokio_postgres::{Statement, ToStatement};
use crate::ToSql;
use af_core::prelude::*;
use smallstr::SmallString;
use smallvec::SmallVec;
#[derive(Debug)]
pub struct StatementBuilder<'a> {
text: SmallString<[u8; 2048]>,
params: SmallVec<[&'a (dyn ToSql + Sync); 32]>,
}
impl<'a> StatementBuilder<'a> {
pub fn new() -> Self {
Self { text: SmallString::new(), params: SmallVec::new() }
}
pub fn append(&mut self, text: impl Display) {
write!(self.text, "{}", text).unwrap();
}
pub fn add_param(&mut self, param: &'a (dyn ToSql + Sync)) -> usize {
self.params.push(param);
self.params.len()
}
pub fn params(&self) -> &[&'a (dyn ToSql + Sync)] {
&self.params
}
pub fn text(&self) -> &str {
&self.text
}
}
impl Default for StatementBuilder<'_> {
fn default() -> Self {
Self::new()
}
}
impl<'a> fmt::Write for StatementBuilder<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.text.write_str(s)
}
}