This commit is contained in:
Zengtudor 2024-07-21 23:44:40 +08:00
parent c2b3b5595e
commit 3383dcaffa
5 changed files with 64 additions and 5 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@ Cargo.lock
/target /target
test.db test.db
test.db-journal

View File

@ -19,9 +19,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
db_info.create_table().await.unwrap_or_else(|e: Box<dyn Error>|{ db_info.create_table().await.unwrap_or_else(|e: Box<dyn Error>|{
info!("The table have been created. Don't care this Error!!!!!:{}",e); 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>|{ // 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); // 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 { let parsed_object: Vec<WordJsonObject> = match parse_json::parse("word.json").await {
Ok(o)=>{ Ok(o)=>{
o o
@ -36,5 +36,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
info!("parsed object Ok!"); info!("parsed object Ok!");
info!("object[0]={:?}",parsed_object[0]); 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
.map_err(|e|{error!("add to database error");error!("{}",e);exit(1)})
.unwrap();
return Ok(()); return Ok(());
} }

View File

@ -2,8 +2,6 @@ use serde::{Serialize,Deserialize};
#[derive(Clone, Debug,Serialize,Deserialize)] #[derive(Clone, Debug,Serialize,Deserialize)]
pub struct Word{ pub struct Word{
#[serde(skip)]
pub id: i32,
pub bsm: String, pub bsm: String,
pub hz:String, pub hz:String,
#[serde(alias="pyJs")] #[serde(alias="pyJs")]

View File

@ -1,6 +1,12 @@
use std::process::exit;
use log::error;
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
use serde::{Deserialize,Serialize}; use serde::{Deserialize,Serialize};
use crate::parse_json::json_object;
pub use renew::add_to_database as add_to_database;
pub mod tests; pub mod tests;
pub mod renew;
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel,Serialize,Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel,Serialize,Deserialize)]
#[sea_orm(table_name = "words")] #[sea_orm(table_name = "words")]
@ -14,6 +20,24 @@ pub struct Model{
pub py_js: String, pub py_js: String,
} }
impl From<json_object::Word> for Model{
fn from(obj:json_object::Word)->Self{
Model{
id:0,
bsm:obj.bsm,
hz:obj.hz,
py_js:match serde_json::to_string(&obj.py_js) {
Ok(o)=>o,
Err(e)=>{
error!("Cannot let {:?} parse to String",obj.py_js);
error!("{}",e);
exit(1);
}
}
}
}
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation { pub enum Relation {
// #[sea_orm(has_many = "super::fruit::Entity")] // #[sea_orm(has_many = "super::fruit::Entity")]

25
src/word_entity/renew.rs Normal file
View File

@ -0,0 +1,25 @@
use std::error::Error;
use sea_orm::{ActiveModelTrait, ActiveValue, DatabaseConnection};
use super::{ActiveModel, Model};
pub async fn add_to_database(objs:Vec<Model>,db:&DatabaseConnection)->Result<(),Box<dyn Error>>{
let mut tasks = vec![];
for i in objs{
let inner_db: DatabaseConnection = db.clone();
tasks.push(tokio::spawn(async move{
ActiveModel{
id:ActiveValue::not_set(),
bsm:ActiveValue::set(i.bsm),
hz:ActiveValue::set(i.hz),
py_js:ActiveValue::set(i.py_js)
}.insert(&inner_db).await
}));
}
for i in tasks{
i.await??;
}
return Ok(());
}