aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2022-03-12 19:11:17 +0100
committerTeddy Wing2022-03-12 20:23:30 +0100
commitc487910ee09e0cfd0e5cc2ee1e6232dd7accd073 (patch)
tree28182d1d1c646199d22fd2913b6ce741804ec1c7
parent41b1d59419a18cc63386e8e246ad485a66dd603e (diff)
downloadyaqlite-c487910ee09e0cfd0e5cc2ee1e6232dd7accd073.tar.bz2
yaml_extract(): Don't try to insert data for nonexistent columns
If a hash key in the input YAML does not match one of the column names in the table we're inserting into, ignore that YAML field and use the other columns for insertion.
-rw-r--r--src/main.rs45
-rw-r--r--test2.yml5
2 files changed, 35 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs
index d5849ff..f958f40 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,14 +11,14 @@ fn main() {
let table_columns = get_column_names(&dbconn);
dbg!(&table_columns);
- let text_data = std::fs::read_to_string("test.yml").unwrap();
+ let text_data = std::fs::read_to_string("test2.yml").unwrap();
- let yaml_data = yaml::YamlLoader::load_from_str(&text_data).unwrap();
+ let mut yaml_data = yaml::YamlLoader::load_from_str(&text_data).unwrap();
- for doc in &yaml_data {
+ for mut doc in &mut yaml_data {
let tx = dbconn.transaction().unwrap();
- yaml_extract(&doc, &tx, &table_columns);
+ yaml_extract(&mut doc, &tx, &table_columns);
tx.commit().unwrap();
}
@@ -29,26 +29,41 @@ fn main() {
}
fn yaml_extract(
- doc: &yaml::Yaml,
+ doc: &mut yaml::Yaml,
tx: &rusqlite::Transaction,
table_columns: &HashMap<String, rusqlite::types::Type>,
) {
match doc {
- yaml::Yaml::Array(ref array) => {
+ yaml::Yaml::Array(ref mut array) => {
for yaml_value in array {
yaml_extract(yaml_value, tx, table_columns);
}
}
- yaml::Yaml::Hash(ref hash) => {
+ yaml::Yaml::Hash(ref mut hash) => {
// Begin transaction
- for (k, v) in hash {
- // TODO: Put k,v in a HashMap prepared for SQLite interfacing
- // 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);
+ // for (k, v) in hash {
+ // // TODO: Put k,v in a HashMap prepared for SQLite interfacing
+ // // 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);
+ // }
+ // }
+
+ // let keys: Vec<&str> = hash
+ // .keys()
+ // .map(|k| k.as_str().unwrap())
+ // .collect();
+ 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);
}
}
diff --git a/test2.yml b/test2.yml
new file mode 100644
index 0000000..831e4e2
--- /dev/null
+++ b/test2.yml
@@ -0,0 +1,5 @@
+- name: Rust
+ description:
+ email:
+ person_id: 875
+ nonexistent: should be ignored