feat(breaking): new local db dependency format

Signed-off-by: sisungo <[email protected]>
This commit is contained in:
sisungo committed 2026-10-28 09:31:53 +00:00
1 parent a68d98af30
commit d1c60097b3
2 files changed
+123 -83

No files matched your search

+8 -4
View File
@@ -1,6 +1,6 @@
use crate::{
configure::ConfigureOptions,
local::{InstallFlags, Installation},
local::{DependencyRecord, InstallFlags, Installation},
package::{Package, PackageError, PkgIdent, PkgSpec},
repo::RepoError,
};
@@ -92,7 +92,7 @@ impl super::Packie {
candidates.iter().map(|x| x.pkg_manifest.pkg_ident()),
&self.profile().host_arch,
);
real_dependencies.push(prior);
real_dependencies.push((pkg_spec_b.clone(), prior));
}
// Copy package files
@@ -115,8 +115,12 @@ impl super::Packie {
self.local_db.insert_installation(&installation)?;
// Record "real" dependencies in the database
for pkg_ident_b in real_dependencies {
self.local_db.insert_dependency(&pkg_ident, &pkg_ident_b)?;
for (pkg_spec_b, pkg_ident_b) in real_dependencies {
self.local_db.insert_dependency(&DependencyRecord {
pkg_ident_a: pkg_ident.clone(),
pkg_spec_b,
pkg_ident_b,
})?;
}
// Record concept provision in the database
+115 -79
View File
@@ -127,6 +127,7 @@ CREATE TABLE IF NOT EXISTS "installation"(
CREATE TABLE IF NOT EXISTS "dependency"(
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"pkg_ident_a" TEXT NOT NULL,
"pkg_spec_b" TEXT NOT NULL,
"pkg_ident_b" TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "concept"(
@@ -136,6 +137,8 @@ CREATE TABLE IF NOT EXISTS "concept"(
);
"#;
// ==- Opening the database -==
pub fn open_ro<P: AsRef<Path>>(path: P) -> rusqlite::Result<Self> {
rusqlite::Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY).map(Self)
}
@@ -146,6 +149,8 @@ CREATE TABLE IF NOT EXISTS "concept"(
Ok(Self(conn))
}
// ==- Installation management -==
pub fn insert_installation(&mut self, installation: &Installation) -> rusqlite::Result<()> {
let pkg_manifest = serde_json::to_string(&installation.pkg_manifest)
.map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))?;
@@ -174,30 +179,6 @@ CREATE TABLE IF NOT EXISTS "concept"(
Ok(())
}
pub fn insert_dependency(
&mut self,
pkg_ident_a: &PkgIdent,
pkg_ident_b: &PkgIdent,
) -> rusqlite::Result<()> {
self.0
.prepare_cached(
r#"INSERT INTO "dependency"("pkg_ident_a", "pkg_ident_b") VALUES(?1, ?2)"#,
)?
.execute(params![pkg_ident_a.to_string(), pkg_ident_b.to_string()])?;
Ok(())
}
pub fn insert_concept(
&mut self,
concept_ident: &ConceptIdent,
provider: &PkgIdent,
) -> rusqlite::Result<()> {
self.0
.prepare_cached(r#"INSERT INTO "concept"("concept_ident", "provider") VALUES(?1, ?2)"#)?
.execute(params![concept_ident.to_string(), provider.to_string()])?;
Ok(())
}
pub fn select_installation_by_name(
&mut self,
pkg_name: &str,
@@ -222,57 +203,6 @@ CREATE TABLE IF NOT EXISTS "concept"(
"#)?.query_one(params![pkg_ident.name, pkg_ident.version.to_string(), pkg_ident.arch], map_installation)
}
pub fn select_concept_by_ident(
&mut self,
ident: &ConceptIdent,
) -> rusqlite::Result<Vec<(ConceptIdent, PkgIdent)>> {
let mut stmt = self
.0
.prepare_cached(r#"SELECT * FROM "concept" WHERE "concept_ident" = ?1"#)?;
let rows = stmt.query_map(params![ident.to_string()], map_concept)?;
let mut concept = Vec::new();
for i in rows {
concept.push(i?);
}
Ok(concept)
}
/// Selects all package that the provided package depends on.
pub fn select_dependencies(
&mut self,
pkg_ident_a: &PkgIdent,
) -> rusqlite::Result<Vec<PkgIdent>> {
self._select_dependency_by_pkg_ident("pkg_ident_a", "pkg_ident_b", pkg_ident_a)
}
/// Selects all packages depending on the provided package.
pub fn select_dependents(&mut self, pkg_ident_b: &PkgIdent) -> rusqlite::Result<Vec<PkgIdent>> {
self._select_dependency_by_pkg_ident("pkg_ident_b", "pkg_ident_a", pkg_ident_b)
}
fn _select_dependency_by_pkg_ident(
&mut self,
provided: &str,
returns: &str,
data: &PkgIdent,
) -> rusqlite::Result<Vec<PkgIdent>> {
let mut all = Vec::with_capacity(32);
let mut stmt = self.0.prepare_cached(&format!(
"SELECT \"{returns}\" FROM \"dependency\" WHERE \"{provided}\" == ?1"
))?;
let rows = stmt.query_map(params![data.to_string()], |x| x.get::<_, String>(returns))?;
for row in rows {
all.push(row?.parse().map_err(|e| {
rusqlite::Error::FromSqlConversionFailure(
0,
rusqlite::types::Type::Text,
Box::new(e),
)
})?);
}
Ok(all)
}
pub fn for_each_installation<E: From<rusqlite::Error>>(
&mut self,
mut f: impl FnMut(Installation) -> Result<(), E>,
@@ -292,11 +222,51 @@ CREATE TABLE IF NOT EXISTS "concept"(
.map(|x| x > 0)
}
pub fn remove_concept_provided_by(&mut self, provider: &PkgIdent) -> rusqlite::Result<bool> {
// ==- Dependency management -==
pub fn insert_dependency(&mut self, dependency: &DependencyRecord) -> rusqlite::Result<()> {
self.0
.prepare_cached(r#"DELETE FROM "concept" WHERE "provider" = ?1"#)?
.execute(params![provider.to_string()])
.map(|x| x > 0)
.prepare_cached(
r#"INSERT INTO "dependency"("pkg_ident_a", "pkg_spec_b", "pkg_ident_b") VALUES(?1, ?2, ?3)"#,
)?
.execute(params![
dependency.pkg_ident_a.to_string(),
dependency.pkg_spec_b.to_string(),
dependency.pkg_ident_b.to_string(),
])?;
Ok(())
}
/// Selects all package that the provided package depends on.
pub fn select_dependencies(
&mut self,
pkg_ident_a: &PkgIdent,
) -> rusqlite::Result<Vec<DependencyRecord>> {
self._select_dependency_by_pkg_ident("pkg_ident_a", pkg_ident_a)
}
/// Selects all packages depending on the provided package.
pub fn select_dependents(
&mut self,
pkg_ident_b: &PkgIdent,
) -> rusqlite::Result<Vec<DependencyRecord>> {
self._select_dependency_by_pkg_ident("pkg_ident_b", pkg_ident_b)
}
fn _select_dependency_by_pkg_ident(
&mut self,
provided: &str,
data: &PkgIdent,
) -> rusqlite::Result<Vec<DependencyRecord>> {
let mut all = Vec::with_capacity(32);
let mut stmt = self.0.prepare_cached(&format!(
"SELECT * FROM \"dependency\" WHERE \"{provided}\" == ?1"
))?;
let rows = stmt.query_map(params![data.to_string()], map_dependency)?;
for row in rows {
all.push(row?);
}
Ok(all)
}
pub fn remove_dependency_by_a(&mut self, pkg_ident_a: &PkgIdent) -> rusqlite::Result<bool> {
@@ -328,8 +298,44 @@ CREATE TABLE IF NOT EXISTS "concept"(
};
self.0.prepare_cached(sql)?.execute(params).map(|x| x > 0)
}
// ==- Concept management -==
pub fn insert_concept(
&mut self,
concept_ident: &ConceptIdent,
provider: &PkgIdent,
) -> rusqlite::Result<()> {
self.0
.prepare_cached(r#"INSERT INTO "concept"("concept_ident", "provider") VALUES(?1, ?2)"#)?
.execute(params![concept_ident.to_string(), provider.to_string()])?;
Ok(())
}
pub fn select_concept_by_ident(
&mut self,
ident: &ConceptIdent,
) -> rusqlite::Result<Vec<(ConceptIdent, PkgIdent)>> {
let mut stmt = self
.0
.prepare_cached(r#"SELECT * FROM "concept" WHERE "concept_ident" = ?1"#)?;
let rows = stmt.query_map(params![ident.to_string()], map_concept)?;
let mut concept = Vec::new();
for i in rows {
concept.push(i?);
}
Ok(concept)
}
pub fn remove_concept_provided_by(&mut self, provider: &PkgIdent) -> rusqlite::Result<bool> {
self.0
.prepare_cached(r#"DELETE FROM "concept" WHERE "provider" = ?1"#)?
.execute(params![provider.to_string()])
.map(|x| x > 0)
}
}
/// Record of an installation.
#[derive(Debug, Clone)]
pub struct Installation {
pub pkg_manifest: PkgManifest,
@@ -340,19 +346,24 @@ pub struct Installation {
pub install_flags: InstallFlags,
}
/// Flags of an installation.
#[derive(Debug, Clone, Default)]
pub struct InstallFlags(Vec<String>);
impl InstallFlags {
/// The "auto-install" flag, which is set if a package is auto installed (as a dependency or recommendation).
pub const AUTO_INSTALL: &str = "auto-install";
/// Creates a new [`InstallFlags`] instance.
pub fn new(v: &[&str]) -> Self {
Self(v.iter().map(ToString::to_string).collect())
}
/// Returns true if the provided flag is set.
pub fn contains(&self, s: &str) -> bool {
self.0.iter().any(|x| x == s)
}
/// Sets the provided flag.
pub fn set(&mut self, s: &str) {
if self.contains(s) {
return;
@@ -376,6 +387,14 @@ impl Display for InstallFlags {
}
}
/// A dependency record.
#[derive(Debug, Clone)]
pub struct DependencyRecord {
pub pkg_ident_a: PkgIdent,
pub pkg_spec_b: PkgSpec,
pub pkg_ident_b: PkgIdent,
}
fn map_installation(row: &Row) -> rusqlite::Result<Installation> {
Ok(Installation {
pkg_manifest: serde_json::from_str(&row.get::<_, String>("pkg_manifest")?)
@@ -391,6 +410,23 @@ fn map_installation(row: &Row) -> rusqlite::Result<Installation> {
})
}
fn map_dependency(row: &Row) -> rusqlite::Result<DependencyRecord> {
Ok(DependencyRecord {
pkg_ident_a: row
.get::<_, String>("pkg_ident_a")?
.parse()
.map_err(from_sql_error)?,
pkg_spec_b: row
.get::<_, String>("pkg_spec_b")?
.parse()
.map_err(from_sql_error)?,
pkg_ident_b: row
.get::<_, String>("pkg_ident_b")?
.parse()
.map_err(from_sql_error)?,
})
}
fn map_concept(row: &Row) -> rusqlite::Result<(ConceptIdent, PkgIdent)> {
let provider = row
.get::<_, String>("provider")?