From be39c0532567e05988339e3befbac45f71c2d8c3 Mon Sep 17 00:00:00 2001 From: Dragyx <66752602+Dragyx@users.noreply.github.com> Date: Tue, 6 May 2025 16:29:16 +0200 Subject: [PATCH] store: move database url into constant --- src/store.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/store.rs b/src/store.rs index e396755..018bac3 100644 --- a/src/store.rs +++ b/src/store.rs @@ -6,6 +6,8 @@ use rusqlite::Connection; // Use type alias for Result with our custom error type type Result = std::result::Result; +const DATABASE_URL: &str = "/nix/var/nix/db/db.sqlite"; + const QUERY_PKGS: &str = " WITH RECURSIVE graph(p) AS ( @@ -59,12 +61,12 @@ SELECT p, c from graph; /// in the future, we might wan't to switch to async pub fn get_packages(path: &std::path::Path) -> Result> { let p: String = path.canonicalize()?.to_string_lossy().into_owned(); - let conn = Connection::open("/nix/var/nix/db/db.sqlite")?; + let conn = Connection::open(DATABASE_URL)?; let mut stmt = conn.prepare(QUERY_PKGS)?; - let queried_pkgs: std::result::Result, _> = - stmt.query_map([p], |row| Ok((row.get(0)?, row.get(1)?)))? - .collect(); + let queried_pkgs: std::result::Result, _> = stmt + .query_map([p], |row| Ok((row.get(0)?, row.get(1)?)))? + .collect(); Ok(queried_pkgs?) } @@ -76,14 +78,13 @@ pub fn get_packages(path: &std::path::Path) -> Result> { /// in the future, we might wan't to switch to async pub fn get_closure_size(path: &std::path::Path) -> Result { let p: String = path.canonicalize()?.to_string_lossy().into_owned(); - let conn = Connection::open("/nix/var/nix/db/db.sqlite")?; + let conn = Connection::open(DATABASE_URL)?; let mut stmt = conn.prepare(QUERY_CLOSURE_SIZE)?; let queried_sum = stmt.query_row([p], |row| row.get(0))?; Ok(queried_sum) } - /// returns the complete dependency graph of /// of the derivation as an adjacency list. The nodes are /// represented by the DB ids @@ -92,19 +93,17 @@ pub fn get_closure_size(path: &std::path::Path) -> Result { /// well in the future, depending on how much we use them /// in the operations on the graph /// -/// The mapping from id to graph can be obtained by using [get_packages] +/// The mapping from id to graph can be obtained by using [``get_packages``] pub fn get_dependency_graph(path: &std::path::Path) -> Result>> { let p: String = path.canonicalize()?.to_string_lossy().into_owned(); - let conn = Connection::open("/nix/var/nix/db/db.sqlite")?; + let conn = Connection::open(DATABASE_URL)?; let mut stmt = conn.prepare(QUERY_DEPENDENCY_GRAPH)?; let mut adj = HashMap::>::new(); - let queried_sum = stmt.query_map( - [p], - |row| Ok::<(i64, i64), _>((row.get(0)?, row.get(1)?)))?; + let queried_sum = stmt.query_map([p], |row| Ok::<(i64, i64), _>((row.get(0)?, row.get(1)?)))?; for row in queried_sum { - let (from, to) = row?; - adj.entry(from).or_default().push(to); + let (from, to) = row?; + adj.entry(from).or_default().push(to); } Ok(adj)