-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(contracts-info): make insert_with_select to be a function in…
…stead of macro
- Loading branch information
1 parent
51c437e
commit da2750c
Showing
2 changed files
with
146 additions
and
101 deletions.
There are no files selected for viewing
136 changes: 82 additions & 54 deletions
136
eth-bytecode-db/verifier-alliance-database/src/helpers.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,91 @@ | ||
macro_rules! insert_then_select { | ||
( $txn:expr, $entity_module:ident, $active_model:expr, $update_on_conflict:expr, [ $( ($column:ident, $value:expr) ),+ $(,)? ] ) => { | ||
{ | ||
use anyhow::Context; | ||
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; | ||
use anyhow::Context; | ||
use sea_orm::{ | ||
sea_query::OnConflict, ActiveModelBehavior, ActiveModelTrait, ColumnTrait, ConnectionTrait, | ||
DbErr, EntityTrait, IntoActiveModel, ModelTrait, PrimaryKeyToColumn, QueryFilter, | ||
}; | ||
|
||
let result: Result<_, sea_orm::DbErr> = $entity_module::Entity::insert($active_model.clone()) | ||
.on_conflict(sea_orm::sea_query::OnConflict::new().do_nothing().to_owned()) | ||
.exec($txn) | ||
.await; | ||
pub async fn insert_then_select<C, Entity, ActiveModel>( | ||
txn: &C, | ||
entity: Entity, | ||
active_model: ActiveModel, | ||
unique_columns: impl IntoIterator<Item = (Entity::Column, sea_orm::Value)>, | ||
) -> Result<(Entity::Model, bool), anyhow::Error> | ||
where | ||
C: ConnectionTrait, | ||
Entity: EntityTrait, | ||
ActiveModel: ActiveModelTrait<Entity = Entity> + ActiveModelBehavior + Send, | ||
<Entity as EntityTrait>::Model: IntoActiveModel<ActiveModel>, | ||
{ | ||
insert_then_select_internal(txn, entity, active_model, unique_columns, false).await | ||
} | ||
|
||
// Returns the model and the bool flag showing whether the model was actually inserted. | ||
match result { | ||
Ok(res) => { | ||
let last_insert_id = res.last_insert_id; | ||
let model = $entity_module::Entity::find_by_id(last_insert_id.clone()) | ||
.one($txn) | ||
.await | ||
.context(format!("select from \"{}\" by \"id\"", stringify!($entity_module)))? | ||
.ok_or(anyhow::anyhow!( | ||
"select from \"{}\" by \"id\"={:?} returned no data", | ||
stringify!($entity_module), | ||
last_insert_id | ||
))?; | ||
async fn insert_then_select_internal<C, Entity, ActiveModel>( | ||
txn: &C, | ||
entity: Entity, | ||
active_model: ActiveModel, | ||
unique_columns: impl IntoIterator<Item = (Entity::Column, sea_orm::Value)>, | ||
update_on_conflict: bool, | ||
) -> Result<(Entity::Model, bool), anyhow::Error> | ||
where | ||
C: ConnectionTrait, | ||
Entity: EntityTrait, | ||
ActiveModel: ActiveModelTrait<Entity = Entity> + ActiveModelBehavior + Send, | ||
<Entity as EntityTrait>::Model: IntoActiveModel<ActiveModel>, | ||
{ | ||
let entity_table_name = entity.table_name(); | ||
|
||
Ok((model, true)) | ||
} | ||
Err(sea_orm::DbErr::RecordNotInserted) => { | ||
let mut model = | ||
$entity_module::Entity::find() | ||
$( | ||
.filter($entity_module::Column::$column.eq($value)) | ||
)* | ||
.one($txn) | ||
.await | ||
.context(format!("select from \"{}\" by unique columns", stringify!($entity_module)))? | ||
.ok_or(anyhow::anyhow!("select from \"{}\" by unique columns returned no data", stringify!($entity_module)))?; | ||
// The active model have not been inserted. | ||
// Thus, there were a value already that we need to update. | ||
if $update_on_conflict { | ||
let mut active_model_to_update = $active_model; | ||
for primary_key in <$entity_module::PrimaryKey as sea_orm::Iterable>::iter() { | ||
let column = sea_orm::PrimaryKeyToColumn::into_column(primary_key); | ||
let value = sea_orm::ModelTrait::get(&model, column); | ||
sea_orm::ActiveModelTrait::set(&mut active_model_to_update, column, value); | ||
} | ||
let updated_model = sea_orm::ActiveModelTrait::update( | ||
active_model_to_update, $txn | ||
).await.context(format!("update on conflict in \"{}\"", stringify!($entity_module)))?; | ||
let result: Result<_, DbErr> = Entity::insert(active_model.clone()) | ||
.on_conflict(OnConflict::new().do_nothing().to_owned()) | ||
.exec(txn) | ||
.await; | ||
|
||
// Returns the model and the bool flag showing whether the model was actually inserted. | ||
match result { | ||
Ok(res) => { | ||
let last_insert_id = res.last_insert_id; | ||
let id_debug_str = format!("{last_insert_id:?}"); | ||
let model = Entity::find_by_id(last_insert_id) | ||
.one(txn) | ||
.await | ||
.context(format!("select from \"{entity_table_name}\" by \"id\""))? | ||
.ok_or(anyhow::anyhow!( | ||
"select from \"{entity_table_name}\" by \"id\"={id_debug_str} returned no data" | ||
))?; | ||
|
||
if updated_model != model { | ||
model = updated_model; | ||
} | ||
} | ||
Ok((model, true)) | ||
} | ||
Err(DbErr::RecordNotInserted) => { | ||
let mut query = Entity::find(); | ||
for (column, value) in unique_columns { | ||
query = query.filter(column.eq(value)); | ||
} | ||
let mut model = query | ||
.one(txn) | ||
.await | ||
.context(format!( | ||
"select from \"{entity_table_name}\" by unique columns" | ||
))? | ||
.ok_or(anyhow::anyhow!( | ||
"select from \"{entity_table_name}\" by unique columns returned no data" | ||
))?; | ||
|
||
Ok((model, false)) | ||
// The active model have not been inserted. | ||
// Thus, there were a value already that we need to update. | ||
if update_on_conflict { | ||
let mut active_model_to_update = active_model; | ||
for primary_key in <Entity::PrimaryKey as sea_orm::Iterable>::iter() { | ||
let column = PrimaryKeyToColumn::into_column(primary_key); | ||
let value = ModelTrait::get(&model, column); | ||
ActiveModelTrait::set(&mut active_model_to_update, column, value); | ||
} | ||
Err(err) => Err(err).context(format!("insert into \"{}\"", stringify!($entity_module))), | ||
model = active_model_to_update | ||
.update(txn) | ||
.await | ||
.context(format!("update on conflict in \"{entity_table_name}\""))?; | ||
} | ||
|
||
Ok((model, false)) | ||
} | ||
}; | ||
Err(err) => Err(err).context(format!("insert into \"{entity_table_name}\"")), | ||
} | ||
} | ||
pub(crate) use insert_then_select; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters