sqlx_core_oldapi/mssql/options/
mod.rs

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
use std::path::Path;

use super::protocol::pre_login::Encrypt;
use crate::{connection::LogSettings, net::CertificateInput};

mod connect;
mod parse;

/// Options and flags which can be used to configure a Microsoft SQL Server connection.
///
/// Connection strings should be in the form:
/// ```text
/// mssql://[username[:password]@]host/database[?instance=instance_name&packet_size=packet_size&client_program_version=client_program_version&client_pid=client_pid&hostname=hostname&app_name=app_name&server_name=server_name&client_interface_name=client_interface_name&language=language]
/// ```
#[derive(Debug, Clone)]
pub struct MssqlConnectOptions {
    pub(crate) host: String,
    pub(crate) port: u16,
    pub(crate) username: String,
    pub(crate) database: String,
    pub(crate) password: Option<String>,
    pub(crate) instance: Option<String>,
    pub(crate) log_settings: LogSettings,
    pub(crate) client_program_version: u32,
    pub(crate) client_pid: u32,
    pub(crate) hostname: String,
    pub(crate) app_name: String,
    pub(crate) server_name: String,
    pub(crate) client_interface_name: String,
    pub(crate) language: String,
    /// Size in bytes of TDS packets to exchange with the server
    pub(crate) requested_packet_size: u32,
    pub(crate) encrypt: Encrypt,
    pub(crate) trust_server_certificate: bool,
    pub(crate) hostname_in_certificate: Option<String>,
    pub(crate) ssl_root_cert: Option<CertificateInput>,
}

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

impl MssqlConnectOptions {
    pub fn new() -> Self {
        Self {
            port: 1433,
            host: String::from("localhost"),
            database: String::from("master"),
            username: String::from("sa"),
            password: None,
            instance: None,
            log_settings: Default::default(),
            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(),
            encrypt: Encrypt::On,
            trust_server_certificate: true,
            hostname_in_certificate: None,
            ssl_root_cert: None,
        }
    }

    pub fn host(mut self, host: &str) -> Self {
        self.host = host.to_owned();
        self
    }

    pub fn port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }

    pub fn username(mut self, username: &str) -> Self {
        self.username = username.to_owned();
        self
    }

    pub fn password(mut self, password: &str) -> Self {
        self.password = Some(password.to_owned());
        self
    }

    pub fn database(mut self, database: &str) -> Self {
        self.database = database.to_owned();
        self
    }

    pub fn instance(mut self, instance: &str) -> Self {
        self.instance = Some(instance.to_owned());
        self
    }

    pub fn client_program_version(mut self, client_program_version: u32) -> Self {
        self.client_program_version = client_program_version;
        self
    }

    pub fn client_pid(mut self, client_pid: u32) -> Self {
        self.client_pid = client_pid;
        self
    }

    pub fn hostname(mut self, hostname: &str) -> Self {
        self.hostname = hostname.to_owned();
        self
    }

    pub fn app_name(mut self, app_name: &str) -> Self {
        self.app_name = app_name.to_owned();
        self
    }

    pub fn server_name(mut self, server_name: &str) -> Self {
        self.server_name = server_name.to_owned();
        self
    }

    pub fn client_interface_name(mut self, client_interface_name: &str) -> Self {
        self.client_interface_name = client_interface_name.to_owned();
        self
    }

    pub fn language(mut self, language: &str) -> Self {
        self.language = language.to_owned();
        self
    }

    /// Size in bytes of TDS packets to exchange with the server.
    /// Returns an error if the size is smaller than 512 bytes
    pub fn requested_packet_size(mut self, size: u32) -> Result<Self, Self> {
        if size < 512 {
            Err(self)
        } else {
            self.requested_packet_size = size;
            Ok(self)
        }
    }

    pub fn encrypt(mut self, encrypt: Encrypt) -> Self {
        self.encrypt = encrypt;
        self
    }

    pub fn trust_server_certificate(mut self, trust: bool) -> Self {
        self.trust_server_certificate = trust;
        self
    }

    pub fn hostname_in_certificate(mut self, hostname: &str) -> Self {
        self.hostname_in_certificate = Some(hostname.to_owned());
        self
    }

    /// Sets the name of a file containing SSL certificate authority (CA) certificate(s).
    /// If the file exists, the server's certificate will be verified to be signed by
    /// one of these authorities.
    pub fn ssl_root_cert(mut self, cert: impl AsRef<Path>) -> Self {
        self.ssl_root_cert = Some(CertificateInput::File(cert.as_ref().to_path_buf()));
        self
    }
}