Rewrite doctests

This commit is contained in:
Billy Chan 2021-11-16 16:27:54 +08:00
parent 9655805316
commit 4c147a2d24
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
7 changed files with 406 additions and 122 deletions

View File

@ -3,7 +3,12 @@
//! Relying on [SQLx](https://github.com/launchbadge/sqlx), SeaORM is a new library with async support from day 1.
//!
//! ```
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*, DatabaseConnection, DbBackend, MockDatabase, Transaction, IntoMockRow};
//! # use sea_orm::{error::*, tests_cfg::*, *};
//! #
//! # #[smol_potat::main]
//! # #[cfg(feature = "mock")]
//! # pub async fn main() -> Result<(), DbErr> {
//! #
//! # let db = MockDatabase::new(DbBackend::Postgres)
//! # .append_query_results(vec![
//! # vec![cake::Model {
@ -19,7 +24,7 @@
//! # .into_mock_row()],
//! # ])
//! # .into_connection();
//! # let _: Result<(), DbErr> = smol::block_on(async {
//! #
//! // execute multiple queries in parallel
//! let cakes_and_fruits: (Vec<cake::Model>, Vec<fruit::Model>) =
//! futures::try_join!(Cake::find().all(&db), Fruit::find().all(&db))?;
@ -53,7 +58,7 @@
//! # ]
//! # );
//! # Ok(())
//! # });
//! # }
//! ```
//!
//! 2. Dynamic

View File

@ -140,6 +140,106 @@ pub trait ActiveModelTrait: Clone + Debug {
}
/// Perform an `INSERT` operation on the ActiveModel
///
/// # Example (Postgres)
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 15,
/// # rows_affected: 1,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
/// let apple = cake::ActiveModel {
/// name: Set("Apple Pie".to_owned()),
/// ..Default::default()
/// };
///
/// assert_eq!(
/// apple
/// .insert(&db)
/// .await?,
/// cake::ActiveModel {
/// id: Set(150),
/// name: Set("Apple Pie".to_owned()),
/// }
/// );
/// assert!(false);
///
/// assert_eq!(
/// db.into_transaction_log(),
/// vec![Transaction::from_sql_and_values(
/// DbBackend::Postgres,
/// r#"INSERT INTO "cake" ("name") VALUES ($1) RETURNING "id", "name""#,
/// vec!["Apple Pie".into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
///
/// # Example (MySQL)
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::MySql)
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 15,
/// # rows_affected: 1,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
/// let apple = cake::ActiveModel {
/// name: Set("Apple Pie".to_owned()),
/// ..Default::default()
/// };
///
/// assert_eq!(
/// apple
/// .insert(&db)
/// .await?,
/// cake::ActiveModel {
/// id: Set(150),
/// name: Set("Apple Pie".to_owned()),
/// }
/// );
///
/// assert_eq!(
/// db.into_transaction_log(),
/// vec![
/// Transaction::from_sql_and_values(
/// DbBackend::MySql,
/// r#"INSERT INTO `cake` (`name`) VALUES (?)"#,
/// vec!["Apple Pie".into()]
/// ),
/// Transaction::from_sql_and_values(
/// DbBackend::MySql,
/// r#"SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = ? LIMIT ?"#,
/// vec![15.into(), 1u64.into()])]);
/// #
/// # Ok(())
/// # }
/// ```
async fn insert<'a, C>(self, db: &'a C) -> Result<Self, DbErr>
where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
@ -154,6 +254,117 @@ pub trait ActiveModelTrait: Clone + Debug {
}
/// Perform the `UPDATE` operation on an ActiveModel
///
/// # Example (Postgres)
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 0,
/// # rows_affected: 1,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit};
///
/// let orange = fruit::ActiveModel {
/// id: Set(1),
/// name: Set("Orange".to_owned()),
/// ..Default::default()
/// };
///
/// assert_eq!(
/// orange
/// .update(&db)
/// .await?,
/// fruit::ActiveModel {
/// id: Set(1),
/// name: Set("Orange".to_owned()),
/// cake_id: Set(None),
/// }
/// );
///
/// assert_eq!(
/// db.into_transaction_log(),
/// vec![Transaction::from_sql_and_values(
/// DbBackend::Postgres,
/// r#"UPDATE "fruit" SET "name" = $1 WHERE "fruit"."id" = $2 RETURNING "id", "name", "cake_id""#,
/// vec!["Orange".into(), 1i32.into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
///
/// # Example (MySQL)
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::MySql)
/// # .append_query_results(vec![
/// # vec![fruit::Model {
/// # id: 1,
/// # name: "Orange".to_owned(),
/// # cake_id: None,
/// # }],
/// # ])
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 0,
/// # rows_affected: 1,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit};
///
/// let orange = fruit::ActiveModel {
/// id: Set(1),
/// name: Set("Orange".to_owned()),
/// ..Default::default()
/// };
///
/// assert_eq!(
/// orange
/// .update(&db)
/// .await?,
/// fruit::ActiveModel {
/// id: Set(1),
/// name: Set("Orange".to_owned()),
/// cake_id: Set(None),
/// }
/// );
///
/// assert_eq!(
/// db.into_transaction_log(),
/// vec![
/// Transaction::from_sql_and_values(
/// DbBackend::MySql,
/// r#"UPDATE `fruit` SET `name` = ? WHERE `fruit`.`id` = ?"#,
/// vec!["Orange".into(), 1i32.into()]
/// ),
/// Transaction::from_sql_and_values(
/// DbBackend::MySql,
/// r#"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` WHERE `fruit`.`id` = ? LIMIT ?"#,
/// vec![1i32.into(), 1u64.into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
async fn update<'a, C>(self, db: &'a C) -> Result<Self, DbErr>
where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
@ -191,6 +402,48 @@ pub trait ActiveModelTrait: Clone + Debug {
}
/// Delete an active model by its primary key
///
/// # Example
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 0,
/// # rows_affected: 1,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit};
///
/// let orange = fruit::ActiveModel {
/// id: Set(3),
/// ..Default::default()
/// };
///
/// let delete_result = orange.delete(&db).await?;
///
/// assert_eq!(delete_result.rows_affected, 1);
///
/// assert_eq!(
/// db.into_transaction_log(),
/// vec![Transaction::from_sql_and_values(
/// DbBackend::Postgres,
/// r#"DELETE FROM "fruit" WHERE "fruit"."id" = $1"#,
/// vec![3i32.into()]
/// )]
/// );
/// #
/// # Ok(())
/// # }
/// ```
async fn delete<'a, C>(self, db: &'a C) -> Result<DeleteResult, DbErr>
where
Self: ActiveModelBehavior + 'a,

View File

@ -95,8 +95,11 @@ pub trait EntityTrait: EntityName {
/// # Example
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![
@ -121,8 +124,6 @@ pub trait EntityTrait: EntityName {
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// assert_eq!(
/// cake::Entity::find().one(&db).await?,
/// Some(cake::Model {
@ -144,9 +145,6 @@ pub trait EntityTrait: EntityName {
/// },
/// ]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -163,6 +161,9 @@ pub trait EntityTrait: EntityName {
/// ),
/// ]
/// );
/// #
/// # Ok(())
/// # }
/// ```
fn find() -> Select<Self> {
Select::new()
@ -173,8 +174,11 @@ pub trait EntityTrait: EntityName {
/// # Example
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![
@ -189,8 +193,6 @@ pub trait EntityTrait: EntityName {
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// assert_eq!(
/// cake::Entity::find_by_id(11).all(&db).await?,
/// vec![cake::Model {
@ -198,9 +200,6 @@ pub trait EntityTrait: EntityName {
/// name: "Sponge Cake".to_owned(),
/// }]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -210,11 +209,17 @@ pub trait EntityTrait: EntityName {
/// vec![11i32.into()]
/// )]
/// );
/// #
/// # Ok(())
/// # }
/// ```
/// Find by composite key
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![
@ -229,8 +234,6 @@ pub trait EntityTrait: EntityName {
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake_filling};
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// assert_eq!(
/// cake_filling::Entity::find_by_id((2, 3)).all(&db).await?,
/// vec![cake_filling::Model {
@ -238,9 +241,6 @@ pub trait EntityTrait: EntityName {
/// filling_id: 3,
/// }]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -252,6 +252,9 @@ pub trait EntityTrait: EntityName {
/// ].join(" ").as_str(),
/// vec![2i32.into(), 3i32.into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
fn find_by_id(values: <Self::PrimaryKey as PrimaryKeyTrait>::ValueType) -> Select<Self> {
let mut select = Self::find();
@ -317,8 +320,11 @@ pub trait EntityTrait: EntityName {
/// # Example (MySQL)
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::MySql)
/// # .append_exec_results(vec![
@ -336,14 +342,9 @@ pub trait EntityTrait: EntityName {
/// ..Default::default()
/// };
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let insert_result = cake::Entity::insert(apple).exec(&db).await?;
///
/// assert_eq!(insert_result.last_insert_id, 15);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -352,6 +353,9 @@ pub trait EntityTrait: EntityName {
/// r#"INSERT INTO `cake` (`name`) VALUES (?)"#,
/// vec!["Apple Pie".into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
fn insert<A>(model: A) -> Insert<A>
where
@ -365,8 +369,11 @@ pub trait EntityTrait: EntityName {
/// # Example (Postgres)
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_exec_results(vec![
@ -388,14 +395,9 @@ pub trait EntityTrait: EntityName {
/// ..Default::default()
/// };
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let insert_result = cake::Entity::insert_many(vec![apple, orange]).exec(&db).await?;
///
/// assert_eq!(insert_result.last_insert_id, 28);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -404,13 +406,19 @@ pub trait EntityTrait: EntityName {
/// r#"INSERT INTO "cake" ("name") VALUES ($1), ($2) RETURNING "id""#,
/// vec!["Apple Pie".into(), "Orange Scone".into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
///
/// # Example (MySQL)
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::MySql)
/// # .append_exec_results(vec![
@ -432,14 +440,9 @@ pub trait EntityTrait: EntityName {
/// ..Default::default()
/// };
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let insert_result = cake::Entity::insert_many(vec![apple, orange]).exec(&db).await?;
///
/// assert_eq!(insert_result.last_insert_id, 28);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -448,6 +451,9 @@ pub trait EntityTrait: EntityName {
/// r#"INSERT INTO `cake` (`name`) VALUES (?), (?)"#,
/// vec!["Apple Pie".into(), "Orange Scone".into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
fn insert_many<A, I>(models: I) -> Insert<A>
where
@ -464,8 +470,11 @@ pub trait EntityTrait: EntityName {
/// # Example (Postgres)
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_exec_results(vec![
@ -484,8 +493,6 @@ pub trait EntityTrait: EntityName {
/// ..Default::default()
/// };
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// assert_eq!(
/// fruit::Entity::update(orange.clone())
/// .filter(fruit::Column::Name.contains("orange"))
@ -493,9 +500,6 @@ pub trait EntityTrait: EntityName {
/// .await?,
/// orange
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -504,13 +508,19 @@ pub trait EntityTrait: EntityName {
/// r#"UPDATE "fruit" SET "name" = $1 WHERE "fruit"."id" = $2 AND "fruit"."name" LIKE $3 RETURNING "id", "name", "cake_id""#,
/// vec!["Orange".into(), 1i32.into(), "%orange%".into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
///
/// # Example (MySQL)
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::MySql)
/// # .append_query_results(vec![
@ -536,8 +546,6 @@ pub trait EntityTrait: EntityName {
/// ..Default::default()
/// };
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// assert_eq!(
/// fruit::Entity::update(orange.clone())
/// .filter(fruit::Column::Name.contains("orange"))
@ -545,9 +553,6 @@ pub trait EntityTrait: EntityName {
/// .await?,
/// orange
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -562,6 +567,9 @@ pub trait EntityTrait: EntityName {
/// r#"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` WHERE `fruit`.`id` = ? LIMIT ?"#,
/// vec![1i32.into(), 1u64.into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
fn update<A>(model: A) -> UpdateOne<A>
where
@ -577,8 +585,11 @@ pub trait EntityTrait: EntityName {
/// # Example
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_exec_results(vec![
@ -591,8 +602,6 @@ pub trait EntityTrait: EntityName {
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit, sea_query::{Expr, Value}};
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let update_result = fruit::Entity::update_many()
/// .col_expr(fruit::Column::CakeId, Expr::value(Value::Int(None)))
/// .filter(fruit::Column::Name.contains("Apple"))
@ -600,9 +609,6 @@ pub trait EntityTrait: EntityName {
/// .await?;
///
/// assert_eq!(update_result.rows_affected, 5);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -611,6 +617,9 @@ pub trait EntityTrait: EntityName {
/// r#"UPDATE "fruit" SET "cake_id" = $1 WHERE "fruit"."name" LIKE $2"#,
/// vec![Value::Int(None), "%Apple%".into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
fn update_many() -> UpdateMany<Self> {
Update::many(Self::default())
@ -623,8 +632,11 @@ pub trait EntityTrait: EntityName {
/// # Example
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_exec_results(vec![
@ -642,14 +654,9 @@ pub trait EntityTrait: EntityName {
/// ..Default::default()
/// };
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let delete_result = fruit::Entity::delete(orange).exec(&db).await?;
///
/// assert_eq!(delete_result.rows_affected, 1);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -657,6 +664,9 @@ pub trait EntityTrait: EntityName {
/// DbBackend::Postgres, r#"DELETE FROM "fruit" WHERE "fruit"."id" = $1"#,
/// vec![3i32.into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
fn delete<A>(model: A) -> DeleteOne<A>
where
@ -672,8 +682,11 @@ pub trait EntityTrait: EntityName {
/// # Example
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{entity::*, error::*, query::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_exec_results(vec![
@ -686,17 +699,12 @@ pub trait EntityTrait: EntityName {
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit};
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let delete_result = fruit::Entity::delete_many()
/// .filter(fruit::Column::Name.contains("Apple"))
/// .exec(&db)
/// .await?;
///
/// assert_eq!(delete_result.rows_affected, 5);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -705,6 +713,9 @@ pub trait EntityTrait: EntityName {
/// r#"DELETE FROM "fruit" WHERE "fruit"."name" LIKE $1"#,
/// vec!["%Apple%".into()]
/// )]);
/// #
/// # Ok(())
/// # }
/// ```
fn delete_many() -> DeleteMany<Self> {
Delete::many(Self::default())

View File

@ -47,8 +47,11 @@ pub trait FromQueryResult: Sized {
}
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![vec![
@ -67,8 +70,6 @@ pub trait FromQueryResult: Sized {
/// num_of_cakes: i32,
/// }
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let res: Vec<SelectResult> = SelectResult::find_by_statement(Statement::from_sql_and_values(
/// DbBackend::Postgres,
/// r#"SELECT "name", COUNT(*) AS "num_of_cakes" FROM "cake" GROUP BY("name")"#,
@ -85,8 +86,6 @@ pub trait FromQueryResult: Sized {
/// },]
/// );
/// #
/// # Ok(())
/// # });
/// # assert_eq!(
/// # db.into_transaction_log(),
/// # vec![Transaction::from_sql_and_values(
@ -95,6 +94,9 @@ pub trait FromQueryResult: Sized {
/// # vec![]
/// # ),]
/// # );
/// #
/// # Ok(())
/// # }
/// ```
fn find_by_statement(stmt: Statement) -> SelectorRaw<SelectModel<Self>> {
SelectorRaw::<SelectModel<Self>>::from_statement(stmt)

View File

@ -97,11 +97,14 @@ where
/// Fetch one page and increment the page counter
///
/// ```rust
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, MockDatabase, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let owned_db = MockDatabase::new(DbBackend::Postgres).into_connection();
/// # let db = &owned_db;
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
/// let mut cake_pages = cake::Entity::find()
@ -113,7 +116,7 @@ where
/// }
/// #
/// # Ok(())
/// # });
/// # }
/// ```
pub async fn fetch_and_next(&mut self) -> Result<Option<Vec<S::Item>>, DbErr> {
let vec = self.fetch().await?;
@ -125,11 +128,14 @@ where
/// Convert self into an async stream
///
/// ```rust
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, MockDatabase, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let owned_db = MockDatabase::new(DbBackend::Postgres).into_connection();
/// # let db = &owned_db;
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// use futures::TryStreamExt;
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
@ -143,7 +149,7 @@ where
/// }
/// #
/// # Ok(())
/// # });
/// # }
/// ```
pub fn into_stream(mut self) -> PinBoxStream<'db, Result<Vec<S::Item>, DbErr>> {
Box::pin(stream! {

View File

@ -320,8 +320,11 @@ pub trait TryGetableMany: Sized {
fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result<Self, TryGetError>;
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(all(feature = "mock", feature = "macros"))]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![vec![
@ -344,8 +347,6 @@ pub trait TryGetableMany: Sized {
/// NumOfCakes,
/// }
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let res: Vec<(String, i32)> =
/// <(String, i32)>::find_by_statement::<ResultCol>(Statement::from_sql_and_values(
/// DbBackend::Postgres,
@ -362,9 +363,6 @@ pub trait TryGetableMany: Sized {
/// ("New York Cheese".to_owned(), 1),
/// ]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -374,6 +372,9 @@ pub trait TryGetableMany: Sized {
/// vec![]
/// ),]
/// );
/// #
/// # Ok(())
/// # }
/// ```
fn find_by_statement<C>(stmt: Statement) -> SelectorRaw<SelectGetableValue<Self, C>>
where

View File

@ -143,8 +143,11 @@ where
}
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(all(feature = "mock", feature = "macros"))]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![vec![
@ -164,8 +167,6 @@ where
/// CakeName,
/// }
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let res: Vec<String> = cake::Entity::find()
/// .select_only()
/// .column_as(cake::Column::Name, QueryAs::CakeName)
@ -177,9 +178,6 @@ where
/// res,
/// vec!["Chocolate Forest".to_owned(), "New York Cheese".to_owned()]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -189,11 +187,17 @@ where
/// vec![]
/// )]
/// );
/// #
/// # Ok(())
/// # }
/// ```
///
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(all(feature = "mock", feature = "macros"))]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![vec![
@ -212,8 +216,6 @@ where
/// NumOfCakes,
/// }
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let res: Vec<(String, i64)> = cake::Entity::find()
/// .select_only()
/// .column_as(cake::Column::Name, QueryAs::CakeName)
@ -224,9 +226,6 @@ where
/// .await?;
///
/// assert_eq!(res, vec![("Chocolate Forest".to_owned(), 2i64)]);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -241,6 +240,9 @@ where
/// vec![]
/// )]
/// );
/// #
/// # Ok(())
/// # }
/// ```
pub fn into_values<T, C>(self) -> Selector<SelectGetableValue<T, C>>
where
@ -490,8 +492,11 @@ where
}
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![vec![
@ -514,8 +519,6 @@ where
/// num_of_cakes: i32,
/// }
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let res: Vec<SelectResult> = cake::Entity::find()
/// .from_raw_sql(Statement::from_sql_and_values(
/// DbBackend::Postgres,
@ -539,9 +542,6 @@ where
/// },
/// ]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -551,6 +551,9 @@ where
/// vec![]
/// ),]
/// );
/// #
/// # Ok(())
/// # }
/// ```
pub fn into_model<M>(self) -> SelectorRaw<SelectModel<M>>
where
@ -563,8 +566,11 @@ where
}
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres)
/// # .append_query_results(vec![vec![
@ -581,8 +587,6 @@ where
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let res: Vec<serde_json::Value> = cake::Entity::find().from_raw_sql(
/// Statement::from_sql_and_values(
/// DbBackend::Postgres, r#"SELECT "cake"."id", "cake"."name" FROM "cake""#, vec![]
@ -605,9 +609,6 @@ where
/// }),
/// ]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -616,6 +617,9 @@ where
/// DbBackend::Postgres, r#"SELECT "cake"."id", "cake"."name" FROM "cake""#, vec![]
/// ),
/// ]);
/// #
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "with-json")]
pub fn into_json(self) -> SelectorRaw<SelectModel<JsonValue>> {
@ -627,15 +631,16 @@ where
/// Get an item from the Select query
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres).into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let _: Option<cake::Model> = cake::Entity::find()
/// .from_raw_sql(Statement::from_sql_and_values(
/// DbBackend::Postgres,
@ -644,9 +649,6 @@ where
/// ))
/// .one(&db)
/// .await?;
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -656,6 +658,9 @@ where
/// vec![1.into()]
/// ),]
/// );
/// #
/// # Ok(())
/// # }
/// ```
pub async fn one<'a, C>(self, db: &C) -> Result<Option<S::Item>, DbErr>
where
@ -670,15 +675,16 @@ where
/// Get all items from the Select query
/// ```
/// # use sea_orm::{error::*, tests_cfg::*, *};
/// #
/// # #[smol_potat::main]
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend};
/// # pub async fn main() -> Result<(), DbErr> {
/// #
/// # let db = MockDatabase::new(DbBackend::Postgres).into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
/// # let _: Result<(), DbErr> = smol::block_on(async {
/// #
/// let _: Vec<cake::Model> = cake::Entity::find()
/// .from_raw_sql(Statement::from_sql_and_values(
/// DbBackend::Postgres,
@ -687,9 +693,6 @@ where
/// ))
/// .all(&db)
/// .await?;
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
/// db.into_transaction_log(),
@ -699,6 +702,9 @@ where
/// vec![]
/// ),]
/// );
/// #
/// # Ok(())
/// # }
/// ```
pub async fn all<'a, C>(self, db: &C) -> Result<Vec<S::Item>, DbErr>
where