diff options
Diffstat (limited to 'src/db/snapshots.rs')
| -rw-r--r-- | src/db/snapshots.rs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/db/snapshots.rs b/src/db/snapshots.rs new file mode 100644 index 0000000..754d7b7 --- /dev/null +++ b/src/db/snapshots.rs @@ -0,0 +1,90 @@ +use rusqlite::{params, OptionalExtension}; + +use crate::db::*; +use crate::error::Result; +use crate::time; + +// ---------------------------------------------------------------- snapshot -- + +#[allow(clippy::too_many_arguments)] +pub fn put_snapshot( + writer: &Writer, + pkgbase_id: i64, + aur_commit: &str, + recipe_hash: &str, + srcinfo_json: &str, + files_json: &str, +) -> Result<i64> { + let (c, rh, si, fj) = ( + aur_commit.to_string(), + recipe_hash.to_string(), + srcinfo_json.to_string(), + files_json.to_string(), + ); + let at = time::now(); + writer.tx(move |tx| { + tx.execute( + "INSERT INTO snapshot (pkgbase_id, aur_commit, recipe_hash, srcinfo_json, files_json, fetched_at) + VALUES (?1,?2,?3,?4,?5,?6) + ON CONFLICT(pkgbase_id, aur_commit) DO NOTHING", + params![pkgbase_id, c, rh, si, fj, at], + )?; + tx.query_row( + "SELECT id FROM snapshot WHERE pkgbase_id=?1 AND aur_commit=?2", + params![pkgbase_id, c], + |r| r.get(0), + ) + .map_err(Into::into) + }) +} + +#[derive(Debug, Clone)] +pub struct SnapshotRow { + pub id: i64, + pub aur_commit: String, + pub recipe_hash: String, + pub srcinfo_json: String, + pub files_json: String, +} + +pub fn snapshot_by_commit(db: &Db, pkgbase_id: i64, commit: &str) -> Result<Option<SnapshotRow>> { + db.read(|c| { + c.query_row( + "SELECT id, aur_commit, recipe_hash, srcinfo_json, files_json FROM snapshot + WHERE pkgbase_id=?1 AND aur_commit=?2", + params![pkgbase_id, commit], + |r| { + Ok(SnapshotRow { + id: r.get(0)?, + aur_commit: r.get(1)?, + recipe_hash: r.get(2)?, + srcinfo_json: r.get(3)?, + files_json: r.get(4)?, + }) + }, + ) + .optional() + .map_err(Into::into) + }) +} + +pub fn snapshot_by_id(db: &Db, id: i64) -> Result<Option<SnapshotRow>> { + db.read(|c| { + c.query_row( + "SELECT id, aur_commit, recipe_hash, srcinfo_json, files_json FROM snapshot WHERE id=?1", + params![id], + |r| { + Ok(SnapshotRow { + id: r.get(0)?, + aur_commit: r.get(1)?, + recipe_hash: r.get(2)?, + srcinfo_json: r.get(3)?, + files_json: r.get(4)?, + }) + }, + ) + .optional() + .map_err(Into::into) + }) +} + |
