sqlx-sqlserver 0.0.1

Independent Microsoft SQL Server driver crate for SQLx.
Documentation
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use percent_encoding::percent_decode_str;
use sqlx_core::connection::ConnectOptions;
use sqlx_core::error::Error;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::Duration;
use thiserror::Error;
use url::Url;

use crate::MssqlConnection;

/// SQL Server connection encryption preference.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Encrypt {
    /// Encryption is not supported by the client.
    NotSupported,
    /// Use encryption when the server supports it.
    Off,
    /// Require encryption.
    On,
    /// Require encryption and certificate validation.
    Required,
}

/// SQL Server connection options.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MssqlConnectOptions {
    host: String,
    port: Option<u16>,
    username: String,
    password: Option<String>,
    database: String,
    instance: Option<String>,
    encrypt: Encrypt,
    trust_server_certificate: bool,
    hostname_in_certificate: Option<String>,
    ssl_root_cert: Option<PathBuf>,
    requested_packet_size: u32,
    client_program_version: u32,
    client_pid: u32,
    hostname: String,
    app_name: String,
    server_name: String,
    client_interface_name: String,
    language: String,
}

impl Default for MssqlConnectOptions {
    fn default() -> Self {
        Self::new()
    }
}

impl MssqlConnectOptions {
    /// Creates options with SQL Server defaults.
    pub fn new() -> Self {
        Self {
            host: "localhost".to_owned(),
            port: None,
            username: "sa".to_owned(),
            password: None,
            database: "master".to_owned(),
            instance: None,
            encrypt: Encrypt::On,
            trust_server_certificate: true,
            hostname_in_certificate: None,
            ssl_root_cert: None,
            requested_packet_size: 4096,
            client_program_version: 0,
            client_pid: 0,
            hostname: String::new(),
            app_name: String::new(),
            server_name: String::new(),
            client_interface_name: String::new(),
            language: String::new(),
        }
    }

    /// Parses SQL Server connection options while preserving detailed parser errors.
    pub fn parse_url(input: &str) -> Result<Self, MssqlInvalidOption> {
        parse_url(input)
    }

    /// Returns the configured host.
    pub fn host(&self) -> &str {
        &self.host
    }

    /// Returns the configured port, if one was explicitly set.
    pub fn port(&self) -> Option<u16> {
        self.port
    }

    /// Returns the configured username.
    pub fn username(&self) -> &str {
        &self.username
    }

    /// Returns the configured password.
    pub fn password(&self) -> Option<&str> {
        self.password.as_deref()
    }

    /// Returns the configured database.
    pub fn database(&self) -> &str {
        &self.database
    }

    /// Returns the configured named instance, if any.
    pub fn instance(&self) -> Option<&str> {
        self.instance.as_deref()
    }

    /// Returns the encryption preference.
    pub fn encrypt(&self) -> Encrypt {
        self.encrypt
    }

    /// Returns whether server certificate validation is bypassed.
    pub fn trust_server_certificate(&self) -> bool {
        self.trust_server_certificate
    }

    /// Returns the hostname expected in the server certificate.
    pub fn hostname_in_certificate(&self) -> Option<&str> {
        self.hostname_in_certificate.as_deref()
    }

    /// Returns the configured root certificate path.
    pub fn ssl_root_cert(&self) -> Option<&Path> {
        self.ssl_root_cert.as_deref()
    }

    /// Returns the requested TDS packet size.
    pub fn requested_packet_size(&self) -> u32 {
        self.requested_packet_size
    }

    /// Returns the client program version sent during login.
    pub fn client_program_version(&self) -> u32 {
        self.client_program_version
    }

    /// Returns the client process ID sent during login.
    pub fn client_pid(&self) -> u32 {
        self.client_pid
    }

    /// Returns the client host name sent during login.
    pub fn hostname(&self) -> &str {
        &self.hostname
    }

    /// Returns the application name sent during login.
    pub fn app_name(&self) -> &str {
        &self.app_name
    }

    /// Returns the server name sent during login.
    pub fn server_name(&self) -> &str {
        &self.server_name
    }

    /// Returns the client interface name sent during login.
    pub fn client_interface_name(&self) -> &str {
        &self.client_interface_name
    }

    /// Returns the language sent during login.
    pub fn language(&self) -> &str {
        &self.language
    }

    fn set_requested_packet_size(&mut self, size: u32) -> Result<(), MssqlInvalidOption> {
        if size < 512 {
            return Err(MssqlInvalidOption::InvalidValue {
                key: "packet_size".to_owned(),
                value: size.to_string(),
                message: "packet_size must be at least 512 bytes".to_owned(),
            });
        }

        self.requested_packet_size = size;
        Ok(())
    }

    #[cfg(test)]
    pub(crate) fn set_hostname_for_test(&mut self, hostname: String) {
        self.hostname = hostname;
    }

    #[cfg(feature = "migrate")]
    pub(crate) fn set_database_for_maintenance(&mut self) {
        self.database = "master".to_owned();
    }
}

impl FromStr for MssqlConnectOptions {
    type Err = Error;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        Self::parse_url(input).map_err(Error::config)
    }
}

impl ConnectOptions for MssqlConnectOptions {
    type Connection = MssqlConnection;

    fn from_url(url: &Url) -> Result<Self, Error> {
        Self::parse_url(url.as_str()).map_err(Error::config)
    }

    async fn connect(&self) -> Result<Self::Connection, Error>
    where
        Self::Connection: Sized,
    {
        MssqlConnection::establish(self).await
    }

    fn log_statements(self, _level: log::LevelFilter) -> Self {
        self
    }

    fn log_slow_statements(self, _level: log::LevelFilter, _duration: Duration) -> Self {
        self
    }
}

fn parse_url(input: &str) -> Result<MssqlConnectOptions, MssqlInvalidOption> {
    let url = Url::parse(input).map_err(MssqlInvalidOption::Url)?;
    match url.scheme() {
        "mssql" | "sqlserver" => {}
        scheme => return Err(MssqlInvalidOption::UnsupportedScheme(scheme.to_owned())),
    }

    let mut options = MssqlConnectOptions::new();

    if let Some(host) = url.host_str() {
        options.host = host.to_owned();
    }

    options.port = url.port();

    let username = url.username();
    if !username.is_empty() {
        options.username = percent_decode_str(username)
            .decode_utf8()
            .map_err(MssqlInvalidOption::Utf8)?
            .into_owned();
    }

    if let Some(password) = url.password() {
        options.password = Some(
            percent_decode_str(password)
                .decode_utf8()
                .map_err(MssqlInvalidOption::Utf8)?
                .into_owned(),
        );
    }

    let path = url.path().trim_start_matches('/');
    if !path.is_empty() {
        options.database = percent_decode_str(path)
            .decode_utf8()
            .map_err(MssqlInvalidOption::Utf8)?
            .into_owned();
    }

    for (key, value) in url.query_pairs() {
        match key.as_ref() {
            "instance" => options.instance = Some(value.into_owned()),
            "encrypt" => {
                options.encrypt =
                    parse_encrypt(&value).ok_or_else(|| MssqlInvalidOption::InvalidValue {
                        key: "encrypt".to_owned(),
                        value: value.into_owned(),
                        message: "expected strict, mandatory, optional, not_supported, true, false, yes, or no"
                            .to_owned(),
                    })?;
            }
            "sslrootcert" | "ssl-root-cert" | "ssl-ca" => {
                options.ssl_root_cert = Some(PathBuf::from(value.as_ref()));
            }
            "trust_server_certificate" => {
                options.trust_server_certificate =
                    parse_bool(&value).ok_or_else(|| MssqlInvalidOption::InvalidValue {
                        key: key.into_owned(),
                        value: value.into_owned(),
                        message: "expected true, false, yes, or no".to_owned(),
                    })?;
            }
            "hostname_in_certificate" => {
                options.hostname_in_certificate = Some(value.into_owned());
            }
            "packet_size" => {
                let size = value
                    .parse()
                    .map_err(|_| MssqlInvalidOption::InvalidValue {
                        key: "packet_size".to_owned(),
                        value: value.to_string(),
                        message: "expected an integer".to_owned(),
                    })?;
                options.set_requested_packet_size(size)?;
            }
            "client_program_version" => options.client_program_version = parse_u32(&key, &value)?,
            "client_pid" => options.client_pid = parse_u32(&key, &value)?,
            "hostname" => options.hostname = value.into_owned(),
            "app_name" => options.app_name = value.into_owned(),
            "server_name" => options.server_name = value.into_owned(),
            "client_interface_name" => options.client_interface_name = value.into_owned(),
            "language" => options.language = value.into_owned(),
            _ => return Err(MssqlInvalidOption::UnknownOption(key.into_owned())),
        }
    }

    Ok(options)
}

fn parse_encrypt(value: &str) -> Option<Encrypt> {
    match value.to_ascii_lowercase().as_str() {
        "strict" => Some(Encrypt::Required),
        "mandatory" | "true" | "yes" => Some(Encrypt::On),
        "optional" | "false" | "no" => Some(Encrypt::Off),
        "not_supported" => Some(Encrypt::NotSupported),
        _ => None,
    }
}

fn parse_bool(value: &str) -> Option<bool> {
    match value.to_ascii_lowercase().as_str() {
        "true" | "yes" => Some(true),
        "false" | "no" => Some(false),
        _ => None,
    }
}

fn parse_u32(key: &str, value: &str) -> Result<u32, MssqlInvalidOption> {
    value.parse().map_err(|_| MssqlInvalidOption::InvalidValue {
        key: key.to_owned(),
        value: value.to_owned(),
        message: "expected an integer".to_owned(),
    })
}

/// Error returned while parsing SQL Server connection options.
#[derive(Debug, Error)]
pub enum MssqlInvalidOption {
    /// URL syntax was invalid.
    #[error("invalid SQL Server URL: {0}")]
    Url(#[from] url::ParseError),
    /// Percent-decoded URL component was not valid UTF-8.
    #[error("invalid UTF-8 in SQL Server URL component: {0}")]
    Utf8(#[from] std::str::Utf8Error),
    /// The URL scheme is not supported by this driver.
    #[error("unsupported SQL Server URL scheme `{0}`")]
    UnsupportedScheme(String),
    /// A query parameter is not recognized.
    #[error("unknown SQL Server connection option `{0}`")]
    UnknownOption(String),
    /// A query parameter value is invalid.
    #[error("invalid value `{value}` for SQL Server connection option `{key}`: {message}")]
    InvalidValue {
        /// Option name.
        key: String,
        /// Option value.
        value: String,
        /// Validation message.
        message: String,
    },
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_username_with_at_sign() {
        let opts =
            MssqlConnectOptions::parse_url("mssql://user%40domain:secret@example.com/database")
                .unwrap();

        assert_eq!("user@domain", opts.username());
        assert_eq!(Some("secret"), opts.password());
    }

    #[test]
    fn parses_password_with_at_sign() {
        let opts =
            MssqlConnectOptions::parse_url("mssql://username:p%40ssw0rd@example.com/database")
                .unwrap();

        assert_eq!(Some("p@ssw0rd"), opts.password());
    }

    #[test]
    fn parses_named_instance_without_resolving_port() {
        let opts = MssqlConnectOptions::parse_url(
            "mssql://sa:secret@example.com/master?instance=SQLEXPRESS",
        )
        .unwrap();

        assert_eq!("example.com", opts.host());
        assert_eq!(None, opts.port());
        assert_eq!(Some("SQLEXPRESS"), opts.instance());
    }

    #[test]
    fn keeps_explicit_port_with_named_instance() {
        let opts = MssqlConnectOptions::parse_url(
            "mssql://sa:secret@example.com:1434/master?instance=SQLEXPRESS",
        )
        .unwrap();

        assert_eq!(Some(1434), opts.port());
        assert_eq!(Some("SQLEXPRESS"), opts.instance());
    }

    #[test]
    fn parses_encryption_options() {
        let strict =
            MssqlConnectOptions::parse_url("mssql://localhost/master?encrypt=strict").unwrap();
        let optional =
            MssqlConnectOptions::parse_url("mssql://localhost/master?encrypt=optional").unwrap();
        let disabled =
            MssqlConnectOptions::parse_url("mssql://localhost/master?encrypt=not_supported")
                .unwrap();

        assert_eq!(Encrypt::Required, strict.encrypt());
        assert_eq!(Encrypt::Off, optional.encrypt());
        assert_eq!(Encrypt::NotSupported, disabled.encrypt());
    }

    #[test]
    fn rejects_invalid_packet_size() {
        let err = MssqlConnectOptions::parse_url("mssql://localhost/master?packet_size=128")
            .expect_err("packet_size below 512 should be rejected");

        assert!(err.to_string().contains("packet_size"));
    }

    #[test]
    fn rejects_unknown_options() {
        let err = MssqlConnectOptions::parse_url("mssql://localhost/master?mars=true")
            .expect_err("unsupported options should fail loudly");

        assert!(matches!(err, MssqlInvalidOption::UnknownOption(_)));
    }
}