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 {
    // next ordinal to be used when formatting a positional parameter name
    pub(crate) ordinal: usize,
    // temporary string buffer used to format parameter names
    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); // [ParamName]
        self.data.push(0); // [StatusFlags]

        ty.0.put(&mut self.data); // [TYPE_INFO]
        ty.0.put_value(&mut self.data, value); // [ParamLenData]
    }

    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); // [ParamName]
        self.data.push(StatusFlags::BY_REF_VALUE.bits()); // [StatusFlags]

        ty.0.put(&mut self.data); // [TYPE_INFO]
        ty.0.put_value(&mut self.data, initial_value); // [ParamLenData]
    }

    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);

        // produce an ordinal parameter name
        //  @p1, @p2, ... @pN

        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;

        // add this to our variable declaration list
        //  @p1 int, @p2 nvarchar(10), ...

        if !declarations.is_empty() {
            declarations.push_str(",");
        }

        declarations.push_str(name);
        declarations.push(' ');
        ty.0.fmt(declarations);

        // write out the parameter

        data.put_b_varchar(name); // [ParamName]
        data.push(0); // [StatusFlags]

        ty.0.put(data); // [TYPE_INFO]
        ty.0.put_value(data, value); // [ParamLenData]
    }
}

impl<'q> Arguments<'q> for MssqlArguments {
    type Database = Mssql;

    fn reserve(&mut self, _additional: usize, size: usize) {
        self.data.reserve(size + 10); // est. 4 chars for name, 1 for status, 1 for TYPE_INFO
    }

    fn add<T>(&mut self, value: T)
    where
        T: 'q + Encode<'q, Self::Database> + Type<Mssql>,
    {
        self.add(value)
    }
}