Skip to content

Commit

Permalink
Upstream Changes - 4
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Mar 28, 2024
1 parent 0ff000b commit b4ebf40
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/executor/delete.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -68,10 +65,13 @@ where
Deleter::new(query).exec(db).await
}

async fn exec_delete<C>(statement: Statement, db: &C) -> Result<DeleteResult, DbErr>
async fn exec_delete<C>(query: DeleteStatement, db: &C) -> Result<DeleteResult, DbErr>
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(),
Expand Down
17 changes: 10 additions & 7 deletions src/executor/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -208,13 +207,16 @@ where

async fn exec_insert<A, C>(
primary_key: Option<ValueTuple>,
statement: Statement,
statement: InsertStatement,
db: &C,
) -> Result<InsertResult<A>, DbErr>
where
C: ConnectionTrait,
A: ActiveModelTrait,
{
let db_backend = db.get_database_backend();
let statement = db_backend.build(&statement);

type PrimaryKey<A> = <<A as ActiveModelTrait>::Entity as EntityTrait>::PrimaryKey;
type ValueTypeOf<A> = <PrimaryKey<A> as PrimaryKeyTrait>::ValueType;

Expand Down Expand Up @@ -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())
}

Expand All @@ -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::<SelectModel<<A::Entity as EntityTrait>::Model>>::from_statement(
db_backend.build(&insert_statement),
insert_statement,
)
.one(db)
.await?
}
false => {
let insert_res =
exec_insert::<A, _>(primary_key, db_backend.build(&insert_statement), db).await?;
let insert_res = exec_insert::<A, _>(primary_key, insert_statement, db).await?;
<A::Entity as EntityTrait>::find_by_id(insert_res.last_insert_id)
.one(db)
.await?
Expand Down
26 changes: 13 additions & 13 deletions src/executor/paginator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ where
/// Get the total number of items
pub async fn num_items(&self) -> Result<u64, DbErr> {
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),
Expand Down

0 comments on commit b4ebf40

Please sign in to comment.