Skip to content

Commit 7b1b8c1

Browse files
dmarcusemehcode
authored andcommitted
feat: handle hostaddr postgres URI param and add more tests
1 parent 591d33b commit 7b1b8c1

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

sqlx-core/src/postgres/options/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub use ssl_mode::PgSslMode;
2626
/// | `sslrootcert` | `None` | Sets the name of a file containing a list of trusted SSL Certificate Authorities. |
2727
/// | `statement-cache-capacity` | `100` | The maximum number of prepared statements stored in the cache. Set to `0` to disable. |
2828
/// | `host` | `None` | Path to the directory containing a PostgreSQL unix domain socket, which will be used instead of TCP if set. |
29+
/// | `hostaddr` | `None` | Same as `host`, but only accepts IP addresses. |
2930
/// | `application-name` | `None` | The name will be displayed in the pg_stat_activity view and included in CSV log entries. |
3031
/// | `user` | result of `whoami` | PostgreSQL user name to connect as. |
3132
/// | `password` | `None` | Password to be used if the server demands password authentication. |

sqlx-core/src/postgres/options/parse.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::error::Error;
22
use crate::postgres::PgConnectOptions;
33
use percent_encoding::percent_decode_str;
4+
use std::net::IpAddr;
45
use std::str::FromStr;
56
use url::Url;
67

@@ -65,21 +66,22 @@ impl FromStr for PgConnectOptions {
6566
}
6667
}
6768

68-
"application_name" => {
69-
options = options.application_name(&*value);
69+
"hostaddr" => {
70+
value.parse::<IpAddr>().map_err(Error::config)?;
71+
options = options.host(&*value)
7072
}
7173

72-
"port" => {
73-
options = options.port(value.parse().map_err(Error::config)?);
74-
}
74+
"port" => options = options.port(value.parse().map_err(Error::config)?),
7575

7676
"dbname" => options = options.database(&*value),
7777

7878
"user" => options = options.username(&*value),
7979

8080
"password" => options = options.password(&*value),
8181

82-
_ => {}
82+
"application_name" => options = options.application_name(&*value),
83+
84+
_ => log::warn!("ignoring unrecognized connect parameter: {}={}", key, value),
8385
}
8486
}
8587

@@ -104,6 +106,51 @@ fn it_parses_host_correctly_from_parameter() {
104106
assert_eq!("google.database.com", &opts.host);
105107
}
106108

109+
#[test]
110+
fn it_parses_hostaddr_correctly_from_parameter() {
111+
let uri = "postgres:///?hostaddr=8.8.8.8";
112+
let opts = PgConnectOptions::from_str(uri).unwrap();
113+
114+
assert_eq!(None, opts.socket);
115+
assert_eq!("8.8.8.8", &opts.host);
116+
}
117+
118+
#[test]
119+
fn it_parses_port_correctly_from_parameter() {
120+
let uri = "postgres:///?port=1234";
121+
let opts = PgConnectOptions::from_str(uri).unwrap();
122+
123+
assert_eq!(None, opts.socket);
124+
assert_eq!(1234, opts.port);
125+
}
126+
127+
#[test]
128+
fn it_parses_dbname_correctly_from_parameter() {
129+
let uri = "postgres:///?dbname=some_db";
130+
let opts = PgConnectOptions::from_str(uri).unwrap();
131+
132+
assert_eq!(None, opts.socket);
133+
assert_eq!(Some("some_db"), opts.database.as_deref());
134+
}
135+
136+
#[test]
137+
fn it_parses_user_correctly_from_parameter() {
138+
let uri = "postgres:///?user=some_user";
139+
let opts = PgConnectOptions::from_str(uri).unwrap();
140+
141+
assert_eq!(None, opts.socket);
142+
assert_eq!("some_user", opts.username);
143+
}
144+
145+
#[test]
146+
fn it_parses_password_correctly_from_parameter() {
147+
let uri = "postgres:///?password=some_pass";
148+
let opts = PgConnectOptions::from_str(uri).unwrap();
149+
150+
assert_eq!(None, opts.socket);
151+
assert_eq!(Some("some_pass"), opts.password.as_deref());
152+
}
153+
107154
#[test]
108155
fn it_parses_application_name_correctly_from_parameter() {
109156
let uri = "postgres:///?application_name=some_name";

0 commit comments

Comments
 (0)