diff --git a/Cargo.toml b/Cargo.toml index 5c94ea7..938c588 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ categories = ["database", "development-tools::debugging", "development-tools::pr [features] postgres = ["sqlx/postgres"] sqlite = ["sqlx/sqlite"] +mysql = ["sqlx/mysql"] [dependencies] futures = { version = "0.3" } diff --git a/src/lib.rs b/src/lib.rs index ee99dbc..46f1c76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,9 @@ pub mod postgres; #[cfg(feature = "sqlite")] pub mod sqlite; +#[cfg(feature = "mysql")] +pub mod mysql; + /// Attributes describing the database connection and context. /// Used for span enrichment and attribute propagation. #[derive(Debug, Default)] @@ -73,6 +76,26 @@ impl From> for PoolBuilder { } } +// this is required because `pool.connect_options().to_url_lossy()` panics with sqlite +#[cfg(feature = "mysql")] +impl From> for PoolBuilder { + /// Create a new builder from an existing SQLx pool. + fn from(pool: sqlx::Pool) -> Self { + use sqlx::ConnectOptions; + + let url = pool.connect_options().to_url_lossy(); + let attributes = Attributes { + name: None, + host: url.host_str().map(String::from), + port: url.port(), + database: url + .path_segments() + .and_then(|mut segments| segments.next().map(String::from)), + }; + Self { pool, attributes } + } +} + impl PoolBuilder { /// Set a custom name for the pool (for peer.service attribute). pub fn with_name(mut self, name: impl Into) -> Self { diff --git a/src/mysql.rs b/src/mysql.rs new file mode 100644 index 0000000..924a0d8 --- /dev/null +++ b/src/mysql.rs @@ -0,0 +1,3 @@ +impl crate::prelude::Database for sqlx::MySql { + const SYSTEM: &'static str = "mysql"; +}