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 d78085f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
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}")
}
}
21 changes: 11 additions & 10 deletions mithril-aggregator/src/database/record/certificate_pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

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 d78085f

Please sign in to comment.