-
Notifications
You must be signed in to change notification settings - Fork 7
/
simple.rs
199 lines (184 loc) · 6.78 KB
/
simple.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::{UpdateWrapper, Wrapper, wrapper, BaseMapper, AkitaError, value::*};
use crate::pool::{AkitaConfig, LogLevel};
use crate::{pool::Pool, data::*, IPage};
use crate::manager::{GetTableName, TableName, FieldName, GetFields, FieldType, AkitaEntityManager};
#[derive(Debug, FromValue, ToValue, AkitaTable, Clone)]
#[table(name="t_system_user")]
struct SystemUser {
#[field = "name"]
id: Option<i32>,
#[table_id]
username: String,
#[field(name="ages", exist = "false")]
age: i32,
}
fn get_table_info() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let table = em
.get_table(&TableName::from("t_system_user"))
.expect("must have a table");
println!("table: {:#?}", table);
}
fn remove() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let mut wrap = UpdateWrapper::new();
wrap.eq("username", "'ussd'");
match em.remove::<SystemUser, UpdateWrapper>(&mut wrap) {
Ok(res) => {
println!("success removed data!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}
fn count() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let mut wrap = UpdateWrapper::new();
wrap.eq("username", "'ussd'");
match em.count::<SystemUser, UpdateWrapper>(&mut wrap) {
Ok(res) => {
println!("success count data!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}
fn remove_by_id() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
match em.remove_by_id::<SystemUser, String>("'fffsd'".to_string()) {
Ok(res) => {
println!("success removed data!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}
fn update() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let user = SystemUser { id: 1.into(), username: "fff".to_string(), age: 1 };
let mut wrap = UpdateWrapper::new();
wrap.eq("username", "'ussd'");
match em.update(&user, &mut wrap) {
Ok(res) => {
println!("success update data!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}
fn update_by_id() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let user = SystemUser { id: 1.into(), username: "fff".to_string(), age: 1 };
match em.update_by_id(&user, "id") {
Ok(res) => {
println!("success update data by id!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}
fn save() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let user = SystemUser { id: 1.into(), username: "fff".to_string(), age: 1 };
match em.save(&user) {
Ok(res) => {
println!("success save data!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}
fn save_batch() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let user = SystemUser { id: 1.into(), username: "fff".to_string(), age: 1 };
match em.save_batch::<_>(&vec![&user]) {
Ok(res) => {
println!("success save_batch data!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}
fn list() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let mut wrapper = UpdateWrapper::new();
wrapper.eq("username", "'ussd'");
match em.list::<SystemUser, UpdateWrapper>(&mut wrapper) {
Ok(res) => {
println!("success list data!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}
fn page() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let mut wrapper = UpdateWrapper::new();
wrapper.eq("username", "'ussd'");
match em.page::<SystemUser, UpdateWrapper>(1, 10,&mut wrapper) {
Ok(res) => {
println!("success page data!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}
fn select_by_id() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let mut wrapper = UpdateWrapper::new();
wrapper.eq("username", "'ussd'");
match em.select_by_id::<SystemUser, i32>(1) {
Ok(res) => {
println!("success select one data!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}
fn select_one() {
let db_url = String::from("mysql://root:password@localhost:3306/akita");
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: db_url, log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let mut wrapper = UpdateWrapper::new();
wrapper.eq("username", "'ussd'");
match em.select_one::<SystemUser, UpdateWrapper>(&mut wrapper) {
Ok(res) => {
println!("success select one data!");
}
Err(err) => {
println!("error:{:?}",err);
}
}
}