Good place

This commit is contained in:
2021-12-09 10:29:55 -06:00
commit a42dfb42ee
6 changed files with 515 additions and 0 deletions

109
src/main.rs Normal file
View File

@@ -0,0 +1,109 @@
use std::error::Error;
use calamine::{open_workbook, Reader, Xlsx, RangeDeserializerBuilder};
use rusqlite::{Connection, Result};
const SCHEMA: &str = r#"CREATE TABLE IF NOT EXISTS data (
description text,
sku text,
level2 text,
level3 text,
revenue float,
cost float,
quantity integer,
year integer,
month integer
);
"#;
#[derive(Debug)]
struct Data {
description: String,
sku: String,
level2: String,
level3: String,
revenue: f64,
cost: f64,
quantity: i64,
year: i64,
month: i64,
}
fn main() -> Result<(), Box<dyn Error>> {
let now = chrono::offset::Local::now();
let mut data_vec = Vec::with_capacity(100000);
let mut excel: Xlsx<_> = open_workbook("file.xlsx").unwrap();
if let Some(Ok(r)) = excel.worksheet_range("Export") {
for row in r.rows().skip(1) {
if row[34].get_string() == None {
break;
}
let datestring = row[28].get_string().expect("Date Parsing Issue");
let year = datestring[0..4].parse::<i64>().unwrap();
let month = datestring[5..].parse::<i64>().unwrap();
data_vec.push(Data {
description: String::from(row[45].get_string().unwrap()),
sku: String::from(row[34].get_string().unwrap()),
level2: String::from(row[30].get_string().unwrap()),
level3: String::from(row[31].get_string().unwrap()),
revenue: match row[76].get_float() {
Some(x) => x,
None => match row[76].get_int() {
Some(x) => x as f64,
None => 0.0,
},
},
cost: match row[74].get_float() {
Some(x) => x,
None => match row[74].get_int() {
Some(x) => x as f64,
None => 0.0,
},
},
quantity: match row[75].get_int() {
Some(x) => x,
None => match row[76].get_float() {
Some(x) => x as i64,
None => 0,
},
},
year: year,
month: month,
});
}
}
let conn = Connection::open("data.db")?;
conn.execute("DROP TABLE IF EXISTS data;", [])?;
conn.execute(SCHEMA, [])?;
let mut inserts = Vec::with_capacity(100000);
inserts.push(String::from("INSERT INTO data (description,sku,level2,level3,revenue,cost,quantity,year,month) VALUES "));
for row in data_vec {
let other: String = format!(
"(\"{}\",\"{}\",\"{}\",\"{}\",{},{},{},{},{}),",
row.description,
row.sku,
row.level2,
row.level3,
row.revenue,
row.cost,
row.quantity,
row.year,
row.month
);
inserts.push(other);
}
let mut ins = inserts.join("");
ins.pop();
ins.push(';');
conn.execute(&ins, [])?;
let after = chrono::offset::Local::now();
let diff = after - now;
println!("{:?}", diff.num_milliseconds());
Ok(())
}