Commit 65916ac9 authored by Éloïs's avatar Éloïs

[ref] gva:schema: create macro exec_in_db_transaction to factor code

parent 97c7f91c
Pipeline #7351 failed with stages
in 24 minutes and 50 seconds
......@@ -1050,7 +1050,6 @@ version = "0.1.0"
dependencies = [
"actix-web 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
"assert-json-diff 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
"dubp-block-doc 0.1.0",
"dubp-blocks-tests-tools 0.1.0",
......
......@@ -11,7 +11,6 @@ path = "src/lib.rs"
[dependencies]
actix-web = "1.0.9"
cfg-if = "0.1.10"
dubp-block-doc = { path = "../../dubp/block-doc"} #, version = "0.1.0" }
dup-crypto = { path = "../../crypto" }
durs-bc-db-reader = { path = "../../modules-lib/bc-db-reader", features = ["client-indexer"] }
......
......@@ -33,6 +33,26 @@ use juniper_from_schema::graphql_schema_from_file;
// generate schema from schema file
graphql_schema_from_file!("resources/schema.gql", context_type: QueryContext);
/// Macro that execute a query resolver in db read transaction
#[cfg(not(test))]
macro_rules! exec_in_db_transaction {
($f:ident($e:ident, $($param:expr),*)) => {
{
let db = $e.context().get_db();
db.read(|r| queries::$f::execute(&BcDbRoWithReader { db, r }$(, $param)*)).map_err(Into::into)
}
};
}
#[cfg(test)]
macro_rules! exec_in_db_transaction {
($f:ident($e:ident, $($param:expr),*)) => {
{
let db = $e.context().get_db();
queries::$f::execute(db$(, $param)*).map_err(Into::into)
}
};
}
pub struct Query;
impl QueryFields for Query {
......@@ -54,14 +74,7 @@ impl QueryFields for Query {
executor: &Executor<'_, QueryContext>,
trail: &QueryTrail<'_, Block, Walked>,
) -> FieldResult<Option<Block>> {
let db = executor.context().get_db();
cfg_if::cfg_if! {
if #[cfg(not(test))] {
db.read(|r| queries::current::execute(&BcDbRoWithReader { db, r }, trail)).map_err(Into::into)
} else {
queries::current::execute(db, trail).map_err(Into::into)
}
}
exec_in_db_transaction!(current(executor, trail))
}
#[inline]
fn field_block(
......@@ -70,14 +83,7 @@ impl QueryFields for Query {
trail: &QueryTrail<'_, Block, Walked>,
number: i32,
) -> FieldResult<Option<Block>> {
let db = executor.context().get_db();
cfg_if::cfg_if! {
if #[cfg(not(test))] {
db.read(|r| queries::block::execute(&BcDbRoWithReader { db, r }, trail, number)).map_err(Into::into)
} else {
queries::block::execute(db, trail, number).map_err(Into::into)
}
}
exec_in_db_transaction!(block(executor, trail, number))
}
#[inline]
fn field_blocks(
......@@ -92,32 +98,14 @@ impl QueryFields for Query {
if step <= 0 {
step = 1;
}
let db = executor.context().get_db();
cfg_if::cfg_if! {
if #[cfg(not(test))] {
db.read(|r| {
queries::blocks::execute(
&BcDbRoWithReader { db, r },
trail,
paging_opt,
block_interval_opt,
step as usize,
sort_order,
)
})
.map_err(Into::into)
} else {
queries::blocks::execute(
db,
trail,
paging_opt,
block_interval_opt,
step as usize,
sort_order,
)
.map_err(Into::into)
}
}
exec_in_db_transaction!(blocks(
executor,
trail,
paging_opt,
block_interval_opt,
step as usize,
sort_order
))
}
#[inline]
fn field_current_ud(
......@@ -125,14 +113,7 @@ impl QueryFields for Query {
executor: &Executor<'_, QueryContext>,
trail: &QueryTrail<'_, CurrentUd, Walked>,
) -> FieldResult<Option<CurrentUd>> {
let db = executor.context().get_db();
cfg_if::cfg_if! {
if #[cfg(not(test))] {
db.read(|r| queries::current_ud::execute(&BcDbRoWithReader { db, r }, trail)).map_err(Into::into)
} else {
queries::current_ud::execute(db, trail).map_err(Into::into)
}
}
exec_in_db_transaction!(current_ud(executor, trail))
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment