Add local DB resource registration
This commit is contained in:
parent
21920b51b5
commit
4ec50d7473
14 changed files with 313 additions and 8 deletions
|
|
@ -222,6 +222,59 @@ impl Store {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_resource_by_kind_name(
|
||||
&self,
|
||||
kind: &str,
|
||||
name: &str,
|
||||
) -> Result<Option<StoredResource>, StoreError> {
|
||||
let mut stmt = self.conn.prepare(
|
||||
"SELECT resource_id, kind, name, status FROM resources WHERE kind = ?1 AND name = ?2",
|
||||
)?;
|
||||
let mut rows = stmt.query(params![kind, name])?;
|
||||
if let Some(row) = rows.next()? {
|
||||
Ok(Some(StoredResource {
|
||||
resource_id: row.get(0)?,
|
||||
kind: row.get(1)?,
|
||||
name: row.get(2)?,
|
||||
status: row.get(3)?,
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_db_resource(&self, db: &StoredDbResource) -> Result<(), StoreError> {
|
||||
self.conn.execute(
|
||||
r#"INSERT OR REPLACE INTO db_resources(db_id, resource_id, path)
|
||||
VALUES (?1, ?2, ?3)"#,
|
||||
params![db.db_id, db.resource_id, db.path],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_db_resource_by_name(
|
||||
&self,
|
||||
name: &str,
|
||||
) -> Result<Option<StoredDbResource>, StoreError> {
|
||||
let mut stmt = self.conn.prepare(
|
||||
r#"SELECT db.db_id, db.resource_id, resources.name, db.path
|
||||
FROM db_resources db
|
||||
JOIN resources ON resources.resource_id = db.resource_id
|
||||
WHERE resources.kind = 'db' AND resources.name = ?1"#,
|
||||
)?;
|
||||
let mut rows = stmt.query(params![name])?;
|
||||
if let Some(row) = rows.next()? {
|
||||
Ok(Some(StoredDbResource {
|
||||
db_id: row.get(0)?,
|
||||
resource_id: row.get(1)?,
|
||||
name: row.get(2)?,
|
||||
path: row.get(3)?,
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn record_cas_object(
|
||||
&self,
|
||||
hash: &str,
|
||||
|
|
@ -561,6 +614,14 @@ pub struct StoredResource {
|
|||
pub status: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct StoredDbResource {
|
||||
pub db_id: String,
|
||||
pub resource_id: String,
|
||||
pub name: String,
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct CasObject {
|
||||
pub hash: String,
|
||||
|
|
@ -788,4 +849,38 @@ mod tests {
|
|||
assert!(store.list_cas_objects().expect("objects").is_empty());
|
||||
assert!(store.list_cas_pins().expect("pins").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn db_resource_roundtrip_by_name() {
|
||||
let store = Store::open_memory().expect("open");
|
||||
let resource = StoredResource {
|
||||
resource_id: "resource:db:notes".to_owned(),
|
||||
kind: "db".to_owned(),
|
||||
name: "notes".to_owned(),
|
||||
status: "active".to_owned(),
|
||||
};
|
||||
store.insert_resource(&resource).expect("insert resource");
|
||||
store
|
||||
.insert_db_resource(&StoredDbResource {
|
||||
db_id: "db:notes".to_owned(),
|
||||
resource_id: resource.resource_id.clone(),
|
||||
name: resource.name.clone(),
|
||||
path: "/tmp/notes.sqlite".to_owned(),
|
||||
})
|
||||
.expect("insert db");
|
||||
|
||||
assert_eq!(
|
||||
store
|
||||
.get_resource_by_kind_name("db", "notes")
|
||||
.expect("get resource"),
|
||||
Some(resource)
|
||||
);
|
||||
assert_eq!(
|
||||
store
|
||||
.get_db_resource_by_name("notes")
|
||||
.expect("get db")
|
||||
.map(|db| db.path),
|
||||
Some("/tmp/notes.sqlite".to_owned())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue