|
1 | 1 | use std::env::var;
|
| 2 | +use std::fmt::Display; |
2 | 3 | use std::path::{Path, PathBuf};
|
3 | 4 |
|
4 | 5 | mod connect;
|
@@ -33,6 +34,7 @@ pub use ssl_mode::PgSslMode;
|
33 | 34 | /// | `password` | `None` | Password to be used if the server demands password authentication. |
|
34 | 35 | /// | `port` | `5432` | Port number to connect to at the server host, or socket file name extension for Unix-domain connections. |
|
35 | 36 | /// | `dbname` | `None` | The database name. |
|
| 37 | +/// | `options` | `None` | The runtime parameters to send to the server at connection start. | |
36 | 38 | ///
|
37 | 39 | /// The URI scheme designator can be either `postgresql://` or `postgres://`.
|
38 | 40 | /// Each of the URI parts is optional.
|
@@ -85,6 +87,7 @@ pub struct PgConnectOptions {
|
85 | 87 | pub(crate) statement_cache_capacity: usize,
|
86 | 88 | pub(crate) application_name: Option<String>,
|
87 | 89 | pub(crate) log_settings: LogSettings,
|
| 90 | + pub(crate) options: Option<String>, |
88 | 91 | }
|
89 | 92 |
|
90 | 93 | impl Default for PgConnectOptions {
|
@@ -145,6 +148,7 @@ impl PgConnectOptions {
|
145 | 148 | statement_cache_capacity: 100,
|
146 | 149 | application_name: var("PGAPPNAME").ok(),
|
147 | 150 | log_settings: Default::default(),
|
| 151 | + options: var("PGOPTIONS").ok(), |
148 | 152 | }
|
149 | 153 | }
|
150 | 154 |
|
@@ -332,6 +336,34 @@ impl PgConnectOptions {
|
332 | 336 | self
|
333 | 337 | }
|
334 | 338 |
|
| 339 | + /// Set additional startup options for the connection as a list of key-value pairs. |
| 340 | + /// |
| 341 | + /// # Example |
| 342 | + /// |
| 343 | + /// ```rust |
| 344 | + /// # use sqlx_core::postgres::PgConnectOptions; |
| 345 | + /// let options = PgConnectOptions::new() |
| 346 | + /// .options([("geqo", "off"), ("statement_timeout", "5min")]); |
| 347 | + /// ``` |
| 348 | + pub fn options<K, V, I>(mut self, options: I) -> Self |
| 349 | + where |
| 350 | + K: Display, |
| 351 | + V: Display, |
| 352 | + I: IntoIterator<Item = (K, V)>, |
| 353 | + { |
| 354 | + let mut options_str = String::new(); |
| 355 | + for (k, v) in options { |
| 356 | + options_str += &format!("-c {}={}", k, v); |
| 357 | + } |
| 358 | + if let Some(ref mut v) = self.options { |
| 359 | + v.push(' '); |
| 360 | + v.push_str(&options_str); |
| 361 | + } else { |
| 362 | + self.options = Some(options_str); |
| 363 | + } |
| 364 | + self |
| 365 | + } |
| 366 | + |
335 | 367 | /// We try using a socket if hostname starts with `/` or if socket parameter
|
336 | 368 | /// is specified.
|
337 | 369 | pub(crate) fn fetch_socket(&self) -> Option<String> {
|
|
0 commit comments