Turns out we didn't even need the database after all
parent
21f958d0fe
commit
b757d8f5f3
@ -1,25 +0,0 @@
|
|||||||
use crate::internal::exit_code::AppExitCode;
|
|
||||||
use crate::internal::rpc::Package;
|
|
||||||
use crate::{crash, log, Options};
|
|
||||||
|
|
||||||
use super::get_database_connection;
|
|
||||||
|
|
||||||
pub fn add(pkg: Package, options: Options) {
|
|
||||||
// Initialise database connection
|
|
||||||
let conn = get_database_connection();
|
|
||||||
|
|
||||||
// Log the package name
|
|
||||||
if options.verbosity >= 1 {
|
|
||||||
log!("Adding package {} to database", pkg.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Push the package to the database
|
|
||||||
let pkg_description = pkg
|
|
||||||
.description
|
|
||||||
.unwrap_or_else(|| "No description found.".parse().unwrap());
|
|
||||||
conn.execute("INSERT OR REPLACE INTO packages (name, version, description, depends, make_depends) VALUES (?1, ?2, ?3, ?4, ?5)",
|
|
||||||
[&pkg.name, &pkg.version, &pkg_description, &pkg.depends.join(" "), &pkg.make_depends.join(" ")],
|
|
||||||
).unwrap_or_else(|e|
|
|
||||||
crash!(AppExitCode::FailedAddingPkg, "Failed adding package {} to the database: {}", pkg.name, e)
|
|
||||||
);
|
|
||||||
}
|
|
@ -1,44 +0,0 @@
|
|||||||
use rusqlite::Connection;
|
|
||||||
use std::env;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use crate::internal::exit_code::AppExitCode;
|
|
||||||
use crate::{crash, log, Options};
|
|
||||||
|
|
||||||
pub fn init(options: Options) {
|
|
||||||
// Initialise variables
|
|
||||||
let path = format!("{}/.local/share/ame/db.sqlite", env::var("HOME").unwrap());
|
|
||||||
let dbpath = Path::new(&path);
|
|
||||||
let verbosity = options.verbosity;
|
|
||||||
|
|
||||||
// Log database path
|
|
||||||
if verbosity >= 1 {
|
|
||||||
log!("Creating database at {}", &path);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialise database connection
|
|
||||||
let conn =
|
|
||||||
Connection::open(dbpath).expect("Couldn't create database at ~/.local/share/ame/db.sqlite");
|
|
||||||
if verbosity >= 1 {
|
|
||||||
log!("Populating database with table");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create table
|
|
||||||
conn.execute(
|
|
||||||
"CREATE TABLE packages (
|
|
||||||
name TEXT PRIMARY KEY NOT NULL,
|
|
||||||
version TEXT NOT NULL,
|
|
||||||
description TEXT,
|
|
||||||
depends BLOB,
|
|
||||||
make_depends BLOB
|
|
||||||
)",
|
|
||||||
[],
|
|
||||||
)
|
|
||||||
.unwrap_or_else(|e| {
|
|
||||||
crash!(
|
|
||||||
AppExitCode::FailedInitDb,
|
|
||||||
"Couldn't initialise database: {}",
|
|
||||||
e,
|
|
||||||
)
|
|
||||||
});
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
use std::{env, path::PathBuf};
|
|
||||||
|
|
||||||
pub mod add;
|
|
||||||
pub mod initialise;
|
|
||||||
pub mod query;
|
|
||||||
pub mod remove;
|
|
||||||
|
|
||||||
pub use add::*;
|
|
||||||
pub use initialise::*;
|
|
||||||
pub use query::*;
|
|
||||||
pub use remove::*;
|
|
||||||
use rusqlite::Connection;
|
|
||||||
|
|
||||||
fn get_database_connection() -> Connection {
|
|
||||||
let db_path = format!("{}/.local/share/ame/db.sqlite", env::var("HOME").unwrap());
|
|
||||||
Connection::open(PathBuf::from(db_path)).expect("Couldn't connect to database")
|
|
||||||
}
|
|
@ -1,66 +0,0 @@
|
|||||||
use rusqlite::Connection;
|
|
||||||
use std::env;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use crate::internal::rpc::Package;
|
|
||||||
use crate::{log, Options};
|
|
||||||
|
|
||||||
pub fn query(options: Options) -> Vec<Package> {
|
|
||||||
// Initialise variables
|
|
||||||
let verbosity = options.verbosity;
|
|
||||||
|
|
||||||
if verbosity >= 1 {
|
|
||||||
log!("Connecting to database");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialise database connection
|
|
||||||
let conn = Connection::open(Path::new(&format!(
|
|
||||||
"{}/.local/share/ame/db.sqlite",
|
|
||||||
env::var("HOME").unwrap()
|
|
||||||
)))
|
|
||||||
.expect("Couldn't connect to database");
|
|
||||||
|
|
||||||
if verbosity >= 1 {
|
|
||||||
log!("Querying database for input");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all database contents
|
|
||||||
let mut rs = conn.prepare("SELECT * FROM packages;").unwrap();
|
|
||||||
let packages_iter = rs
|
|
||||||
.query_map([], |row| {
|
|
||||||
Ok(Package {
|
|
||||||
name: row.get(0).unwrap(),
|
|
||||||
version: row.get(1).unwrap(),
|
|
||||||
description: row.get(2).unwrap(),
|
|
||||||
depends: row
|
|
||||||
.get::<usize, String>(3)
|
|
||||||
.unwrap()
|
|
||||||
.split(' ')
|
|
||||||
.map(|s| s.to_string())
|
|
||||||
.collect::<Vec<String>>(),
|
|
||||||
make_depends: row
|
|
||||||
.get::<usize, String>(4)
|
|
||||||
.unwrap()
|
|
||||||
.split(' ')
|
|
||||||
.map(|s| s.to_string())
|
|
||||||
.collect::<Vec<String>>(),
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.expect("Couldn't query database for packages");
|
|
||||||
|
|
||||||
if verbosity >= 1 {
|
|
||||||
log!("Retrieved results");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert to vector
|
|
||||||
let mut results: Vec<Package> = vec![];
|
|
||||||
for package in packages_iter {
|
|
||||||
results.push(package.unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
if verbosity >= 1 {
|
|
||||||
log!("Collected results");
|
|
||||||
}
|
|
||||||
|
|
||||||
results
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
use crate::{log, Options};
|
|
||||||
|
|
||||||
use super::get_database_connection;
|
|
||||||
|
|
||||||
pub fn remove(pkg: &str, options: Options) {
|
|
||||||
// Initialise database connection
|
|
||||||
let conn = get_database_connection();
|
|
||||||
|
|
||||||
// Initialise variables
|
|
||||||
let verbosity = options.verbosity;
|
|
||||||
|
|
||||||
if verbosity >= 1 {
|
|
||||||
log!("Removing package {} from database", pkg);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove the package from the database
|
|
||||||
conn.execute(
|
|
||||||
"DELETE FROM packages
|
|
||||||
WHERE EXISTS
|
|
||||||
(SELECT *
|
|
||||||
FROM packages
|
|
||||||
WHERE name = ?);",
|
|
||||||
[pkg],
|
|
||||||
)
|
|
||||||
.expect("Couldn't delete package from database");
|
|
||||||
}
|
|
Loading…
Reference in New Issue