From b4ebf40cabf2f0d1069df77482d319df49714108 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 28 Mar 2024 15:50:25 +0800 Subject: [PATCH] Upstream Changes - 4 --- src/executor/delete.rs | 12 ++++++------ src/executor/insert.rs | 17 ++++++++++------- src/executor/paginator.rs | 26 +++++++++++++------------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/executor/delete.rs b/src/executor/delete.rs index 08d1d8f1aa..e1663a6c85 100644 --- a/src/executor/delete.rs +++ b/src/executor/delete.rs @@ -1,6 +1,4 @@ -use crate::{ - error::*, ActiveModelTrait, ConnectionTrait, DeleteMany, DeleteOne, EntityTrait, Statement, -}; +use crate::{error::*, ActiveModelTrait, ConnectionTrait, DeleteMany, DeleteOne, EntityTrait}; use sea_query::DeleteStatement; use std::future::Future; @@ -56,8 +54,7 @@ impl Deleter { where C: ConnectionTrait, { - let builder = db.get_database_backend(); - exec_delete(builder.build(&self.query), db) + exec_delete(self.query, db) } } @@ -68,10 +65,13 @@ where Deleter::new(query).exec(db).await } -async fn exec_delete(statement: Statement, db: &C) -> Result +async fn exec_delete(query: DeleteStatement, db: &C) -> Result where C: ConnectionTrait, { + let builder = db.get_database_backend(); + let statement = builder.build(&query); + let result = db.execute(statement).await?; Ok(DeleteResult { rows_affected: result.rows_affected(), diff --git a/src/executor/insert.rs b/src/executor/insert.rs index 1bd074d6f3..78d154a8ee 100644 --- a/src/executor/insert.rs +++ b/src/executor/insert.rs @@ -176,8 +176,7 @@ where C: ConnectionTrait, A: 'a, { - let builder = db.get_database_backend(); - exec_insert(self.primary_key, builder.build(&self.query), db) + exec_insert(self.primary_key, self.query, db) } /// Execute an insert operation @@ -208,13 +207,16 @@ where async fn exec_insert( primary_key: Option, - statement: Statement, + statement: InsertStatement, db: &C, ) -> Result, DbErr> where C: ConnectionTrait, A: ActiveModelTrait, { + let db_backend = db.get_database_backend(); + let statement = db_backend.build(&statement); + type PrimaryKey = <::Entity as EntityTrait>::PrimaryKey; type ValueTypeOf = as PrimaryKeyTrait>::ValueType; @@ -259,7 +261,8 @@ where C: ConnectionTrait, { let db_backend = db.get_database_backend(); - let exec_result = db.execute(db_backend.build(&insert_statement)).await?; + let insert_statement = db_backend.build(&insert_statement); + let exec_result = db.execute(insert_statement).await?; Ok(exec_result.rows_affected()) } @@ -281,15 +284,15 @@ where .map(|c| c.select_as(c.into_returning_expr(db_backend))), ); insert_statement.returning(returning); + let insert_statement = db_backend.build(&insert_statement); SelectorRaw::::Model>>::from_statement( - db_backend.build(&insert_statement), + insert_statement, ) .one(db) .await? } false => { - let insert_res = - exec_insert::(primary_key, db_backend.build(&insert_statement), db).await?; + let insert_res = exec_insert::(primary_key, insert_statement, db).await?; ::find_by_id(insert_res.last_insert_id) .one(db) .await? diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index 71b55bc45f..5551258541 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -67,19 +67,19 @@ where /// Get the total number of items pub async fn num_items(&self) -> Result { let builder = self.db.get_database_backend(); - let stmt = builder.build( - SelectStatement::new() - .expr(Expr::cust("COUNT(*) AS num_items")) - .from_subquery( - self.query - .clone() - .reset_limit() - .reset_offset() - .clear_order_by() - .to_owned(), - Alias::new("sub_query"), - ), - ); + let stmt = SelectStatement::new() + .expr(Expr::cust("COUNT(*) AS num_items")) + .from_subquery( + self.query + .clone() + .reset_limit() + .reset_offset() + .clear_order_by() + .to_owned(), + Alias::new("sub_query"), + ) + .to_owned(); + let stmt = builder.build(&stmt); let result = match self.db.query_one(stmt).await? { Some(res) => res, None => return Ok(0),