bootrust 0.1.0

An elegant macroless data access layer abstraction, simple and easy-use object-relational mapping powered by the Serde serialization framework. 一个优雅的无宏的数据访问层抽象, 由serde序列化框架提供支持的简单易用的对象关系映射
Documentation
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::database::{
    Connection, DatabaseConfig, DbError, QueryErrorKind, RelationalDatabase, Row, Value,
};
use chrono::{Datelike, NaiveDateTime, TimeZone, Timelike, Utc};
use mysql::OptsBuilder;
use r2d2::{Pool, PooledConnection};
use r2d2_mysql::mysql::{prelude::*, Value as MySqlValue};
use r2d2_mysql::MySqlConnectionManager;
use std::sync::{Arc, Mutex};

#[derive(Debug, Clone)]
pub struct MySqlDatabase {
    pool: Arc<Pool<MySqlConnectionManager>>,
    current_transaction: Arc<Mutex<Option<PooledConnection<MySqlConnectionManager>>>>,
}

impl MySqlDatabase {
    fn new_pool(config: &DatabaseConfig) -> Result<Pool<MySqlConnectionManager>, r2d2::Error> {
        let opts = OptsBuilder::new()
            .ip_or_hostname(Some(&config.host))
            .tcp_port(config.port)
            .user(Some(&config.username))
            .pass(Some(&config.password))
            .db_name(Some(&config.database_name));

        let manager = MySqlConnectionManager::new(opts);
        Pool::builder().max_size(config.max_size).build(manager)
    }

    fn value_to_mysql(value: &Value) -> MySqlValue {
        match value {
            Value::Null => MySqlValue::NULL,
            Value::Bigint(i) => MySqlValue::Int(*i),
            Value::Float(f) => MySqlValue::Float(*f as f32),
            Value::Double(f) => MySqlValue::Double(*f),
            // Value::Text(s) => MySqlValue::Bytes(s.clone().into_bytes()),
            Value::Text(s) => MySqlValue::from(s),
            Value::Boolean(b) => MySqlValue::Int(if *b { 1 } else { 0 }),
            Value::Bytes(b) => MySqlValue::from(b),
            Value::DateTime(dt) => MySqlValue::Date(
                dt.year() as u16,
                dt.month() as u8,
                dt.day() as u8,
                dt.hour() as u8,
                dt.minute() as u8,
                dt.second() as u8,
                dt.timestamp_subsec_micros(),
            ),
            _ => unimplemented!(),
        }
    }

    fn convert_mysql_to_value(value: MySqlValue) -> Result<Value, DbError> {
        match value {
            MySqlValue::NULL => Ok(Value::Null),
            MySqlValue::Int(i) => Ok(Value::Bigint(i)),
            MySqlValue::Float(f) => Ok(Value::Float(f)),
            MySqlValue::Double(f) => Ok(Value::Double(f)),
            MySqlValue::Bytes(bytes) => Ok(Value::Bytes(bytes)),
            MySqlValue::Date(year, month, day, hour, minute, second, micros) => {
                let naive = NaiveDateTime::new(
                    chrono::NaiveDate::from_ymd_opt(year as i32, month as u32, day as u32)
                        .ok_or_else(|| DbError::ConversionError("Invalid date".to_string()))?,
                    chrono::NaiveTime::from_hms_micro_opt(
                        hour as u32,
                        minute as u32,
                        second as u32,
                        micros,
                    )
                    .ok_or_else(|| DbError::ConversionError("Invalid time".to_string()))?,
                );
                Ok(Value::DateTime(Utc.from_utc_datetime(&naive)))
            }
            _ => Err(DbError::ConversionError(
                "Unsupported MySQL type".to_string(),
            )),
        }
    }

    fn execute_with_connection<F, T>(&self, f: F) -> Result<T, DbError>
    where
        // F: FnOnce(&mut PooledConnection<MySqlConnectionManager>) -> Result<T, DbError>
        F: FnOnce(&mut PooledConnection<MySqlConnectionManager>) -> Result<T, DbError>,
    {
        let mut transaction_guard = self
            .current_transaction
            .lock()
            .map_err(|e| DbError::TransactionError(e.to_string()))?;

        let mut conn = if let Some(conn) = &mut *transaction_guard {
            conn
        } else {
            &mut self
                .pool
                .get()
                .map_err(|e| DbError::ConnectionError(e.to_string()))?
        };

        // f(conn)
        f(&mut conn)
    }
}

impl RelationalDatabase for MySqlDatabase {
    fn placeholders(&self, keys: &[String]) -> Vec<String> {
        vec!["?".to_string(); keys.len()]
    }
    fn connect(config: DatabaseConfig) -> Result<Self, DbError> {
        let pool = Self::new_pool(&config).map_err(|e| DbError::ConnectionError(e.to_string()))?;

        Ok(MySqlDatabase {
            pool: Arc::new(pool),
            current_transaction: Arc::new(Mutex::new(None)),
        })
    }

    fn close(&self) -> Result<(), DbError> {
        Ok(())
    }

    fn ping(&self) -> Result<(), DbError> {
        let mut conn = self
            .pool
            .get()
            .map_err(|e| DbError::ConnectionError(e.to_string()))?;
        conn.query_drop("SELECT 1")
            .map_err(|e| DbError::ConnectionError(e.to_string()))?;
        Ok(())
    }

    fn begin_transaction(&self) -> Result<(), DbError> {
        let mut conn = self
            .pool
            .get()
            .map_err(|e| DbError::TransactionError(e.to_string()))?;

        conn.query_drop("START TRANSACTION")
            .map_err(|e| DbError::TransactionError(e.to_string()))?;

        let mut guard = self
            .current_transaction
            .lock()
            .map_err(|e| DbError::TransactionError(e.to_string()))?;
        *guard = Some(conn);

        Ok(())
    }

    fn commit(&self) -> Result<(), DbError> {
        let mut guard = self
            .current_transaction
            .lock()
            .map_err(|e| DbError::TransactionError(e.to_string()))?;

        if let Some(mut conn) = guard.take() {
            conn.query_drop("COMMIT")
                .map_err(|e| DbError::TransactionError(e.to_string()))?;
        }
        Ok(())
    }

    fn rollback(&self) -> Result<(), DbError> {
        let mut guard = self
            .current_transaction
            .lock()
            .map_err(|e| DbError::TransactionError(e.to_string()))?;

        if let Some(mut conn) = guard.take() {
            conn.query_drop("ROLLBACK")
                .map_err(|e| DbError::TransactionError(e.to_string()))?;
        }
        Ok(())
    }

    fn execute(&self, query: &str, params: Vec<Value>) -> Result<u64, DbError> {
        self.execute_with_connection(|conn| {
            let params: Vec<mysql::Value> =
                params.iter().map(MySqlDatabase::value_to_mysql).collect();

            let stmt = conn
                .prep(query)
                .map_err(|e| DbError::ConversionError(e.to_string()))?;

            conn.exec_drop(&stmt, &params).map_err(|e| {
                match e {
                    mysql::Error::MySqlError(ref mysql_err) => {
                        // 获取 MySQL 错误码
                        match mysql_err.code {
                            1451 | 1452 => {
                                // 外键约束错误
                                DbError::QueryError(QueryErrorKind::ForeignKeyViolation(
                                    mysql_err.message.clone(),
                                ))
                            }
                            1062 => {
                                // 唯一约束错误
                                DbError::QueryError(QueryErrorKind::UniqueViolation(
                                    mysql_err.message.clone(),
                                ))
                            }
                            1048 => {
                                // 非空约束错误
                                DbError::QueryError(QueryErrorKind::NotNullViolation(
                                    mysql_err.message.clone(),
                                ))
                            }
                            // 其他错误
                            other_code => DbError::QueryError(QueryErrorKind::Other(format!(
                                "code: {}, message: {}",
                                other_code, mysql_err.message
                            ))),
                        }
                    }
                    // 其他类型的错误(比如连接错误、IO错误等)
                    _ => DbError::QueryError(QueryErrorKind::Other(format!("message: {}", e))),
                }
            })?;
            Ok(conn.affected_rows() as u64)
        })
    }

    fn query(&self, query: &str, params: Vec<Value>) -> Result<Vec<Row>, DbError> {
        self.execute_with_connection(|conn| {
            let params: Vec<mysql::Value> =
                params.iter().map(MySqlDatabase::value_to_mysql).collect();
            let stmt = conn
                .prep(query)
                .map_err(|e| DbError::ConversionError(e.to_string()))?;

            let result = conn
                .exec_map(&stmt, params, |row: mysql::Row| {
                    let mut values = Vec::new();
                    let columns = row.columns();

                    for (i, _column) in columns.iter().enumerate() {
                        let value = row.get(i).ok_or_else(|| {
                            DbError::QueryError("Missing column value".to_string().into())
                        })?;
                        values.push(Self::convert_mysql_to_value(value)?);
                    }

                    Ok::<Row, DbError>(Row {
                        columns: columns.iter().map(|c| c.name_str().to_string()).collect(),
                        values,
                    })
                })
                .map_err(|e| DbError::QueryError(e.to_string().into()))?;

            let mut rows = Vec::new();
            for row_result in result {
                rows.push(row_result?);
            }
            Ok(rows)
        })
    }

    fn query_one(&self, query: &str, params: Vec<Value>) -> Result<Option<Row>, DbError> {
        let mut rows = self.query(query, params)?;
        Ok(rows.pop())
    }

    fn get_connection(&self) -> Result<Connection, DbError> {
        let _conn = self
            .pool
            .get()
            .map_err(|e| DbError::PoolError(e.to_string()))?;
        Ok(Connection {})
    }

    fn release_connection(&self, _conn: Connection) -> Result<(), DbError> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use serial_test::serial;

    fn setup_test_db() -> MySqlDatabase {
        let config = DatabaseConfig {
            host: "localhost".to_string(),
            port: 3306,
            username: "root".to_string(),
            password: "root".to_string(),
            database_name: "test".to_string(),
            max_size: 10,
        };
        MySqlDatabase::connect(config).unwrap()
    }

    #[test]
    // #[ignore] // 需要MySQL服务器才能运行
    #[serial]
    fn test_basic_connection() {
        let db = setup_test_db();
        assert!(db.ping().is_ok());
    }

    #[test]
    // #[ignore]
    #[serial]
    fn test_execute() {
        let db = setup_test_db();
        db.execute("DROP TABLE IF EXISTS users", vec![]).unwrap();
        db.execute(
            "CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)",
            vec![],
        )
        .unwrap();

        let affected_rows = db
            .execute(
                "INSERT INTO users (name, age) VALUES (?, ?)",
                vec![Value::Text("Alice".to_string()), Value::Bigint(30)],
            )
            .unwrap();
        assert_eq!(affected_rows, 1);

        let affected_rows = db
            .execute(
                "UPDATE users SET age = ? WHERE name = ?",
                vec![Value::Bigint(31), Value::Text("Alice".to_string())],
            )
            .unwrap();
        assert_eq!(affected_rows, 1);

        db.execute("DROP TABLE users", vec![]).unwrap();
    }

    #[test]
    // #[ignore]
    #[serial]
    fn test_query() {
        let db = setup_test_db();
        db.execute("DROP TABLE IF EXISTS users", vec![]).unwrap();
        db.execute(
            "CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT, created_at DATETIME)",
            vec![],
        )
        .unwrap();

        let now = Utc::now();
        db.execute(
            "INSERT INTO users (name, age, created_at) VALUES (?, ?, ?)",
            vec![
                Value::Text("Alice".to_string()),
                Value::Bigint(30),
                Value::DateTime(now),
            ],
        )
        .unwrap();

        let rows = db
            .query("SELECT id, name, age, created_at FROM users", vec![])
            .unwrap();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].columns, vec!["id", "name", "age", "created_at"]);
        assert_eq!(rows[0].values.len(), 4);
        assert!(matches!(rows[0].values[0], Value::Bigint(_)));
        assert!(matches!(rows[0].values[1], Value::Bytes(_)));
        assert!(matches!(rows[0].values[2], Value::Bigint(_)));
        assert!(matches!(rows[0].values[3], Value::DateTime(_)));

        if let Value::Bytes(name) = &rows[0].values[1] {
            assert_eq!(name, b"Alice");
        } else {
            dbg!(&rows);
            panic!("Expected name to be a string");
        }

        if let Value::Bigint(age) = &rows[0].values[2] {
            assert_eq!(age, &30);
        } else {
            panic!("Expected age to be an integer");
        }

        db.execute("DROP TABLE users", vec![]).unwrap();
    }

    #[test]
    // #[ignore]
    #[serial]
    fn test_query_one() {
        let db = setup_test_db();
        db.execute("DROP TABLE IF EXISTS users", vec![]).unwrap();
        db.execute(
            "CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))",
            vec![],
        )
        .unwrap();

        db.execute(
            "INSERT INTO users (name) VALUES (?), (?)",
            vec![
                Value::Text("Alice".to_string()),
                Value::Text("Bob".to_string()),
            ],
        )
        .unwrap();

        let row = db
            .query_one(
                "SELECT id, name FROM users WHERE name = ?",
                vec![Value::Text("Alice".to_string())],
            )
            .unwrap();
        assert!(row.is_some());

        if let Some(row) = row {
            assert_eq!(row.columns, vec!["id", "name"]);
            if let Value::Bytes(name) = &row.values[1] {
                assert_eq!(name, b"Alice");
            } else {
                panic!("Expected name to be a string");
            }
        }

        let none = db
            .query_one(
                "SELECT * FROM users WHERE name = ?",
                vec![Value::Text("Charlie".to_string())],
            )
            .unwrap();
        assert!(none.is_none());

        db.execute("DROP TABLE users", vec![]).unwrap();
    }

    #[test]
    #[serial]
    fn test_transaction() {
        let db = setup_test_db();
        db.execute("DROP TABLE IF EXISTS users", vec![]).unwrap();
        db.execute(
            "CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))",
            vec![],
        )
        .unwrap();

        db.begin_transaction().unwrap();
        db.execute(
            "INSERT INTO users (name) VALUES (?)",
            vec![Value::Text("Alice".to_string())],
        )
        .unwrap();
        db.rollback().unwrap();

        let rows = db.query("SELECT * FROM users", vec![]).unwrap();
        assert_eq!(rows.len(), 0);

        db.begin_transaction().unwrap();
        db.execute(
            "INSERT INTO users (name) VALUES (?)",
            vec![Value::Text("Bob".to_string())],
        )
        .unwrap();
        db.commit().unwrap();

        let rows = db.query("SELECT * FROM users", vec![]).unwrap();
        assert_eq!(rows.len(), 1);

        db.execute("DROP TABLE users", vec![]).unwrap();
    }

    #[test]
    #[serial]
    fn test_value_conversion() {
        let _db = setup_test_db();

        let now = Utc::now();
        let mysql_now = MySqlDatabase::value_to_mysql(&Value::DateTime(now));
        let converted_now = MySqlDatabase::convert_mysql_to_value(mysql_now).unwrap();

        if let Value::DateTime(dt) = converted_now {
            assert_eq!(dt.date_naive(), now.date_naive());
            // assert_eq!(dt.time(), now.time());
            // 比较时间时,允许1微秒的误差
            assert!((dt.timestamp_micros() - now.timestamp_micros()).abs() <= 1);
        } else {
            panic!("Expected DateTime");
        }
    }
}