Skip to content

Commit

Permalink
Replace get_projection_with_table by expand_projection
Browse files Browse the repository at this point in the history
  • Loading branch information
sfauvel committed Nov 26, 2024
1 parent e450847 commit 61fd2ee
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mithril_persistence::sqlite::{Query, SourceAlias, WhereCondition};
use mithril_persistence::sqlite::{Query, WhereCondition};

use crate::database::record::CertificatePendingRecord;

Expand All @@ -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}")
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mithril_persistence::sqlite::{Query, SourceAlias, WhereCondition};
use mithril_persistence::sqlite::{Query, WhereCondition};

use crate::database::record::CertificatePendingRecord;

Expand All @@ -23,12 +23,8 @@ 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"
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use chrono::Utc;
use sqlite::Value;

use mithril_persistence::sqlite::{Query, SourceAlias, WhereCondition};
use mithril_persistence::sqlite::{Query, WhereCondition};

use crate::database::record::CertificatePendingRecord;

/// Query to update [CertificatePendingRecord] in the sqlite database
/// Query to save [CertificatePendingRecord] in the sqlite database
pub struct SavePendingCertificateRecordQuery {
condition: WhereCondition,
}
Expand Down Expand Up @@ -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}")
}
}
23 changes: 12 additions & 11 deletions mithril-aggregator/src/database/record/certificate_pending.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono::{DateTime, Utc};
use mithril_common::entities::{CertificatePending, Epoch};
use mithril_persistence::sqlite::{HydrationError, Projection, SqLiteEntity};
use mithril_persistence::sqlite::{HydrationError, Projection, SourceAlias, SqLiteEntity};

/// CertificatePending record is the representation of a stored pending certificate.
pub struct CertificatePendingRecord {
Expand All @@ -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) -> String {
let aliases = SourceAlias::new(&[("{:pending_certificate:}", table)]);
Self::get_projection().expand(aliases)
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 61fd2ee

Please sign in to comment.