Skip to content

Commit

Permalink
use narrowed enum
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanierh committed Nov 16, 2023
1 parent d93d04c commit 2ee45d9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
4 changes: 2 additions & 2 deletions core/src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

use super::table_schema::TableSchema;
use super::table_schema::{SqlParam, TableSchema};

#[derive(Debug, Clone, Copy, Serialize, PartialEq, Deserialize)]
#[serde(rename_all = "lowercase")]
Expand Down Expand Up @@ -174,7 +174,7 @@ impl Database {
let (sql, field_names) = table_schema.get_insert_sql(table_name);
let mut stmt = conn.prepare(&sql)?;

let params: Vec<Vec<Value>> = rows
let params: Vec<Vec<SqlParam>> = rows
.par_iter()
.map(|r| table_schema.get_insert_params(&field_names, r))
.collect::<Result<Vec<_>>>()?;
Expand Down
46 changes: 39 additions & 7 deletions core/src/databases/table_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;

use anyhow::{anyhow, Result};

use rusqlite::{types::ToSqlOutput, ToSql};
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand All @@ -16,6 +17,30 @@ pub enum TableSchemaFieldType {
Bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum SqlParam {
Int(i64),
Float(f64),
Text(String),
Bool(bool),
Null,
}

impl ToSql for SqlParam {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
SqlParam::Int(i) => i.to_sql(),
SqlParam::Float(f) => f.to_sql(),
SqlParam::Text(s) => Ok(ToSqlOutput::Owned(format!("\"{}\"", s).into())),
SqlParam::Bool(b) => match b {
true => 1.to_sql(),
false => 0.to_sql(),
},
SqlParam::Null => Ok(ToSqlOutput::Owned(rusqlite::types::Value::Null)),
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct TableSchema(HashMap<String, TableSchemaFieldType>);

Expand Down Expand Up @@ -121,18 +146,25 @@ impl TableSchema {
&self,
field_names: &Vec<&String>,
row: &DatabaseRow,
) -> Result<Vec<serde_json::Value>> {
) -> Result<Vec<SqlParam>> {
match row.content().as_object() {
None => Err(anyhow!("Row content is not an object")),
Some(object) => field_names
.iter()
.map(|col| match object.get(*col) {
// convert bools to 1/0 for SQLite
Some(Value::Bool(true)) => Ok(1.into()),
Some(Value::Bool(false)) => Ok(0.into()),
Some(x) => Ok(x.clone()),
// if the field is missing, insert null
None => Ok(Value::Null),
Some(Value::Bool(b)) => Ok(SqlParam::Bool(*b)),
Some(Value::Number(x)) => {
if x.is_i64() {
Ok(SqlParam::Int(x.as_i64().unwrap()))
} else if x.is_f64() {
Ok(SqlParam::Float(x.as_f64().unwrap()))
} else {
Err(anyhow!("Number is not an i64 or f64"))
}
}
Some(Value::String(s)) => Ok(SqlParam::Text(s.clone())),
None | Some(Value::Null) => Ok(SqlParam::Null),
_ => Err(anyhow!("Cannot convert value {:?} to SqlParam", object)),
})
.collect::<Result<Vec<_>>>(),
}
Expand Down

0 comments on commit 2ee45d9

Please sign in to comment.