Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -73,6 +76,26 @@ impl From<sqlx::Pool<sqlx::Sqlite>> for PoolBuilder<sqlx::Sqlite> {
}
}

// this is required because `pool.connect_options().to_url_lossy()` panics with sqlite
#[cfg(feature = "mysql")]
impl From<sqlx::Pool<sqlx::MySql>> for PoolBuilder<sqlx::MySql> {
/// Create a new builder from an existing SQLx pool.
fn from(pool: sqlx::Pool<sqlx::MySql>) -> 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<DB: sqlx::Database> PoolBuilder<DB> {
/// Set a custom name for the pool (for peer.service attribute).
pub fn with_name(mut self, name: impl Into<String>) -> Self {
Expand Down
3 changes: 3 additions & 0 deletions src/mysql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
impl crate::prelude::Database for sqlx::MySql {
const SYSTEM: &'static str = "mysql";
}