1
Fork 0
mirror of https://github.com/RGBCube/dix synced 2025-07-28 04:07:46 +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
type Result<T> = std::result::Result<T, AppError>;
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<Vec<(i64, String)>> {
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<Vec<(i64, String)>, _> =
stmt.query_map([p], |row| Ok((row.get(0)?, row.get(1)?)))?
.collect();
let queried_pkgs: std::result::Result<Vec<(i64, String)>, _> = 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<Vec<(i64, String)>> {
/// in the future, we might wan't to switch to async
pub fn get_closure_size(path: &std::path::Path) -> Result<i64> {
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<i64> {
/// 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<HashMap<i64, Vec<i64>>> {
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::<i64, Vec<i64>>::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)