1- use std:: path:: { Path , PathBuf } ;
1+ use std:: {
2+ path:: { Path , PathBuf } ,
3+ sync:: { Arc , OnceLock } ,
4+ } ;
25
36mod connect;
47mod parse;
58mod ssl_mode;
69
710use crate :: { connection:: LogSettings , net:: tls:: CertificateInput } ;
11+ use sqlx_core:: net:: tls:: TlsConnector ;
812pub use ssl_mode:: MySqlSslMode ;
913
1014/// Options and flags which can be used to configure a MySQL connection.
@@ -67,10 +71,7 @@ pub struct MySqlConnectOptions {
6771 pub ( crate ) username : String ,
6872 pub ( crate ) password : Option < String > ,
6973 pub ( crate ) database : Option < String > ,
70- pub ( crate ) ssl_mode : MySqlSslMode ,
71- pub ( crate ) ssl_ca : Option < CertificateInput > ,
72- pub ( crate ) ssl_client_cert : Option < CertificateInput > ,
73- pub ( crate ) ssl_client_key : Option < CertificateInput > ,
74+ pub ( crate ) ssl_options : SslOptions ,
7475 pub ( crate ) statement_cache_capacity : usize ,
7576 pub ( crate ) charset : String ,
7677 pub ( crate ) collation : Option < String > ,
@@ -88,6 +89,15 @@ impl Default for MySqlConnectOptions {
8889 }
8990}
9091
92+ #[ derive( Debug , Clone ) ]
93+ pub ( crate ) struct SslOptions {
94+ pub ( crate ) ssl_mode : MySqlSslMode ,
95+ pub ( crate ) ssl_ca : Option < CertificateInput > ,
96+ pub ( crate ) ssl_client_cert : Option < CertificateInput > ,
97+ pub ( crate ) ssl_client_key : Option < CertificateInput > ,
98+ pub ( crate ) cached_connector : Arc < OnceLock < TlsConnector > > ,
99+ }
100+
91101impl MySqlConnectOptions {
92102 /// Creates a new, default set of options ready for configuration
93103 pub fn new ( ) -> Self {
@@ -100,10 +110,13 @@ impl MySqlConnectOptions {
100110 database : None ,
101111 charset : String :: from ( "utf8mb4" ) ,
102112 collation : None ,
103- ssl_mode : MySqlSslMode :: Preferred ,
104- ssl_ca : None ,
105- ssl_client_cert : None ,
106- ssl_client_key : None ,
113+ ssl_options : SslOptions {
114+ ssl_mode : MySqlSslMode :: Preferred ,
115+ ssl_ca : None ,
116+ ssl_client_cert : None ,
117+ ssl_client_key : None ,
118+ cached_connector : Arc :: new ( OnceLock :: new ( ) ) ,
119+ } ,
107120 statement_cache_capacity : 100 ,
108121 log_settings : Default :: default ( ) ,
109122 pipes_as_concat : true ,
@@ -158,6 +171,11 @@ impl MySqlConnectOptions {
158171 self
159172 }
160173
174+ fn ssl_options_mut ( & mut self ) -> & mut SslOptions {
175+ Arc :: make_mut ( & mut self . ssl_options . cached_connector ) . take ( ) ;
176+ & mut self . ssl_options
177+ }
178+
161179 /// Sets whether or with what priority a secure SSL TCP/IP connection will be negotiated
162180 /// with the server.
163181 ///
@@ -172,7 +190,7 @@ impl MySqlConnectOptions {
172190 /// .ssl_mode(MySqlSslMode::Required);
173191 /// ```
174192 pub fn ssl_mode ( mut self , mode : MySqlSslMode ) -> Self {
175- self . ssl_mode = mode;
193+ self . ssl_options_mut ( ) . ssl_mode = mode;
176194 self
177195 }
178196
@@ -187,7 +205,7 @@ impl MySqlConnectOptions {
187205 /// .ssl_ca("path/to/ca.crt");
188206 /// ```
189207 pub fn ssl_ca ( mut self , file_name : impl AsRef < Path > ) -> Self {
190- self . ssl_ca = Some ( CertificateInput :: File ( file_name. as_ref ( ) . to_owned ( ) ) ) ;
208+ self . ssl_options_mut ( ) . ssl_ca = Some ( CertificateInput :: File ( file_name. as_ref ( ) . to_owned ( ) ) ) ;
191209 self
192210 }
193211
@@ -202,7 +220,7 @@ impl MySqlConnectOptions {
202220 /// .ssl_ca_from_pem(vec![]);
203221 /// ```
204222 pub fn ssl_ca_from_pem ( mut self , pem_certificate : Vec < u8 > ) -> Self {
205- self . ssl_ca = Some ( CertificateInput :: Inline ( pem_certificate) ) ;
223+ self . ssl_options_mut ( ) . ssl_ca = Some ( CertificateInput :: Inline ( pem_certificate) ) ;
206224 self
207225 }
208226
@@ -217,7 +235,8 @@ impl MySqlConnectOptions {
217235 /// .ssl_client_cert("path/to/client.crt");
218236 /// ```
219237 pub fn ssl_client_cert ( mut self , cert : impl AsRef < Path > ) -> Self {
220- self . ssl_client_cert = Some ( CertificateInput :: File ( cert. as_ref ( ) . to_path_buf ( ) ) ) ;
238+ self . ssl_options_mut ( ) . ssl_client_cert =
239+ Some ( CertificateInput :: File ( cert. as_ref ( ) . to_path_buf ( ) ) ) ;
221240 self
222241 }
223242
@@ -242,7 +261,8 @@ impl MySqlConnectOptions {
242261 /// .ssl_client_cert_from_pem(CERT);
243262 /// ```
244263 pub fn ssl_client_cert_from_pem ( mut self , cert : impl AsRef < [ u8 ] > ) -> Self {
245- self . ssl_client_cert = Some ( CertificateInput :: Inline ( cert. as_ref ( ) . to_vec ( ) ) ) ;
264+ self . ssl_options_mut ( ) . ssl_client_cert =
265+ Some ( CertificateInput :: Inline ( cert. as_ref ( ) . to_vec ( ) ) ) ;
246266 self
247267 }
248268
@@ -257,7 +277,8 @@ impl MySqlConnectOptions {
257277 /// .ssl_client_key("path/to/client.key");
258278 /// ```
259279 pub fn ssl_client_key ( mut self , key : impl AsRef < Path > ) -> Self {
260- self . ssl_client_key = Some ( CertificateInput :: File ( key. as_ref ( ) . to_path_buf ( ) ) ) ;
280+ self . ssl_options_mut ( ) . ssl_client_key =
281+ Some ( CertificateInput :: File ( key. as_ref ( ) . to_path_buf ( ) ) ) ;
261282 self
262283 }
263284
@@ -282,7 +303,8 @@ impl MySqlConnectOptions {
282303 /// .ssl_client_key_from_pem(KEY);
283304 /// ```
284305 pub fn ssl_client_key_from_pem ( mut self , key : impl AsRef < [ u8 ] > ) -> Self {
285- self . ssl_client_key = Some ( CertificateInput :: Inline ( key. as_ref ( ) . to_vec ( ) ) ) ;
306+ self . ssl_options_mut ( ) . ssl_client_key =
307+ Some ( CertificateInput :: Inline ( key. as_ref ( ) . to_vec ( ) ) ) ;
286308 self
287309 }
288310
@@ -497,7 +519,7 @@ impl MySqlConnectOptions {
497519 /// assert!(matches!(options.get_ssl_mode(), MySqlSslMode::Preferred));
498520 /// ```
499521 pub fn get_ssl_mode ( & self ) -> MySqlSslMode {
500- self . ssl_mode
522+ self . ssl_options . ssl_mode
501523 }
502524
503525 /// Get the server charset.
0 commit comments