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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#[cfg(feature = "etl")]
use std::net::IpAddr;

use serde::{ser::SerializeSeq, Serialize, Serializer};
use serde_json::Deserializer;
use sqlx_core::Error as SqlxError;

use crate::{
    arguments::ExaBuffer,
    options::{ExaConnectOptionsRef, ProtocolVersion},
    responses::ExaAttributes,
    ExaTypeInfo,
};

/// Enum encapsulating the various requests that can be made to the database.
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "command")]
pub(crate) enum ExaCommand<'a> {
    Disconnect,
    GetAttributes,
    SetAttributes(SetAttributes<'a>),
    Login(LoginInfo),
    LoginToken(LoginInfo),
    #[cfg(feature = "etl")]
    GetHosts(GetHosts),
    Execute(Sql<'a>),
    Fetch(Fetch),
    CloseResultSet(CloseResultSet),
    CreatePreparedStatement(Sql<'a>),
    ExecutePreparedStatement(ExecutePreparedStmt<'a>),
    ClosePreparedStatement(ClosePreparedStmt),
    #[cfg(feature = "migrate")]
    ExecuteBatch(BatchSql<'a>),
}

impl<'a> ExaCommand<'a> {
    pub fn new_set_attributes(attributes: &'a ExaAttributes) -> Self {
        Self::SetAttributes(SetAttributes { attributes })
    }

    pub fn new_login(protocol_version: ProtocolVersion) -> Self {
        Self::Login(LoginInfo { protocol_version })
    }

    pub fn new_login_token(protocol_version: ProtocolVersion) -> Self {
        Self::LoginToken(LoginInfo { protocol_version })
    }

    #[cfg(feature = "etl")]
    pub fn new_get_hosts(host_ip: IpAddr) -> Self {
        Self::GetHosts(GetHosts { host_ip })
    }

    pub fn new_execute(sql: &'a str, attributes: &'a ExaAttributes) -> Self {
        Self::Execute(Sql {
            attributes: Some(attributes),
            sql_text: sql,
        })
    }

    pub fn new_fetch(handle: u16, pos: usize, num_bytes: usize) -> Self {
        Self::Fetch(Fetch {
            result_set_handle: handle,
            start_position: pos,
            num_bytes,
        })
    }

    pub fn new_close_result(handle: u16) -> Self {
        Self::CloseResultSet(CloseResultSet {
            result_set_handles: [handle],
        })
    }

    pub fn new_create_prepared(sql: &'a str) -> Self {
        Self::CreatePreparedStatement(Sql {
            attributes: None,
            sql_text: sql,
        })
    }

    pub fn new_execute_prepared(
        handle: u16,
        columns: &'a [ExaTypeInfo],
        buf: ExaBuffer,
        attributes: &'a ExaAttributes,
    ) -> Self {
        let prepared = ExecutePreparedStmt::new(handle, columns, buf, attributes);
        Self::ExecutePreparedStatement(prepared)
    }

    pub fn new_close_prepared(handle: u16) -> Self {
        Self::ClosePreparedStatement(ClosePreparedStmt {
            statement_handle: handle,
        })
    }

    #[cfg(feature = "migrate")]
    pub fn new_execute_batch(sql_batch: Vec<&'a str>, attributes: &'a ExaAttributes) -> Self {
        Self::ExecuteBatch(BatchSql {
            attributes,
            sql_texts: sql_batch,
        })
    }
}

/// Represents a serialized command, ready to be sent to the server.
// We use this wrapper so that we can serialize the command (which would happen anyway)
// and get rid of lifetimes and borrow checker conflicts sooner.
//
// The wrapper ensures that the only ways of creating a command
// is through the implemented conversions.
#[derive(Debug)]
pub struct Command(String);

impl Command {
    pub(crate) fn into_inner(self) -> String {
        self.0
    }
}

impl<'a> TryFrom<ExaCommand<'a>> for Command {
    type Error = SqlxError;

    fn try_from(value: ExaCommand<'a>) -> Result<Self, Self::Error> {
        serde_json::to_string(&value)
            .map_err(|e| SqlxError::Protocol(e.to_string()))
            .map(Self)
    }
}

impl<'a> TryFrom<&'a ExaConnectOptionsRef<'a>> for Command {
    type Error = SqlxError;

    fn try_from(value: &'a ExaConnectOptionsRef<'a>) -> Result<Self, Self::Error> {
        serde_json::to_string(value)
            .map_err(|e| SqlxError::Protocol(e.to_string()))
            .map(Self)
    }
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct SetAttributes<'a> {
    attributes: &'a ExaAttributes,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct LoginInfo {
    protocol_version: ProtocolVersion,
}

#[cfg(feature = "etl")]
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct GetHosts {
    host_ip: IpAddr,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Sql<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    attributes: Option<&'a ExaAttributes>,
    sql_text: &'a str,
}

#[cfg(feature = "migrate")]
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct BatchSql<'a> {
    attributes: &'a ExaAttributes,
    sql_texts: Vec<&'a str>,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Fetch {
    result_set_handle: u16,
    start_position: usize,
    num_bytes: usize,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CloseResultSet {
    result_set_handles: [u16; 1],
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ExecutePreparedStmt<'a> {
    attributes: &'a ExaAttributes,
    statement_handle: u16,
    num_columns: usize,
    num_rows: usize,
    #[serde(skip_serializing_if = "<[ExaTypeInfo]>::is_empty")]
    #[serde(serialize_with = "ExecutePreparedStmt::serialize_parameters")]
    columns: &'a [ExaTypeInfo],
    #[serde(skip_serializing_if = "PreparedStmtData::is_empty")]
    data: PreparedStmtData,
}

impl<'a> ExecutePreparedStmt<'a> {
    fn new(
        handle: u16,
        columns: &'a [ExaTypeInfo],
        data: ExaBuffer,
        attributes: &'a ExaAttributes,
    ) -> Self {
        Self {
            attributes,
            statement_handle: handle,
            num_columns: columns.len(),
            num_rows: data.num_param_sets(),
            columns,
            data: data.into(),
        }
    }

    fn serialize_parameters<S>(parameters: &[ExaTypeInfo], serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq_serializer = serializer.serialize_seq(Some(parameters.len()))?;
        for param in parameters {
            seq_serializer.serialize_element(&ExaParameter::from(param))?;
        }
        seq_serializer.end()
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ExaParameter<'a> {
    data_type: &'a ExaTypeInfo,
}

impl<'a> From<&'a ExaTypeInfo> for ExaParameter<'a> {
    fn from(value: &'a ExaTypeInfo) -> Self {
        Self { data_type: value }
    }
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ClosePreparedStmt {
    statement_handle: u16,
}

/// Type containing the parameters data to be passed
/// as part of executing a prepared statement.
///
/// The type ensures the parameter sequence in the
/// [`ExaBuffer`] is appropriately ended.
#[derive(Debug, Clone)]
struct PreparedStmtData {
    inner: Vec<u8>,
    num_rows: usize,
}

impl PreparedStmtData {
    fn is_empty(&self) -> bool {
        self.num_rows == 0
    }
}

impl Serialize for PreparedStmtData {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serde_transcode::transcode(&mut Deserializer::from_slice(&self.inner), serializer)
    }
}

impl From<ExaBuffer> for PreparedStmtData {
    fn from(mut value: ExaBuffer) -> Self {
        value.finalize();

        Self {
            num_rows: value.num_param_sets(),
            inner: value.inner,
        }
    }
}