diff options
| author | Teddy Wing | 2022-03-09 00:12:40 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2022-03-09 00:12:40 +0100 | 
| commit | f7cea1fbcd0907df2f3efb3f6b35cfc122a8f9cb (patch) | |
| tree | b9917865572c9ef4de9fe0e9261b076201273f18 | |
| parent | 5c3880c78c096c7ee34c790eedd65a40af143fff (diff) | |
| download | yaqlite-f7cea1fbcd0907df2f3efb3f6b35cfc122a8f9cb.tar.bz2 | |
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.
| -rw-r--r-- | src/main.rs | 16 | 
1 files changed, 9 insertions, 7 deletions
| 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<String, Zero>) {      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<String, Zero> {      let mut stmt = dbconn.prepare(r#"          SELECT "name" -        FROM pragma_table_info("test"); +        FROM pragma_table_info("people");      "#).unwrap();      let rows = stmt.query_map( | 
