Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
v2.1.0
Browse files Browse the repository at this point in the history
added Makefile now uninstalls etc dir
added command categories
added cmd "merge-db"
  • Loading branch information
SchokiCoder committed Jun 10, 2022
1 parent 0786c94 commit 3951426
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smng"
version = "2.0.0"
version = "2.1.0"
authors = ["SchokiCoder <afschoknecht@gmail.com>"]
repository = "https://github.com/SchokiCoder/smng"
edition = "2018"
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ install: build mkconfig
chmod 755 ${INSTALL_BIN_DIR}/${APP_NAME}

uninstall:
rm -r -f /etc/smng.d
rm -f ${INSTALL_BIN_DIR}/${APP_NAME}
144 changes: 140 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ pub const SHOW_MONTH_NAME: &str = "show-month";
pub const SHOW_MONTH_ABBR: &str = "sm";
pub const SHOW_MONTH_ARGS: &str = "[year month]";

pub const MERGE_DB_INFO: &str = "merges projects and records of two databases";
pub const MERGE_DB_NAME: &str = "merge-db";
pub const MERGE_DB_ABBR: &str = "mdb";
pub const MERGE_DB_ARGS: &str = "source_database_path destination_database_path";

pub fn print_cmd_help(info: &str, name: &str, abbr: Option<&str>, args: Option<&str>) {
println!(" {}:", info);
print!(" {}", name);
Expand Down Expand Up @@ -238,6 +243,7 @@ pub fn help() {
println!("{} [COMMAND] [ARGS]", env!("CARGO_PKG_NAME"));
println!("");

println!("-- Info --");
print_cmd_help(
HELP_INFO,
HELP_NAME,
Expand All @@ -248,6 +254,8 @@ pub fn help() {
ABOUT_NAME,
Some(ABOUT_ABBR),
None);

println!("-- Projects --");
print_cmd_help(
ADD_PROJECT_INFO,
ADD_PROJECT_NAME,
Expand All @@ -268,6 +276,8 @@ pub fn help() {
DELETE_PROJECT_NAME,
None,
Some(DELETE_PROJECT_ARGS));

println!("-- Records --");
print_cmd_help(
RECORD_INFO,
RECORD_NAME,
Expand Down Expand Up @@ -318,6 +328,8 @@ pub fn help() {
TRANSFER_PROJECT_RECORDS_NAME,
None,
Some(TRANSFER_PROJECT_RECORDS_ARGS));

println!("-- Report --");
print_cmd_help(
SHOW_WEEK_INFO,
SHOW_WEEK_NAME,
Expand All @@ -328,6 +340,13 @@ pub fn help() {
SHOW_MONTH_NAME,
Some(SHOW_MONTH_ABBR),
Some(SHOW_MONTH_ARGS));

println!("-- Administration --");
print_cmd_help(
MERGE_DB_INFO,
MERGE_DB_NAME,
Some(MERGE_DB_ABBR),
Some(MERGE_DB_ARGS));
}

pub fn about() {
Expand Down Expand Up @@ -453,9 +472,11 @@ pub fn record(project_id: i64) {
// if last record is not done, stop
let rec_state = RecordState::last();

if rec_state.state == 0 {
println!("ERROR: Last record ({}) is not yet done.", rec_state.id);
return;
if rec_state.id != 0 {
if rec_state.state == 0 {
println!("ERROR: Last record ({}) is not yet done.", rec_state.id);
return;
}
}

// exec
Expand Down Expand Up @@ -489,9 +510,15 @@ pub fn status() {
}

pub fn stop(description: &str) {
// if last record is done, stop
let rec_state = RecordState::last();

// if last record is 0, stop
if rec_state.id == 0 {
println!("ERROR: There are no records yet.");
return;
}

// if last record is done, stop
if rec_state.state == 1 {
println!("ERROR: Last record ({}) is already done.", rec_state.id);
return;
Expand Down Expand Up @@ -873,3 +900,112 @@ pub fn show_month(year: i32, month: u32) {
let month = MonthBeginAndEnd::from_date(Local.ymd(year, month, 1));
show_records(month.begin, month.end);
}

pub fn merge_db(src_db_path: &str, dest_db_path: &str) {
use std::collections::HashMap;

// try connecting to src db
let src_db = sqlite::open(src_db_path);

if src_db.is_ok() == false {
panic!("ERROR: Could not connect to source database at \"{}\".", src_db_path);
}

let src_db = src_db.unwrap();

// try connecting to dest db
let dest_db = sqlite::open(dest_db_path);

if dest_db.is_ok() == false {
panic!("ERROR: Could not connect to destination database at \"{}\".", dest_db_path);
}

let dest_db = dest_db.unwrap();

// create a project_id map, so we can redirect records
// if project_ids differ but name doesn't
let mut src_projects = HashMap::<String, i64>::new();
let mut dest_projects = HashMap::<String, i64>::new();
let mut dest_highest_prjid: i64 = 0;

let mut stmt = src_db
.prepare(
"SELECT project_id, project_name\n\
FROM tbl_projects\n\
ORDER BY project_id;")
.unwrap();

while stmt.next().unwrap() == sqlite::State::Row {
src_projects.insert(
String::from(stmt.read::<String>(1).unwrap()),
stmt.read::<i64>(0).unwrap());
}

let mut stmt = dest_db
.prepare(
"SELECT project_id, project_name\n\
FROM tbl_projects\n\
ORDER BY project_id;")
.unwrap();

while stmt.next().unwrap() == sqlite::State::Row {
let temp = stmt.read::<i64>(0).unwrap();

if temp > dest_highest_prjid {
dest_highest_prjid = temp;
}

dest_projects.insert(
stmt.read::<String>(1).unwrap(),
temp);
}

// in dest, create all projects that dest doesn't have
let mut stmt = dest_db
.prepare(
"INSERT INTO tbl_projects(project_name)\n\
VALUES(?);")
.unwrap();

for prj in src_projects {
// if dest doesn't have this project
if dest_projects.contains_key(&prj.0) == false {
// bring over
stmt.bind(1, prj.0.as_str()).unwrap();
stmt.next().unwrap();

// add to map
dest_highest_prjid += 1;
dest_projects.insert(prj.0, dest_highest_prjid);
}
}

// bring all records over
let mut read_stmt = src_db
.prepare(
"SELECT project_id, begin, end, description\n\
FROM tbl_work_records;")
.unwrap();

let mut write_stmt = dest_db
.prepare(
"INSERT INTO tbl_work_records(project_id, begin, end, description)\n\
VALUES(?, ?, ?, ?);")
.unwrap();

while read_stmt.next().unwrap() == sqlite::State::Row {
let prj_id = read_stmt.read::<i64>(0).unwrap();
let begin = read_stmt.read::<i64>(1).unwrap();
let end = read_stmt.read::<i64>(2).unwrap();
let desc = read_stmt.read::<String>(3).unwrap();

write_stmt.bind(1, prj_id).unwrap();
write_stmt.bind(2, begin).unwrap();
write_stmt.bind(3, end).unwrap();
write_stmt.bind(4, desc.as_str()).unwrap();
write_stmt.next().unwrap();
}

println!("All projects and work-records successfully carried\nfrom: \"{}\"\nto : \"{}\"",
src_db_path, dest_db_path);
}
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,23 @@ fn main() {
commands::show_month_cur();
}
},

commands::MERGE_DB_NAME | commands::MERGE_DB_ABBR => {
if args.len() == 2 {
commands::print_cmd_help(
commands::MERGE_DB_INFO,
commands::MERGE_DB_NAME,
Some(commands::MERGE_DB_ABBR),
Some(commands::MERGE_DB_ARGS));
return;
}

if argcount_check(args.len(), 4, 4) {
return;
}

commands::merge_db(&args[2], &args[3]);
},

_ => println!("Command not recognised."),
}
Expand Down
19 changes: 8 additions & 11 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
{
-assign commands to .
groups in help print
-add administrative command .
"merge-db"
}
{}

{
-config pos priority v_11-19_09-06-2022
-if path to db doesn't exist, .
non-explicit panic
-first record cmd tells you to .
-first record cmd tells you to v_09-16_10-06-2022
complete record 0
-Makefile uninstall: remove .
-Makefile uninstall: remove v
etc dir
-v . -> 2.0.1
-assign commands to v_09-25
groups in help print
-add administrative command v_11-50
"merge-db"
-v . -> 2.1.0
}

{
Expand Down

0 comments on commit 3951426

Please sign in to comment.