Skip to content

Commit a836e18

Browse files
committed
Add pg connection options creation without environment
1 parent 91d26ba commit a836e18

File tree

1 file changed

+36
-10
lines changed
  • sqlx-postgres/src/options

1 file changed

+36
-10
lines changed

sqlx-postgres/src/options/mod.rs

+36-10
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ impl PgConnectOptions {
6868

6969
let database = var("PGDATABASE").ok();
7070

71+
let ssl_mode = var("PGSSLMODE")
72+
.ok()
73+
.and_then(|v| v.parse().ok())
74+
.unwrap_or_default();
75+
7176
PgConnectOptions {
72-
port,
73-
host,
74-
socket: None,
75-
username,
7677
password: var("PGPASSWORD").ok(),
7778
database,
7879
ssl_root_cert: var("PGSSLROOTCERT").ok().map(CertificateInput::from),
@@ -81,15 +82,40 @@ impl PgConnectOptions {
8182
// `-----BEGIN CERTIFICATE-----` and so will not attempt to parse
8283
// a PEM-encoded private key.
8384
ssl_client_key: var("PGSSLKEY").ok().map(CertificateInput::from),
84-
ssl_mode: var("PGSSLMODE")
85-
.ok()
86-
.and_then(|v| v.parse().ok())
87-
.unwrap_or_default(),
88-
statement_cache_capacity: 100,
8985
application_name: var("PGAPPNAME").ok(),
86+
options: var("PGOPTIONS").ok(),
87+
..Self::new_without_environment(host, port, username, ssl_mode)
88+
}
89+
}
90+
91+
/// Create a minimal set of connection options _without_ applying any environment.
92+
///
93+
/// To be used in situations the environment is not controlled enough to be relied upon.
94+
/// Does not respect any `PG*` environment variables.
95+
///
96+
/// See the type level-documentation for details.
97+
pub fn new_without_environment(
98+
host: String,
99+
port: u16,
100+
username: String,
101+
ssl_mode: PgSslMode,
102+
) -> Self {
103+
PgConnectOptions {
104+
host,
105+
port,
106+
socket: None,
107+
username,
108+
password: None,
109+
database: None,
110+
ssl_mode,
111+
ssl_root_cert: None,
112+
ssl_client_cert: None,
113+
ssl_client_key: None,
114+
statement_cache_capacity: 100,
115+
application_name: None,
90116
extra_float_digits: Some("2".into()),
91117
log_settings: Default::default(),
92-
options: var("PGOPTIONS").ok(),
118+
options: None,
93119
}
94120
}
95121

0 commit comments

Comments
 (0)