Skip to content

Commit 5d0f548

Browse files
committed
Introduce and use getter methods
1 parent c01f513 commit 5d0f548

File tree

7 files changed

+62
-33
lines changed

7 files changed

+62
-33
lines changed

sqlx-postgres/src/connection/establish.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ impl PgConnection {
3333
("TimeZone", "UTC"),
3434
];
3535

36-
if let Some(ref extra_float_digits) = options.extra_float_digits {
36+
if let Some(extra_float_digits) = options.get_extra_float_digits() {
3737
params.push(("extra_float_digits", extra_float_digits));
3838
}
3939

40-
if let Some(ref application_name) = options.application_name {
40+
if let Some(application_name) = options.get_application_name() {
4141
params.push(("application_name", application_name));
4242
}
4343

44-
if let Some(ref options) = options.options {
44+
if let Some(options) = options.get_options() {
4545
params.push(("options", options));
4646
}
4747

4848
stream.write(Startup {
49-
username: Some(&options.username),
50-
database: options.database.as_deref(),
49+
username: Some(options.get_username()),
50+
database: Some(options.get_database()),
5151
params: &params,
5252
})?;
5353

@@ -77,7 +77,7 @@ impl PgConnection {
7777

7878
stream
7979
.send(Password::Cleartext(
80-
options.password.as_deref().unwrap_or_default(),
80+
options.get_password().unwrap_or_default(),
8181
))
8282
.await?;
8383
}
@@ -90,8 +90,8 @@ impl PgConnection {
9090

9191
stream
9292
.send(Password::Md5 {
93-
username: &options.username,
94-
password: options.password.as_deref().unwrap_or_default(),
93+
username: options.get_username(),
94+
password: options.get_password().unwrap_or_default(),
9595
salt: body.salt,
9696
})
9797
.await?;

sqlx-postgres/src/connection/sasl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub(crate) async fn authenticate(
5252
BASE64_STANDARD.encode_string(GS2_HEADER, &mut channel_binding);
5353

5454
// "n=" saslname ;; Usernames are prepared using SASLprep.
55-
let username = format!("{}={}", USERNAME_ATTR, options.username);
55+
let username = format!("{}={}", USERNAME_ATTR, options.get_username());
5656
let username = match saslprep(&username) {
5757
Ok(v) => v,
5858
// TODO(danielakhterov): Remove panic when we have proper support for configuration errors
@@ -87,7 +87,7 @@ pub(crate) async fn authenticate(
8787

8888
// SaltedPassword := Hi(Normalize(password), salt, i)
8989
let salted_password = hi(
90-
options.password.as_deref().unwrap_or_default(),
90+
options.get_password().unwrap_or_default(),
9191
&cont.salt,
9292
cont.iterations,
9393
)?;

sqlx-postgres/src/connection/stream.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ impl PgStream {
4444
pub(super) async fn connect(options: &PgConnectOptions) -> Result<Self, Error> {
4545
let socket_result = match options.fetch_socket() {
4646
Some(ref path) => net::connect_uds(path, MaybeUpgradeTls(options)).await?,
47-
None => net::connect_tcp(&options.host, options.port, MaybeUpgradeTls(options)).await?,
47+
None => {
48+
net::connect_tcp(
49+
options.get_host(),
50+
options.get_port(),
51+
MaybeUpgradeTls(options),
52+
)
53+
.await?
54+
}
4855
};
4956

5057
let socket = socket_result?;

sqlx-postgres/src/connection/tls.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async fn maybe_upgrade<S: Socket>(
2020
options: &PgConnectOptions,
2121
) -> Result<Box<dyn Socket>, Error> {
2222
// https://www.postgresql.org/docs/12/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS
23-
match options.ssl_mode {
23+
match options.get_ssl_mode() {
2424
// FIXME: Implement ALLOW
2525
PgSslMode::Allow | PgSslMode::Disable => return Ok(Box::new(socket)),
2626

@@ -46,15 +46,15 @@ async fn maybe_upgrade<S: Socket>(
4646
}
4747

4848
let accept_invalid_certs = !matches!(
49-
options.ssl_mode,
49+
options.get_ssl_mode(),
5050
PgSslMode::VerifyCa | PgSslMode::VerifyFull
5151
);
52-
let accept_invalid_hostnames = !matches!(options.ssl_mode, PgSslMode::VerifyFull);
52+
let accept_invalid_hostnames = !matches!(options.get_ssl_mode(), PgSslMode::VerifyFull);
5353

5454
let config = TlsConfig {
5555
accept_invalid_certs,
5656
accept_invalid_hostnames,
57-
hostname: &options.host,
57+
hostname: options.get_host(),
5858
root_cert_path: options.ssl_root_cert.as_ref(),
5959
client_cert_path: options.ssl_client_cert.as_ref(),
6060
client_key_path: options.ssl_client_key.as_ref(),

sqlx-postgres/src/migrate.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,22 @@ use crate::query_scalar::query_scalar;
1818
use crate::{PgConnectOptions, PgConnection, Postgres};
1919

2020
fn parse_for_maintenance(url: &str) -> Result<(PgConnectOptions, String), Error> {
21-
let mut options = PgConnectOptions::from_str(url)?;
21+
let options = PgConnectOptions::from_str(url)?;
2222

2323
// pull out the name of the database to create
2424
let database = options
25-
.database
26-
.as_deref()
27-
.unwrap_or(&options.username)
25+
.get_database()
26+
.unwrap_or(options.get_username())
2827
.to_owned();
2928

3029
// switch us to the maintenance database
3130
// use `postgres` _unless_ the database is postgres, in which case, use `template1`
3231
// this matches the behavior of the `createdb` util
33-
options.database = if database == "postgres" {
34-
Some("template1".into())
32+
if database == "postgres" {
33+
Ok((options.database("template1"), database))
3534
} else {
36-
Some("postgres".into())
37-
};
38-
39-
Ok((options, database))
35+
Ok((options.database("postgres"), database))
36+
}
4037
}
4138

4239
impl MigrateDatabase for Postgres {

sqlx-postgres/src/options/mod.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ impl PgConnectOptions {
9696
pub(crate) fn apply_pgpass(mut self) -> Self {
9797
if self.password.is_none() {
9898
self.password = pgpass::load_password(
99-
&self.host,
100-
self.port,
101-
&self.username,
102-
self.database.as_deref(),
99+
self.get_host(),
100+
self.get_port(),
101+
self.get_username(),
102+
self.get_database(),
103103
);
104104
}
105105

@@ -519,6 +519,18 @@ impl PgConnectOptions {
519519
&self.username
520520
}
521521

522+
/// Get the password.
523+
///
524+
/// ```rust
525+
/// # use sqlx_postgres::PgConnectOptions;
526+
/// let options = PgConnectOptions::new()
527+
/// .password("53C237");
528+
/// assert_eq!(options.get_password(), Some("53C237"));
529+
/// ```
530+
pub fn get_password(&self) -> Option<&str> {
531+
self.password.as_deref()
532+
}
533+
522534
/// Get the current database name.
523535
///
524536
/// # Example
@@ -560,6 +572,19 @@ impl PgConnectOptions {
560572
self.application_name.as_deref()
561573
}
562574

575+
/// Get the extra float digits.
576+
///
577+
/// # Example
578+
///
579+
/// ```rust
580+
/// # use sqlx_postgres::PgConnectOptions;
581+
/// let options = PgConnectOptions::new();
582+
/// assert_eq!(options.get_extra_float_digits(), Some("2"));
583+
/// ```
584+
pub fn get_extra_float_digits(&self) -> std::option::Option<&str> {
585+
self.extra_float_digits.as_deref()
586+
}
587+
563588
/// Get the options.
564589
///
565590
/// # Example

sqlx-postgres/src/testing/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ async fn test_context(args: &TestArgs) -> Result<TestContext<Postgres>, Error> {
108108
Err((existing, pool)) => {
109109
// Sanity checks.
110110
assert_eq!(
111-
existing.connect_options().host,
112-
pool.connect_options().host,
111+
existing.connect_options().get_host(),
112+
pool.connect_options().get_host(),
113113
"DATABASE_URL changed at runtime, host differs"
114114
);
115115

116116
assert_eq!(
117-
existing.connect_options().database,
118-
pool.connect_options().database,
117+
existing.connect_options().get_database(),
118+
pool.connect_options().get_database(),
119119
"DATABASE_URL changed at runtime, database differs"
120120
);
121121

0 commit comments

Comments
 (0)