-
-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
198 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use crate::*; | ||
use sea_orm::{entity::*, query::*, Database}; | ||
|
||
pub async fn all_about_operation(db: &Database) -> Result<(), ExecErr> { | ||
let banana = fruit::ActiveModel { | ||
name: Val::set("banana".to_owned()), | ||
..Default::default() | ||
}; | ||
let mut banana = banana.save(db).await?; | ||
|
||
println!(); | ||
println!("Inserted: {:?}\n", banana); | ||
|
||
banana.name = Val::set("banana banana".to_owned()); | ||
|
||
let banana = banana.save(db).await?; | ||
|
||
println!(); | ||
println!("Updated: {:?}\n", banana); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::{ActiveModelTrait, Connection, Database, ExecErr, Statement, Update}; | ||
use sea_query::{QueryBuilder, UpdateStatement}; | ||
use std::future::Future; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Updater { | ||
query: UpdateStatement, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct UpdateResult { | ||
pub rows_affected: u64, | ||
} | ||
|
||
impl<'a, A: 'a> Update<A> | ||
where | ||
A: ActiveModelTrait, | ||
{ | ||
pub fn exec(self, db: &'a Database) -> impl Future<Output = Result<A, ExecErr>> + 'a { | ||
// so that self is dropped before entering await | ||
exec_update_and_return_original(self.query, self.model, db) | ||
} | ||
} | ||
|
||
impl Updater { | ||
pub fn new(query: UpdateStatement) -> Self { | ||
Self { query } | ||
} | ||
|
||
pub fn build<B>(&self, builder: B) -> Statement | ||
where | ||
B: QueryBuilder, | ||
{ | ||
self.query.build(builder).into() | ||
} | ||
|
||
pub fn exec(self, db: &Database) -> impl Future<Output = Result<UpdateResult, ExecErr>> + '_ { | ||
let builder = db.get_query_builder_backend(); | ||
exec_update(self.build(builder), db) | ||
} | ||
} | ||
|
||
async fn exec_update_and_return_original<A>( | ||
query: UpdateStatement, | ||
model: A, | ||
db: &Database, | ||
) -> Result<A, ExecErr> | ||
where | ||
A: ActiveModelTrait, | ||
{ | ||
Updater::new(query).exec(db).await?; | ||
Ok(model) | ||
} | ||
|
||
// Only Statement impl Send | ||
async fn exec_update(statement: Statement, db: &Database) -> Result<UpdateResult, ExecErr> { | ||
let result = db.get_connection().execute(statement).await?; | ||
Ok(UpdateResult { | ||
rows_affected: result.rows_affected(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters