Skip to content

Commit

Permalink
Get SQLx connection pool (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 authored Jan 5, 2023
1 parent d80e61e commit d332afa
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ actix-rt = { version = "2.2.0" }
maplit = { version = "1" }
rust_decimal_macros = { version = "1" }
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" }
time = { version = "0.3", features = ["macros"] }
uuid = { version = "1", features = ["v4"] }
Expand All @@ -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-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"]
sea-orm-internal = []
sqlx-dep = []
sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"]
sqlx-mysql = ["sqlx-dep", "sea-query-binder/sqlx-mysql", "sqlx/mysql"]
Expand Down
30 changes: 30 additions & 0 deletions src/database/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/// Check if the URI is the same as the specified database backend.
/// Returns true if they match.
Expand Down
2 changes: 1 addition & 1 deletion src/driver/sqlx_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct SqlxMySqlConnector;
/// Defines a sqlx MySQL pool
#[derive(Clone)]
pub struct SqlxMySqlPoolConnection {
pool: MySqlPool,
pub(crate) pool: MySqlPool,
metric_callback: Option<crate::metric::Callback>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/driver/sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct SqlxPostgresConnector;
/// Defines a sqlx PostgreSQL pool
#[derive(Clone)]
pub struct SqlxPostgresPoolConnection {
pool: PgPool,
pub(crate) pool: PgPool,
metric_callback: Option<crate::metric::Callback>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/driver/sqlx_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct SqlxSqliteConnector;
/// Defines a sqlx SQLite pool
#[derive(Clone)]
pub struct SqlxSqlitePoolConnection {
pool: SqlitePool,
pub(crate) pool: SqlitePool,
metric_callback: Option<crate::metric::Callback>,
}

Expand Down

0 comments on commit d332afa

Please sign in to comment.