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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::arguments::Arguments;
use crate::encode::Encode;
use crate::mssql::database::Mssql;
use crate::mssql::io::MssqlBufMutExt;
use crate::mssql::protocol::rpc::StatusFlags;
use crate::types::Type;
#[derive(Default, Clone)]
pub struct MssqlArguments {
pub(crate) ordinal: usize,
name: String,
pub(crate) data: Vec<u8>,
pub(crate) declarations: String,
}
impl MssqlArguments {
pub(crate) fn add_named<'q, T: Encode<'q, Mssql> + Type<Mssql>>(
&mut self,
name: &str,
value: T,
) {
let ty = value.produces().unwrap_or_else(T::type_info);
let mut ty_name = String::new();
ty.0.fmt(&mut ty_name);
self.data.put_b_varchar(name); self.data.push(0); ty.0.put(&mut self.data); ty.0.put_value(&mut self.data, value); }
pub(crate) fn add_unnamed<'q, T: Encode<'q, Mssql> + Type<Mssql>>(&mut self, value: T) {
self.add_named("", value);
}
pub(crate) fn declare<'q, T: Encode<'q, Mssql> + Type<Mssql>>(
&mut self,
name: &str,
initial_value: T,
) {
let ty = initial_value.produces().unwrap_or_else(T::type_info);
let mut ty_name = String::new();
ty.0.fmt(&mut ty_name);
self.data.put_b_varchar(name); self.data.push(StatusFlags::BY_REF_VALUE.bits()); ty.0.put(&mut self.data); ty.0.put_value(&mut self.data, initial_value); }
pub(crate) fn append(&mut self, arguments: &mut MssqlArguments) {
self.ordinal += arguments.ordinal;
self.data.append(&mut arguments.data);
}
pub(crate) fn add<'q, T>(&mut self, value: T)
where
T: Encode<'q, Mssql> + Type<Mssql>,
{
let ty = value.produces().unwrap_or_else(T::type_info);
self.name.clear();
self.name.push_str("@p");
self.ordinal += 1;
self.name.push_str(itoa::Buffer::new().format(self.ordinal));
let MssqlArguments {
ref name,
ref mut declarations,
ref mut data,
..
} = self;
if !declarations.is_empty() {
declarations.push_str(",");
}
declarations.push_str(name);
declarations.push(' ');
ty.0.fmt(declarations);
data.put_b_varchar(name); data.push(0); ty.0.put(data); ty.0.put_value(data, value); }
}
impl<'q> Arguments<'q> for MssqlArguments {
type Database = Mssql;
fn reserve(&mut self, _additional: usize, size: usize) {
self.data.reserve(size + 10); }
fn add<T>(&mut self, value: T)
where
T: 'q + Encode<'q, Self::Database> + Type<Mssql>,
{
self.add(value)
}
}