Skip to content

Commit 61fd2ee

Browse files
committed
Replace get_projection_with_table by expand_projection
1 parent e450847 commit 61fd2ee

File tree

4 files changed

+19
-35
lines changed

4 files changed

+19
-35
lines changed

mithril-aggregator/src/database/query/pending_certificate/delete_pending_certificate.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use mithril_persistence::sqlite::{Query, SourceAlias, WhereCondition};
1+
use mithril_persistence::sqlite::{Query, WhereCondition};
22

33
use crate::database::record::CertificatePendingRecord;
44

@@ -25,14 +25,7 @@ impl Query for DeletePendingCertificateRecordQuery {
2525
fn get_definition(&self, condition: &str) -> String {
2626
// it is important to alias the fields with the same name as the table
2727
// since the table cannot be aliased in a RETURNING statement in SQLite.
28-
29-
// let projection = Self::Entity::get_projection().expand(SourceAlias::new(&[(
30-
// "{:pending_certificate:}",
31-
// "pending_certificate",
32-
// )]));
33-
let projection = Self::Entity::get_projection_with_table("pending_certificate")
34-
.expand(SourceAlias::new(&[]));
35-
28+
let projection = Self::Entity::expand_projection("pending_certificate");
3629
format!("delete from pending_certificate where {condition} returning {projection}")
3730
}
3831
}

mithril-aggregator/src/database/query/pending_certificate/get_pending_certificate.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use mithril_persistence::sqlite::{Query, SourceAlias, WhereCondition};
1+
use mithril_persistence::sqlite::{Query, WhereCondition};
22

33
use crate::database::record::CertificatePendingRecord;
44

@@ -23,12 +23,8 @@ impl Query for GetPendingCertificateRecordQuery {
2323
}
2424

2525
fn get_definition(&self, condition: &str) -> String {
26-
// let aliases = SourceAlias::new(&[("{:pending_certificate:}", "new_pending_certificate")]);
27-
// let projection = Self::Entity::get_projection().expand(aliases);
28-
let projection = Self::Entity::get_projection_with_table("pending_certificate")
29-
.expand(SourceAlias::new(&[]));
26+
let projection = Self::Entity::expand_projection("pending_certificate");
3027
format!(
31-
// TODO check the order to keep
3228
"select {projection} from pending_certificate where {condition} order by ROWID desc"
3329
)
3430
}

mithril-aggregator/src/database/query/pending_certificate/save_pending_certificate.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use chrono::Utc;
22
use sqlite::Value;
33

4-
use mithril_persistence::sqlite::{Query, SourceAlias, WhereCondition};
4+
use mithril_persistence::sqlite::{Query, WhereCondition};
55

66
use crate::database::record::CertificatePendingRecord;
77

8-
/// Query to update [CertificatePendingRecord] in the sqlite database
8+
/// Query to save [CertificatePendingRecord] in the sqlite database
99
pub struct SavePendingCertificateRecordQuery {
1010
condition: WhereCondition,
1111
}
@@ -36,13 +36,7 @@ impl Query for SavePendingCertificateRecordQuery {
3636
// it is important to alias the fields with the same name as the table
3737
// since the table cannot be aliased in a RETURNING statement in SQLite.
3838

39-
// let projection = Self::Entity::get_projection().expand(SourceAlias::new(&[(
40-
// "{:pending_certificate:}",
41-
// "pending_certificate",
42-
// )]));
43-
let projection = Self::Entity::get_projection_with_table("pending_certificate")
44-
.expand(SourceAlias::new(&[]));
45-
39+
let projection = Self::Entity::expand_projection("pending_certificate");
4640
format!("insert or replace into pending_certificate {condition} returning {projection}")
4741
}
4842
}

mithril-aggregator/src/database/record/certificate_pending.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use chrono::{DateTime, Utc};
22
use mithril_common::entities::{CertificatePending, Epoch};
3-
use mithril_persistence::sqlite::{HydrationError, Projection, SqLiteEntity};
3+
use mithril_persistence::sqlite::{HydrationError, Projection, SourceAlias, SqLiteEntity};
44

55
/// CertificatePending record is the representation of a stored pending certificate.
66
pub struct CertificatePendingRecord {
@@ -15,15 +15,10 @@ pub struct CertificatePendingRecord {
1515
}
1616

1717
impl CertificatePendingRecord {
18-
/// Construct a [Projection] that will allow to hydrate this `CertificatePendingRecord`.
19-
pub fn get_projection_with_table(table: &str) -> Projection {
20-
let mut projection = Projection::default();
21-
22-
projection.add_field("epoch", &format!("{table}.epoch"), "integer");
23-
projection.add_field("certificate", &format!("{table}.certificate"), "text");
24-
projection.add_field("created_at", &format!("{table}.created_at"), "text");
25-
26-
projection
18+
/// Construct a [Projection] that will allow to hydrate this `CertificatePendingRecord` and expend table alias.
19+
pub fn expand_projection(table: &str) -> String {
20+
let aliases = SourceAlias::new(&[("{:pending_certificate:}", table)]);
21+
Self::get_projection().expand(aliases)
2722
}
2823
}
2924

@@ -56,7 +51,13 @@ impl SqLiteEntity for CertificatePendingRecord {
5651
}
5752

5853
fn get_projection() -> Projection {
59-
Self::get_projection_with_table("{:pending_certificate:}")
54+
let mut projection = Projection::default();
55+
56+
projection.add_field("epoch", "{:pending_certificate:}.epoch", "integer");
57+
projection.add_field("certificate", "{:pending_certificate:}.certificate", "text");
58+
projection.add_field("created_at", "{:pending_certificate:}.created_at", "text");
59+
60+
projection
6061
}
6162
}
6263

0 commit comments

Comments
 (0)