Skip to content

Commit

Permalink
Added Get functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ds2600 committed Feb 16, 2024
1 parent 1938b72 commit 94d7722
Showing 1 changed file with 95 additions and 2 deletions.
97 changes: 95 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,25 @@ enum Action {
#[clap(flatten)]
data: CommonData,
},
Backup {
#[clap(flatten)]
data: BackupData,

Check failure on line 36 in src/main.rs

View workflow job for this annotation

GitHub Actions / build_and_release

the trait bound `BackupData: clap::Args` is not satisfied
},
Get {
#[clap(flatten)]
data: QueryData,
},
}


#[derive(Parser)]
#[derive(Debug)]
struct QueryData {
#[clap(short = 'n', long, value_name = "NAME")]
name: String,
}


#[derive(Parser)]
#[derive(Debug)]
struct AddData {
Expand Down Expand Up @@ -68,6 +85,27 @@ struct CommonData {

}

#[derive(Parser)]
#[derive(Debug)]
struct NameData {
#[clap(short = 'n', long, value_name = "NAME")]
name: String,
}

#[derive(Parser)]
#[derive(Debug)]
struct IdData {
#[clap(short = 'd', long, value_name = "DEVICE_ID")]
device_id: String,
}

#[derive(Parser)]
#[derive(Debug)]
enum BackupData {
NameData(NameData),
IdData(IdData),
}

fn main() {
let opts: Opts = Opts::parse();

Expand Down Expand Up @@ -122,10 +160,65 @@ fn main() {
}
}
Action::Modify { data } => {
println!("Modifying data: {:?}", data);
println!("Modifying data.");
}
Action::Delete { data } => {
println!("Deleting data: {:?}", data)
println!("Deleting data");
}
Action::Backup { data } => {
if data.name.is_empty() {

Check failure on line 169 in src/main.rs

View workflow job for this annotation

GitHub Actions / build_and_release

no field `name` on type `BackupData`
println!("Invalid name");
return;
}
}
Action::Get { data } => {
if data.name.is_empty() {
println!("Invalid name");
return;
}

let mut settings = Config::default();
settings
.merge(File::from_str(include_str!("Settings.toml"), FileFormat::Toml))

Check warning on line 182 in src/main.rs

View workflow job for this annotation

GitHub Actions / build_and_release

use of deprecated method `config::Config::merge`: please use 'ConfigBuilder' instead
.unwrap();

let db = OptsBuilder::new()
.ip_or_hostname(Some(settings.get::<String>("db_host").unwrap()))
.db_name(Some(settings.get::<String>("db_name").unwrap()))
.user(Some(settings.get::<String>("db_user").unwrap()))
.pass(Some(settings.get::<String>("db_pass").unwrap()));

let pool = match Pool::new(db) {
Ok(pool) => pool,
Err(err) => {
println!("Error: {}", err);
return;
}
};

let mut conn = match pool.get_conn() {
Ok(conn) => conn,
Err(err) => {
println!("Error: {}", err);
return;
}
};

let sql = "SELECT kSelf, sName FROM Device WHERE sName = :name";

let params = params! {
"name" => &data.name,
};

let result: mysql::QueryResult<mysql::Binary> = conn.exec_iter(sql, params).unwrap();

for row_result in result {
let row = row_result.unwrap();
let k_self: i32 = row.get("kSelf").unwrap();
let s_name: String = row.get("sName").unwrap();

println!("Name: {}, ID: {}", s_name, k_self);
}
}
}
}

0 comments on commit 94d7722

Please sign in to comment.