Skip to content

Commit

Permalink
Merge pull request #2 from jshrake/issue-1
Browse files Browse the repository at this point in the history
Handle inserting column names with spaces
  • Loading branch information
jshrake authored Dec 31, 2022
2 parents 167dbf6 + 67e1d00 commit 67531a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Binary file modified example/export.zip
Binary file not shown.
13 changes: 10 additions & 3 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ pub async fn healthkit_to_sqlite(
Ok(())
}

/// Converts an arbitrary string to a valid SQLite identifier
/// This currently isn't robust enough to handle all possible strings
/// But it's good enough for now. See https://stackoverflow.com/a/6701665
fn get_valid_sqlite_identifier(s: &str) -> String {
format!("`{}`", s)
}

/// Derives and creates the SQLite tables from the exported HealthKit XML
async fn sqlite_create_healthkit_tables<R: BufRead>(
tx: &mut Transaction<'_, Sqlite>,
Expand Down Expand Up @@ -115,7 +122,7 @@ async fn sqlite_create_healthkit_tables<R: BufRead>(
name,
columns
.iter()
.map(|(name, ty)| format!("`{}` {}", name, ty))
.map(|(name, ty)| format!("{} {}", get_valid_sqlite_identifier(name), ty))
.collect::<Vec<_>>()
.join(", ")
);
Expand Down Expand Up @@ -687,8 +694,8 @@ async fn insert_database_row(
r#"INSERT INTO {} ({}) VALUES ({})"#,
table_name,
row.iter()
.map(|(name, _)| name.as_str())
.collect::<Vec<&str>>()
.map(|(name, _)| get_valid_sqlite_identifier(name))
.collect::<Vec<_>>()
.join(", "),
row.iter()
.map(|(_, _)| "?")
Expand Down

0 comments on commit 67531a9

Please sign in to comment.