ormer 0.1.5

An ORM framework with a usage style similar to Linq, supporting Turso(SQLite), PostgresQL, MySQL
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#![cfg(any(feature = "turso", feature = "postgresql", feature = "mysql"))]

/// 测试数据库配置模块
/// 提供统一的数据库后端配置,支持在多个数据库上运行测试
use ormer::DbType;

/// 数据库后端配置类型
/// 每个配置包含 (DbType, 连接字符串)
pub type DbConfig = (DbType, &'static str);

/// 获取所有可用的数据库后端配置
///
/// 根据编译时启用的 feature,返回可用的数据库配置列表
/// 测试函数应该遍历此列表,对每个数据库后端执行测试
#[allow(dead_code)]
#[allow(unused_mut)]
pub fn get_all_db_configs() -> Vec<DbConfig> {
    let mut configs = Vec::new();

    // Turso 仅在启用 turso feature 时可用
    #[cfg(feature = "turso")]
    configs.push((DbType::Turso, ":memory:"));

    // PostgreSQL 仅在启用 postgresql feature 时可用
    #[cfg(feature = "postgresql")]
    configs.push((
        DbType::PostgreSQL,
        "postgres://postgres:postgres@localhost:5432/ormer_test",
    ));

    // MySQL 仅在启用 mysql feature 时可用
    #[cfg(feature = "mysql")]
    configs.push((DbType::MySQL, "mysql://root:root@localhost:3306/ormer_test"));

    configs
}

/// 获取仅包含 Turso 的配置(用于快速测试或不支持多数据库的场景)
#[cfg(feature = "turso")]
#[allow(dead_code)]
pub fn get_turso_config() -> Vec<DbConfig> {
    vec![(DbType::Turso, ":memory:")]
}

/// 辅助函数:创建数据库连接
/// 从配置创建数据库连接
#[allow(dead_code)]
pub async fn create_db_connection(
    config: &DbConfig,
) -> Result<ormer::Database, Box<dyn std::error::Error>> {
    let db = ormer::Database::connect(config.0, config.1).await?;
    Ok(db)
}

/// 宏:为所有数据库后端生成测试(用于返回 () 的函数)
///
/// 使用方法:
/// ```rust
/// test_on_all_dbs!(my_test_fn);
/// ```
///
/// 这将为每个可用的数据库后端生成一个测试函数
#[macro_export]
macro_rules! test_on_all_dbs {
    ($test_fn:ident) => {
        paste::paste! {
            #[tokio::test]
            async fn [<test_on_all_dbs_ $test_fn>]() {
                let configs = $crate::_test_common::get_all_db_configs();

                for (idx, config) in configs.iter().enumerate() {
                    let db_type_name = match config.0 {
                        #[cfg(feature = "turso")]
                        ormer::DbType::Turso => "Turso",
                        #[cfg(feature = "postgresql")]
                        ormer::DbType::PostgreSQL => "PostgreSQL",
                        #[cfg(feature = "mysql")]
                        ormer::DbType::MySQL => "MySQL",
                        #[allow(unreachable_patterns)]
                        _ => "Unknown",
                    };

                    println!("\n=== Testing on {} (config {}) ===", db_type_name, idx);

                    // 调用实际的测试函数(返回 ())
                    $test_fn(config).await;
                }
            }
        }
    };
}

/// 宏:为所有数据库后端生成测试(使用 Result 返回类型)
///
/// 使用方法:
/// ```rust
/// test_on_all_dbs_result!(my_test_fn);
/// ```
#[macro_export]
macro_rules! test_on_all_dbs_result {
    ($test_fn:ident) => {
        paste::paste! {
            #[tokio::test]
            async fn [<test_on_all_dbs_ $test_fn>]() -> Result<(), Box<dyn std::error::Error>> {
                let configs = $crate::_test_common::get_all_db_configs();

                for (idx, config) in configs.iter().enumerate() {
                    let db_type_name = match config.0 {
                        #[cfg(feature = "turso")]
                        ormer::DbType::Turso => "Turso",
                        #[cfg(feature = "postgresql")]
                        ormer::DbType::PostgreSQL => "PostgreSQL",
                        #[cfg(feature = "mysql")]
                        ormer::DbType::MySQL => "MySQL",
                        #[allow(unreachable_patterns)]
                        _ => "Unknown",
                    };

                    println!("\n=== Testing on {} (config {}) ===", db_type_name, idx);

                    // 调用实际的测试函数
                    $test_fn(config).await?;
                }

                Ok(())
            }
        }
    };
}

// ==================== 公共测试模型宏定义 ====================
// 用于在测试文件中快速定义具有唯一表名的测试模型

/// 定义基础User模型(带自增主键、唯一约束、索引)
#[macro_export]
macro_rules! define_test_user {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary(auto)]
            id: i32,
            #[unique]
            name: String,
            #[index]
            age: i32,
            email: Option<String>,
        }
    };
}

/// 定义基础Role模型
#[macro_export]
macro_rules! define_test_role {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            #[index]
            uid: i32,
            name: String,
        }
    };
}

/// 定义带score的User模型(用于聚合测试)
#[macro_export]
macro_rules! define_test_user_with_score {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary(auto)]
            id: i32,
            name: String,
            age: i32,
            score: i32,
        }
    };
}

/// 定义简单User模型(仅用于基本测试)
#[macro_export]
macro_rules! define_test_user_simple {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            name: String,
            age: i32,
        }
    };
}

/// 定义带Option id的User模型(用于事务测试)
#[macro_export]
macro_rules! define_test_user_with_option_id {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: Option<i64>,
            name: String,
            email: String,
        }
    };
}

/// 定义带联合唯一的Role模型(用于main_usage测试)
#[macro_export]
macro_rules! define_test_role_with_unique_group {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            #[unique(group = 1)]
            uid: i32,
            #[unique(group = 1)]
            name: String,
        }
    };
}

/// 定义Join测试用的User模型(无email)
#[macro_export]
macro_rules! define_test_user_for_join {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary(auto)]
            id: i32,
            #[unique]
            name: String,
            age: i32,
        }
    };
}

/// 定义Join测试用的Role模型(有role_name)
#[macro_export]
macro_rules! define_test_role_for_join {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            #[index]
            uid: i32,
            role_name: String,
        }
    };
}

/// 定义带score的User模型(用于aggregate_type测试,id为i64)
#[macro_export]
macro_rules! define_test_user_for_aggregate_type {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i64,
            name: String,
            age: i32,
            score: f64,
        }
    };
}

/// 定义带完整类型的模型(用于create_table测试)
#[macro_export]
macro_rules! define_test_complete_types {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i64,
            text_val: String,
            optional_text: Option<String>,
            optional_int: Option<i32>,
            bool_val: bool,
            optional_bool: Option<bool>,
        }
    };
}

/// 定义直接创建测试用的User模型(无email)
#[macro_export]
macro_rules! define_test_user_direct {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary(auto)]
            id: i32,
            name: String,
            age: i32,
        }
    };
}

/// 定义collect测试用的Role模型(有user_id字段)
#[macro_export]
macro_rules! define_test_role_for_collect {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            user_id: i32,
            role_name: String,
        }
    };
}

/// 定义单字段ID模型(用于collect测试)
#[macro_export]
macro_rules! define_test_user_id {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
        }
    };
}

/// 定义最简User模型(只有id和name)
#[macro_export]
macro_rules! define_test_user_minimal {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary(auto)]
            id: i32,
            name: String,
        }
    };
}

/// 定义外键测试用的User模型
#[macro_export]
macro_rules! define_test_user_for_fk {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary(auto)]
            id: i32,
            name: String,
        }
    };
}

/// 定义外键测试用的Role模型(带user_id和role_name)
#[macro_export]
macro_rules! define_test_role_for_fk {
    ($struct_name:ident, $table_name:literal, $foreign_type:ty) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            #[foreign($foreign_type)]
            user_id: i32,
            role_name: String,
        }
    };
}

/// 定义简单Role模型(只有id和name)
#[macro_export]
macro_rules! define_test_role_simple {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            name: String,
        }
    };
}

/// 定义连接池测试用的User模型(无auto)
#[macro_export]
macro_rules! define_test_user_for_pool {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            name: String,
            age: i32,
            email: Option<String>,
        }
    };
}

/// 定义map_to测试用的User模型(无auto,有email为String)
#[macro_export]
macro_rules! define_test_user_for_map_to {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            name: String,
            email: String,
            age: i32,
        }
    };
}

/// 定义map_to测试用的Role模型(有user_id和role_name)
#[macro_export]
macro_rules! define_test_role_for_map_to {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            user_id: i32,
            role_name: String,
        }
    };
}

/// 定义单字段ID模型(用于map_to测试,有PartialEq)
#[macro_export]
macro_rules! define_test_user_id_with_eq {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone, PartialEq)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
        }
    };
}

/// 定义双字段模型(用于map_to测试,有PartialEq)
#[macro_export]
macro_rules! define_test_user_name_age {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone, PartialEq)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            name: String,
            age: i32,
        }
    };
}

/// 定义range测试用的User模型(无email,无auto)
#[macro_export]
macro_rules! define_test_user_for_range {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary]
            id: i32,
            name: String,
            age: i32,
        }
    };
}

/// 定义简单事务测试用的User模型(Option<i64> id,只有name)
#[macro_export]
macro_rules! define_test_user_for_simple_txn {
    ($struct_name:ident, $table_name:literal) => {
        #[derive(Debug, ormer::Model, Clone)]
        #[table = $table_name]
        struct $struct_name {
            #[primary(auto)]
            id: Option<i64>,
            name: String,
        }
    };
}