From d78085fbb863333df7f4eeeea02c6537ce9ed50b Mon Sep 17 00:00:00 2001 From: sfauvel Date: Tue, 26 Nov 2024 16:58:33 +0100 Subject: [PATCH] Replace `get_projection_with_table` by `expand_projection` --- .../delete_pending_certificate.rs | 9 +------- .../get_pending_certificate.rs | 5 +---- .../save_pending_certificate.rs | 8 +------ .../database/record/certificate_pending.rs | 21 ++++++++++--------- 4 files changed, 14 insertions(+), 29 deletions(-) diff --git a/mithril-aggregator/src/database/query/pending_certificate/delete_pending_certificate.rs b/mithril-aggregator/src/database/query/pending_certificate/delete_pending_certificate.rs index a5515509b79..177ad543de3 100644 --- a/mithril-aggregator/src/database/query/pending_certificate/delete_pending_certificate.rs +++ b/mithril-aggregator/src/database/query/pending_certificate/delete_pending_certificate.rs @@ -25,14 +25,7 @@ impl Query for DeletePendingCertificateRecordQuery { fn get_definition(&self, condition: &str) -> String { // it is important to alias the fields with the same name as the table // since the table cannot be aliased in a RETURNING statement in SQLite. - - // let projection = Self::Entity::get_projection().expand(SourceAlias::new(&[( - // "{:pending_certificate:}", - // "pending_certificate", - // )])); - let projection = Self::Entity::get_projection_with_table("pending_certificate") - .expand(SourceAlias::new(&[])); - + let projection = Self::Entity::expand_projection("pending_certificate"); format!("delete from pending_certificate where {condition} returning {projection}") } } diff --git a/mithril-aggregator/src/database/query/pending_certificate/get_pending_certificate.rs b/mithril-aggregator/src/database/query/pending_certificate/get_pending_certificate.rs index aa4144dd8d5..a223f5ed246 100644 --- a/mithril-aggregator/src/database/query/pending_certificate/get_pending_certificate.rs +++ b/mithril-aggregator/src/database/query/pending_certificate/get_pending_certificate.rs @@ -23,10 +23,7 @@ impl Query for GetPendingCertificateRecordQuery { } fn get_definition(&self, condition: &str) -> String { - // let aliases = SourceAlias::new(&[("{:pending_certificate:}", "new_pending_certificate")]); - // let projection = Self::Entity::get_projection().expand(aliases); - let projection = Self::Entity::get_projection_with_table("pending_certificate") - .expand(SourceAlias::new(&[])); + let projection = Self::Entity::expand_projection("pending_certificate"); format!( // TODO check the order to keep "select {projection} from pending_certificate where {condition} order by ROWID desc" diff --git a/mithril-aggregator/src/database/query/pending_certificate/save_pending_certificate.rs b/mithril-aggregator/src/database/query/pending_certificate/save_pending_certificate.rs index 1e0bef5c77e..019d298fa47 100644 --- a/mithril-aggregator/src/database/query/pending_certificate/save_pending_certificate.rs +++ b/mithril-aggregator/src/database/query/pending_certificate/save_pending_certificate.rs @@ -36,13 +36,7 @@ impl Query for SavePendingCertificateRecordQuery { // it is important to alias the fields with the same name as the table // since the table cannot be aliased in a RETURNING statement in SQLite. - // let projection = Self::Entity::get_projection().expand(SourceAlias::new(&[( - // "{:pending_certificate:}", - // "pending_certificate", - // )])); - let projection = Self::Entity::get_projection_with_table("pending_certificate") - .expand(SourceAlias::new(&[])); - + let projection = Self::Entity::expand_projection("pending_certificate"); format!("insert or replace into pending_certificate {condition} returning {projection}") } } diff --git a/mithril-aggregator/src/database/record/certificate_pending.rs b/mithril-aggregator/src/database/record/certificate_pending.rs index 226564d1080..f2e87d4a98f 100644 --- a/mithril-aggregator/src/database/record/certificate_pending.rs +++ b/mithril-aggregator/src/database/record/certificate_pending.rs @@ -15,15 +15,10 @@ pub struct CertificatePendingRecord { } impl CertificatePendingRecord { - /// Construct a [Projection] that will allow to hydrate this `CertificatePendingRecord`. - pub fn get_projection_with_table(table: &str) -> Projection { - let mut projection = Projection::default(); - - projection.add_field("epoch", &format!("{table}.epoch"), "integer"); - projection.add_field("certificate", &format!("{table}.certificate"), "text"); - projection.add_field("created_at", &format!("{table}.created_at"), "text"); - - projection + /// Construct a [Projection] that will allow to hydrate this `CertificatePendingRecord` and expend table alias. + pub fn expand_projection(table: &str) -> Projection { + let aliases = SourceAlias::new(&[("{:pending_certificate:}", table)]); + Self::get_projection().expand(aliases); } } @@ -56,7 +51,13 @@ impl SqLiteEntity for CertificatePendingRecord { } fn get_projection() -> Projection { - Self::get_projection_with_table("{:pending_certificate:}") + let mut projection = Projection::default(); + + projection.add_field("epoch", "{:pending_certificate:}.epoch", "integer"); + projection.add_field("certificate", "{:pending_certificate:}.certificate", "text"); + projection.add_field("created_at", "{:pending_certificate:}.created_at", "text"); + + projection } }