diff --git a/src/database/mod.rs b/src/database/mod.rs index 67a8d7279..7d357a2fa 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,16 +1,7 @@ +use std::borrow::Cow; use std::time::Duration; -mod connection; -mod db_connection; -#[cfg(feature = "mock")] -#[cfg_attr(docsrs, doc(cfg(feature = "mock")))] -mod mock; -#[cfg(feature = "proxy")] -#[cfg_attr(docsrs, doc(cfg(feature = "proxy")))] -mod proxy; -mod statement; -mod stream; -mod transaction; +use tracing::instrument; pub use connection::*; pub use db_connection::*; @@ -21,13 +12,23 @@ pub use mock::*; #[cfg_attr(docsrs, doc(cfg(feature = "proxy")))] pub use proxy::*; pub use statement::*; -use std::borrow::Cow; pub use stream::*; -use tracing::instrument; pub use transaction::*; use crate::error::*; +mod connection; +mod db_connection; +#[cfg(feature = "mock")] +#[cfg_attr(docsrs, doc(cfg(feature = "mock")))] +mod mock; +#[cfg(feature = "proxy")] +#[cfg_attr(docsrs, doc(cfg(feature = "proxy")))] +mod proxy; +mod statement; +mod stream; +mod transaction; + /// Defines a database #[derive(Debug, Default)] pub struct Database; @@ -63,6 +64,7 @@ pub struct ConnectOptions { /// Schema search path (PostgreSQL only) pub(crate) schema_search_path: Option, pub(crate) test_before_acquire: bool, + pub(crate) application_name: Option<&'static str>, } impl Database { @@ -157,6 +159,7 @@ impl ConnectOptions { sqlcipher_key: None, schema_search_path: None, test_before_acquire: true, + application_name: None, } } @@ -297,4 +300,15 @@ impl ConnectOptions { self.test_before_acquire = value; self } + + /// Set the application name for the connection (PostgreSQL only) + pub fn application_name(&mut self, value: &'static str) -> &mut Self { + self.application_name = Some(value); + self + } + + /// Get the application name for the connection (PostgreSQL only) + pub fn get_application_name(&self) -> Option<&'static str> { + self.application_name + } } diff --git a/src/driver/sqlx_postgres.rs b/src/driver/sqlx_postgres.rs index b5deaaff6..a9edfe4f9 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -49,7 +49,9 @@ impl SqlxPostgresConnector { .url .parse::() .map_err(sqlx_error_to_conn_err)?; + use sqlx::ConnectOptions; + if !options.sqlx_logging { opt = opt.disable_statement_logging(); } else { @@ -61,10 +63,15 @@ impl SqlxPostgresConnector { ); } } + + if let Some(application_name) = options.application_name { + opt = opt.application_name(application_name); + } + let set_search_path_sql = options .schema_search_path .as_ref() - .map(|schema| format!("SET search_path = {schema}")); + .map(|schema| format!("SET search_path = '{schema}'")); let mut pool_options = options.sqlx_pool_options(); if let Some(sql) = set_search_path_sql { pool_options = pool_options.after_connect(move |conn, _| {