From 8339dbb8dfd121e60d5b55e9fb7cbdc98343169a Mon Sep 17 00:00:00 2001 From: Colin Woodbury Date: Fri, 4 Oct 2024 18:19:56 +0900 Subject: [PATCH] `DatabaseConnection` directly from `PgPool` (#2348) * feat(postgres): create `DatabaseConnection` directly from `PgPool` This allows sqlx-based connection pools that were already on-hand to be easily converted into SeaORM types. * Move `From` impls into driver * impls for MySQL and SQLite * CHANGELOG * fmt --------- Co-authored-by: Billy Chan --- CHANGELOG.md | 1 + src/driver/sqlx_mysql.rs | 15 +++++++++++++++ src/driver/sqlx_postgres.rs | 15 +++++++++++++++ src/driver/sqlx_sqlite.rs | 15 +++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d900d09f4..567c2d9d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Enhancements * [sea-orm-macros] Call `EnumIter::get` using fully qualified syntax https://github.com/SeaQL/sea-orm/pull/2321 +* Construct `DatabaseConnection` directly from `sqlx::PgPool`, `sqlx::SqlitePool` and `sqlx::MySqlPool` https://github.com/SeaQL/sea-orm/pull/2348 ### Upgrades diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index df9862630..e1c593b0c 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -36,6 +36,21 @@ impl std::fmt::Debug for SqlxMySqlPoolConnection { } } +impl From for SqlxMySqlPoolConnection { + fn from(pool: MySqlPool) -> Self { + SqlxMySqlPoolConnection { + pool, + metric_callback: None, + } + } +} + +impl From for DatabaseConnection { + fn from(pool: MySqlPool) -> Self { + DatabaseConnection::SqlxMySqlPoolConnection(pool.into()) + } +} + impl SqlxMySqlConnector { /// Check if the URI provided corresponds to `mysql://` for a MySQL database pub fn accepts(string: &str) -> bool { diff --git a/src/driver/sqlx_postgres.rs b/src/driver/sqlx_postgres.rs index 6e424a343..ceb855f24 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -36,6 +36,21 @@ impl std::fmt::Debug for SqlxPostgresPoolConnection { } } +impl From for SqlxPostgresPoolConnection { + fn from(pool: PgPool) -> Self { + SqlxPostgresPoolConnection { + pool, + metric_callback: None, + } + } +} + +impl From for DatabaseConnection { + fn from(pool: PgPool) -> Self { + DatabaseConnection::SqlxPostgresPoolConnection(pool.into()) + } +} + impl SqlxPostgresConnector { /// Check if the URI provided corresponds to `postgres://` for a PostgreSQL database pub fn accepts(string: &str) -> bool { diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index 3d5465aa9..4d343027f 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -37,6 +37,21 @@ impl std::fmt::Debug for SqlxSqlitePoolConnection { } } +impl From for SqlxSqlitePoolConnection { + fn from(pool: SqlitePool) -> Self { + SqlxSqlitePoolConnection { + pool, + metric_callback: None, + } + } +} + +impl From for DatabaseConnection { + fn from(pool: SqlitePool) -> Self { + DatabaseConnection::SqlxSqlitePoolConnection(pool.into()) + } +} + impl SqlxSqliteConnector { /// Check if the URI provided corresponds to `sqlite:` for a SQLite database pub fn accepts(string: &str) -> bool {