From 49662e68d599eb8782f0864f44f3ee53bc82aa2f Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sat, 20 Jul 2024 00:17:57 +0800 Subject: [PATCH] update --- .gitignore | 6 ++++++ Cargo.toml | 17 +++++++++++++++++ src/main.rs | 24 ++++++++++++++++++++++++ src/start_db.rs | 23 +++++++++++++++++++++++ src/word_entity.rs | 25 +++++++++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/start_db.rs create mode 100644 src/word_entity.rs diff --git a/.gitignore b/.gitignore index 3ca43ae..dbc294b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,9 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + + +# Added by cargo + +/target +test.db diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c248b60 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "sea_orm" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sea-orm = {version = "0.12.15",features = [ "sqlx-sqlite", "runtime-tokio-native-tls", "macros" ,"debug-print"] } +# serde = "1.0.204" +# serde_json = "1.0.120" +tokio = {version = "1.38.1",features = ["full"]} + +[profile.release] +opt-level = 3 +lto = true +debug = false diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a99e91e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,24 @@ +use std::error::Error; +use sea_orm::{ ActiveModelTrait, ActiveValue}; +use start_db::Starter; +// use serde_json::json; +use tokio; + +pub mod word_entity; +pub mod start_db; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let db_info = Starter::connect().await?; + // db_info.create_table().await?; + + let insert_word = word_entity::ActiveModel{ + id:ActiveValue::not_set(), + bsm: ActiveValue::set("test".to_string()), + hz: ActiveValue::set("test".to_string()), + py_js: ActiveValue::set("test".to_string()), + }; + insert_word.insert(&db_info.db).await?; + + Ok(()) +} diff --git a/src/start_db.rs b/src/start_db.rs new file mode 100644 index 0000000..9ccb666 --- /dev/null +++ b/src/start_db.rs @@ -0,0 +1,23 @@ +use sea_orm::{Condition, ConnectOptions, ConnectionTrait, Database, DatabaseConnection}; +use std::error::Error; +use sea_orm::Schema; + +pub struct Starter { + pub db: DatabaseConnection, +} +impl Starter { + pub async fn connect() -> Result> { + let opt: ConnectOptions = ConnectOptions::new("sqlite://test.db?mode=rwc"); + let db_new: DatabaseConnection = Database::connect(opt).await?; + + Ok(Starter{db:db_new}) + } + pub async fn create_table(&self) -> Result<(), Box> { + let backend = self.db.get_database_backend(); + let schema = Schema::new(backend); + let table_create_statement = schema.create_table_from_entity(super::word_entity::Entity); + let table_create_result = self.db.execute(backend.build(&table_create_statement)).await?; + println!("{:?}",table_create_result); + Ok(()) + } +} diff --git a/src/word_entity.rs b/src/word_entity.rs new file mode 100644 index 0000000..d2919eb --- /dev/null +++ b/src/word_entity.rs @@ -0,0 +1,25 @@ +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] +#[sea_orm(table_name = "words")] +pub struct Model{ + #[sea_orm(primary_key)] + pub id: i32, + pub bsm: String, + pub hz:String, + pub py_js: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + // #[sea_orm(has_many = "super::fruit::Entity")] + // Fruit, +} + +// impl Related for Entity { +// fn to() -> RelationDef { +// Relation::Fruit.def() +// } +// } + +impl ActiveModelBehavior for ActiveModel {} \ No newline at end of file