//! //! Tests. //! use std::fs::File; use std::time::Duration; use base64::Config; use chrono::NaiveDateTime; use akita::*; /// Connection configuration #[derive(Debug)] pub struct Database { pub ip: String, pub username: String, pub password: String, pub db_name: String, pub port: u16, } #[derive(AkitaTable, Clone, Default)] #[table(name = "t_system_user")] pub struct User { #[table_id(name = "id")] pub pk: i64, pub id: String, pub headline: Option, /// 状态 pub status: u8, /// 用户等级 0.普通会员 1.VIP会员 pub level: u8, /// 生日 pub birthday: Option, /// 性别 pub gender: u8, #[field(exist = false)] pub is_org: bool, #[field(name = "token")] pub url_token: String, } // #[allow(unused, non_snake_case, dead_code)] #[test] #[cfg(feature = "akita-mysql")] fn main() { let database = Database { ip: "101.132.66.102".to_string(), username: "weshop".to_string(), password: "6&u@f@Q6#".to_string(), db_name: "advanced_statistics".to_string(), port: 3306, }; // init pool let cfg = AkitaConfig::default() .set_max_size(5) .set_connection_timeout(Duration::from_secs(5)) .set_log_level(LogLevel::Info) .set_platform("mysql") .set_password(database.password) .set_username(database.username) .set_port(database.port) .set_db_name(database.db_name) .set_ip_or_hostname(database.ip); let akita = Akita::new(cfg.clone()).unwrap(); // Insert let insert_id: Option = akita.save(&User::default()).unwrap(); let insert_id_second = model.insert::, _>(&akita).unwrap(); let insert_ids = akita.save_batch(&[&User::default()]).unwrap(); // Delete by Wrapper let res = akita.remove::(Wrapper::new().eq("name", "Jack")).unwrap(); // Delete with primary id let res = akita.remove_by_id::(0).unwrap(); // Update User property by Wrapper let res = akita.update(&User::default(), Wrapper::new().eq("name", "Jack")).unwrap(); // Update with primary id let res = akita.update_by_id(&User::default()); // The Wrapper to build query condition let wrapper = Wrapper::new() .select(vec!["id".to_string(), "gender".to_string()]) .eq("username", "ussd") // username = 'ussd' .gt("age", 1) // age > 1 .lt("age", 10) // age < 10 .inside("user_type", vec!["admin", "super"]); // Select // Find all let vec: Vec = akita.list::(wrapper.clone()).unwrap(); // The number of paginated pages starts from subscript one let page_no = 1; // Number of paginated display let page_size = 10; let page = akita.page::(page_no, page_size, wrapper.clone()).unwrap(); // total number of pagination let total = page.total; // paginated data let vec_second = page.records; // Get the record count let count = akita.count::(Wrapper::new().eq("name", "Jack")).unwrap(); // Query with original sql let user: User = akita.exec_first("select * from t_system_user where name = ? and id = ?", ("Jack", 1)).unwrap(); // Or let user: User = akita.exec_first("select * from t_system_user where name = :name and id = :id", params! { "name" => "Jack", "id" => 1 }).unwrap(); let res = akita.exec_drop("select now()", ()).unwrap(); // CRUD with Entity let model = User::default(); // update let res = model.update_by_id::<_>(&akita).unwrap(); // delete let res = model.delete_by_id::(&akita, 1).unwrap(); // list let list = User::list::<_>(Wrapper::new().eq("name", "Jack"), &akita).unwrap(); // page let page = User::page::<_>(page_no, page_size, Wrapper::new().eq("name", "Jack"), &akita).unwrap(); // Fast with Akita // ... // Transaction akita.start_transaction().and_then(|mut transaction| { // do anything transaction.save::(&User { pk: 0, id: "".to_string(), headline: None, status: 0, level: 0, birthday: None, gender: 0, is_org: false, url_token: "".to_string(), })?; // final commit or rollback transaction.commit() // transaction.unwrap()?; }).unwrap(); }