From 58c39e16b86a6fb33da965751ecd0e9cff061385 Mon Sep 17 00:00:00 2001 From: Jacob Trueb Date: Mon, 7 Nov 2022 11:31:25 -0600 Subject: [PATCH] Add Insert query execution without RETURNING clause --- src/executor/insert.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/executor/insert.rs b/src/executor/insert.rs index 51b826234..29ab6b440 100644 --- a/src/executor/insert.rs +++ b/src/executor/insert.rs @@ -48,6 +48,20 @@ where Inserter::::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> + '_ + where + ::Model: IntoActiveModel, + C: ConnectionTrait, + A: 'a, + { + Inserter::::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, @@ -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, DbErr>> + '_ where C: ConnectionTrait, @@ -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> + '_ + 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, @@ -134,6 +160,18 @@ where Ok(InsertResult { last_insert_id }) } +async fn exec_insert_without_returning( + insert_statement: InsertStatement, + db: &C, +) -> Result +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( primary_key: Option, mut insert_statement: InsertStatement,