From f7cea1fbcd0907df2f3efb3f6b35cfc122a8f9cb Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 9 Mar 2022 00:12:40 +0100 Subject: Check if YAML hash key matches column name Possibly ugly passing `table_columns` into `yaml_extract`, but just trying to get things working for now. Query a "people" DB table instead as that matches the test YAML file. --- src/main.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index cfdf0ab..ca815fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,14 +9,14 @@ fn main() { let dbconn = rusqlite::Connection::open("./test.db").unwrap(); let table_columns = get_column_names(&dbconn); - dbg!(table_columns); + dbg!(&table_columns); let text_data = std::fs::read_to_string("test.yml").unwrap(); let yaml_data = yaml::YamlLoader::load_from_str(&text_data).unwrap(); for doc in &yaml_data { - yaml_extract(&doc); + yaml_extract(&doc, &table_columns); } dbg!(yaml_data); @@ -24,22 +24,24 @@ fn main() { dbconn.close().unwrap(); } -fn yaml_extract(doc: &yaml::Yaml) { +fn yaml_extract(doc: &yaml::Yaml, table_columns: &HashMap) { match doc { yaml::Yaml::Array(ref array) => { for yaml_value in array { - yaml_extract(yaml_value); + yaml_extract(yaml_value, table_columns); } } yaml::Yaml::Hash(ref hash) => { // Begin transaction for (k, v) in hash { // TODO: Put k,v in a HashMap prepared for SQLite interfacing - dbg!(k, v); - // Each hash is a new record for SQLite insertion // If key matches a column name, add it to the insert statement + + if table_columns.contains_key(k.as_str().unwrap()) { + dbg!(k, v); + } } } _ => {} @@ -56,7 +58,7 @@ fn get_column_names(dbconn: &rusqlite::Connection) -> HashMap { let mut stmt = dbconn.prepare(r#" SELECT "name" - FROM pragma_table_info("test"); + FROM pragma_table_info("people"); "#).unwrap(); let rows = stmt.query_map( -- cgit v1.2.3