diff options
author | Teddy Wing | 2022-03-12 21:56:23 +0100 |
---|---|---|
committer | Teddy Wing | 2022-03-12 21:56:23 +0100 |
commit | 2388395752fbd56b8e1213ff60ccb2a40d23c806 (patch) | |
tree | 2fee0b8590728376e941e9b5969513919f2d699e /src/main.rs | |
parent | f4d5d7ee9897ed59cf83313774d160c832c091f3 (diff) | |
download | yaqlite-2388395752fbd56b8e1213ff60ccb2a40d23c806.tar.bz2 |
Move `yaml_extract()` to `yaqlite::yaml::extract()`
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 52 |
1 files changed, 1 insertions, 51 deletions
diff --git a/src/main.rs b/src/main.rs index b212c46..424c4ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,6 @@ use rusqlite; use yaml_rust::yaml; -use std::collections::HashMap; - fn main() { println!("Hello, world!"); @@ -21,7 +19,7 @@ fn main() { for mut doc in &mut yaml_data { let tx = dbconn.transaction().unwrap(); - yaml_extract(&mut doc, &tx, &table_columns); + yaqlite::yaml::extract(&mut doc, &tx, &table_columns); tx.commit().unwrap(); } @@ -30,51 +28,3 @@ fn main() { dbconn.close().unwrap(); } - -fn yaml_extract( - doc: &mut yaml::Yaml, - tx: &rusqlite::Transaction, - table_columns: &HashMap<String, yaqlite::sqlite::Zero>, -) { - match doc { - yaml::Yaml::Array(ref mut array) => { - for yaml_value in array { - yaml_extract(yaml_value, tx, table_columns); - } - } - yaml::Yaml::Hash(ref mut hash) => { - let keys: Vec<yaml::Yaml> = hash.keys().map(|k| k.clone()).collect(); - let columns_as_yaml: Vec<yaml::Yaml> = table_columns.keys() - .map(|c| yaml::Yaml::from_str(c)) - .collect(); - - for key in keys.iter() { - if !columns_as_yaml.contains(key) { - hash.remove(key); - } - } - - let mut stmt = tx.prepare( - &format!( - r#" - INSERT INTO "people" - ({}) - VALUES - ({}); - "#, - // Wrap column names in quotes. - hash.keys() - .map(|k| format!(r#""{}""#, k.as_str().unwrap())) - .collect::<Vec<String>>() - .join(", "), - // TODO: get len "?"s - format!("{}?", "?, ".repeat(hash.len() - 1)), - ) - ).unwrap(); - - let values = hash.values().map(|v| yaqlite::yaml::Yaml(v)); - stmt.insert(rusqlite::params_from_iter(values)).unwrap(); - } - _ => {} - } -} |