Skip to content

Commit

Permalink
Upstream Changes - 1 (#2145)
Browse files Browse the repository at this point in the history
* upstream changes

* universal `#[sea_orm_macros::test]`

* fix

* fix

* `ColumnTrait::into_returning_expr`

* fix

* fix

* Do not pub sqlx_common

---------

Co-authored-by: Chris Tsang <[email protected]>
  • Loading branch information
billy1624 and tyt2y3 authored Mar 10, 2024
1 parent b775027 commit f3967fd
Show file tree
Hide file tree
Showing 49 changed files with 1,120 additions and 1,297 deletions.
5 changes: 5 additions & 0 deletions sea-orm-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,11 @@ pub fn test(_: TokenStream, input: TokenStream) -> TokenStream {

quote::quote! (
#[test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres",
))]
#(#attrs)*
fn #name() #ret {
let _ = ::tracing_subscriber::fmt()
Expand Down
12 changes: 4 additions & 8 deletions src/database/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,14 +579,10 @@ impl DbBackend {

/// Check if the database supports `RETURNING` syntax on insert and update
pub fn support_returning(&self) -> bool {
#[cfg(not(feature = "sqlite-use-returning-for-3_35"))]
{
matches!(self, Self::Postgres)
}

#[cfg(feature = "sqlite-use-returning-for-3_35")]
{
matches!(self, Self::Postgres | Self::Sqlite)
match self {
Self::Postgres => true,
Self::Sqlite if cfg!(feature = "sqlite-use-returning-for-3_35") => true,
_ => false,
}
}
}
Expand Down
29 changes: 0 additions & 29 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,35 +160,6 @@ impl ConnectOptions {
}
}

#[cfg(feature = "sqlx-dep")]
/// Convert [ConnectOptions] into [sqlx::pool::PoolOptions]
pub fn pool_options<DB>(self) -> sqlx::pool::PoolOptions<DB>
where
DB: sqlx::Database,
{
let mut opt = sqlx::pool::PoolOptions::new();
if let Some(max_connections) = self.max_connections {
opt = opt.max_connections(max_connections);
}
if let Some(min_connections) = self.min_connections {
opt = opt.min_connections(min_connections);
}
if let Some(connect_timeout) = self.connect_timeout {
opt = opt.acquire_timeout(connect_timeout);
}
if let Some(idle_timeout) = self.idle_timeout {
opt = opt.idle_timeout(Some(idle_timeout));
}
if let Some(acquire_timeout) = self.acquire_timeout {
opt = opt.acquire_timeout(acquire_timeout);
}
if let Some(max_lifetime) = self.max_lifetime {
opt = opt.max_lifetime(Some(max_lifetime));
}
opt = opt.test_before_acquire(self.test_before_acquire);
opt
}

/// Get the database URL of the pool
pub fn get_url(&self) -> &str {
&self.url
Expand Down
675 changes: 3 additions & 672 deletions src/database/proxy.rs

Large diffs are not rendered by default.

106 changes: 4 additions & 102 deletions src/database/stream/query.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#![allow(missing_docs, unreachable_code, unused_variables)]

use tracing::instrument;

#[cfg(any(feature = "mock", feature = "proxy"))]
use std::sync::Arc;
use futures::Stream;
use std::{pin::Pin, task::Poll};
use tracing::instrument;

use futures::Stream;
#[cfg(feature = "sqlx-dep")]
use futures::TryStreamExt;

#[cfg(feature = "sqlx-dep")]
use sqlx::{pool::PoolConnection, Executor};
use sqlx::Executor;

use super::metric::MetricStream;
#[cfg(feature = "sqlx-dep")]
Expand All @@ -29,101 +26,6 @@ pub struct QueryStream {
stream: MetricStream<'this>,
}

#[cfg(feature = "sqlx-mysql")]
impl
From<(
PoolConnection<sqlx::MySql>,
Statement,
Option<crate::metric::Callback>,
)> for QueryStream
{
fn from(
(conn, stmt, metric_callback): (
PoolConnection<sqlx::MySql>,
Statement,
Option<crate::metric::Callback>,
),
) -> Self {
QueryStream::build(stmt, InnerConnection::MySql(conn), metric_callback)
}
}

#[cfg(feature = "sqlx-postgres")]
impl
From<(
PoolConnection<sqlx::Postgres>,
Statement,
Option<crate::metric::Callback>,
)> for QueryStream
{
fn from(
(conn, stmt, metric_callback): (
PoolConnection<sqlx::Postgres>,
Statement,
Option<crate::metric::Callback>,
),
) -> Self {
QueryStream::build(stmt, InnerConnection::Postgres(conn), metric_callback)
}
}

#[cfg(feature = "sqlx-sqlite")]
impl
From<(
PoolConnection<sqlx::Sqlite>,
Statement,
Option<crate::metric::Callback>,
)> for QueryStream
{
fn from(
(conn, stmt, metric_callback): (
PoolConnection<sqlx::Sqlite>,
Statement,
Option<crate::metric::Callback>,
),
) -> Self {
QueryStream::build(stmt, InnerConnection::Sqlite(conn), metric_callback)
}
}

#[cfg(feature = "mock")]
impl
From<(
Arc<crate::MockDatabaseConnection>,
Statement,
Option<crate::metric::Callback>,
)> for QueryStream
{
fn from(
(conn, stmt, metric_callback): (
Arc<crate::MockDatabaseConnection>,
Statement,
Option<crate::metric::Callback>,
),
) -> Self {
QueryStream::build(stmt, InnerConnection::Mock(conn), metric_callback)
}
}

#[cfg(feature = "proxy")]
impl
From<(
Arc<crate::ProxyDatabaseConnection>,
Statement,
Option<crate::metric::Callback>,
)> for QueryStream
{
fn from(
(conn, stmt, metric_callback): (
Arc<crate::ProxyDatabaseConnection>,
Statement,
Option<crate::metric::Callback>,
),
) -> Self {
QueryStream::build(stmt, InnerConnection::Proxy(conn), metric_callback)
}
}

impl std::fmt::Debug for QueryStream {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "QueryStream")
Expand All @@ -132,7 +34,7 @@ impl std::fmt::Debug for QueryStream {

impl QueryStream {
#[instrument(level = "trace", skip(metric_callback))]
fn build(
pub(crate) fn build(
stmt: Statement,
conn: InnerConnection,
metric_callback: Option<crate::metric::Callback>,
Expand Down
104 changes: 5 additions & 99 deletions src/database/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use crate::{sqlx_error_to_exec_err, sqlx_error_to_query_err};
use futures::lock::Mutex;
#[cfg(feature = "sqlx-dep")]
use sqlx::{pool::PoolConnection, TransactionManager};
use sqlx::TransactionManager;
use std::{future::Future, pin::Pin, sync::Arc};
use tracing::instrument;

Expand All @@ -28,91 +28,8 @@ impl std::fmt::Debug for DatabaseTransaction {
}

impl DatabaseTransaction {
#[cfg(feature = "sqlx-mysql")]
pub(crate) async fn new_mysql(
inner: PoolConnection<sqlx::MySql>,
metric_callback: Option<crate::metric::Callback>,
isolation_level: Option<IsolationLevel>,
access_mode: Option<AccessMode>,
) -> Result<DatabaseTransaction, DbErr> {
Self::begin(
Arc::new(Mutex::new(InnerConnection::MySql(inner))),
DbBackend::MySql,
metric_callback,
isolation_level,
access_mode,
)
.await
}

#[cfg(feature = "sqlx-postgres")]
pub(crate) async fn new_postgres(
inner: PoolConnection<sqlx::Postgres>,
metric_callback: Option<crate::metric::Callback>,
isolation_level: Option<IsolationLevel>,
access_mode: Option<AccessMode>,
) -> Result<DatabaseTransaction, DbErr> {
Self::begin(
Arc::new(Mutex::new(InnerConnection::Postgres(inner))),
DbBackend::Postgres,
metric_callback,
isolation_level,
access_mode,
)
.await
}

#[cfg(feature = "sqlx-sqlite")]
pub(crate) async fn new_sqlite(
inner: PoolConnection<sqlx::Sqlite>,
metric_callback: Option<crate::metric::Callback>,
isolation_level: Option<IsolationLevel>,
access_mode: Option<AccessMode>,
) -> Result<DatabaseTransaction, DbErr> {
Self::begin(
Arc::new(Mutex::new(InnerConnection::Sqlite(inner))),
DbBackend::Sqlite,
metric_callback,
isolation_level,
access_mode,
)
.await
}

#[cfg(feature = "mock")]
pub(crate) async fn new_mock(
inner: Arc<crate::MockDatabaseConnection>,
metric_callback: Option<crate::metric::Callback>,
) -> Result<DatabaseTransaction, DbErr> {
let backend = inner.get_database_backend();
Self::begin(
Arc::new(Mutex::new(InnerConnection::Mock(inner))),
backend,
metric_callback,
None,
None,
)
.await
}

#[cfg(feature = "proxy")]
pub(crate) async fn new_proxy(
inner: Arc<crate::ProxyDatabaseConnection>,
metric_callback: Option<crate::metric::Callback>,
) -> Result<DatabaseTransaction, DbErr> {
let backend = inner.get_database_backend();
Self::begin(
Arc::new(Mutex::new(InnerConnection::Proxy(inner))),
backend,
metric_callback,
None,
None,
)
.await
}

#[instrument(level = "trace", skip(metric_callback))]
async fn begin(
pub(crate) async fn begin(
conn: Arc<Mutex<InnerConnection>>,
backend: DbBackend,
metric_callback: Option<crate::metric::Callback>,
Expand Down Expand Up @@ -293,17 +210,6 @@ impl DatabaseTransaction {
}
Ok(())
}

#[cfg(feature = "sqlx-dep")]
fn map_err_ignore_not_found<T: std::fmt::Debug>(
err: Result<Option<T>, sqlx::Error>,
) -> Result<Option<T>, DbErr> {
if let Err(sqlx::Error::RowNotFound) = err {
Ok(None)
} else {
err.map_err(sqlx_error_to_query_err)
}
}
}

impl Drop for DatabaseTransaction {
Expand Down Expand Up @@ -411,7 +317,7 @@ impl ConnectionTrait for DatabaseTransaction {
let query = crate::driver::sqlx_mysql::sqlx_query(&stmt);
let conn: &mut sqlx::MySqlConnection = &mut *conn;
crate::metric::metric!(self.metric_callback, &stmt, {
Self::map_err_ignore_not_found(
crate::sqlx_map_err_ignore_not_found(
query.fetch_one(conn).await.map(|row| Some(row.into())),
)
})
Expand All @@ -421,7 +327,7 @@ impl ConnectionTrait for DatabaseTransaction {
let query = crate::driver::sqlx_postgres::sqlx_query(&stmt);
let conn: &mut sqlx::PgConnection = &mut *conn;
crate::metric::metric!(self.metric_callback, &stmt, {
Self::map_err_ignore_not_found(
crate::sqlx_map_err_ignore_not_found(
query.fetch_one(conn).await.map(|row| Some(row.into())),
)
})
Expand All @@ -431,7 +337,7 @@ impl ConnectionTrait for DatabaseTransaction {
let query = crate::driver::sqlx_sqlite::sqlx_query(&stmt);
let conn: &mut sqlx::SqliteConnection = &mut *conn;
crate::metric::metric!(self.metric_callback, &stmt, {
Self::map_err_ignore_not_found(
crate::sqlx_map_err_ignore_not_found(
query.fetch_one(conn).await.map(|row| Some(row.into())),
)
})
Expand Down
36 changes: 36 additions & 0 deletions src/driver/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,39 @@ impl MockDatabaseConnection {
self.mocker.lock().map_err(query_err)?.ping()
}
}

impl
From<(
Arc<crate::MockDatabaseConnection>,
Statement,
Option<crate::metric::Callback>,
)> for crate::QueryStream
{
fn from(
(conn, stmt, metric_callback): (
Arc<crate::MockDatabaseConnection>,
Statement,
Option<crate::metric::Callback>,
),
) -> Self {
crate::QueryStream::build(stmt, crate::InnerConnection::Mock(conn), metric_callback)
}
}

impl crate::DatabaseTransaction {
pub(crate) async fn new_mock(
inner: Arc<crate::MockDatabaseConnection>,
metric_callback: Option<crate::metric::Callback>,
) -> Result<crate::DatabaseTransaction, DbErr> {
use futures::lock::Mutex;
let backend = inner.get_database_backend();
Self::begin(
Arc::new(Mutex::new(crate::InnerConnection::Mock(inner))),
backend,
metric_callback,
None,
None,
)
.await
}
}
2 changes: 1 addition & 1 deletion src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use mock::*;
#[cfg(feature = "proxy")]
pub use proxy::*;
#[cfg(feature = "sqlx-dep")]
pub use sqlx_common::*;
pub(crate) use sqlx_common::*;
#[cfg(feature = "sqlx-mysql")]
pub use sqlx_mysql::*;
#[cfg(feature = "sqlx-postgres")]
Expand Down
Loading

0 comments on commit f3967fd

Please sign in to comment.