sqlx_core_oldapi/mssql/options/
mod.rs

1use std::path::Path;
2
3use super::protocol::pre_login::Encrypt;
4use crate::{connection::LogSettings, net::CertificateInput};
5
6mod connect;
7mod parse;
8
9/// Options and flags which can be used to configure a Microsoft SQL Server connection.
10///
11/// Connection strings should be in the form:
12/// ```text
13/// 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]
14/// ```
15#[derive(Debug, Clone)]
16pub struct MssqlConnectOptions {
17    pub(crate) host: String,
18    pub(crate) port: u16,
19    pub(crate) username: String,
20    pub(crate) database: String,
21    pub(crate) password: Option<String>,
22    pub(crate) instance: Option<String>,
23    pub(crate) log_settings: LogSettings,
24    pub(crate) client_program_version: u32,
25    pub(crate) client_pid: u32,
26    pub(crate) hostname: String,
27    pub(crate) app_name: String,
28    pub(crate) server_name: String,
29    pub(crate) client_interface_name: String,
30    pub(crate) language: String,
31    /// Size in bytes of TDS packets to exchange with the server
32    pub(crate) requested_packet_size: u32,
33    pub(crate) encrypt: Encrypt,
34    pub(crate) trust_server_certificate: bool,
35    pub(crate) hostname_in_certificate: Option<String>,
36    pub(crate) ssl_root_cert: Option<CertificateInput>,
37}
38
39impl Default for MssqlConnectOptions {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45impl MssqlConnectOptions {
46    pub fn new() -> Self {
47        Self {
48            port: 1433,
49            host: String::from("localhost"),
50            database: String::from("master"),
51            username: String::from("sa"),
52            password: None,
53            instance: None,
54            log_settings: Default::default(),
55            requested_packet_size: 4096,
56            client_program_version: 0,
57            client_pid: 0,
58            hostname: String::new(),
59            app_name: String::new(),
60            server_name: String::new(),
61            client_interface_name: String::new(),
62            language: String::new(),
63            encrypt: Encrypt::On,
64            trust_server_certificate: true,
65            hostname_in_certificate: None,
66            ssl_root_cert: None,
67        }
68    }
69
70    pub fn host(mut self, host: &str) -> Self {
71        self.host = host.to_owned();
72        self
73    }
74
75    pub fn port(mut self, port: u16) -> Self {
76        self.port = port;
77        self
78    }
79
80    pub fn username(mut self, username: &str) -> Self {
81        self.username = username.to_owned();
82        self
83    }
84
85    pub fn password(mut self, password: &str) -> Self {
86        self.password = Some(password.to_owned());
87        self
88    }
89
90    pub fn database(mut self, database: &str) -> Self {
91        self.database = database.to_owned();
92        self
93    }
94
95    pub fn instance(mut self, instance: &str) -> Self {
96        self.instance = Some(instance.to_owned());
97        self
98    }
99
100    pub fn client_program_version(mut self, client_program_version: u32) -> Self {
101        self.client_program_version = client_program_version;
102        self
103    }
104
105    pub fn client_pid(mut self, client_pid: u32) -> Self {
106        self.client_pid = client_pid;
107        self
108    }
109
110    pub fn hostname(mut self, hostname: &str) -> Self {
111        self.hostname = hostname.to_owned();
112        self
113    }
114
115    pub fn app_name(mut self, app_name: &str) -> Self {
116        self.app_name = app_name.to_owned();
117        self
118    }
119
120    pub fn server_name(mut self, server_name: &str) -> Self {
121        self.server_name = server_name.to_owned();
122        self
123    }
124
125    pub fn client_interface_name(mut self, client_interface_name: &str) -> Self {
126        self.client_interface_name = client_interface_name.to_owned();
127        self
128    }
129
130    pub fn language(mut self, language: &str) -> Self {
131        self.language = language.to_owned();
132        self
133    }
134
135    /// Size in bytes of TDS packets to exchange with the server.
136    /// Returns an error if the size is smaller than 512 bytes
137    pub fn requested_packet_size(mut self, size: u32) -> Result<Self, Self> {
138        if size < 512 {
139            Err(self)
140        } else {
141            self.requested_packet_size = size;
142            Ok(self)
143        }
144    }
145
146    pub fn encrypt(mut self, encrypt: Encrypt) -> Self {
147        self.encrypt = encrypt;
148        self
149    }
150
151    pub fn trust_server_certificate(mut self, trust: bool) -> Self {
152        self.trust_server_certificate = trust;
153        self
154    }
155
156    pub fn hostname_in_certificate(mut self, hostname: &str) -> Self {
157        self.hostname_in_certificate = Some(hostname.to_owned());
158        self
159    }
160
161    /// Sets the name of a file containing SSL certificate authority (CA) certificate(s).
162    /// If the file exists, the server's certificate will be verified to be signed by
163    /// one of these authorities.
164    pub fn ssl_root_cert(mut self, cert: impl AsRef<Path>) -> Self {
165        self.ssl_root_cert = Some(CertificateInput::File(cert.as_ref().to_path_buf()));
166        self
167    }
168}