Skip to content

Commit

Permalink
Add Insert query execution without RETURNING clause (#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueb2 authored Nov 11, 2022
1 parent 7755680 commit cf4657e
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/executor/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ where
Inserter::<A>::new(self.primary_key, query).exec(db)
}

/// Execute an insert operation without returning (don't use `RETURNING` syntax)
/// Number of rows affected is returned
pub fn exec_without_returning<'a, C>(
self,
db: &'a C,
) -> impl Future<Output = Result<u64, DbErr>> + '_
where
<A::Entity as EntityTrait>::Model: IntoActiveModel<A>,
C: ConnectionTrait,
A: 'a,
{
Inserter::<A>::new(self.primary_key, self.query).exec_without_returning(db)
}

/// Execute an insert operation and return the inserted model (use `RETURNING` syntax if database supported)
pub fn exec_with_returning<'a, C>(
self,
Expand Down Expand Up @@ -75,7 +89,7 @@ where
}
}

/// Execute an insert operation
/// Execute an insert operation, returning the last inserted id
pub fn exec<'a, C>(self, db: &'a C) -> impl Future<Output = Result<InsertResult<A>, DbErr>> + '_
where
C: ConnectionTrait,
Expand All @@ -85,6 +99,18 @@ where
exec_insert(self.primary_key, builder.build(&self.query), db)
}

/// Execute an insert operation
pub fn exec_without_returning<'a, C>(
self,
db: &'a C,
) -> impl Future<Output = Result<u64, DbErr>> + '_
where
C: ConnectionTrait,
A: 'a,
{
exec_insert_without_returning(self.query, db)
}

/// Execute an insert operation and return the inserted model (use `RETURNING` syntax if database supported)
pub fn exec_with_returning<'a, C>(
self,
Expand Down Expand Up @@ -134,6 +160,18 @@ where
Ok(InsertResult { last_insert_id })
}

async fn exec_insert_without_returning<C>(
insert_statement: InsertStatement,
db: &C,
) -> Result<u64, DbErr>
where
C: ConnectionTrait,
{
let db_backend = db.get_database_backend();
let exec_result = db.execute(db_backend.build(&insert_statement)).await?;
Ok(exec_result.rows_affected())
}

async fn exec_insert_with_returning<A, C>(
primary_key: Option<ValueTuple>,
mut insert_statement: InsertStatement,
Expand Down

0 comments on commit cf4657e

Please sign in to comment.