This commit is contained in:
Mateusz Gruszczyński
2026-08-30 13:39:29 +02:00
parent 3e950ab5fa
commit 5c05eddb8f
83 changed files with 10130 additions and 9954 deletions
+70
View File
@@ -0,0 +1,70 @@
impl Db {
pub fn open(path: &Path) -> Result<Self> {
let conn = Connection::open(path)
.with_context(|| format!("cannot open SQLite database {}", path.display()))?;
conn.busy_timeout(std::time::Duration::from_secs(5))?;
conn.execute_batch(queries::INIT_SCHEMA)?;
Ok(Self { conn: Arc::new(Mutex::new(conn)) })
}
fn lock(&self) -> Result<std::sync::MutexGuard<'_, Connection>> {
self.conn.lock().map_err(|_| anyhow::anyhow!("database mutex poisoned"))
}
fn from_json<T: DeserializeOwned>(payload: String) -> Result<T> {
Ok(serde_json::from_str(&payload)?)
}
fn to_json<T: Serialize>(value: &T) -> Result<String> {
Ok(serde_json::to_string(value)?)
}
pub fn count_devices(&self) -> Result<u64> {
let conn = self.lock()?;
let count: i64 = conn.query_row(queries::COUNT_DEVICES, [], |row| row.get(0))?;
Ok(count.max(0) as u64)
}
pub fn save_device(&self, device: &Device) -> Result<()> {
let payload = Self::to_json(device)?;
let conn = self.lock()?;
conn.execute(
queries::UPSERT_DEVICE,
params![device.id, device.mac, device.name, device.ip, device.simulated as i64, payload, device.updated_at.to_rfc3339()],
)?;
Ok(())
}
pub fn list_devices(&self) -> Result<Vec<Device>> {
let conn = self.lock()?;
let mut stmt = conn.prepare(queries::LIST_DEVICES)?;
let payloads = stmt.query_map([], |row| row.get::<_, String>(0))?
.collect::<std::result::Result<Vec<_>, _>>()?;
payloads.into_iter().map(Self::from_json).collect()
}
pub fn get_device(&self, id: &str) -> Result<Option<Device>> {
let conn = self.lock()?;
let payload: Option<String> = conn.query_row(queries::GET_DEVICE_BY_ID, [id], |row| row.get(0)).optional()?;
payload.map(Self::from_json).transpose()
}
pub fn get_device_by_mac(&self, mac: &str) -> Result<Option<Device>> {
let conn = self.lock()?;
let payload: Option<String> = conn.query_row(queries::GET_DEVICE_BY_MAC, [mac], |row| row.get(0)).optional()?;
payload.map(Self::from_json).transpose()
}
pub fn delete_device(&self, id: &str) -> Result<bool> {
let mut conn = self.lock()?;
let tx = conn.transaction()?;
tx.execute(queries::DELETE_DEVICE_READINGS, [id])?;
tx.execute(queries::DELETE_ZONE_READINGS_BY_DEVICE_ID, [id])?;
tx.execute(queries::DELETE_SCHEDULES_BY_DEVICE_ID, [id])?;
tx.execute(queries::DELETE_ZONES_BY_DEVICE_ID, [id])?;
let changed = tx.execute(queries::DELETE_DEVICE, [id])? > 0;
tx.commit()?;
Ok(changed)
}
}