1
Fork 0
mirror of https://github.com/RGBCube/dix synced 2025-07-28 12:17:45 +00:00

store: move database url into constant

This commit is contained in:
Dragyx 2025-05-06 16:29:16 +02:00
parent 9a2a5cb4ff
commit be39c05325

View file

@ -6,6 +6,8 @@ use rusqlite::Connection;
// Use type alias for Result with our custom error type // Use type alias for Result with our custom error type
type Result<T> = std::result::Result<T, AppError>; type Result<T> = std::result::Result<T, AppError>;
const DATABASE_URL: &str = "/nix/var/nix/db/db.sqlite";
const QUERY_PKGS: &str = " const QUERY_PKGS: &str = "
WITH RECURSIVE WITH RECURSIVE
graph(p) AS ( graph(p) AS (
@ -59,11 +61,11 @@ SELECT p, c from graph;
/// in the future, we might wan't to switch to async /// in the future, we might wan't to switch to async
pub fn get_packages(path: &std::path::Path) -> Result<Vec<(i64, String)>> { pub fn get_packages(path: &std::path::Path) -> Result<Vec<(i64, String)>> {
let p: String = path.canonicalize()?.to_string_lossy().into_owned(); 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 mut stmt = conn.prepare(QUERY_PKGS)?;
let queried_pkgs: std::result::Result<Vec<(i64, String)>, _> = let queried_pkgs: std::result::Result<Vec<(i64, String)>, _> = stmt
stmt.query_map([p], |row| Ok((row.get(0)?, row.get(1)?)))? .query_map([p], |row| Ok((row.get(0)?, row.get(1)?)))?
.collect(); .collect();
Ok(queried_pkgs?) Ok(queried_pkgs?)
} }
@ -76,14 +78,13 @@ pub fn get_packages(path: &std::path::Path) -> Result<Vec<(i64, String)>> {
/// in the future, we might wan't to switch to async /// in the future, we might wan't to switch to async
pub fn get_closure_size(path: &std::path::Path) -> Result<i64> { pub fn get_closure_size(path: &std::path::Path) -> Result<i64> {
let p: String = path.canonicalize()?.to_string_lossy().into_owned(); 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 mut stmt = conn.prepare(QUERY_CLOSURE_SIZE)?;
let queried_sum = stmt.query_row([p], |row| row.get(0))?; let queried_sum = stmt.query_row([p], |row| row.get(0))?;
Ok(queried_sum) Ok(queried_sum)
} }
/// returns the complete dependency graph of /// returns the complete dependency graph of
/// of the derivation as an adjacency list. The nodes are /// of the derivation as an adjacency list. The nodes are
/// represented by the DB ids /// represented by the DB ids
@ -92,16 +93,14 @@ pub fn get_closure_size(path: &std::path::Path) -> Result<i64> {
/// well in the future, depending on how much we use them /// well in the future, depending on how much we use them
/// in the operations on the graph /// 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<HashMap<i64, Vec<i64>>> { pub fn get_dependency_graph(path: &std::path::Path) -> Result<HashMap<i64, Vec<i64>>> {
let p: String = path.canonicalize()?.to_string_lossy().into_owned(); 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 stmt = conn.prepare(QUERY_DEPENDENCY_GRAPH)?;
let mut adj = HashMap::<i64, Vec<i64>>::new(); let mut adj = HashMap::<i64, Vec<i64>>::new();
let queried_sum = stmt.query_map( let queried_sum = stmt.query_map([p], |row| Ok::<(i64, i64), _>((row.get(0)?, row.get(1)?)))?;
[p],
|row| Ok::<(i64, i64), _>((row.get(0)?, row.get(1)?)))?;
for row in queried_sum { for row in queried_sum {
let (from, to) = row?; let (from, to) = row?;
adj.entry(from).or_default().push(to); adj.entry(from).or_default().push(to);