diff options
| author | Teddy Wing | 2022-03-12 22:12:45 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2022-03-12 22:12:45 +0100 | 
| commit | 8dd59ee6fee0f023a75d4657ad07f6f09b14fc9c (patch) | |
| tree | b2334bfef45beaa3d2b5a0570b52f943354edd3c | |
| parent | 2388395752fbd56b8e1213ff60ccb2a40d23c806 (diff) | |
| download | yaqlite-8dd59ee6fee0f023a75d4657ad07f6f09b14fc9c.tar.bz2 | |
Add an `insert()` function for inserting YAML in database
Make an interface that more cleanly says "insert this YAML into this
database".
| -rw-r--r-- | src/lib.rs | 17 | ||||
| -rw-r--r-- | src/main.rs | 8 | 
2 files changed, 18 insertions, 7 deletions
| @@ -1,2 +1,19 @@  pub mod sqlite;  pub mod yaml; + + +pub fn insert( +    dbconn: &mut rusqlite::Connection, +    table_name: &str, +    data: &mut [yaml_rust::Yaml], +) { +    let table_columns = crate::sqlite::get_column_names(&dbconn, table_name); + +    for mut doc in data { +        let tx = dbconn.transaction().unwrap(); + +        crate::yaml::extract(&mut doc, &tx, &table_columns); + +        tx.commit().unwrap(); +    } +} diff --git a/src/main.rs b/src/main.rs index 424c4ba..8b4e9af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,13 +16,7 @@ fn main() {      let mut yaml_data = yaml::YamlLoader::load_from_str(&text_data).unwrap(); -    for mut doc in &mut yaml_data { -        let tx = dbconn.transaction().unwrap(); - -        yaqlite::yaml::extract(&mut doc, &tx, &table_columns); - -        tx.commit().unwrap(); -    } +    yaqlite::insert(&mut dbconn, "people", &mut yaml_data);      dbg!(yaml_data); | 
