From 03d90d8630f40d0a81fb2c8a24605402fb63b327 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 9 Nov 2021 16:14:51 +0800 Subject: [PATCH 01/18] Docs --- src/database/statement.rs | 7 ++++--- src/executor/select.rs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/database/statement.rs b/src/database/statement.rs index ce911295..a1ba8126 100644 --- a/src/database/statement.rs +++ b/src/database/statement.rs @@ -8,9 +8,10 @@ use std::fmt; pub struct Statement { /// The SQL query pub sql: String, - /// The values for the SQL statement + /// The values for the SQL statement's parameters pub values: Option, - /// The database backend to use + /// The database backend this statement is constructed for. + /// The SQL dialect and values should be valid for the DbBackend. pub db_backend: DbBackend, } @@ -31,7 +32,7 @@ impl Statement { } /// Create a SQL statement from a [crate::DatabaseBackend], a - /// raw SQL statement and defined values + /// raw SQL statement and param values pub fn from_sql_and_values(db_backend: DbBackend, sql: &str, values: I) -> Self where I: IntoIterator, diff --git a/src/executor/select.rs b/src/executor/select.rs index 5acdae02..f9bbd756 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -11,7 +11,7 @@ use std::pin::Pin; #[cfg(feature = "with-json")] use crate::JsonValue; -/// Defines a type to do `SELECT` operations though a [SelectStatement] on a Model +/// Defines a type to do `SELECT` operations through a [SelectStatement] on a Model #[derive(Clone, Debug)] pub struct Selector where From 23498892b06fe9c3e0023a8d9ec0291a766b771f Mon Sep 17 00:00:00 2001 From: jasper Date: Tue, 9 Nov 2021 21:21:42 +0800 Subject: [PATCH 02/18] Add PaginatorTrait and CountTrait --- src/executor/paginator.rs | 86 +++++++++++++++++++++++++++++++++++++-- src/executor/select.rs | 58 +------------------------- tests/crud/updates.rs | 2 +- 3 files changed, 86 insertions(+), 60 deletions(-) diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index fd8e822c..727f9076 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -1,4 +1,4 @@ -use crate::{error::*, ConnectionTrait, DbBackend, SelectorTrait}; +use crate::{ConnectionTrait, DbBackend, EntityTrait, FromQueryResult, Select, SelectModel, SelectTwo, SelectTwoModel, Selector, SelectorTrait, error::*}; use async_stream::stream; use futures::Stream; use sea_query::{Alias, Expr, SelectStatement}; @@ -95,7 +95,7 @@ where /// /// ```rust /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, MockDatabase, DbBackend}; + /// # use sea_orm::{error::*, MockDatabase, DbBackend, PaginatorTrait}; /// # let owned_db = MockDatabase::new(DbBackend::Postgres).into_connection(); /// # let db = &owned_db; /// # let _: Result<(), DbErr> = smol::block_on(async { @@ -123,7 +123,7 @@ where /// /// ```rust /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, MockDatabase, DbBackend}; + /// # use sea_orm::{error::*, MockDatabase, DbBackend, PaginatorTrait}; /// # let owned_db = MockDatabase::new(DbBackend::Postgres).into_connection(); /// # let db = &owned_db; /// # let _: Result<(), DbErr> = smol::block_on(async { @@ -155,12 +155,92 @@ where } } +/// Used to enforce constraints on any type that wants to paginate results +pub trait PaginatorTrait<'db, C> +where + C: ConnectionTrait<'db>, +{ + /// Select operation + type Selector: SelectorTrait + 'db; + + /// Paginate the result of a select operation. + fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, Self::Selector>; +} + +impl<'db, C, S> PaginatorTrait<'db, C> for Selector +where + C: ConnectionTrait<'db>, + S: SelectorTrait + 'db, +{ + type Selector = S; + + fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, S> { + Paginator { + query: self.query, + page: 0, + page_size, + db, + selector: PhantomData, + } + } +} + +impl<'db, C, M, E> PaginatorTrait<'db, C> for Select +where + C: ConnectionTrait<'db>, + E: EntityTrait, + M: FromQueryResult + Sized + 'db, +{ + type Selector = SelectModel; + + fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, Self::Selector> { + self.into_model().paginate(db, page_size) + } +} + +impl<'db, C, M, N, E, F> PaginatorTrait<'db, C> for SelectTwo +where + C: ConnectionTrait<'db>, + E: EntityTrait, + F: EntityTrait, + M: FromQueryResult + Sized + 'db, + N: FromQueryResult + Sized + 'db, +{ + type Selector = SelectTwoModel; + + fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, Self::Selector> { + self.into_model().paginate(db, page_size) + } +} + +/// Used to enforce constraints on any type that wants to count results using pagination. +#[async_trait::async_trait] +pub trait CountTrait<'db, C>: PaginatorTrait<'db, C> +where + C: ConnectionTrait<'db>, +{ + /// Perform a count on the paginated results + async fn count(self, db: &'db C) -> Result; +} + +#[async_trait::async_trait] +impl<'db, C, P, S> CountTrait<'db, C> for P +where + C: ConnectionTrait<'db>, + P: PaginatorTrait<'db, C, Selector = S> + Send, + S: SelectorTrait + Send + Sync + 'db +{ + async fn count(self, db:&'db C) -> Result { + self.paginate(db, 1).num_items().await + } +} #[cfg(test)] #[cfg(feature = "mock")] mod tests { use crate::entity::prelude::*; use crate::{tests_cfg::*, ConnectionTrait}; use crate::{DatabaseConnection, DbBackend, MockDatabase, Transaction}; + use super::*; use futures::TryStreamExt; use sea_query::{Alias, Expr, SelectStatement, Value}; diff --git a/src/executor/select.rs b/src/executor/select.rs index f9bbd756..2dd9e3c3 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -1,6 +1,6 @@ use crate::{ error::*, ConnectionTrait, EntityTrait, FromQueryResult, IdenStatic, Iterable, ModelTrait, - Paginator, PrimaryKeyToColumn, QueryResult, Select, SelectA, SelectB, SelectTwo, SelectTwoMany, + PrimaryKeyToColumn, QueryResult, Select, SelectA, SelectB, SelectTwo, SelectTwoMany, Statement, TryGetableMany, }; use futures::{Stream, TryStreamExt}; @@ -17,7 +17,7 @@ pub struct Selector where S: SelectorTrait, { - query: SelectStatement, + pub(crate) query: SelectStatement, selector: S, } @@ -276,26 +276,6 @@ where { self.into_model().stream(db).await } - - /// Paginate the results of a SELECT operation on a Model - pub fn paginate<'a, C>( - self, - db: &'a C, - page_size: usize, - ) -> Paginator<'a, C, SelectModel> - where - C: ConnectionTrait<'a>, - { - self.into_model().paginate(db, page_size) - } - - /// Perform a `COUNT` operation on a items on a Model using pagination - pub async fn count<'a, C>(self, db: &'a C) -> Result - where - C: ConnectionTrait<'a>, - { - self.paginate(db, 1).num_items().await - } } impl SelectTwo @@ -350,26 +330,6 @@ where { self.into_model().stream(db).await } - - /// Paginate the results of a select operation on two models - pub fn paginate<'a, C>( - self, - db: &'a C, - page_size: usize, - ) -> Paginator<'a, C, SelectTwoModel> - where - C: ConnectionTrait<'a>, - { - self.into_model().paginate(db, page_size) - } - - /// Perform a count on the paginated results - pub async fn count<'a, C>(self, db: &'a C) -> Result - where - C: ConnectionTrait<'a>, - { - self.paginate(db, 1).num_items().await - } } impl SelectTwoMany @@ -499,20 +459,6 @@ where futures::future::ready(S::from_raw_query_result(row)) }))) } - - /// Paginate the result of a select operation on a Model - pub fn paginate<'a, C>(self, db: &'a C, page_size: usize) -> Paginator<'a, C, S> - where - C: ConnectionTrait<'a>, - { - Paginator { - query: self.query, - page: 0, - page_size, - db, - selector: PhantomData, - } - } } impl SelectorRaw diff --git a/tests/crud/updates.rs b/tests/crud/updates.rs index 262031ef..04aea292 100644 --- a/tests/crud/updates.rs +++ b/tests/crud/updates.rs @@ -1,6 +1,6 @@ pub use super::*; use rust_decimal_macros::dec; -use sea_orm::DbErr; +use sea_orm::{DbErr, CountTrait}; use uuid::Uuid; pub async fn test_update_cake(db: &DbConn) { From 4b11a106805cdb1afc5b696aea633fd2895e2db3 Mon Sep 17 00:00:00 2001 From: jasper Date: Tue, 9 Nov 2021 22:19:31 +0800 Subject: [PATCH 03/18] Update examples and test --- examples/actix4_example/src/main.rs | 2 +- examples/actix_example/src/main.rs | 2 +- examples/axum_example/src/main.rs | 2 +- examples/basic/src/operation.rs | 2 +- examples/basic/src/select.rs | 2 +- examples/rocket_example/src/main.rs | 2 +- tests/basic.rs | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/actix4_example/src/main.rs b/examples/actix4_example/src/main.rs index 6a52b81b..c30b20d7 100644 --- a/examples/actix4_example/src/main.rs +++ b/examples/actix4_example/src/main.rs @@ -5,7 +5,7 @@ use actix_web::{ use listenfd::ListenFd; use sea_orm::DatabaseConnection; -use sea_orm::{entity::*, query::*}; +use sea_orm::{entity::*, query::*, PaginatorTrait}; use serde::{Deserialize, Serialize}; use std::env; use tera::Tera; diff --git a/examples/actix_example/src/main.rs b/examples/actix_example/src/main.rs index e2d1e956..e7bae507 100644 --- a/examples/actix_example/src/main.rs +++ b/examples/actix_example/src/main.rs @@ -4,7 +4,7 @@ use actix_web::{ }; use listenfd::ListenFd; use sea_orm::DatabaseConnection; -use sea_orm::{entity::*, query::*}; +use sea_orm::{entity::*, query::*, PaginatorTrait}; use serde::{Deserialize, Serialize}; use std::env; use tera::Tera; diff --git a/examples/axum_example/src/main.rs b/examples/axum_example/src/main.rs index 7b0b428e..fd07fa72 100644 --- a/examples/axum_example/src/main.rs +++ b/examples/axum_example/src/main.rs @@ -12,7 +12,7 @@ use axum::{ }; use flash::{get_flash_cookie, post_response, PostResponse}; use post::Entity as Post; -use sea_orm::{prelude::*, Database, QueryOrder, Set}; +use sea_orm::{prelude::*, Database, QueryOrder, Set, PaginatorTrait}; use serde::{Deserialize, Serialize}; use std::str::FromStr; use std::{env, net::SocketAddr}; diff --git a/examples/basic/src/operation.rs b/examples/basic/src/operation.rs index 34e5441f..49d23919 100644 --- a/examples/basic/src/operation.rs +++ b/examples/basic/src/operation.rs @@ -1,5 +1,5 @@ use super::*; -use sea_orm::{entity::*, error::*, query::*, DbConn}; +use sea_orm::{entity::*, error::*, DbConn}; pub async fn all_about_operation(db: &DbConn) -> Result<(), DbErr> { insert_and_update(db).await?; diff --git a/examples/basic/src/select.rs b/examples/basic/src/select.rs index ce26f9e2..ae7e1912 100644 --- a/examples/basic/src/select.rs +++ b/examples/basic/src/select.rs @@ -1,5 +1,5 @@ use super::*; -use sea_orm::{entity::*, error::*, query::*, DbConn, FromQueryResult}; +use sea_orm::{entity::*, error::*, query::*, DbConn, FromQueryResult, PaginatorTrait}; pub async fn all_about_select(db: &DbConn) -> Result<(), DbErr> { find_all(db).await?; diff --git a/examples/rocket_example/src/main.rs b/examples/rocket_example/src/main.rs index e0b01bea..df3e841f 100644 --- a/examples/rocket_example/src/main.rs +++ b/examples/rocket_example/src/main.rs @@ -9,7 +9,7 @@ use rocket::response::{Flash, Redirect}; use rocket::{Build, Request, Rocket}; use rocket_dyn_templates::{context, Template}; -use sea_orm::{entity::*, query::*}; +use sea_orm::{entity::*, query::*, PaginatorTrait}; use sea_orm_rocket::{Connection, Database}; mod pool; diff --git a/tests/basic.rs b/tests/basic.rs index ef379779..74d6a3ba 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,6 +1,6 @@ pub mod common; -pub use sea_orm::{entity::*, error::*, query::*, sea_query, tests_cfg::*, Database, DbConn}; +pub use sea_orm::{entity::*, error::*, query::*, sea_query, tests_cfg::*, Database, DbConn, CountTrait}; // cargo test --features sqlx-sqlite,runtime-async-std-native-tls --test basic #[sea_orm_macros::test] From 05181994d32284f011faeb9b7b77b10d7db31261 Mon Sep 17 00:00:00 2001 From: jasper Date: Wed, 10 Nov 2021 08:57:05 +0800 Subject: [PATCH 04/18] Move count to PaginatorTrait --- src/executor/paginator.rs | 40 ++++++++++++++------------------------- tests/basic.rs | 2 +- tests/crud/updates.rs | 2 +- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index 727f9076..0775848a 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -155,22 +155,31 @@ where } } +#[async_trait::async_trait] /// Used to enforce constraints on any type that wants to paginate results pub trait PaginatorTrait<'db, C> where C: ConnectionTrait<'db>, { /// Select operation - type Selector: SelectorTrait + 'db; + type Selector: SelectorTrait + Send + Sync + 'db; /// Paginate the result of a select operation. fn paginate(self, db: &'db C, page_size: usize) -> Paginator<'db, C, Self::Selector>; + + /// Perform a count on the paginated results + async fn count(self, db: &'db C) -> Result + where + Self: Send + Sized + { + self.paginate(db, 1).num_items().await + } } impl<'db, C, S> PaginatorTrait<'db, C> for Selector where C: ConnectionTrait<'db>, - S: SelectorTrait + 'db, + S: SelectorTrait + Send + Sync + 'db, { type Selector = S; @@ -189,7 +198,7 @@ impl<'db, C, M, E> PaginatorTrait<'db, C> for Select where C: ConnectionTrait<'db>, E: EntityTrait, - M: FromQueryResult + Sized + 'db, + M: FromQueryResult + Sized + Send + Sync + 'db, { type Selector = SelectModel; @@ -203,8 +212,8 @@ where C: ConnectionTrait<'db>, E: EntityTrait, F: EntityTrait, - M: FromQueryResult + Sized + 'db, - N: FromQueryResult + Sized + 'db, + M: FromQueryResult + Sized + Send + Sync + 'db, + N: FromQueryResult + Sized + Send + Sync + 'db, { type Selector = SelectTwoModel; @@ -213,27 +222,6 @@ where } } -/// Used to enforce constraints on any type that wants to count results using pagination. -#[async_trait::async_trait] -pub trait CountTrait<'db, C>: PaginatorTrait<'db, C> -where - C: ConnectionTrait<'db>, -{ - /// Perform a count on the paginated results - async fn count(self, db: &'db C) -> Result; -} - -#[async_trait::async_trait] -impl<'db, C, P, S> CountTrait<'db, C> for P -where - C: ConnectionTrait<'db>, - P: PaginatorTrait<'db, C, Selector = S> + Send, - S: SelectorTrait + Send + Sync + 'db -{ - async fn count(self, db:&'db C) -> Result { - self.paginate(db, 1).num_items().await - } -} #[cfg(test)] #[cfg(feature = "mock")] mod tests { diff --git a/tests/basic.rs b/tests/basic.rs index 74d6a3ba..18d80a6b 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,6 +1,6 @@ pub mod common; -pub use sea_orm::{entity::*, error::*, query::*, sea_query, tests_cfg::*, Database, DbConn, CountTrait}; +pub use sea_orm::{entity::*, error::*, query::*, sea_query, tests_cfg::*, Database, DbConn, PaginatorTrait}; // cargo test --features sqlx-sqlite,runtime-async-std-native-tls --test basic #[sea_orm_macros::test] diff --git a/tests/crud/updates.rs b/tests/crud/updates.rs index 04aea292..4a3a05a0 100644 --- a/tests/crud/updates.rs +++ b/tests/crud/updates.rs @@ -1,6 +1,6 @@ pub use super::*; use rust_decimal_macros::dec; -use sea_orm::{DbErr, CountTrait}; +use sea_orm::{DbErr, PaginatorTrait}; use uuid::Uuid; pub async fn test_update_cake(db: &DbConn) { From 1905f6243052b5480b4e3f02111dcb9f74190524 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Wed, 10 Nov 2021 14:58:06 +0800 Subject: [PATCH 05/18] cargo fmt --- src/executor/paginator.rs | 9 ++++++--- src/executor/select.rs | 4 ++-- tests/basic.rs | 4 +++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index 0775848a..7216d45c 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -1,4 +1,7 @@ -use crate::{ConnectionTrait, DbBackend, EntityTrait, FromQueryResult, Select, SelectModel, SelectTwo, SelectTwoModel, Selector, SelectorTrait, error::*}; +use crate::{ + error::*, ConnectionTrait, DbBackend, EntityTrait, FromQueryResult, Select, SelectModel, + SelectTwo, SelectTwoModel, Selector, SelectorTrait, +}; use async_stream::stream; use futures::Stream; use sea_query::{Alias, Expr, SelectStatement}; @@ -170,7 +173,7 @@ where /// Perform a count on the paginated results async fn count(self, db: &'db C) -> Result where - Self: Send + Sized + Self: Send + Sized, { self.paginate(db, 1).num_items().await } @@ -225,10 +228,10 @@ where #[cfg(test)] #[cfg(feature = "mock")] mod tests { + use super::*; use crate::entity::prelude::*; use crate::{tests_cfg::*, ConnectionTrait}; use crate::{DatabaseConnection, DbBackend, MockDatabase, Transaction}; - use super::*; use futures::TryStreamExt; use sea_query::{Alias, Expr, SelectStatement, Value}; diff --git a/src/executor/select.rs b/src/executor/select.rs index 2dd9e3c3..832096b7 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -1,7 +1,7 @@ use crate::{ error::*, ConnectionTrait, EntityTrait, FromQueryResult, IdenStatic, Iterable, ModelTrait, - PrimaryKeyToColumn, QueryResult, Select, SelectA, SelectB, SelectTwo, SelectTwoMany, - Statement, TryGetableMany, + PrimaryKeyToColumn, QueryResult, Select, SelectA, SelectB, SelectTwo, SelectTwoMany, Statement, + TryGetableMany, }; use futures::{Stream, TryStreamExt}; use sea_query::SelectStatement; diff --git a/tests/basic.rs b/tests/basic.rs index 18d80a6b..4b6f5b0c 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,6 +1,8 @@ pub mod common; -pub use sea_orm::{entity::*, error::*, query::*, sea_query, tests_cfg::*, Database, DbConn, PaginatorTrait}; +pub use sea_orm::{ + entity::*, error::*, query::*, sea_query, tests_cfg::*, Database, DbConn, PaginatorTrait, +}; // cargo test --features sqlx-sqlite,runtime-async-std-native-tls --test basic #[sea_orm_macros::test] From ac804a16427afeb823dc6062260e29d24c63b99a Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Wed, 10 Nov 2021 15:15:50 +0800 Subject: [PATCH 06/18] Imports `PaginatorTrait` by default making it backward compatible --- examples/actix4_example/src/main.rs | 2 +- examples/actix_example/src/main.rs | 2 +- examples/axum_example/src/main.rs | 2 +- examples/basic/src/select.rs | 2 +- examples/rocket_example/src/main.rs | 2 +- src/entity/mod.rs | 3 +++ src/entity/prelude.rs | 4 ++-- src/executor/paginator.rs | 4 ++-- tests/basic.rs | 4 +--- tests/crud/updates.rs | 2 +- 10 files changed, 14 insertions(+), 13 deletions(-) diff --git a/examples/actix4_example/src/main.rs b/examples/actix4_example/src/main.rs index c30b20d7..6a52b81b 100644 --- a/examples/actix4_example/src/main.rs +++ b/examples/actix4_example/src/main.rs @@ -5,7 +5,7 @@ use actix_web::{ use listenfd::ListenFd; use sea_orm::DatabaseConnection; -use sea_orm::{entity::*, query::*, PaginatorTrait}; +use sea_orm::{entity::*, query::*}; use serde::{Deserialize, Serialize}; use std::env; use tera::Tera; diff --git a/examples/actix_example/src/main.rs b/examples/actix_example/src/main.rs index e7bae507..e2d1e956 100644 --- a/examples/actix_example/src/main.rs +++ b/examples/actix_example/src/main.rs @@ -4,7 +4,7 @@ use actix_web::{ }; use listenfd::ListenFd; use sea_orm::DatabaseConnection; -use sea_orm::{entity::*, query::*, PaginatorTrait}; +use sea_orm::{entity::*, query::*}; use serde::{Deserialize, Serialize}; use std::env; use tera::Tera; diff --git a/examples/axum_example/src/main.rs b/examples/axum_example/src/main.rs index fd07fa72..7b0b428e 100644 --- a/examples/axum_example/src/main.rs +++ b/examples/axum_example/src/main.rs @@ -12,7 +12,7 @@ use axum::{ }; use flash::{get_flash_cookie, post_response, PostResponse}; use post::Entity as Post; -use sea_orm::{prelude::*, Database, QueryOrder, Set, PaginatorTrait}; +use sea_orm::{prelude::*, Database, QueryOrder, Set}; use serde::{Deserialize, Serialize}; use std::str::FromStr; use std::{env, net::SocketAddr}; diff --git a/examples/basic/src/select.rs b/examples/basic/src/select.rs index ae7e1912..ce26f9e2 100644 --- a/examples/basic/src/select.rs +++ b/examples/basic/src/select.rs @@ -1,5 +1,5 @@ use super::*; -use sea_orm::{entity::*, error::*, query::*, DbConn, FromQueryResult, PaginatorTrait}; +use sea_orm::{entity::*, error::*, query::*, DbConn, FromQueryResult}; pub async fn all_about_select(db: &DbConn) -> Result<(), DbErr> { find_all(db).await?; diff --git a/examples/rocket_example/src/main.rs b/examples/rocket_example/src/main.rs index df3e841f..e0b01bea 100644 --- a/examples/rocket_example/src/main.rs +++ b/examples/rocket_example/src/main.rs @@ -9,7 +9,7 @@ use rocket::response::{Flash, Redirect}; use rocket::{Build, Request, Rocket}; use rocket_dyn_templates::{context, Template}; -use sea_orm::{entity::*, query::*, PaginatorTrait}; +use sea_orm::{entity::*, query::*}; use sea_orm_rocket::{Connection, Database}; mod pool; diff --git a/src/entity/mod.rs b/src/entity/mod.rs index 6b413bdf..f85f1ee1 100644 --- a/src/entity/mod.rs +++ b/src/entity/mod.rs @@ -117,3 +117,6 @@ pub use model::*; // pub use prelude::*; pub use primary_key::*; pub use relation::*; + +// Imports paginator utility by default making it backward compatible. +pub use crate::PaginatorTrait; diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index 211cf853..28f5fe70 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -1,8 +1,8 @@ pub use crate::{ error::*, ActiveEnum, ActiveModelBehavior, ActiveModelTrait, ColumnDef, ColumnTrait, ColumnType, DatabaseConnection, DbConn, EntityName, EntityTrait, EnumIter, ForeignKeyAction, - Iden, IdenStatic, Linked, ModelTrait, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, - QueryResult, Related, RelationDef, RelationTrait, Select, Value, + Iden, IdenStatic, Linked, ModelTrait, PaginatorTrait, PrimaryKeyToColumn, PrimaryKeyTrait, + QueryFilter, QueryResult, Related, RelationDef, RelationTrait, Select, Value, }; #[cfg(feature = "macros")] diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index 7216d45c..c8bd503a 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -98,7 +98,7 @@ where /// /// ```rust /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, MockDatabase, DbBackend, PaginatorTrait}; + /// # use sea_orm::{error::*, MockDatabase, DbBackend}; /// # let owned_db = MockDatabase::new(DbBackend::Postgres).into_connection(); /// # let db = &owned_db; /// # let _: Result<(), DbErr> = smol::block_on(async { @@ -126,7 +126,7 @@ where /// /// ```rust /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, MockDatabase, DbBackend, PaginatorTrait}; + /// # use sea_orm::{error::*, MockDatabase, DbBackend}; /// # let owned_db = MockDatabase::new(DbBackend::Postgres).into_connection(); /// # let db = &owned_db; /// # let _: Result<(), DbErr> = smol::block_on(async { diff --git a/tests/basic.rs b/tests/basic.rs index 4b6f5b0c..ef379779 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,8 +1,6 @@ pub mod common; -pub use sea_orm::{ - entity::*, error::*, query::*, sea_query, tests_cfg::*, Database, DbConn, PaginatorTrait, -}; +pub use sea_orm::{entity::*, error::*, query::*, sea_query, tests_cfg::*, Database, DbConn}; // cargo test --features sqlx-sqlite,runtime-async-std-native-tls --test basic #[sea_orm_macros::test] diff --git a/tests/crud/updates.rs b/tests/crud/updates.rs index 4a3a05a0..262031ef 100644 --- a/tests/crud/updates.rs +++ b/tests/crud/updates.rs @@ -1,6 +1,6 @@ pub use super::*; use rust_decimal_macros::dec; -use sea_orm::{DbErr, PaginatorTrait}; +use sea_orm::DbErr; use uuid::Uuid; pub async fn test_update_cake(db: &DbConn) { From 9c1646b1985cd6e0effc48e9223fd091df5d17da Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Wed, 10 Nov 2021 15:53:37 +0800 Subject: [PATCH 07/18] Fixup --- src/entity/mod.rs | 3 --- src/query/mod.rs | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/entity/mod.rs b/src/entity/mod.rs index f85f1ee1..6b413bdf 100644 --- a/src/entity/mod.rs +++ b/src/entity/mod.rs @@ -117,6 +117,3 @@ pub use model::*; // pub use prelude::*; pub use primary_key::*; pub use relation::*; - -// Imports paginator utility by default making it backward compatible. -pub use crate::PaginatorTrait; diff --git a/src/query/mod.rs b/src/query/mod.rs index fcf8b168..474fa5ba 100644 --- a/src/query/mod.rs +++ b/src/query/mod.rs @@ -23,3 +23,6 @@ pub use update::*; pub use util::*; pub use crate::{ConnectionTrait, InsertResult, Statement, UpdateResult, Value, Values}; + +// Imports paginator utility by default making it backward compatible. +pub use crate::PaginatorTrait; From f975223d9ee3f3868636b258ab3d3b78948e8304 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Wed, 10 Nov 2021 16:08:06 +0800 Subject: [PATCH 08/18] Fixup --- tests/crud/updates.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/crud/updates.rs b/tests/crud/updates.rs index 262031ef..55af604e 100644 --- a/tests/crud/updates.rs +++ b/tests/crud/updates.rs @@ -1,6 +1,6 @@ pub use super::*; use rust_decimal_macros::dec; -use sea_orm::DbErr; +use sea_orm::{query::*, DbErr}; use uuid::Uuid; pub async fn test_update_cake(db: &DbConn) { From a95d99e186b28c7f046e30ce9d9c02b3bf27a712 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 11 Nov 2021 12:12:53 +0800 Subject: [PATCH 09/18] Refactoring --- src/query/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/query/mod.rs b/src/query/mod.rs index 474fa5ba..3168e81e 100644 --- a/src/query/mod.rs +++ b/src/query/mod.rs @@ -22,7 +22,6 @@ pub use traits::*; pub use update::*; pub use util::*; -pub use crate::{ConnectionTrait, InsertResult, Statement, UpdateResult, Value, Values}; - -// Imports paginator utility by default making it backward compatible. -pub use crate::PaginatorTrait; +pub use crate::{ + ConnectionTrait, InsertResult, PaginatorTrait, Statement, UpdateResult, Value, Values, +}; From f504ad1eef41d892925d5924a61bd911e1e567e9 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 11 Nov 2021 15:11:52 +0800 Subject: [PATCH 10/18] Refactor `Schema` --- src/schema/entity.rs | 20 +++++++++++--------- src/schema/mod.rs | 16 ++++++++++++++-- tests/common/setup/mod.rs | 7 +++++-- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/schema/entity.rs b/src/schema/entity.rs index c24a1385..8b0ad144 100644 --- a/src/schema/entity.rs +++ b/src/schema/entity.rs @@ -9,27 +9,27 @@ use sea_query::{ impl Schema { /// Creates Postgres enums from an Entity. See [TypeCreateStatement] for more details - pub fn create_enum_from_entity(entity: E, db_backend: DbBackend) -> Vec + pub fn create_enum_from_entity(&self, entity: E) -> Vec where E: EntityTrait, { - create_enum_from_entity(entity, db_backend) + create_enum_from_entity(entity, self.backend) } /// Creates a table from an Entity. See [TableCreateStatement] for more details - pub fn create_table_from_entity(entity: E, db_backend: DbBackend) -> TableCreateStatement + pub fn create_table_from_entity(&self, entity: E) -> TableCreateStatement where E: EntityTrait, { - create_table_from_entity(entity, db_backend) + create_table_from_entity(entity, self.backend) } } -pub(crate) fn create_enum_from_entity(_: E, db_backend: DbBackend) -> Vec +pub(crate) fn create_enum_from_entity(_: E, backend: DbBackend) -> Vec where E: EntityTrait, { - if matches!(db_backend, DbBackend::MySql | DbBackend::Sqlite) { + if matches!(backend, DbBackend::MySql | DbBackend::Sqlite) { return Vec::new(); } let mut vec = Vec::new(); @@ -52,7 +52,7 @@ where vec } -pub(crate) fn create_table_from_entity(entity: E, db_backend: DbBackend) -> TableCreateStatement +pub(crate) fn create_table_from_entity(entity: E, backend: DbBackend) -> TableCreateStatement where E: EntityTrait, { @@ -61,7 +61,7 @@ where for column in E::Column::iter() { let orm_column_def = column.def(); let types = match orm_column_def.col_type { - ColumnType::Enum(s, variants) => match db_backend { + ColumnType::Enum(s, variants) => match backend { DbBackend::MySql => { ColumnType::Custom(format!("ENUM('{}')", variants.join("', '"))) } @@ -175,8 +175,10 @@ mod tests { #[test] fn test_create_table_from_entity() { + let schema = Schema::new(DbBackend::MySql); assert_eq!( - Schema::create_table_from_entity(CakeFillingPrice, DbBackend::MySql) + schema + .create_table_from_entity(CakeFillingPrice) .to_string(MysqlQueryBuilder), Table::create() .table(CakeFillingPrice) diff --git a/src/schema/mod.rs b/src/schema/mod.rs index 3a5ce734..e89d9cc5 100644 --- a/src/schema/mod.rs +++ b/src/schema/mod.rs @@ -1,5 +1,17 @@ +use crate::DbBackend; + mod entity; -/// This structure defines a schema for a table +/// This is a helper struct to convert [`EntityTrait`](crate::EntityTrait) +/// into different [`sea_query`](crate::sea_query) statements. #[derive(Debug)] -pub struct Schema; +pub struct Schema { + backend: DbBackend, +} + +impl Schema { + /// Create a helper for a specific database backend + pub fn new(backend: DbBackend) -> Self { + Self { backend } + } +} diff --git a/tests/common/setup/mod.rs b/tests/common/setup/mod.rs index fae61984..62e06b65 100644 --- a/tests/common/setup/mod.rs +++ b/tests/common/setup/mod.rs @@ -108,7 +108,9 @@ where } let expect_stmts: Vec = creates.iter().map(|stmt| builder.build(stmt)).collect(); - let create_from_entity_stmts: Vec = Schema::create_enum_from_entity(entity, builder) + let schema = Schema::new(builder); + let create_from_entity_stmts: Vec = schema + .create_enum_from_entity(entity) .iter() .map(|stmt| builder.build(stmt)) .collect(); @@ -131,8 +133,9 @@ where E: EntityTrait, { let builder = db.get_database_backend(); + let schema = Schema::new(builder); assert_eq!( - builder.build(&Schema::create_table_from_entity(entity, builder)), + builder.build(&schema.create_table_from_entity(entity)), builder.build(create) ); From da705f6629db0dce4bf78de0bb6240285091fa9a Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 11 Nov 2021 16:58:11 +0800 Subject: [PATCH 11/18] Detailed connection errors --- src/driver/sqlx_common.rs | 5 +++++ src/driver/sqlx_mysql.rs | 9 ++++----- src/driver/sqlx_postgres.rs | 9 ++++----- src/driver/sqlx_sqlite.rs | 9 ++++----- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/driver/sqlx_common.rs b/src/driver/sqlx_common.rs index 5e035c9c..18ca059d 100644 --- a/src/driver/sqlx_common.rs +++ b/src/driver/sqlx_common.rs @@ -9,3 +9,8 @@ pub fn sqlx_error_to_exec_err(err: sqlx::Error) -> DbErr { pub fn sqlx_error_to_query_err(err: sqlx::Error) -> DbErr { DbErr::Query(err.to_string()) } + +/// Converts an [sqlx::error] connection error to a [DbErr] +pub fn sqlx_error_to_conn_err(err: sqlx::Error) -> DbErr { + DbErr::Conn(err.to_string()) +} diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index b2b89c68..3f9936e4 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -41,12 +41,11 @@ impl SqlxMySqlConnector { use sqlx::ConnectOptions; opt.disable_statement_logging(); } - if let Ok(pool) = options.pool_options().connect_with(opt).await { - Ok(DatabaseConnection::SqlxMySqlPoolConnection( + match options.pool_options().connect_with(opt).await { + Ok(pool) => Ok(DatabaseConnection::SqlxMySqlPoolConnection( SqlxMySqlPoolConnection { pool }, - )) - } else { - Err(DbErr::Conn("Failed to connect.".to_owned())) + )), + Err(e) => Err(sqlx_error_to_conn_err(e)), } } } diff --git a/src/driver/sqlx_postgres.rs b/src/driver/sqlx_postgres.rs index 84645a58..3abebe31 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -41,12 +41,11 @@ impl SqlxPostgresConnector { use sqlx::ConnectOptions; opt.disable_statement_logging(); } - if let Ok(pool) = options.pool_options().connect_with(opt).await { - Ok(DatabaseConnection::SqlxPostgresPoolConnection( + match options.pool_options().connect_with(opt).await { + Ok(pool) => Ok(DatabaseConnection::SqlxPostgresPoolConnection( SqlxPostgresPoolConnection { pool }, - )) - } else { - Err(DbErr::Conn("Failed to connect.".to_owned())) + )), + Err(e) => Err(sqlx_error_to_conn_err(e)), } } } diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index 69eee575..255686d2 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -45,12 +45,11 @@ impl SqlxSqliteConnector { if options.get_max_connections().is_none() { options.max_connections(1); } - if let Ok(pool) = options.pool_options().connect_with(opt).await { - Ok(DatabaseConnection::SqlxSqlitePoolConnection( + match options.pool_options().connect_with(opt).await { + Ok(pool) => Ok(DatabaseConnection::SqlxSqlitePoolConnection( SqlxSqlitePoolConnection { pool }, - )) - } else { - Err(DbErr::Conn("Failed to connect.".to_owned())) + )), + Err(e) => Err(sqlx_error_to_conn_err(e)), } } } From 5f2fa55253de162db18e2ca11addaa52a88dadef Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 14 Nov 2021 16:58:52 +0800 Subject: [PATCH 12/18] Edit docs --- src/database/connection.rs | 2 +- src/driver/mock.rs | 2 +- src/entity/active_model.rs | 10 ++++------ src/entity/base_entity.rs | 2 +- src/entity/link.rs | 2 +- src/entity/model.rs | 4 ++-- src/entity/primary_key.rs | 2 +- src/executor/paginator.rs | 2 +- src/executor/select.rs | 2 +- src/query/traits.rs | 2 +- 10 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index be47f9da..34b82081 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -4,7 +4,7 @@ use crate::{ use futures::Stream; use std::{future::Future, pin::Pin}; -/// Creates constraints for any structure that wants to create a database connection +/// Creates constraints for any structure that can create a database connection /// and execute SQL statements #[async_trait::async_trait] pub trait ConnectionTrait<'a>: Sync { diff --git a/src/driver/mock.rs b/src/driver/mock.rs index 3f271759..c0ffa0a2 100644 --- a/src/driver/mock.rs +++ b/src/driver/mock.rs @@ -23,7 +23,7 @@ pub struct MockDatabaseConnection { mocker: Mutex>, } -/// A set of constraints for any type wanting to perform operations on the [MockDatabase] +/// A Trait for any type wanting to perform operations on the [MockDatabase] pub trait MockDatabaseTrait: Send + Debug { /// Execute a statement in the [MockDatabase] fn execute(&mut self, counter: usize, stmt: Statement) -> Result; diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index d46c6dbf..c58ea5a9 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -76,12 +76,12 @@ where ActiveValue::unchanged(value) } -/// Enforces a set of constraints on any type performing an Create, Update or Delete operation. +/// A Trait for ActiveModel to perform Create, Update or Delete operation. /// The type must also implement the [EntityTrait]. /// See module level docs [crate::entity] for a full example #[async_trait] pub trait ActiveModelTrait: Clone + Debug { - /// Enforce the type to the constraints of the [EntityTrait] + /// The Entity this ActiveModel belongs to type Entity: EntityTrait; /// Get a mutable [ActiveValue] from an ActiveModel @@ -208,9 +208,7 @@ pub trait ActiveModelTrait: Clone + Debug { } } -/// Enforce a set of constraints to a override the ActiveModel behavior -/// Behaviors for users to override. -/// The type must also implement the [ActiveModelTrait] +/// A Trait for overriding the ActiveModel behavior /// /// ### Example /// ```ignore @@ -265,7 +263,7 @@ pub trait ActiveModelBehavior: ActiveModelTrait { } } -/// Enforce constraints for conversion to an ActiveModel +/// A Trait for any type that can be converted into an ActiveModel pub trait IntoActiveModel where A: ActiveModelTrait, diff --git a/src/entity/base_entity.rs b/src/entity/base_entity.rs index c03199d7..9c8a921d 100644 --- a/src/entity/base_entity.rs +++ b/src/entity/base_entity.rs @@ -13,7 +13,7 @@ pub trait IdenStatic: Iden + Copy + Debug + 'static { fn as_str(&self) -> &str; } -/// Enforces the naming of an entity to a set of constraints +/// A Trait for mapping an Entity to a database table pub trait EntityName: IdenStatic + Default { /// Method to get the name for the schema, defaults to [Option::None] if not set fn schema_name(&self) -> Option<&str> { diff --git a/src/entity/link.rs b/src/entity/link.rs index 53d23b97..b929624d 100644 --- a/src/entity/link.rs +++ b/src/entity/link.rs @@ -6,7 +6,7 @@ use sea_query::{Alias, IntoIden, JoinType, SeaRc}; /// Same as [RelationDef] pub type LinkDef = RelationDef; -/// A set of constraints for links between Entities +/// A Trait for links between Entities pub trait Linked { #[allow(missing_docs)] type FromEntity: EntityTrait; diff --git a/src/entity/model.rs b/src/entity/model.rs index 3f162dbe..b11a700b 100644 --- a/src/entity/model.rs +++ b/src/entity/model.rs @@ -5,7 +5,7 @@ use crate::{ pub use sea_query::Value; use std::fmt::Debug; -/// A set of constraints for a Model +/// A Trait for a Model pub trait ModelTrait: Clone + Send + Debug { #[allow(missing_docs)] type Entity: EntityTrait; @@ -35,7 +35,7 @@ pub trait ModelTrait: Clone + Send + Debug { } } -/// A set of constraints for implementing a [QueryResult] +/// A Trait for implementing a [QueryResult] pub trait FromQueryResult: Sized { /// Instantiate a Model from a [QueryResult] fn from_query_result(res: &QueryResult, pre: &str) -> Result; diff --git a/src/entity/primary_key.rs b/src/entity/primary_key.rs index d4075a2c..2c596dde 100644 --- a/src/entity/primary_key.rs +++ b/src/entity/primary_key.rs @@ -4,7 +4,7 @@ use sea_query::{FromValueTuple, IntoValueTuple}; use std::fmt::Debug; //LINT: composite primary key cannot auto increment -/// A set of constraints to be used to define a Primary Key. +/// A Trait for to be used to define a Primary Key. /// /// A primary key can be derived manually /// diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index c8bd503a..f52b3bd8 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -159,7 +159,7 @@ where } #[async_trait::async_trait] -/// Used to enforce constraints on any type that wants to paginate results +/// A Trait for any type that can paginate results pub trait PaginatorTrait<'db, C> where C: ConnectionTrait<'db>, diff --git a/src/executor/select.rs b/src/executor/select.rs index 832096b7..1d72e9c2 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -31,7 +31,7 @@ where selector: S, } -/// Used to enforce constraints on any type that wants to perform SELECT queries +/// A Trait for any type that can perform SELECT queries pub trait SelectorTrait { #[allow(missing_docs)] type Item: Sized; diff --git a/src/query/traits.rs b/src/query/traits.rs index 0517cc64..cfc35350 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -1,7 +1,7 @@ use crate::{DbBackend, Statement}; use sea_query::QueryStatementBuilder; -/// Enforces a set of constraints to any type performing queries on a Model or ActiveModel +/// A Trait for any type performing queries on a Model or ActiveModel pub trait QueryTrait { /// Constrain the QueryStatement to [QueryStatementBuilder] trait type QueryStatement: QueryStatementBuilder; From aaf6c2555d3265cba85cd7ed8a69a73b8effe09f Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 14 Nov 2021 20:49:04 +0800 Subject: [PATCH 13/18] Refactor and add `stream()` to `SelectorRaw` --- src/executor/select.rs | 55 ++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/src/executor/select.rs b/src/executor/select.rs index 1d72e9c2..d6ee1651 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -416,18 +416,25 @@ where } } + fn into_selector_raw<'a, C>(self, db: &C) -> SelectorRaw + where + C: ConnectionTrait<'a>, + { + let builder = db.get_database_backend(); + let stmt = builder.build(&self.query); + SelectorRaw { + stmt, + selector: self.selector, + } + } + /// Get a Model from a Select operation pub async fn one<'a, C>(mut self, db: &C) -> Result, DbErr> where C: ConnectionTrait<'a>, { - let builder = db.get_database_backend(); self.query.limit(1); - let row = db.query_one(builder.build(&self.query)).await?; - match row { - Some(row) => Ok(Some(S::from_raw_query_result(row)?)), - None => Ok(None), - } + self.into_selector_raw(db).one(db).await } /// Get all results from a Select operation @@ -435,16 +442,10 @@ where where C: ConnectionTrait<'a>, { - let builder = db.get_database_backend(); - let rows = db.query_all(builder.build(&self.query)).await?; - let mut models = Vec::new(); - for row in rows.into_iter() { - models.push(S::from_raw_query_result(row)?); - } - Ok(models) + self.into_selector_raw(db).all(db).await } - /// Stream the results of the operation + /// Stream the results of the Select operation pub async fn stream<'a: 'b, 'b, C>( self, db: &'a C, @@ -453,11 +454,7 @@ where C: ConnectionTrait<'a>, S: 'b, { - let builder = db.get_database_backend(); - let stream = db.stream(builder.build(&self.query)).await?; - Ok(Box::pin(stream.and_then(|row| { - futures::future::ready(S::from_raw_query_result(row)) - }))) + self.into_selector_raw(db).stream(db).await } } @@ -465,8 +462,7 @@ impl SelectorRaw where S: SelectorTrait, { - /// Create `SelectorRaw` from Statment. Executing this `SelectorRaw` will - /// return a type `M` which implement `FromQueryResult`. + /// Select a custom Model from a raw SQL [Statement]. pub fn from_statement(stmt: Statement) -> SelectorRaw> where M: FromQueryResult, @@ -629,6 +625,7 @@ where } } + /// Get a Model from a Select operation /// ``` /// # #[cfg(feature = "mock")] /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; @@ -671,6 +668,7 @@ where } } + /// Get all results from a Select operation /// ``` /// # #[cfg(feature = "mock")] /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; @@ -713,6 +711,21 @@ where } Ok(models) } + + /// Stream the results of the Select operation + pub async fn stream<'a: 'b, 'b, C>( + self, + db: &'a C, + ) -> Result> + 'b>>, DbErr> + where + C: ConnectionTrait<'a>, + S: 'b, + { + let stream = db.stream(self.stmt).await?; + Ok(Box::pin(stream.and_then(|row| { + futures::future::ready(S::from_raw_query_result(row)) + }))) + } } fn consolidate_query_result( From d66538d7bc5a13137931c61ef1fcc44b8558a82e Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 14 Nov 2021 20:54:15 +0800 Subject: [PATCH 14/18] Edit docs --- src/executor/select.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/executor/select.rs b/src/executor/select.rs index d6ee1651..9fb906cf 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -250,7 +250,7 @@ where Selector::>::with_columns(self.query) } - /// Get one Model from a SELECT operation + /// Get one Model from the SELECT query pub async fn one<'a, C>(self, db: &C) -> Result, DbErr> where C: ConnectionTrait<'a>, @@ -258,7 +258,7 @@ where self.into_model().one(db).await } - /// Get all the Models from a SELECT operation + /// Get all Models from the SELECT query pub async fn all<'a, C>(self, db: &C) -> Result, DbErr> where C: ConnectionTrait<'a>, @@ -304,7 +304,7 @@ where } } - /// Get one Model from a Select operation + /// Get one Model from the Select query pub async fn one<'a, C>(self, db: &C) -> Result)>, DbErr> where C: ConnectionTrait<'a>, @@ -312,7 +312,7 @@ where self.into_model().one(db).await } - /// Get all Models from a Select operation + /// Get all Models from the Select query pub async fn all<'a, C>(self, db: &C) -> Result)>, DbErr> where C: ConnectionTrait<'a>, @@ -377,7 +377,7 @@ where self.into_model().stream(db).await } - /// Get all the Models from the select operation + /// Get all Models from the select operation pub async fn all<'a, C>(self, db: &C) -> Result)>, DbErr> where C: ConnectionTrait<'a>, @@ -428,7 +428,7 @@ where } } - /// Get a Model from a Select operation + /// Get an item from the Select query pub async fn one<'a, C>(mut self, db: &C) -> Result, DbErr> where C: ConnectionTrait<'a>, @@ -437,7 +437,7 @@ where self.into_selector_raw(db).one(db).await } - /// Get all results from a Select operation + /// Get all items from the Select query pub async fn all<'a, C>(self, db: &C) -> Result, DbErr> where C: ConnectionTrait<'a>, @@ -625,7 +625,7 @@ where } } - /// Get a Model from a Select operation + /// Get an item from the Select query /// ``` /// # #[cfg(feature = "mock")] /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; @@ -668,7 +668,7 @@ where } } - /// Get all results from a Select operation + /// Get all items from the Select query /// ``` /// # #[cfg(feature = "mock")] /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; From f4008f9f7cefb50ac503a4fb9d523c1fbd072653 Mon Sep 17 00:00:00 2001 From: Billy Chan <30400950+billy1624@users.noreply.github.com> Date: Mon, 15 Nov 2021 11:14:32 +0800 Subject: [PATCH 15/18] Suppress `ouroboros` missing docs warnings (#288) * Suppress `ouroboros` missing docs warnings * Suppress warnings --- src/database/stream/query.rs | 2 ++ src/database/stream/transaction.rs | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/database/stream/query.rs b/src/database/stream/query.rs index 03a0fc6e..c2c88d0d 100644 --- a/src/database/stream/query.rs +++ b/src/database/stream/query.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use std::{pin::Pin, task::Poll}; #[cfg(feature = "mock")] diff --git a/src/database/stream/transaction.rs b/src/database/stream/transaction.rs index 2dddc59c..77a59819 100644 --- a/src/database/stream/transaction.rs +++ b/src/database/stream/transaction.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use std::{ops::DerefMut, pin::Pin, task::Poll}; use futures::Stream; @@ -11,9 +13,9 @@ use futures::lock::MutexGuard; use crate::{DbErr, InnerConnection, QueryResult, Statement}; -#[ouroboros::self_referencing] /// `TransactionStream` cannot be used in a `transaction` closure as it does not impl `Send`. /// It seems to be a Rust limitation right now, and solution to work around this deemed to be extremely hard. +#[ouroboros::self_referencing] pub struct TransactionStream<'a> { stmt: Statement, conn: MutexGuard<'a, InnerConnection>, From 63c764d6a4fa8d9d08ffc5928a74a1460fcb22d9 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 15 Nov 2021 21:27:27 +0800 Subject: [PATCH 16/18] `with-json` feature requires `chrono/serde` --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2345844c..46d972dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,7 +63,7 @@ default = [ ] macros = ["sea-orm-macros"] mock = [] -with-json = ["serde_json", "sea-query/with-json"] +with-json = ["serde_json", "sea-query/with-json", "chrono/serde"] with-chrono = ["chrono", "sea-query/with-chrono"] with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal"] with-uuid = ["uuid", "sea-query/with-uuid"] From 1c4fc11efeb9c0b2bf6a9dc7eedd4412033fc8d7 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 15 Nov 2021 21:27:54 +0800 Subject: [PATCH 17/18] Test [issues] --- issues/319/Cargo.toml | 18 ++++++++++++++++++ issues/319/src/main.rs | 14 ++++++++++++++ issues/319/src/material.rs | 20 ++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 issues/319/Cargo.toml create mode 100644 issues/319/src/main.rs create mode 100644 issues/319/src/material.rs diff --git a/issues/319/Cargo.toml b/issues/319/Cargo.toml new file mode 100644 index 00000000..3eb4adaa --- /dev/null +++ b/issues/319/Cargo.toml @@ -0,0 +1,18 @@ +[workspace] +# A separate workspace + +[package] +name = "sea-orm-issues-319" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-std = { version = "^1", features = ["attributes"] } +serde = { version = "^1", features = ["derive"] } +sea-orm = { path = "../../", features = [ + "sqlx-mysql", + "runtime-async-std-native-tls", + "with-json", + "macros", +], default-features = false } diff --git a/issues/319/src/main.rs b/issues/319/src/main.rs new file mode 100644 index 00000000..1710c37c --- /dev/null +++ b/issues/319/src/main.rs @@ -0,0 +1,14 @@ +mod material; +use sea_orm::*; + +#[async_std::main] +pub async fn main() { + let db = Database::connect("mysql://sea:sea@localhost/bakery") + .await + .unwrap(); + + async_std::task::spawn(async move { + material::Entity::find().one(&db).await.unwrap(); + }) + .await; +} diff --git a/issues/319/src/material.rs b/issues/319/src/material.rs new file mode 100644 index 00000000..9586189d --- /dev/null +++ b/issues/319/src/material.rs @@ -0,0 +1,20 @@ +use sea_orm::entity::prelude::*; +use serde::{Serialize, Deserialize}; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)] +#[sea_orm(table_name = "materials")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub created_at: DateTimeWithTimeZone, + pub updated_at: DateTimeWithTimeZone, + pub name: String, + #[sea_orm(column_type = "Text", nullable)] + pub description: Option, + pub tag_ids: Vec, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} From 2e5534279d21ba68075ec3ecdc1d9c3864efd7ea Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 15 Nov 2021 22:16:15 +0800 Subject: [PATCH 18/18] Test [issues] --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index ad18203e..40c20d01 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -314,7 +314,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - path: [86, 249, 262] + path: [86, 249, 262, 319] steps: - uses: actions/checkout@v2