mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-31 15:39:34 +00:00
feat: handle hostaddr postgres URI param and add more tests
This commit is contained in:
committed by
Ryan Leckey
parent
591d33b877
commit
7b1b8c12ba
@@ -26,6 +26,7 @@ pub use ssl_mode::PgSslMode;
|
||||
/// | `sslrootcert` | `None` | Sets the name of a file containing a list of trusted SSL Certificate Authorities. |
|
||||
/// | `statement-cache-capacity` | `100` | The maximum number of prepared statements stored in the cache. Set to `0` to disable. |
|
||||
/// | `host` | `None` | Path to the directory containing a PostgreSQL unix domain socket, which will be used instead of TCP if set. |
|
||||
/// | `hostaddr` | `None` | Same as `host`, but only accepts IP addresses. |
|
||||
/// | `application-name` | `None` | The name will be displayed in the pg_stat_activity view and included in CSV log entries. |
|
||||
/// | `user` | result of `whoami` | PostgreSQL user name to connect as. |
|
||||
/// | `password` | `None` | Password to be used if the server demands password authentication. |
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::error::Error;
|
||||
use crate::postgres::PgConnectOptions;
|
||||
use percent_encoding::percent_decode_str;
|
||||
use std::net::IpAddr;
|
||||
use std::str::FromStr;
|
||||
use url::Url;
|
||||
|
||||
@@ -65,13 +66,12 @@ impl FromStr for PgConnectOptions {
|
||||
}
|
||||
}
|
||||
|
||||
"application_name" => {
|
||||
options = options.application_name(&*value);
|
||||
"hostaddr" => {
|
||||
value.parse::<IpAddr>().map_err(Error::config)?;
|
||||
options = options.host(&*value)
|
||||
}
|
||||
|
||||
"port" => {
|
||||
options = options.port(value.parse().map_err(Error::config)?);
|
||||
}
|
||||
"port" => options = options.port(value.parse().map_err(Error::config)?),
|
||||
|
||||
"dbname" => options = options.database(&*value),
|
||||
|
||||
@@ -79,7 +79,9 @@ impl FromStr for PgConnectOptions {
|
||||
|
||||
"password" => options = options.password(&*value),
|
||||
|
||||
_ => {}
|
||||
"application_name" => options = options.application_name(&*value),
|
||||
|
||||
_ => log::warn!("ignoring unrecognized connect parameter: {}={}", key, value),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +106,51 @@ fn it_parses_host_correctly_from_parameter() {
|
||||
assert_eq!("google.database.com", &opts.host);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_hostaddr_correctly_from_parameter() {
|
||||
let uri = "postgres:///?hostaddr=8.8.8.8";
|
||||
let opts = PgConnectOptions::from_str(uri).unwrap();
|
||||
|
||||
assert_eq!(None, opts.socket);
|
||||
assert_eq!("8.8.8.8", &opts.host);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_port_correctly_from_parameter() {
|
||||
let uri = "postgres:///?port=1234";
|
||||
let opts = PgConnectOptions::from_str(uri).unwrap();
|
||||
|
||||
assert_eq!(None, opts.socket);
|
||||
assert_eq!(1234, opts.port);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_dbname_correctly_from_parameter() {
|
||||
let uri = "postgres:///?dbname=some_db";
|
||||
let opts = PgConnectOptions::from_str(uri).unwrap();
|
||||
|
||||
assert_eq!(None, opts.socket);
|
||||
assert_eq!(Some("some_db"), opts.database.as_deref());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_user_correctly_from_parameter() {
|
||||
let uri = "postgres:///?user=some_user";
|
||||
let opts = PgConnectOptions::from_str(uri).unwrap();
|
||||
|
||||
assert_eq!(None, opts.socket);
|
||||
assert_eq!("some_user", opts.username);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_password_correctly_from_parameter() {
|
||||
let uri = "postgres:///?password=some_pass";
|
||||
let opts = PgConnectOptions::from_str(uri).unwrap();
|
||||
|
||||
assert_eq!(None, opts.socket);
|
||||
assert_eq!(Some("some_pass"), opts.password.as_deref());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_parses_application_name_correctly_from_parameter() {
|
||||
let uri = "postgres:///?application_name=some_name";
|
||||
|
||||
Reference in New Issue
Block a user