(rust) sqlx: inserted a rust type and sql column for the log type
This commit is contained in:
+52
-23
@@ -1,7 +1,6 @@
|
||||
#![allow(dead_code, unused)]
|
||||
|
||||
mod log_handler;
|
||||
mod log_structures;
|
||||
|
||||
use chrono::{
|
||||
DateTime, Local,
|
||||
@@ -14,7 +13,7 @@ use sqlx::{
|
||||
}, Row, Sqlite
|
||||
};
|
||||
|
||||
use log_handler::Logger;
|
||||
use log_handler::{LogType, Logger};
|
||||
|
||||
const DATABASE_URL: &str = "./test.db";
|
||||
const NAME_LOG_TABLE: &str = "logs";
|
||||
@@ -33,44 +32,59 @@ struct Arguments {
|
||||
pub enum Action {
|
||||
/// perform action on logs
|
||||
#[command(subcommand)]
|
||||
log(SubAction),
|
||||
log(LogAction),
|
||||
|
||||
// /// perform action on tests
|
||||
// test(SubAction),
|
||||
/// perform action on tests
|
||||
#[command(subcommand)]
|
||||
test(TestAction),
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Debug, Clone, Subcommand)]
|
||||
pub enum SubAction {
|
||||
add(AddArgs),
|
||||
get(GetArgs),
|
||||
remove(RemoveArgs),
|
||||
show(ShowArgs),
|
||||
pub enum LogAction {
|
||||
add(LogAddArgs),
|
||||
get(LogGetArgs),
|
||||
remove(LogRemoveArgs),
|
||||
show(LogShowArgs),
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Debug, Clone, Subcommand)]
|
||||
pub enum TestAction {
|
||||
add(TestArgs),
|
||||
remove(TestArgs),
|
||||
show,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Args)]
|
||||
pub struct AddArgs {
|
||||
test: String,
|
||||
message: String,
|
||||
pub struct LogAddArgs {
|
||||
log_type: String,
|
||||
log_test: String,
|
||||
log_message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Args)]
|
||||
pub struct GetArgs {
|
||||
pub struct LogGetArgs {
|
||||
id: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Args)]
|
||||
pub struct RemoveArgs {
|
||||
pub struct LogRemoveArgs {
|
||||
range_start: usize,
|
||||
range_stop: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Args)]
|
||||
pub struct ShowArgs {
|
||||
pub struct LogShowArgs {
|
||||
range_start: Option<usize>,
|
||||
range_stop: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Args)]
|
||||
pub struct TestArgs {
|
||||
test_name: String,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let args = Arguments::try_parse()?;
|
||||
@@ -80,22 +94,37 @@ async fn main() -> anyhow::Result<()> {
|
||||
match args.action {
|
||||
Action::log(subaction) => {
|
||||
match subaction {
|
||||
SubAction::add(subargs) => {
|
||||
logger.add_log(&subargs.test, &subargs.message).await.unwrap();
|
||||
LogAction::add(args) => {
|
||||
logger.add_log(LogType::from_str(&args.log_type.as_str())?, &args.log_test, &args.log_message).await.unwrap();
|
||||
}
|
||||
SubAction::get(subargs) => {
|
||||
let record = logger.query_for_log(&format!("DELETE FROM logs WHERE log_id = {} RETURNING log_id, log_time, test_name, log_message;", subargs.id)).await.unwrap();
|
||||
LogAction::get(args) => {
|
||||
let record = logger.query_for_log(&format!("DELETE FROM logs WHERE log_id = {} RETURNING log_id, log_time, test_name, log_message;", args.id)).await.unwrap();
|
||||
println!("record: {:#?}", record);
|
||||
}
|
||||
SubAction::remove(subargs) => {
|
||||
// remove_log(&db, subargs.range_start, subargs.range_stop).await?;
|
||||
LogAction::remove(args) => {
|
||||
_ = logger.query(&format!("DELETE FROM logs WHERE log_id BETWEEN {} AND {};", args.range_start, args.range_stop)).await;
|
||||
}
|
||||
SubAction::show(subargs) => {
|
||||
LogAction::show(args) => {
|
||||
// let records = read_logger.query_for_log(&format!("SELECT log_id, log_time, log_type, test_name, log_message FROM logs;")).await.unwrap();
|
||||
let records = read_logger.query_for_log(&format!("SELECT * FROM logs;")).await.unwrap();
|
||||
println!("records: {:#?}", records);
|
||||
}
|
||||
}
|
||||
}
|
||||
Action::test(subaction) => {
|
||||
match subaction {
|
||||
TestAction::add(args) => {
|
||||
logger.add_test(args.test_name.as_str(), Local::now()).await?;
|
||||
}
|
||||
TestAction::remove(args) => {
|
||||
logger.remove_test(args.test_name.as_str()).await?;
|
||||
}
|
||||
TestAction::show => {
|
||||
let tests = logger.get_tests(None).await?;
|
||||
println!("tests: {:#?}", tests);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user