(feat): improve database handling.

This commit is contained in:
Eduard Tolosa 2025-05-04 01:59:44 -05:00
parent 9c7f10c186
commit f23e74c610
2 changed files with 23 additions and 21 deletions

View file

@ -2,7 +2,7 @@ use {
crate::{args::Args, database, operations, structs::ExtraArgs},
anyhow::Result,
md5,
prettytable::{row, Table},
prettytable::{Table, row},
sqlite::Connection,
};
@ -80,7 +80,7 @@ pub fn manage_listing(database_connection: &Connection) -> Result<()> {
"SOURCE DIR",
"DESTINATION DIR",
"MACHINE",
"RO/RW",
"RW",
"DATE"
]);

View file

@ -17,7 +17,13 @@ pub fn setup_initial_database(connection: &Connection) -> Result<()> {
pub fn commit_to_database(args: &Args, extra_args: &ExtraArgs) -> Result<()> {
let statement = format!(
"INSERT INTO snapshots (name, snap_id, kind, source, destination, machine, ro_rw) VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}')",
&extra_args.snapshot_name, args.snapshot_id, args.snapshot_kind, args.source_dir, args.dest_dir, args.machine, args.read_write
&extra_args.snapshot_name,
args.snapshot_id,
args.snapshot_kind,
args.source_dir,
args.dest_dir,
args.machine,
args.read_write
);
extra_args.database_connection.execute(statement)?;
@ -42,7 +48,7 @@ pub fn return_snapshot_data(connection: &Connection, args: &Args) -> Result<Data
let mut snap_data = Database::default();
while statement.next()? == State::Row {
snap_data = populate_db_struct(&statement, snap_data)?;
snap_data = populate_db_struct(&statement)?;
}
Ok(snap_data)
@ -55,8 +61,7 @@ pub fn return_all_data(connection: &Connection) -> Result<Vec<Database>> {
.prepare("SELECT name,snap_id,kind,source,destination,machine,ro_rw,datetime(date, 'localtime') FROM snapshots ORDER BY date DESC")?;
while statement.next()? == State::Row {
let db_struct = Database::default();
snapshots_data.push(populate_db_struct(&statement, db_struct)?);
snapshots_data.push(populate_db_struct(&statement)?);
}
Ok(snapshots_data)
@ -68,24 +73,21 @@ pub fn return_only_x_items(connection: &Connection, args: &Args) -> Result<Vec<D
let mut statement = connection.prepare(format!("SELECT name,snap_id,kind,source,destination,machine,ro_rw,date FROM (SELECT row_number() over(ORDER BY date DESC) n,* from snapshots WHERE name like '{}%' AND kind = '{}' AND ro_rw = '{}') WHERE n > {}", args.snapshot_prefix, args.snapshot_kind, args.read_write, args.keep_only))?;
while statement.next()? == State::Row {
let db_struct = Database::default();
snapshots_data.push(populate_db_struct(&statement, db_struct)?);
snapshots_data.push(populate_db_struct(&statement)?);
}
Ok(snapshots_data)
}
fn populate_db_struct(row: &Statement, mut db_struct: Database) -> Result<Database> {
// I'll improve this later.
db_struct.name = row.read(0)?;
db_struct.snap_id = row.read(1)?;
db_struct.kind = row.read(2)?;
db_struct.source = row.read(3)?;
db_struct.destination = row.read(4)?;
db_struct.machine = row.read(5)?;
db_struct.ro_rw = row.read(6)?;
db_struct.date = row.read(7)?;
Ok(db_struct)
fn populate_db_struct(stmt: &Statement) -> Result<Database> {
Ok(Database {
name: stmt.read(0)?,
snap_id: stmt.read(1)?,
kind: stmt.read(2)?,
source: stmt.read(3)?,
destination: stmt.read(4)?,
machine: stmt.read(5)?,
ro_rw: stmt.read(6)?,
date: stmt.read(7)?,
})
}