feat: add ref_date field to entity

This commit is contained in:
2025-10-01 07:56:24 +02:00
parent cf3ab35144
commit 36bb9e3845
4 changed files with 15 additions and 5 deletions

View File

@@ -5,7 +5,8 @@ pub fn run(path: &path::Path) {
let init_statement = r#"
CREATE TABLE IF NOT EXISTS entity (
id TEXT PRIMARY KEY,
class TEXT NOT NULL
class TEXT NOT NULL,
ref_date INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS attribute (
id TEXT,

View File

@@ -3,7 +3,11 @@ use crate::*;
pub fn run(row: &rusqlite::Row) -> rusqlite::Result<Entity> {
let id: String = row.get(0)?;
let class: String = row.get(1)?;
Ok(Entity::default().with_id(&id).with_class(&class))
let ref_date: u64 = row.get(2)?;
Ok(Entity::default()
.with_id(&id)
.with_class(&class)
.with_ref_date(ref_date))
}
// #[cfg(test)]

View File

@@ -17,8 +17,8 @@ const DELETE_ENTITY_STATEMENT: &str = r#"
"#;
const INSERT_ENTITY_STATEMENT: &str = r#"
INSERT INTO entity (id, class)
VALUES (?1, ?2);
INSERT INTO entity (id, class, ref_date)
VALUES (?1, ?2, ?3);
"#;
const INSERT_ATTRIBUTE_STATEMENT_TEMPLATE: &str = r#"
@@ -36,7 +36,7 @@ fn write_attribute(attribute: &Attribute, entity: &Entity, transaction: &rusqlit
fn write_entity(entity: &Entity, transaction: &rusqlite::Transaction) {
let entity_id = [&entity.id];
let entity_values = (&entity.id, &entity.class);
let entity_values = (&entity.id, &entity.class, entity.ref_date);
let _ = transaction.execute(DELETE_ENTITY_STATEMENT, entity_id);
let _ = transaction.execute(INSERT_ENTITY_STATEMENT, entity_values);
for (_key, attribute) in entity.attributes.iter() {

View File

@@ -4,6 +4,7 @@ use crate::*;
pub struct O {
pub id: String,
pub state: EntityState,
pub ref_date: u64,
pub class: String,
pub attributes: std::collections::HashMap<String, Attribute>,
}
@@ -26,6 +27,10 @@ impl O {
self.class = class.to_string();
self
}
pub fn with_ref_date(mut self, ref_date: u64) -> Self {
self.ref_date = ref_date;
self
}
pub fn with_attribute(mut self, id: &str, value: impl Into<Value>) -> Self {
let attribute = Attribute::new(id, value);
self.attributes.insert(attribute.id.clone(), attribute);