diff --git a/Cargo.toml b/Cargo.toml index 54f00f1..7fd3472 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,4 +29,4 @@ itertools = "0.15" ureq = { version = "3", features = ["socks-proxy"] } url = { version = "2", features = ["serde"] } nix = { version = "0.31", features = ["fs"] } -zstd = { version = "0.13", optional = true } +zstd = { version = "0.14", optional = true } diff --git a/cli/src/common.rs b/cli/src/common.rs index efdcc99..042159e 100644 --- a/cli/src/common.rs +++ b/cli/src/common.rs @@ -96,8 +96,12 @@ pub fn print_summary(summary: Vec) { println!("{}", table); } -pub fn format_size(bytes: u64) -> impl Display { - HumanBytes(bytes) +pub fn format_size(bytes: i64) -> String { + if bytes.is_positive() { + HumanBytes(bytes as u64).to_string() + } else { + format!("-{}", HumanBytes(bytes.abs() as u64)) + } } pub fn format_datetime(timestamp: i64) -> String { @@ -113,12 +117,20 @@ pub fn search_unique_package( ) -> Result { let mut found = packie.search_installation(&pkgspec)?; if found.is_empty() { - return Err(crate::error!("common.package_not_found")); + return Err(crate::error!("common.package_not_found", pkgspec = pkgspec)); } if found.len() != 1 { + let mut note = String::with_capacity(128); + note.push_str(&*rust_i18n::t!( + "common.note_pkgspec_not_unique", + pkgspec = pkgspec + )); + for i in &found { + note.push_str(&format!("\n - {}", i.pkg_manifest.pkg_ident())); + } return Err(crate::Error { - message: rust_i18n::t!("common.error_pkgspec_not_unique").into(), - note: Some(rust_i18n::t!("common.note_pkgspec_not_unique").into()), + message: rust_i18n::t!("common.error_pkgspec_not_unique", pkgspec = pkgspec).into(), + note: Some(note), }); } Ok(found.remove(0)) @@ -138,7 +150,8 @@ pub fn download_packages( let repo_pkg = pkginfo(package); let init_pos = progress_bar.position(); progress_bar.println(format!( - "[{count}/{nr_packages}] {}", + "[{}/{nr_packages}] {}", + count + 1, t!( "common.download_packages.downloading_hint", pkg = style(repo_pkg.pkg_manifest.pkg_ident()).green(), diff --git a/cli/src/info.rs b/cli/src/info.rs index 383bfcb..499ea1f 100644 --- a/cli/src/info.rs +++ b/cli/src/info.rs @@ -1,102 +1,178 @@ use crate::{common::format_size, error}; use clap::Parser; +use console::style; use packie::{ PackieBuilder, - local::Installation, + local::{InstallFlags, Installation}, package::{PkgManifest, PkgSpec}, + repo::RepoPackage, }; use rust_i18n::t; -use std::fmt::Write; +use std::collections::HashMap; #[derive(Debug, Parser)] pub struct Cli { + /// Search only local-installed packages, even if remote packages are available + #[arg(short, long)] + local_only: bool, + pkgspec: PkgSpec, } pub fn main(cli: Cli) -> Result<(), crate::Error> { let mut packie = PackieBuilder::new().readonly(true).build()?; - let mut found = packie.search_installation(&cli.pkgspec)?; - if found.len() == 0 { + + let mut everything = HashMap::new(); + + let remote = if cli.local_only { + Vec::new() + } else { + packie.query_repo().search_package(&cli.pkgspec) + }; + for i in remote { + everything.insert( + i.pkg_manifest.pkg_ident(), + ComprehensivePackageInfo::from(i), + ); + } + + let local = packie.search_installation(&cli.pkgspec)?; + for i in local { + let val = everything + .entry(i.pkg_manifest.pkg_ident()) + .or_insert_with(|| ComprehensivePackageInfo::from(i.clone())); + val.installed = true; + val.installed_size = i.installed_size; + val.install_pkgspec = Some(i.install_pkgspec); + val.auto_installed = Some(i.install_flags.contains(InstallFlags::AUTO_INSTALL)); + val.install_date = Some(i.install_date); + } + + if everything.is_empty() { return Err(error!("info.no_package_found")); } - if found.len() != 1 { - let mut note = String::new(); - writeln!(&mut note, "{}", t!("info.note_pkgspec_not_unique")).unwrap(); - for i in found.iter() { - writeln!(&mut note, " - {}", i.pkg_manifest.pkg_ident()).unwrap(); - } - let note = note.trim().into(); - return Err(crate::Error { - message: t!("info.error_pkgspec_not_unique").into(), - note: Some(note), - }); + for i in everything.values() { + print_info(&i); + println!(); } - let found = found.remove(0); - - print_pkgmanifest(&found.pkg_manifest); - print_install_misc(&found); Ok(()) } -fn print_pkgmanifest(pkg_manifest: &PkgManifest) { - println!( - "{}: {}", - t!("info.print_pkgmanifest.name"), - pkg_manifest.name - ); - println!( - "{}: {}", - t!("info.print_pkgmanifest.version"), - pkg_manifest.version - ); - println!( - "{}: {}", - t!("info.print_pkgmanifest.arch"), - pkg_manifest.arch - ); - if !pkg_manifest.description.is_empty() { - println!( - "{}: {}", - t!("info.print_pkgmanifest.description"), - pkg_manifest.description - ); - } - if !pkg_manifest.maintainers.is_empty() { - println!("{}:", t!("info.print_pkgmanifest.maintainers")); - for i in pkg_manifest.maintainers.iter() { - println!(" - {i}"); +#[derive(Debug, Clone)] +struct ComprehensivePackageInfo { + pkg_manifest: PkgManifest, + installed: bool, + install_pkgspec: Option, + install_date: Option, + download_size: Option, + installed_size: u64, + auto_installed: Option, +} +impl From for ComprehensivePackageInfo { + fn from(value: RepoPackage) -> Self { + Self { + pkg_manifest: value.pkg_manifest, + installed: false, + install_pkgspec: None, + install_date: None, + download_size: Some(value.download_size), + installed_size: value.installed_size, + auto_installed: None, } } - if !pkg_manifest.dependencies.is_empty() { - println!("{}:", t!("info.print_pkgmanifest.dependencies")); - for i in pkg_manifest.dependencies.iter() { - println!(" - {i}"); - } - } - if !pkg_manifest.recommendations.is_empty() { - println!("{}:", t!("info.print_pkgmanifest.recommendations")); - for i in pkg_manifest.recommendations.iter() { - println!(" - {i}"); +} +impl From for ComprehensivePackageInfo { + fn from(value: Installation) -> Self { + Self { + pkg_manifest: value.pkg_manifest, + installed: true, + install_pkgspec: Some(value.install_pkgspec), + install_date: Some(value.install_date), + download_size: None, + installed_size: value.installed_size, + auto_installed: Some(value.install_flags.contains(InstallFlags::AUTO_INSTALL)), } } } -fn print_install_misc(installation: &Installation) { +fn print_info(info: &ComprehensivePackageInfo) { + if info.installed { + println!( + "+ {} [{}]", + style(info.pkg_manifest.pkg_ident()).green(), + t!("info.print_info.installed"), + ); + } else { + println!("+ {}", style(info.pkg_manifest.pkg_ident()).green()); + } println!( - "{}: {}", - t!("info.print_install_misc.install_pkgspec"), - installation.install_pkgspec + " {}: {}", + t!("info.print_info.name"), + info.pkg_manifest.name ); println!( - "{}: {}", - t!("info.print_install_misc.install_date"), - crate::common::format_datetime(installation.install_date), + " {}: {}", + t!("info.print_info.version"), + info.pkg_manifest.version ); println!( - "{}: {}", - t!("info.print_install_misc.installed_size"), - format_size(installation.installed_size), + " {}: {}", + t!("info.print_info.arch"), + info.pkg_manifest.arch ); + if !info.pkg_manifest.description.is_empty() { + println!( + " {}: {}", + t!("info.print_info.description"), + info.pkg_manifest.description + ); + } + if !info.pkg_manifest.maintainers.is_empty() { + println!(" {}:", t!("info.print_info.maintainers")); + for i in info.pkg_manifest.maintainers.iter() { + println!(" - {i}"); + } + } + if !info.pkg_manifest.dependencies.is_empty() { + println!(" {}:", t!("info.print_info.dependencies")); + for i in info.pkg_manifest.dependencies.iter() { + println!(" - {i}"); + } + } + if !info.pkg_manifest.recommendations.is_empty() { + println!(" {}:", t!("info.print_info.recommendations")); + for i in info.pkg_manifest.recommendations.iter() { + println!(" - {i}"); + } + } + if let Some(pkgspec) = &info.install_pkgspec { + println!(" {}: {pkgspec}", t!("info.print_info.install_pkgspec")); + } + if let Some(install_date) = &info.install_date { + println!( + " {}: {}", + t!("info.print_info.install_date"), + crate::common::format_datetime(*install_date), + ); + } + if let Some(download_size) = &info.download_size { + println!( + " {}: {}", + t!("info.print_info.download_size"), + format_size(*download_size as _), + ); + } + println!( + " {}: {}", + t!("info.print_info.installed_size"), + format_size(info.installed_size as _), + ); + if let Some(auto_installed) = info.auto_installed { + println!( + " {}: {auto_installed}", + t!("info.print_info.auto_installed") + ); + } } diff --git a/cli/src/install.rs b/cli/src/install.rs index ae3b214..e9892d5 100644 --- a/cli/src/install.rs +++ b/cli/src/install.rs @@ -63,11 +63,17 @@ pub fn main(cli: Cli) -> Result<(), crate::Error> { println!(); println!( "{}", - t!("install.download_size", size = format_size(download_size)) + t!( + "install.download_size", + size = format_size(download_size as _), + ) ); println!( "{}", - t!("install.installed_size", size = format_size(installed_size)) + t!( + "install.installed_size", + size = format_size(installed_size as _), + ) ); if cli.yes { eprintln!("y"); diff --git a/cli/src/upgrade.rs b/cli/src/upgrade.rs index 8c343a1..ebdcc93 100644 --- a/cli/src/upgrade.rs +++ b/cli/src/upgrade.rs @@ -2,7 +2,10 @@ use crate::common::{SummaryItem, format_size, print_summary, yesno}; use clap::Parser; use console::style; use indicatif::ProgressBar; -use packie::{PackieBuilder, install::InstallOptions, package::PkgSpec}; +use packie::{ + PackieBuilder, graph::UpgradationOp, install::InstallOptions, local::InstallFlags, + package::PkgSpec, +}; use rust_i18n::t; use std::path::PathBuf; @@ -17,53 +20,65 @@ pub struct Cli { pub fn main(cli: Cli) -> Result<(), crate::Error> { let mut packie = PackieBuilder::new().build()?; - let upgradation_summary = packie.calculate_upgradation(cli.items.unwrap_or_default())?; + let ops = packie.calculate_upgradation(cli.items.unwrap_or_default())?; - if upgradation_summary.is_empty() { + if ops.is_empty() { eprintln!("{}", t!("upgrade.nothing_to_do")); return Ok(()); } println!("{}", t!("upgrade.prompt_to_upgrade")); println!(); - let mut ui_summary = Vec::new(); - for install in &upgradation_summary.installs { - ui_summary.push(SummaryItem { - pkgspec: install.install_pkgspec.clone(), - pkgident: install.repo_pkg.pkg_manifest.pkg_ident(), - operation: SummaryItem::INSTALL, - }); - } - for remove in &upgradation_summary.removes { - let pkgspec = PkgSpec { - name: remove.name.clone(), - version: packie::version::VersionFilter::Any, - arch: None, - }; - ui_summary.push(SummaryItem { - pkgspec, - pkgident: remove.clone(), - operation: SummaryItem::REMOVE, - }); + let mut ui_summary = Vec::with_capacity(ops.len()); + for op in &ops { + match op { + UpgradationOp::Install(dep) => { + ui_summary.push(SummaryItem { + pkgspec: dep.install_pkgspec.clone(), + pkgident: dep.repo_pkg.pkg_manifest.pkg_ident(), + operation: SummaryItem::INSTALL, + }); + } + UpgradationOp::Replace(rep) => { + ui_summary.push(SummaryItem { + pkgspec: rep.original.install_pkgspec.clone(), + pkgident: rep.new.pkg_manifest.pkg_ident(), + operation: SummaryItem::INSTALL, + }); + ui_summary.push(SummaryItem { + pkgspec: rep.original.install_pkgspec.clone(), + pkgident: rep.original.pkg_manifest.pkg_ident(), + operation: SummaryItem::REMOVE, + }); + } + } } print_summary(ui_summary); println!(); - let mut installs: Vec<_> = upgradation_summary - .installs - .into_iter() - .map(|x| (x, PathBuf::new())) - .collect(); - let removes = upgradation_summary.removes; - - let download_size = installs.iter().map(|(x, _)| x.repo_pkg.download_size).sum(); - let installed_size = installs + let download_size: u64 = ops .iter() - .map(|(x, _)| x.repo_pkg.installed_size) + .map(|x| match x { + UpgradationOp::Install(dep) => dep.repo_pkg.download_size, + UpgradationOp::Replace(rep) => rep.new.download_size, + }) .sum(); + let installed_size = ops + .iter() + .map(|x| match x { + UpgradationOp::Install(dep) => dep.repo_pkg.installed_size as i64, + UpgradationOp::Replace(rep) => { + (rep.new.installed_size as i64) - (rep.original.installed_size as i64) + } + }) + .sum(); + println!( "{}", - t!("upgrade.download_size", size = format_size(download_size)) + t!( + "upgrade.download_size", + size = format_size(download_size as i64) + ) ); println!( "{}", @@ -77,34 +92,51 @@ pub fn main(cli: Cli) -> Result<(), crate::Error> { return Ok(()); } - crate::common::download_packages(&mut packie, &mut installs, |x| &x.repo_pkg)?; - - let nr_actions = installs.len() + removes.len(); - let progress_bar = ProgressBar::new(nr_actions as _); - for removal in removes { - progress_bar.println(format!( - "[{}/{nr_actions}] {}", - progress_bar.position() + 1, - t!("upgrade.removing_hint", pkg = style(&removal).red()), - )); - packie.remove(&removal.into())?; - progress_bar.inc(1); - } - for (package, file_path) in installs { - progress_bar.println(format!( - "[{}/{nr_actions}] {}", - progress_bar.position() + 1, - t!( - "upgrade.installing_hint", - pkg = style(&package.install_pkgspec).green(), - ) - )); - - let mut options = InstallOptions::default(); - options.auto_installed = package.auto_installed; - options.install_pkgspec = Some(package.install_pkgspec); - packie.install_package_file(file_path, &options)?; + let mut ops = ops.into_iter().map(|x| (x, PathBuf::new())).collect(); + crate::common::download_packages(&mut packie, &mut ops, |x| match x { + UpgradationOp::Install(x) => &x.repo_pkg, + UpgradationOp::Replace(x) => &x.new, + })?; + let nr_ops = ops.len(); + let progress_bar = ProgressBar::new(ops.len() as _); + for (op, file_path) in ops { + match op { + UpgradationOp::Install(dep) => { + progress_bar.println(format!( + "[{}/{}] {}", + progress_bar.position() + 1, + nr_ops, + t!( + "upgrade.installing_hint", + pkg = style(&dep.install_pkgspec).green(), + ) + )); + let mut options = InstallOptions::default(); + options.auto_installed = dep.auto_installed; + options.install_pkgspec = Some(dep.install_pkgspec); + packie.install_package_file(file_path, &options)?; + } + UpgradationOp::Replace(rep) => { + progress_bar.println(format!( + "[{}/{}] {}", + progress_bar.position() + 1, + nr_ops, + t!( + "upgrade.installing_hint", + pkg = style(&rep.new.pkg_manifest.pkg_ident()).green(), + ) + )); + let mut options = InstallOptions::default(); + options.auto_installed = rep + .original + .install_flags + .contains(InstallFlags::AUTO_INSTALL); + options.install_pkgspec = Some(rep.original.install_pkgspec); + options.is_upgradation_of = Some(rep.original.pkg_manifest.pkg_ident()); + packie.install_package_file(file_path, &options)?; + } + } progress_bar.inc(1); } diff --git a/locales/packie-cli/main.yml b/locales/packie-cli/main.yml index 0d65875..7efe68d 100644 --- a/locales/packie-cli/main.yml +++ b/locales/packie-cli/main.yml @@ -63,10 +63,6 @@ upgrade.nothing_to_do: en: "Nothing to do today." zh-CN: "今日无事可做。" ja: "本日は行うことがありません。" -upgrade.removing_hint: - en: "Removing \"%{pkg}\"..." - zh-CN: "正在移除 \"%{pkg}\"..." - ja: "「%{pkg}」を削除しています..." upgrade.installing_hint: en: "Installing \"%{pkg}\"..." zh-CN: "正在安装 \"%{pkg}\"..." @@ -83,46 +79,58 @@ info.note_pkgspec_not_unique: en: "The following packages were matched:" zh-CN: "匹配到了下面的包:" ja: "以下のパッケージが一致しました:" -info.print_pkgmanifest.name: +info.print_info.name: en: "Package name" zh-CN: "包名" ja: "パッケージ名" -info.print_pkgmanifest.version: +info.print_info.version: en: "Package version" zh-CN: "版本" ja: "バージョン" -info.print_pkgmanifest.arch: +info.print_info.arch: en: "Package architecture" zh-CN: "架构" ja: "アーキテクチャ" -info.print_pkgmanifest.dependencies: +info.print_info.dependencies: en: "Dependencies" zh-CN: "依赖" ja: "依存関係" -info.print_pkgmanifest.recommendations: +info.print_info.recommendations: en: "Recommendations" zh-CN: "建议安装" ja: "推奨パッケージ" -info.print_pkgmanifest.description: +info.print_info.description: en: "Description" zh-CN: "描述" ja: "説明" -info.print_pkgmanifest.maintainers: +info.print_info.maintainers: en: "Maintainers" zh-CN: "维护者" ja: "メンテナー" -info.print_install_misc.install_pkgspec: +info.print_info.install_pkgspec: en: "Install package spec" zh-CN: "安装请求" ja: "インストール要求" -info.print_install_misc.installed_size: +info.print_info.download_size: + en: "Download size" + zh-CN: "下载大小" + ja: "ダウンロードサイズ" +info.print_info.installed_size: en: "Installed size" - zh-CN: "安装后大小" + zh-CN: "安装大小" ja: "インストール後サイズ" -info.print_install_misc.install_date: +info.print_info.install_date: en: "Install date" zh-CN: "安装日期" ja: "インストール日時" +info.print_info.auto_installed: + en: "Auto installed" + zh-CN: "自动安装" + ja: "自動インストール済み" +info.print_info.installed: + en: "INSTALLED" + zh-CN: "已安装" + ja: "インストール済み" remove.package_not_found: en: "The specified package \"%{pkgspec}\" was not found." zh-CN: "找不到指定的包 \"%{pkgspec}\"。" diff --git a/src/graph.rs b/src/graph.rs index 7b84594..3002a32 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1,4 +1,5 @@ use crate::{ + local::Installation, package::{PkgIdent, PkgSpec}, repo::RepoPackage, }; @@ -109,59 +110,79 @@ impl super::Packie { pub fn calculate_upgradation( &mut self, - pkgs: Vec, - ) -> Result { - let mut installations = Vec::new(); - if pkgs.is_empty() { + pkgspecs: Vec, + ) -> Result, GraphError> { + // Collect all package idents required to upgrade + let mut installations = Vec::with_capacity(pkgspecs.len()); + if installations.is_empty() { self.for_each_installation(|x| Ok::<_, rusqlite::Error>(installations.push(x))) .unwrap(); } else { - for pkg in pkgs { - let mut searched = self.search_installation(&pkg).unwrap_or_default(); + for pkgspec in pkgspecs { + let mut searched = self.search_installation(&pkgspec).unwrap_or_default(); installations.append(&mut searched); } } - let mut removes = Vec::new(); - let mut installs = Vec::new(); - - for pkg in installations { - let mut summary = self._calculate_upgradation(&pkg.pkg_manifest.pkg_ident())?; - removes.append(&mut summary.removes); - installs.append(&mut summary.installs); + // Calculate upgradation nodes for each package ident required to upgrade + let mut nodes = Vec::with_capacity(installations.len() / 3); + for i in installations { + let Some(node) = self._calculate_upgradation(&i.pkg_manifest.pkg_ident())? else { + continue; + }; + nodes.push(node); } - let removes = removes.into_iter().unique().collect(); - let installs = installs - .into_iter() - .unique_by(|x| x.repo_pkg.pkg_manifest.pkg_ident()) - .collect(); + // Collect upgradation summary + let mut ops = Vec::with_capacity(nodes.len()); + for node in &nodes { + let mut ti = self.calculate_installation( + &[node.new.pkg_manifest.pkg_ident().into()], + &CalculateInstallationOptions { + skip_recommendations: false, + no_existence_check: false, + }, + )?; + ti.pop(); + ti.retain(|x| { + !nodes + .iter() + .any(|y| y.new.pkg_manifest.pkg_ident() == x.repo_pkg.pkg_manifest.pkg_ident()) + }); + for i in ti { + ops.push(UpgradationOp::Install(i)); + } + ops.push(UpgradationOp::Replace(node.clone())); + } - Ok(UpgradationSummary { removes, installs }) + Ok(ops) } - fn _calculate_upgradation(&mut self, pkg: &PkgIdent) -> Result { - let installed = self + fn _calculate_upgradation( + &mut self, + pkg: &PkgIdent, + ) -> Result, GraphError> { + let original = self .local_db .select_installation_by_ident(pkg) .map_err(|_| GraphError::NotFound(pkg.to_string()))?; let depgraph = self._calculate_installation( - &installed.install_pkgspec, + &original.install_pkgspec, &CalculateInstallationOptions { skip_recommendations: false, no_existence_check: true, }, ); - let Ok(depgraph) = depgraph else { - return Ok(UpgradationSummary::empty()); + let Ok(mut depgraph) = depgraph else { + return Ok(None); }; if depgraph.last().map(|x| x.repo_pkg.pkg_manifest.pkg_ident()) == Some(pkg.clone()) { - return Ok(UpgradationSummary::empty()); + return Ok(None); } - Ok(UpgradationSummary { - removes: vec![pkg.clone()], - installs: depgraph, - }) + Ok(Some(UpgradationNode { + original, + new: depgraph.pop().unwrap().repo_pkg, + })) } } @@ -193,21 +214,15 @@ pub struct InstallationNode { } #[derive(Debug, Clone)] -pub struct UpgradationSummary { - pub removes: Vec, - pub installs: Vec, +pub struct UpgradationNode { + pub original: Installation, + pub new: RepoPackage, } -impl UpgradationSummary { - pub fn empty() -> Self { - Self { - removes: vec![], - installs: vec![], - } - } - pub fn is_empty(&self) -> bool { - self.removes.is_empty() && self.installs.is_empty() - } +#[derive(Debug, Clone)] +pub enum UpgradationOp { + Install(InstallationNode), + Replace(UpgradationNode), } /// Gets prior package that is installed by default in package candidates. diff --git a/src/install.rs b/src/install.rs index 072af25..8e2524b 100644 --- a/src/install.rs +++ b/src/install.rs @@ -17,6 +17,9 @@ pub struct InstallOptions { /// Whether this package is auto-installed. pub auto_installed: bool, + + /// Mark this installation as an upgradation of an installed package. + pub is_upgradation_of: Option, } impl InstallOptions { pub fn new() -> Self { @@ -24,6 +27,7 @@ impl InstallOptions { install_pkgspec: None, interactive_configure: true, auto_installed: false, + is_upgradation_of: None, } } } @@ -138,6 +142,14 @@ impl super::Packie { std::fs::copy(i.path(), dst).map_err(|err| InstallError::Database(Box::new(err)))?; } + // Process upgradation + if let Some(original) = &options.is_upgradation_of { + self.local_db + .update_dependency(None, original, &pkg_ident)?; + self.remove(&PkgSpec::from(original.clone())) + .map_err(InstallError::Remove)?; + } + // Enable default links if let Some(links) = package.links() { for linkdes in links.iter().ok().into_iter().flatten() { @@ -190,6 +202,9 @@ pub enum InstallError { #[error("{0}")] Repo(RepoError), + #[error("{0}")] + Remove(crate::remove::RemoveError), + #[error("{}", t!("install.InstallError.Database", error = .0))] Database(Box), } diff --git a/src/local.rs b/src/local.rs index 73575cd..8a1f0f6 100644 --- a/src/local.rs +++ b/src/local.rs @@ -305,6 +305,29 @@ CREATE TABLE IF NOT EXISTS "concept"( .execute(params![pkg_ident_a.to_string()]) .map(|x| x > 0) } + + pub fn update_dependency( + &mut self, + pkg_ident_a: Option<&PkgIdent>, + pkg_ident_b_orig: &PkgIdent, + pkg_ident_b_new: &PkgIdent, + ) -> rusqlite::Result { + let (sql, params) = match pkg_ident_a { + Some(pkg_ident_a) => ( + r#"UPDATE "dependency" SET "pkg_ident_b" = ?1 WHERE "pkg_ident_a" = ?2 AND "pkg_ident_b" = ?3"#, + params![ + pkg_ident_b_new.to_string(), + pkg_ident_a.to_string(), + pkg_ident_b_orig.to_string() + ], + ), + None => ( + r#"UPDATE "dependency" SET "pkg_ident_b" = ?1 WHERE "pkg_ident_b" = ?2"#, + params![pkg_ident_b_new.to_string(), pkg_ident_b_orig.to_string()], + ), + }; + self.0.prepare_cached(sql)?.execute(params).map(|x| x > 0) + } } #[derive(Debug, Clone)]