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

Make insert statement not panic when inserting nothing #1708

Merged
merged 7 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions src/entity/base_entity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
ActiveModelTrait, ColumnTrait, Delete, DeleteMany, DeleteOne, FromQueryResult, Insert,
ModelTrait, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, Related, RelationBuilder,
RelationTrait, RelationType, Select, Update, UpdateMany, UpdateOne,
InsertTrait, ModelTrait, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, Related,
RelationBuilder, RelationTrait, RelationType, Select, Update, UpdateMany, UpdateOne,
};
use sea_query::{Alias, Iden, IntoIden, IntoTableRef, IntoValueTuple, TableRef};
use std::fmt::Debug;
Expand Down
73 changes: 71 additions & 2 deletions src/executor/insert.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
error::*, ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, Insert, IntoActiveModel,
Iterable, PrimaryKeyToColumn, PrimaryKeyTrait, SelectModel, SelectorRaw, Statement, TryFromU64,
error::*, ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, Insert, InsertAttempt,
IntoActiveModel, Iterable, PrimaryKeyToColumn, PrimaryKeyTrait, SelectModel, SelectorRaw,
Statement, TryFromU64,
};
use sea_query::{Expr, FromValueTuple, Iden, InsertStatement, IntoColumnRef, Query, ValueTuple};
use std::{future::Future, marker::PhantomData};
Expand All @@ -26,6 +27,74 @@ where
pub last_insert_id: <<<A as ActiveModelTrait>::Entity as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::ValueType,
}

/// The types of results for an INSERT operation
#[derive(Debug)]
pub enum InsertAttemptResultType<T> {
/// The INSERT operation did not insert any value
Empty,
/// Reserved
Conflicted,
/// Successfully inserted
Inserted(T),
}

impl<A> InsertAttempt<A>
where
A: ActiveModelTrait,
{
/// Execute an insert operation
#[allow(unused_mut)]
pub async fn exec<'a, C>(
self,
db: &'a C,
) -> InsertAttemptResultType<Result<InsertResult<A>, DbErr>>
where
C: ConnectionTrait,
A: 'a,
{
if self.insert_struct.columns.is_empty() {
InsertAttemptResultType::Empty
} else {
InsertAttemptResultType::Inserted(self.insert_struct.exec(db).await)
}
}

/// Execute an insert operation without returning (don't use `RETURNING` syntax)
/// Number of rows affected is returned
pub async fn exec_without_returning<'a, C>(
self,
db: &'a C,
) -> InsertAttemptResultType<Result<u64, DbErr>>
where
<A::Entity as EntityTrait>::Model: IntoActiveModel<A>,
C: ConnectionTrait,
A: 'a,
{
if self.insert_struct.columns.is_empty() {
InsertAttemptResultType::Empty
} else {
InsertAttemptResultType::Inserted(self.insert_struct.exec_without_returning(db).await)
}
}

/// Execute an insert operation and return the inserted model (use `RETURNING` syntax if database supported)
pub async fn exec_with_returning<'a, C>(
self,
db: &'a C,
) -> InsertAttemptResultType<Result<<A::Entity as EntityTrait>::Model, DbErr>>
where
<A::Entity as EntityTrait>::Model: IntoActiveModel<A>,
C: ConnectionTrait,
A: 'a,
{
if self.insert_struct.columns.is_empty() {
InsertAttemptResultType::Empty
} else {
InsertAttemptResultType::Inserted(self.insert_struct.exec_with_returning(db).await)
}
}
}

impl<A> Insert<A>
where
A: ActiveModelTrait,
Expand Down
Loading