learn_orm_word/src/main.rs
2024-07-22 00:16:00 +08:00

51 lines
1.6 KiB
Rust

use std::{error::Error, process::exit};
use start_db::Starter;
use log::{error, info};
use parse_json::json_object::Word as WordJsonObject;
// use serde_json::json;
use tokio;
pub mod word_entity;
pub mod start_db;
pub mod parse_json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
println!("The program is starting.\nIf you want to see the log info,please set env RUST_LOG=info");
info!("DataBase is starting");
let db_info = Starter::connect("sqlite://test.db?mode=rwc").await?;
db_info.create_table().await.unwrap_or_else(|e: Box<dyn Error>|{
info!("The table have been created. Don't care this Error!!!!!:{}",e);
});
// word_entity::tests::insert_tests_value(&db_info.conn).await.unwrap_or_else(|e: Box<dyn Error>|{
// error!("Cannot write data into database.The details are as follows:\n{}",e);
// });
let parsed_object: Vec<WordJsonObject> = match parse_json::parse("word.json").await {
Ok(o)=>{
o
}
Err(e)=>{
error!("Error to parse the object");
error!("{}",e);
exit(1);
}
};
info!("parsed object Ok!Vec.size={}",parsed_object.len());
info!("object[0]={:?}",parsed_object[0]);
let mut word_orm_obj:Vec<word_entity::Model>=vec![];
for i in parsed_object{
word_orm_obj.push(i.into());
}
word_entity::add_to_database(word_orm_obj, &db_info.conn)
.await
.unwrap_or_else(|e: Box<dyn Error>|{error!("add to database error");error!("{}",e);exit(1)});
return Ok(());
}