Get SQLx connection pool (#1297)

This commit is contained in:
Billy Chan 2023-01-05 14:08:10 +08:00 committed by GitHub
parent d80e61ed7e
commit d332afa99c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 4 deletions

View File

@ -55,7 +55,7 @@ actix-rt = { version = "2.2.0" }
maplit = { version = "1" } maplit = { version = "1" }
rust_decimal_macros = { version = "1" } rust_decimal_macros = { version = "1" }
tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] }
sea-orm = { path = ".", features = ["mock", "debug-print", "tests-cfg", "postgres-array"] } sea-orm = { path = ".", features = ["mock", "debug-print", "tests-cfg", "postgres-array", "sea-orm-internal"] }
pretty_assertions = { version = "0.7" } pretty_assertions = { version = "0.7" }
time = { version = "0.3", features = ["macros"] } time = { version = "0.3", features = ["macros"] }
uuid = { version = "1", features = ["v4"] } uuid = { version = "1", features = ["v4"] }
@ -81,6 +81,7 @@ with-bigdecimal = ["bigdecimal", "sea-query/with-bigdecimal", "sea-query-binder?
with-uuid = ["uuid", "sea-query/with-uuid", "sea-query-binder?/with-uuid", "sqlx?/uuid"] with-uuid = ["uuid", "sea-query/with-uuid", "sea-query-binder?/with-uuid", "sqlx?/uuid"]
with-time = ["time", "sea-query/with-time", "sea-query-binder?/with-time", "sqlx?/time"] with-time = ["time", "sea-query/with-time", "sea-query-binder?/with-time", "sqlx?/time"]
postgres-array = ["sea-query/postgres-array", "sea-query-binder?/postgres-array", "sea-orm-macros?/postgres-array"] postgres-array = ["sea-query/postgres-array", "sea-query-binder?/postgres-array", "sea-orm-macros?/postgres-array"]
sea-orm-internal = []
sqlx-dep = [] sqlx-dep = []
sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"] sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"]
sqlx-mysql = ["sqlx-dep", "sea-query-binder/sqlx-mysql", "sqlx/mysql"] sqlx-mysql = ["sqlx-dep", "sea-query-binder/sqlx-mysql", "sqlx/mysql"]

View File

@ -380,6 +380,36 @@ impl DatabaseConnection {
} }
} }
#[cfg(feature = "sea-orm-internal")]
impl DatabaseConnection {
/// Get [sqlx::MySqlPool]
#[cfg(feature = "sqlx-mysql")]
pub fn get_mysql_connection_pool(&self) -> &sqlx::MySqlPool {
match self {
DatabaseConnection::SqlxMySqlPoolConnection(conn) => &conn.pool,
_ => panic!("Not MySQL Connection"),
}
}
/// Get [sqlx::PgPool]
#[cfg(feature = "sqlx-postgres")]
pub fn get_postgres_connection_pool(&self) -> &sqlx::PgPool {
match self {
DatabaseConnection::SqlxPostgresPoolConnection(conn) => &conn.pool,
_ => panic!("Not Postgres Connection"),
}
}
/// Get [sqlx::SqlitePool]
#[cfg(feature = "sqlx-sqlite")]
pub fn get_sqlite_connection_pool(&self) -> &sqlx::SqlitePool {
match self {
DatabaseConnection::SqlxSqlitePoolConnection(conn) => &conn.pool,
_ => panic!("Not SQLite Connection"),
}
}
}
impl DbBackend { impl DbBackend {
/// Check if the URI is the same as the specified database backend. /// Check if the URI is the same as the specified database backend.
/// Returns true if they match. /// Returns true if they match.

View File

@ -24,7 +24,7 @@ pub struct SqlxMySqlConnector;
/// Defines a sqlx MySQL pool /// Defines a sqlx MySQL pool
#[derive(Clone)] #[derive(Clone)]
pub struct SqlxMySqlPoolConnection { pub struct SqlxMySqlPoolConnection {
pool: MySqlPool, pub(crate) pool: MySqlPool,
metric_callback: Option<crate::metric::Callback>, metric_callback: Option<crate::metric::Callback>,
} }

View File

@ -24,7 +24,7 @@ pub struct SqlxPostgresConnector;
/// Defines a sqlx PostgreSQL pool /// Defines a sqlx PostgreSQL pool
#[derive(Clone)] #[derive(Clone)]
pub struct SqlxPostgresPoolConnection { pub struct SqlxPostgresPoolConnection {
pool: PgPool, pub(crate) pool: PgPool,
metric_callback: Option<crate::metric::Callback>, metric_callback: Option<crate::metric::Callback>,
} }

View File

@ -24,7 +24,7 @@ pub struct SqlxSqliteConnector;
/// Defines a sqlx SQLite pool /// Defines a sqlx SQLite pool
#[derive(Clone)] #[derive(Clone)]
pub struct SqlxSqlitePoolConnection { pub struct SqlxSqlitePoolConnection {
pool: SqlitePool, pub(crate) pool: SqlitePool,
metric_callback: Option<crate::metric::Callback>, metric_callback: Option<crate::metric::Callback>,
} }