From 635b0cfc94420dd422527c75d761efea396c5932 Mon Sep 17 00:00:00 2001 From: Luiz Carvalho Date: Tue, 21 Nov 2023 10:29:20 -0300 Subject: [PATCH] feat: expose connect options fields Exposes some of the main fields for PgConnectOptions and MySqlConnectOptions. Exposed fields include: host, port, socket, ssl mode, application name, and charset. --- sqlx-mysql/src/options/mod.rs | 140 +++++++++++++++++++++---- sqlx-postgres/src/options/mod.rs | 173 +++++++++++++++++++++++-------- 2 files changed, 248 insertions(+), 65 deletions(-) diff --git a/sqlx-mysql/src/options/mod.rs b/sqlx-mysql/src/options/mod.rs index b4fe540751..a797576937 100644 --- a/sqlx-mysql/src/options/mod.rs +++ b/sqlx-mysql/src/options/mod.rs @@ -152,20 +152,6 @@ impl MySqlConnectOptions { self } - /// Get the current database name. - /// - /// # Example - /// - /// ```rust - /// # use sqlx_core::mysql::MySqlConnectOptions; - /// let options = MySqlConnectOptions::new() - /// .database("mysqldb"); - /// assert!(options.get_database().is_some()); - /// ``` - pub fn get_database(&self) -> Option<&str> { - self.database.as_deref() - } - /// Sets whether or with what priority a secure SSL TCP/IP connection will be negotiated /// with the server. /// @@ -175,7 +161,7 @@ impl MySqlConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions}; + /// # use sqlx_mysql::{MySqlSslMode, MySqlConnectOptions}; /// let options = MySqlConnectOptions::new() /// .ssl_mode(MySqlSslMode::Required); /// ``` @@ -189,7 +175,7 @@ impl MySqlConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions}; + /// # use sqlx_mysql::{MySqlSslMode, MySqlConnectOptions}; /// let options = MySqlConnectOptions::new() /// .ssl_mode(MySqlSslMode::VerifyCa) /// .ssl_ca("path/to/ca.crt"); @@ -204,7 +190,7 @@ impl MySqlConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions}; + /// # use sqlx_mysql::{MySqlSslMode, MySqlConnectOptions}; /// let options = MySqlConnectOptions::new() /// .ssl_mode(MySqlSslMode::VerifyCa) /// .ssl_ca_from_pem(vec![]); @@ -219,7 +205,7 @@ impl MySqlConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions}; + /// # use sqlx_mysql::{MySqlSslMode, MySqlConnectOptions}; /// let options = MySqlConnectOptions::new() /// .ssl_mode(MySqlSslMode::VerifyCa) /// .ssl_client_cert("path/to/client.crt"); @@ -238,7 +224,7 @@ impl MySqlConnectOptions { /// This is for illustration purposes only. /// /// ```rust - /// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions}; + /// # use sqlx_mysql::{MySqlSslMode, MySqlConnectOptions}; /// /// const CERT: &[u8] = b"\ /// -----BEGIN CERTIFICATE----- @@ -259,7 +245,7 @@ impl MySqlConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions}; + /// # use sqlx_mysql::{MySqlSslMode, MySqlConnectOptions}; /// let options = MySqlConnectOptions::new() /// .ssl_mode(MySqlSslMode::VerifyCa) /// .ssl_client_key("path/to/client.key"); @@ -278,7 +264,7 @@ impl MySqlConnectOptions { /// This is for illustration purposes only. /// /// ```rust - /// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions}; + /// # use sqlx_mysql::{MySqlSslMode, MySqlConnectOptions}; /// /// const KEY: &[u8] = b"\ /// -----BEGIN PRIVATE KEY----- @@ -348,3 +334,115 @@ impl MySqlConnectOptions { self } } + +impl MySqlConnectOptions { + /// Get the current host. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_mysql::MySqlConnectOptions; + /// let options = MySqlConnectOptions::new() + /// .host("127.0.0.1"); + /// assert_eq!(options.get_host(), "127.0.0.1"); + /// ``` + pub fn get_host(&self) -> &str { + &self.host + } + + /// Get the server's port. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_mysql::MySqlConnectOptions; + /// let options = MySqlConnectOptions::new() + /// .port(6543); + /// assert_eq!(options.get_port(), 6543); + /// ``` + pub fn get_port(&self) -> u16 { + self.port + } + + /// Get the socket path. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_mysql::MySqlConnectOptions; + /// let options = MySqlConnectOptions::new() + /// .socket("/tmp"); + /// assert!(options.get_socket().is_some()); + /// ``` + pub fn get_socket(&self) -> Option<&PathBuf> { + self.socket.as_ref() + } + + /// Get the server's port. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_mysql::MySqlConnectOptions; + /// let options = MySqlConnectOptions::new() + /// .username("foo"); + /// assert_eq!(options.get_username(), "foo"); + /// ``` + pub fn get_username(&self) -> &str { + &self.username + } + + /// Get the current database name. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_mysql::MySqlConnectOptions; + /// let options = MySqlConnectOptions::new() + /// .database("postgres"); + /// assert!(options.get_database().is_some()); + /// ``` + pub fn get_database(&self) -> Option<&str> { + self.database.as_deref() + } + + /// Get the SSL mode. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_mysql::{MySqlConnectOptions, MySqlSslMode}; + /// let options = MySqlConnectOptions::new(); + /// assert!(matches!(options.get_ssl_mode(), MySqlSslMode::Preferred)); + /// ``` + pub fn get_ssl_mode(&self) -> MySqlSslMode { + self.ssl_mode + } + + /// Get the server charset. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_mysql::MySqlConnectOptions; + /// let options = MySqlConnectOptions::new(); + /// assert_eq!(options.get_charset(), "utf8mb4"); + /// ``` + pub fn get_charset(&self) -> &str { + &self.charset + } + + /// Get the server collation. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_mysql::MySqlConnectOptions; + /// let options = MySqlConnectOptions::new() + /// .collation("collation"); + /// assert!(options.get_collation().is_some()); + /// ``` + pub fn get_collation(&self) -> Option<&str> { + self.collation.as_deref() + } +} diff --git a/sqlx-postgres/src/options/mod.rs b/sqlx-postgres/src/options/mod.rs index 2da782841c..2a2c43006a 100644 --- a/sqlx-postgres/src/options/mod.rs +++ b/sqlx-postgres/src/options/mod.rs @@ -130,7 +130,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; + /// # use sqlx_postgres::PgConnectOptions; /// let options = PgConnectOptions::new(); /// ``` pub fn new() -> Self { @@ -196,7 +196,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; + /// # use sqlx_postgres::PgConnectOptions; /// let options = PgConnectOptions::new() /// .host("localhost"); /// ``` @@ -205,20 +205,6 @@ impl PgConnectOptions { self } - /// Get the current host. - /// - /// # Example - /// - /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; - /// let options = PgConnectOptions::new() - /// .host("127.0.0.1"); - /// assert_eq!(options.get_host(), "127.0.0.1"); - /// ``` - pub fn get_host(&self) -> &str { - self.host.as_str() - } - /// Sets the port to connect to at the server host. /// /// The default port for PostgreSQL is `5432`. @@ -226,7 +212,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; + /// # use sqlx_postgres::PgConnectOptions; /// let options = PgConnectOptions::new() /// .port(5432); /// ``` @@ -252,7 +238,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; + /// # use sqlx_postgres::PgConnectOptions; /// let options = PgConnectOptions::new() /// .username("postgres"); /// ``` @@ -266,7 +252,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; + /// # use sqlx_postgres::PgConnectOptions; /// let options = PgConnectOptions::new() /// .username("root") /// .password("safe-and-secure"); @@ -281,7 +267,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; + /// # use sqlx_postgres::PgConnectOptions; /// let options = PgConnectOptions::new() /// .database("postgres"); /// ``` @@ -290,20 +276,6 @@ impl PgConnectOptions { self } - /// Get the current database name. - /// - /// # Example - /// - /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; - /// let options = PgConnectOptions::new() - /// .database("postgres"); - /// assert!(options.get_database().is_some()); - /// ``` - pub fn get_database(&self) -> Option<&str> { - self.database.as_deref() - } - /// Sets whether or with what priority a secure SSL TCP/IP connection will be negotiated /// with the server. /// @@ -315,7 +287,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions}; + /// # use sqlx_postgres::{PgSslMode, PgConnectOptions}; /// let options = PgConnectOptions::new() /// .ssl_mode(PgSslMode::Require); /// ``` @@ -331,7 +303,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions}; + /// # use sqlx_postgres::{PgSslMode, PgConnectOptions}; /// let options = PgConnectOptions::new() /// // Providing a CA certificate with less than VerifyCa is pointless /// .ssl_mode(PgSslMode::VerifyCa) @@ -347,7 +319,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions}; + /// # use sqlx_postgres::{PgSslMode, PgConnectOptions}; /// let options = PgConnectOptions::new() /// // Providing a CA certificate with less than VerifyCa is pointless /// .ssl_mode(PgSslMode::VerifyCa) @@ -367,7 +339,7 @@ impl PgConnectOptions { /// This is for illustration purposes only. /// /// ```rust - /// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions}; + /// # use sqlx_postgres::{PgSslMode, PgConnectOptions}; /// /// const CERT: &[u8] = b"\ /// -----BEGIN CERTIFICATE----- @@ -389,7 +361,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions}; + /// # use sqlx_postgres::{PgSslMode, PgConnectOptions}; /// let options = PgConnectOptions::new() /// // Providing a CA certificate with less than VerifyCa is pointless /// .ssl_mode(PgSslMode::VerifyCa) @@ -409,7 +381,7 @@ impl PgConnectOptions { /// This is for illustration purposes only. /// /// ```rust - /// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions}; + /// # use sqlx_postgres::{PgSslMode, PgConnectOptions}; /// /// const KEY: &[u8] = b"\ /// -----BEGIN PRIVATE KEY----- @@ -431,7 +403,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions}; + /// # use sqlx_postgres::{PgSslMode, PgConnectOptions}; /// let options = PgConnectOptions::new() /// // Providing a CA certificate with less than VerifyCa is pointless /// .ssl_mode(PgSslMode::VerifyCa) @@ -458,7 +430,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; + /// # use sqlx_postgres::PgConnectOptions; /// let options = PgConnectOptions::new() /// .application_name("my-app"); /// ``` @@ -505,7 +477,7 @@ impl PgConnectOptions { /// /// ### Examples /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; + /// # use sqlx_postgres::PgConnectOptions; /// /// let mut options = PgConnectOptions::new() /// // for Redshift and Postgres 10 @@ -525,7 +497,7 @@ impl PgConnectOptions { /// # Example /// /// ```rust - /// # use sqlx_core::postgres::PgConnectOptions; + /// # use sqlx_postgres::PgConnectOptions; /// let options = PgConnectOptions::new() /// .options([("geqo", "off"), ("statement_timeout", "5min")]); /// ``` @@ -564,6 +536,119 @@ impl PgConnectOptions { } } +impl PgConnectOptions { + /// Get the current host. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_postgres::PgConnectOptions; + /// let options = PgConnectOptions::new() + /// .host("127.0.0.1"); + /// assert_eq!(options.get_host(), "127.0.0.1"); + /// ``` + pub fn get_host(&self) -> &str { + &self.host + } + + /// Get the server's port. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_postgres::PgConnectOptions; + /// let options = PgConnectOptions::new() + /// .port(6543); + /// assert_eq!(options.get_port(), 6543); + /// ``` + pub fn get_port(&self) -> u16 { + self.port + } + + /// Get the socket path. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_postgres::PgConnectOptions; + /// let options = PgConnectOptions::new() + /// .socket("/tmp"); + /// assert!(options.get_socket().is_some()); + /// ``` + pub fn get_socket(&self) -> Option<&PathBuf> { + self.socket.as_ref() + } + + /// Get the server's port. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_postgres::PgConnectOptions; + /// let options = PgConnectOptions::new() + /// .username("foo"); + /// assert_eq!(options.get_username(), "foo"); + /// ``` + pub fn get_username(&self) -> &str { + &self.username + } + + /// Get the current database name. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_postgres::PgConnectOptions; + /// let options = PgConnectOptions::new() + /// .database("postgres"); + /// assert!(options.get_database().is_some()); + /// ``` + pub fn get_database(&self) -> Option<&str> { + self.database.as_deref() + } + + /// Get the SSL mode. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_postgres::{PgConnectOptions, PgSslMode}; + /// let options = PgConnectOptions::new(); + /// assert!(matches!(options.get_ssl_mode(), PgSslMode::Prefer)); + /// ``` + pub fn get_ssl_mode(&self) -> PgSslMode { + self.ssl_mode + } + + /// Get the application name. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_postgres::PgConnectOptions; + /// let options = PgConnectOptions::new() + /// .application_name("service"); + /// assert!(options.get_application_name().is_some()); + /// ``` + pub fn get_application_name(&self) -> Option<&str> { + self.application_name.as_deref() + } + + /// Get the options. + /// + /// # Example + /// + /// ```rust + /// # use sqlx_postgres::PgConnectOptions; + /// let options = PgConnectOptions::new() + /// .options([("foo", "bar")]); + /// assert!(options.get_options().is_some()); + /// ``` + pub fn get_options(&self) -> Option<&str> { + self.options.as_deref() + } +} + fn default_host(port: u16) -> String { // try to check for the existence of a unix socket and uses that let socket = format!(".s.PGSQL.{port}");