Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Insert query execution without RETURNING clause #1208

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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