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[:port]/database[?param1=value1&param2=value2...]
14/// ```
15///
16/// Port resolution priority:
17/// 1. If an explicit port is specified, it is always used
18/// 2. If a named instance is specified via `?instance=NAME`, the port is discovered via SSRP
19/// 3. Otherwise, the default port 1433 is used
20///
21/// Example with named instance (port auto-discovered):
22/// ```text
23/// mssql://user:pass@localhost/mydb?instance=SQLEXPRESS
24/// ```
25///
26/// Example with explicit port (SSRP not used):
27/// ```text
28/// mssql://user:pass@localhost:1434/mydb?instance=SQLEXPRESS
29/// ```
30#[derive(Debug, Clone)]
31pub struct MssqlConnectOptions {
32    pub(crate) host: String,
33    pub(crate) port: Option<u16>,
34    pub(crate) username: String,
35    pub(crate) database: String,
36    pub(crate) password: Option<String>,
37    pub(crate) instance: Option<String>,
38    pub(crate) log_settings: LogSettings,
39    pub(crate) client_program_version: u32,
40    pub(crate) client_pid: u32,
41    pub(crate) hostname: String,
42    pub(crate) app_name: String,
43    pub(crate) server_name: String,
44    pub(crate) client_interface_name: String,
45    pub(crate) language: String,
46    /// Size in bytes of TDS packets to exchange with the server
47    pub(crate) requested_packet_size: u32,
48    pub(crate) encrypt: Encrypt,
49    pub(crate) trust_server_certificate: bool,
50    pub(crate) hostname_in_certificate: Option<String>,
51    pub(crate) ssl_root_cert: Option<CertificateInput>,
52}
53
54impl Default for MssqlConnectOptions {
55    fn default() -> Self {
56        Self::new()
57    }
58}
59
60impl MssqlConnectOptions {
61    pub fn new() -> Self {
62        Self {
63            port: None,
64            host: String::from("localhost"),
65            database: String::from("master"),
66            username: String::from("sa"),
67            password: None,
68            instance: None,
69            log_settings: Default::default(),
70            requested_packet_size: 4096,
71            client_program_version: 0,
72            client_pid: 0,
73            hostname: String::new(),
74            app_name: String::new(),
75            server_name: String::new(),
76            client_interface_name: String::new(),
77            language: String::new(),
78            encrypt: Encrypt::On,
79            trust_server_certificate: true,
80            hostname_in_certificate: None,
81            ssl_root_cert: None,
82        }
83    }
84
85    pub fn host(mut self, host: &str) -> Self {
86        self.host = host.to_owned();
87        self
88    }
89
90    pub fn port(mut self, port: u16) -> Self {
91        self.port = Some(port);
92        self
93    }
94
95    pub fn username(mut self, username: &str) -> Self {
96        self.username = username.to_owned();
97        self
98    }
99
100    pub fn password(mut self, password: &str) -> Self {
101        self.password = Some(password.to_owned());
102        self
103    }
104
105    pub fn database(mut self, database: &str) -> Self {
106        self.database = database.to_owned();
107        self
108    }
109
110    pub fn instance(mut self, instance: &str) -> Self {
111        self.instance = Some(instance.to_owned());
112        self
113    }
114
115    pub fn client_program_version(mut self, client_program_version: u32) -> Self {
116        self.client_program_version = client_program_version;
117        self
118    }
119
120    pub fn client_pid(mut self, client_pid: u32) -> Self {
121        self.client_pid = client_pid;
122        self
123    }
124
125    pub fn hostname(mut self, hostname: &str) -> Self {
126        self.hostname = hostname.to_owned();
127        self
128    }
129
130    pub fn app_name(mut self, app_name: &str) -> Self {
131        self.app_name = app_name.to_owned();
132        self
133    }
134
135    pub fn server_name(mut self, server_name: &str) -> Self {
136        self.server_name = server_name.to_owned();
137        self
138    }
139
140    pub fn client_interface_name(mut self, client_interface_name: &str) -> Self {
141        self.client_interface_name = client_interface_name.to_owned();
142        self
143    }
144
145    pub fn language(mut self, language: &str) -> Self {
146        self.language = language.to_owned();
147        self
148    }
149
150    /// Size in bytes of TDS packets to exchange with the server.
151    /// Returns an error if the size is smaller than 512 bytes
152    #[allow(clippy::result_large_err)]
153    pub fn requested_packet_size(mut self, size: u32) -> Result<Self, Self> {
154        if size < 512 {
155            Err(self)
156        } else {
157            self.requested_packet_size = size;
158            Ok(self)
159        }
160    }
161
162    pub fn encrypt(mut self, encrypt: Encrypt) -> Self {
163        self.encrypt = encrypt;
164        self
165    }
166
167    pub fn trust_server_certificate(mut self, trust: bool) -> Self {
168        self.trust_server_certificate = trust;
169        self
170    }
171
172    pub fn hostname_in_certificate(mut self, hostname: &str) -> Self {
173        self.hostname_in_certificate = Some(hostname.to_owned());
174        self
175    }
176
177    /// Sets the name of a file containing SSL certificate authority (CA) certificate(s).
178    /// If the file exists, the server's certificate will be verified to be signed by
179    /// one of these authorities.
180    pub fn ssl_root_cert(mut self, cert: impl AsRef<Path>) -> Self {
181        self.ssl_root_cert = Some(CertificateInput::File(cert.as_ref().to_path_buf()));
182        self
183    }
184}