tiny_orm_macro_derive 0.4.5

基于sqlx的将SQL和ORM结合的简易ORM实现的相关辅助宏
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# Rust Tiny Orm

一个基于sqlx的极简orm,本项目将sql代码以derive属性方式和结构体进行关联,并自动生成相关CRUD相关API,可根据需要实现任意复杂度的数据获取。

本项目开发主要原因:

1. 目前rust的orm产品都太大而全了,习惯了Python Django的自动化的实现,对DSL相当不习惯,个人觉得太繁琐了;
2. 实际orm就是api和sql的转换,因为orm的实现限制,sql的复杂查询的实现都比较别扭,为了与数据库交互还得额外学习大量orm dsl的用法,为什么不直接用sql查询呢?!
3. orm为了满足各种需要,实现均较为复杂,带来了大量的学习成本。但是,实际工程角度来看,实际我们项目与数据库的交互是有限的,基本就是根据特定字段查询数据,新增数据和更新数据等,各种查询的逻辑也基本是固定的,并不需要高度灵活性的查询接口,且一般ORM实现高度灵活的查询,是以增加运行时消耗为代价的;
4. 实际orm的核心生成sql交互的代码,整个和rust的宏高度契合,因此使用宏生成我们工程需要的数据库接口,是ORM的最佳实践!
   
因此,是否可以将数据查询的sql和rust结合,由程序员自行控制sql的表现,这样在rust的高性能助力下,我们可以写成性能超级赞的数据库应用,并实现如下功能:

1. 自动实现rust结构体到数据库表的映射;
2. 自动生成数据库交互相关的接口,如insert、update、query、delete和exist类方法;
3. 在自动生成方法基础上,可根据sql自动生成具有高度灵活性的数据库CRUD方法;
4. 在以上基础上最大化的减少手写sql的工作量,减少数据库交互逻辑的处理工作量。

# 项目组成

本项目由两个库组成,分别为:tiny_orm_core和tiny_orm_macro_derive,其中tiny_orm_core为项目核心库,tiny_orm_macro_derive为项目派生宏库

# 特性

根据你的数据库类型选择不同特性,缺省为mysql

1. mysql
2. postgres
3. sqlite
4. mssql
5. any

# 核心库说明

1. tiny_orm_core::TinyOrmDbMeta

结构体对应数据表的相关配置信息,一般由派生宏进行自动生成,并提供如下方法

* pub fn build_select_sql(&self, where_sql: &str) -> String
   * 创建select语句
* pub fn build_exist_sql(&self, where_sql: &str) -> String
   * 创建exist语句
* pub fn build_insert_sql(&self, insert_field_value: &str) -> String
   * 创建insert语句
* pub fn build_delete_sql(&self, delete_where_sql: &str) -> String
   * 创建where语句
* pub fn build_update_sql(&self, set_sql: &str, where_sql: Option<&str>) -> String
   * 创建update语句
* pub fn build_update_sql_with_pk(&self, set_sql: &str) -> String
   * 创建通过主键更新update语句

2. tiny_orm_core::TinyOrmDbModel

trait,实现了db_query创建数据库查询器,方法定义如下:

fn db_query(sql: &str) -> Query<MySql, MySqlArguments>

3. tiny_orm_core::TinyOrmDbQuery

trait,实现了各种数据库交互方法,均以db_开头,该trait中的方法不自动进行数据转换.

在派生宏无法满足需求的情况下,可使用该trait中的方法构建灵活的crud方法。

4. tiny_orm_core::TinyOrmData

trait,实现了与结构体关联的相关数据库交互放阿飞,均以orm_开头,该trait中的查询类方法均实现数据库行与struct的转换,转换时调用如下方法:
fn orm_row_map(row: TinyOrmSqlRow) -> Self;
该方法需要自行实现。

在派生宏无法满足需求的情况下,可使用该trait中的方法构建灵活的crud方法。

# 派生宏说明

1. #[derive(TinyOrm)]
   
为结构体自动实现TinyOrmDbModel数据库模型

# 用法

本宏定义了如下属性,用于设置数据映射接口和参数

# 应用于结构体的属性

1. #[orm_table_name = "core_user"]

可选属性,可设置多个。

自定义数据库表名称。
未设置则表名称为结构名称的蛇形命名,
例如:TestUser转换为test_user。

2. #[orm_table_name_pref = "core"]

可选属性,可设置多个。

为自动生成的数据库表名称增加前缀。
例如:TestUser转换为core_test_user.

3. #[orm_query]

可选属性,可设置多个。

生成Self::orm_query_with开头的获取多条记录的查询方法。

有三个子属性:

* name
   * 生成的函数名称后缀
   * 如果name设置为tel,实际生成的方法名称为:orm_query_with_tel
* sql_where
   * 数据库select语句的where部分
   * 可选,未设置则为获取所有记录
* order_by
   * 数据库select语句的order by部分
   * 可选,未设置则为默认排序
* args
   * 生成方法的参数部分定义
   * 参数数量应与sql_where定义的?参数数量一致
   * 与实际函数定义的写法一致,如定义2个参数,格式为:arg1:type1,arg2:type2
   * 实际生成的函数,会自动添加pool: &TinyOrmDbPool
* doc 
   * 生成方法的文档说明

例如:

```ignore
#[orm_query(
   name = "name_and_tel",
   sql_where = "user.name = ? and mobile_phone = ?",
   order_by = "user.id",
   args = "name:&str,mobile_phone:&str",
   doc = "根据姓名和手机号查询用户",
)]
自动生成如下函数:
/// 根据姓名和手机号查询用户
pub async fn orm_query_with_name_and_tel(pool: &TinyOrmDbPool, name:&str,mobile_phone:&str) ->AnyhowResult<Vec<Self>>{...}
```

4. #[orm_update]

可选属性,可设置多个。

生成Self::orm_update_with开头的将更新记录方法。

有三个子属性:

* name
   * 生成的函数名称后缀
   * 如果name设置为tel,实际生成的方法名称为:orm_update_with_tel
* sql_set
   * 数据库update语句的set部分
* sql_where
   * 数据库update语句的where部分
   * 可选,未设置则为更新所有记录
* args
   * 生成方法的参数部分定义
   * 与实际函数定义的写法一致,如定义2个参数,格式为:arg1:type1,arg2:type2
   * 参数数量应与sql_set+sql_where定义的?参数数量一致
   * 实际生成的函数,会自动添加pool: &TinyOrmDbPool
* doc 
   * 生成方法的文档说明

例如:

```ignore
#[orm_update(
   name = "name_and_tel",
   sql_set = "user.name = ? ,user.mobile_phone = ?",
   sql_where = "id = ?",
   args = "id:u32,name:&str,mobile_phone:&str",
   doc = "根据id更新姓名和手机号",
)]
自动生成如下更新函数:
/// 根据id更新姓名和手机号
pub async orm_update_with_name_and_tel(pool: &TinyOrmDbPool, name: &str,mobile_phone: &str) -> AnyhowResult<()>{...}
```

5. #[orm_delete]

可选属性,可设置多个。

生成Self::orm_delete_with开头的删除记录方法。


有三个子属性:

* name
   * 生成的函数名称后缀
   * 如果name设置为tel,实际生成的方法名称为:orm_delete_with_tel
* sql_where
   * 数据库delete语句的where部分
   * 可选,未设置则为删除所有记录
* args
   * 生成方法的参数部分定义
   * 参数数量应与sql_where定义的?参数数量一致
   * 与实际函数定义的写法一致,如定义2个参数,格式为:arg1:type1,arg2:type2
   * 实际生成的函数,会自动添加pool: &TinyOrmDbPool
* doc 
   * 生成方法的文档说明

例如:

```ignore
#[orm_delete(
   name = "name_and_tel",
   sql_where = "user.name = ? and mobile_phone = ?"
   args = "name:&str,mobile_phone:&str",
   doc = "根据姓名和手机号删除用户"
)]
自动生成如下函数:
/// 根据姓名和手机号查询用户
pub async fn orm_delete_with_name_and_tel(pool: &TinyOrmDbPool, name:&str,mobile_phone:&str) ->AnyhowResult<()>{...}
```

6. #[orm_exist]

可选属性,可设置多个。

生成Self::orm_exist_with开头的删除记录方法。


有三个子属性:

* name
   * 生成的函数名称后缀
   * 如果name设置为tel,实际生成的方法名称为:orm_exist_with_tel
* sql_where
   * 数据库select语句的where部分
      * 实际生成的sql为:select count(1) from table_name where sql_where
   * 可选,未设置则为所有记录
* args
   * 生成方法的参数部分定义
   * 参数数量应与sql_where定义的?参数数量一致
   * 与实际函数定义的写法一致,如定义2个参数,格式为:arg1:type1,arg2:type2
   * 实际生成的函数,会自动添加pool: &TinyOrmDbPool
* doc 
   * 生成方法的文档说明

例如:

```ignore
#[orm_exist(
   name = "id",
   sql_where = "id = ?"
   args = "id:u32",
   doc = "根据id查询记录是否存在"
)]
自动生成如下函数:
/// 根据id查询记录是否存在
pub async fn orm_exist_with_name_and_tel(pool: &TinyOrmDbPool, id: u32) ->AnyhowResult<bool>
```

7. #[orm_join]

可选属性,可设置多个。

设置不与其他struct model关联join信息

有二个子属性:

* select_field
   * 从join表select数据库字段的对应sql语句
   * 该语句会与结构体自身对应数据库表的字段合并为select的选择字段清单
* join
   * 连接表的join语句

例如:

```rust
 #[orm_join(
    select_field="node.name AS node_name, organization.name AS org_name",
    join="JOIN node ON node.id = node_id JOIN organization ON organization.id = node.org_id",
 )]
 struct user {
     id:u32,
     name:String,
     /// 所属网点:连接到其他表
     node_id:String,
 }
 ```

# 应用于结构体字段的方法

1. #[orm_pk(name="id", auto="true")]

至少需要设置1个,可设置多个。

设置当前字段为主键字段

有两个子属性:

* name
   * 设置对应数据库表中的字段名称
   * 可选属性,如果未设置,数据库中的字段名称和结构体字段名称一致
* auto
   * 设置为true,则标识这是个自动增长的主键,在插入时不设置值
   * 可选属性

该属性会自动生成如下方法:

多个主键会自动汇聚为一个方法,并将主键设置为对应的函数方法的参数

* Self::orm_get_with_pk
   * 通过主键获取单条记录
   * 如果设置了多个主键,会将按照字段定义顺序,合并到该方法的参数中
   * 函数定义
      * pub async fn orm_get_with_pk(pool: &TinyOrmDbPool,主键字段1:字段类型1,主键字段2:字段类型2,...) ->  AnyhowResult<Self>
      * 结构体字段类型自动转换
          * Option<T> 自动转换为 T
          * String 自动转换为 &str
* Self::orm_delete_with_pk
   * 通过主键删除记录
   * 如果设置了多个主键,会将按照字段定义顺序,合并到该方法的参数中
   * 函数定义
      * pub async fn orm_delete_with_pk(pool: &TinyOrmDbPool,主键字段1:字段类型1,主键字段2:字段类型2,...) ->  AnyhowResult<()>
      * 结构体字段类型自动转换
          * Option<T> 自动转换为 T
          * String 自动转换为 &str
* self::orm_delete
   * 通过主键删除当前实例在数据库记录
   * 如果设置了多个主键,会将按照字段定义顺序,合并到该方法的参数中
   * 函数定义
      * pub async fn orm_delete(&self,pool: &TinyOrmDbPool) ->  AnyhowResult<Self>
* Self::orm_exist_with_pk
   * 通过主键查询记录是否存在
   * 如果设置了多个主键,会将按照字段定义顺序,合并到该方法的参数中
   * 函数定义
      * pub async fn orm_exists_with_pk(pool: &TinyOrmDbPool,主键字段1:字段类型1,主键字段2:字段类型2,...) ->  AnyhowResult<bool>
      * 结构体字段类型自动转换
          * Option<T> 自动转换为 T
          * String 自动转换为 &str
* Self::orm_exist
   * 通过主键查询当前实例在数据库记录是否存在
   * 如果设置了多个主键,会将按照字段定义顺序,合并到该方法的参数中
   * 函数定义
      * pub async fn orm_exists_with_pk(&self,pool: &TinyOrmDbPool) ->  AnyhowResult<bool>

# 示例

```ignore
struct TestUser {
  #[orm_pk(name="user_id",auto="true")]
  id:u32,
  #[orm_pk]
  tel:String,
}

//以上会自动生成如下方法
pub async fn orm_get_with_pk(pool: &TinyOrmDbPool,id:u32,tel:&str) ->  AnyhowResult<Self>{...}
pub async fn orm_delete_with_pk(pool: &TinyOrmDbPool,id:u32,tel:&str) ->  AnyhowResult<()>{...}
pub async fn orm_exist_with_pk(pool: &TinyOrmDbPool,id:u32,tel:&str) ->  AnyhowResult<bool>{...}
pub async fn orm_delete(self,pool: &TinyOrmDbPool) ->  AnyhowResult<bool>{...}
pub async fn orm_exist(self,pool: &TinyOrmDbPool) ->  AnyhowResult<bool>{...}

```

2. #[orm_join]

可选属性,可设置多个。

设置对应字段为关联其他表的join字段

有六个子属性:

* name
   * 设置对应数据库表中的字段名称
   * 可选属性,如果未设置,数据库中的字段名称和结构体字段名称一致
* select_field
   * 从join表select数据库字段的对应sql语句
   * 该语句会与结构体自身对应数据库表的字段合并为select的选择字段清单
* join
   * 连接表的join语句
* link_id
   * 字段的rust类型的对应连接字段
   * 例如,如某结构体User
      * 其中某一字段定义为:user_type:UserType,设置link_id="id"
      * 写入User表记录时,会自动读取self.user_type.id为对应数据库字段的值
* link_id_type
   * 连接字段的rust类型
   * 如以上UserType::id为u32类型,则link_id_type="u32"
* skip_method
   * 是否不自动生成相关join方法
   * true,则不自动生成

如为设置skip_method="true",该属性会自动生成如下方法:

* Self::orm_query_join_with_字段名称
   * 使用join字段查询记录
   * 函数定义
      * pub async fn orm_query_join_with_字段名称(pool: &TinyOrmDbPool,字段名称:字段类型) -> AnyhowResult<Vec<Self>>
      * 结构体字段类型自动转换
          * Option<T> 自动转换为 T
          * String 自动转换为 &str
* Self::orm_query_join_with_字段名称_{link_id名称}
   * 使用join字段的link_id查询记录
   * 函数定义
      * pub async fn orm_query_join_with_字段名称_{link_id名称}(pool: &TinyOrmDbPool,link_id名称:link_id_type名称) -> AnyhowResult<Vec<Self>>
* Self::orm_delete_join_with_字段名称
   * 使用join字段删除记录
   * 函数定义
      * pub async fn orm_delete_join_with_字段名称(pool: &TinyOrmDbPool,字段名称:字段类型) -> AnyhowResult<()>
      * 结构体字段类型自动转换
          * Option<T> 自动转换为 T
          * String 自动转换为 &str
* Self::orm_delete_join_with_字段名称_{link_id名称}
   * 使用join字段的link_id删除记录
   * 函数定义
      * pub async fn orm_delete_join_with_字段名称_{link_id名称}(pool: &TinyOrmDbPool,link_id名称:link_id_type名称) -> AnyhowResult<()>
* self::orm_update_join_with_字段名称
   * 使用当前实例的对应join字段.link_id保存到数据库中对应字段中
   * 函数定义
      * pub async fn orm_update_join_with_字段名称(&self,pool: &TinyOrmDbPool) -> AnyhowResult<()>

# 示例

```
struct User {
   #[orm_join(
      name="user_type_id", 
      select_field="user_type.name as user_type_name, user_type.template",
      join="JOIN user_type ON user_type.id = user_type_id",
      link_id="id",
      link_id_type="u32",
   )]
   user_type:UserType
}
// 会自动生成如下方法
pub async fn orm_query_join_with_user_type(pool: &TinyOrmDbPool,user_type:UserType) -> AnyhowResult<Vec<Self>>{...}
pub async fn orm_query_join_with_user_type_id(pool: &TinyOrmDbPool,query_value:u32) -> AnyhowResult<Vec<Self>>{...}
pub async fn orm_delete_join_with_user_type(pool: &TinyOrmDbPool,user_type:UserType) -> AnyhowResult<()>{...}
pub async fn orm_delete_join_with_user_type_id(pool: &TinyOrmDbPool,query_value:u32) -> AnyhowResult<()>{...}
pub async fn orm_update_join_with_user_type(&self,pool: &TinyOrmDbPool) -> AnyhowResult<()>{...}
```

3. #[orm_field(name = "custom_table_field_name")]

可选属性,可设置多个。

设置对应数据库表的字段名称

有1个子属性:

* name
   * 设置对应数据库表中的字段名称
   * 可选属性,如果未设置,数据库中的字段名称和结构体字段名称一致

4. #[orm_ignore]

可选属性,可设置多个。

默认情况下,结构体的每个字段均对应一个数据库表字段。
设置该属性后,可忽略对应字段,不对应数据库表的字段名称

5. #[orm_update]


生成更新数据库中的对应字段值的方法,方法定义如下:
pub async fn orm_update_字段名称(&self,pool: &TinyOrmDbPool) ->AnyhowResult<()>

6. #[orm_query]

* order_by
   * 数据库select语句的order by部分
   * 可选,未设置则为默认排序
   
生成使用该字段查询数据库记录的方法,方法定义如下:
* pub async fn orm_query_with_字段名称(pool: &TinyOrmDbPool,query_value:字段类型) -> AnyhowResult<Vec<Self>>
* 结构体字段类型自动转换
   * Option<T> 自动转换为 T
   * String 自动转换为 &str

# 示例

```rust
struct User {
   id:String,
   #[orm_query(
      order_by="user.id", 
   )]
   name:String,

   #[orm_query]
   sex:String,
   user_type:UserType,
}
```

7. #[orm_get]

生成使用该字段获取单条数据库记录的方法,方法定义如下:
* pub async fn orm_get_with_字段名称(pool: &TinyOrmDbPool,query_value:字段类型) -> AnyhowResult<Self>
* 结构体字段类型自动转换
   * Option<T> 自动转换为 T
   * String 自动转换为 &str

# 其他自动生成方法

1. self.orm_insert方法

将当前结构体实例数据插入到数据库表中,用于新增记录的保存

方法定义如下:
pub async fn orm_insert(&mut self, pool: &TinyOrmDbPool) -> AnyhowResult<()>

2. self.orm_update_all方法

将当前结构体实例数据更新到数据库表中,用于变更记录的保存

方法定义如下:
pub async fn orm_update_all(&self,pool: &TinyOrmDbPool) -> AnyhowResult<()>

2. #[derive(TinyOrmQuery)]

自动实现TinyOrmQuery trait宏


# 使用说明

1. 添加依赖

修改Cargo.toml,增加

```
[dependencies]
async-trait = "^0.1"
tiny_orm_macro_derive="^0.3"

[dependencies.tiny_orm_core]
version = "^0.3"
features = [
    "mysql",
]
```

2. 引入相关trait
   

```ignore
use tiny_orm_core::prelude::*;
```

1. 在结构体中使用派生属性

```
struct User{
    #[orm_pk(auto="true")]
    id: u32,
    name: String,
}
```

1. 使用自动生成的相关orm_方法进行数据库交互



# 示例代码

```
//! 数据库模型的测试
use sqlx::mysql::MySqlPoolOptions;
use sqlx::Row;
use tiny_orm_macro_derive::{TinyOrm, TinyOrmQuery};

use super::*;

#[derive(Debug, PartialEq, Eq)]
pub struct UserType {
    /// 类型编号
    pub id: Option<u32>,
    /// 类型名称
    pub name: String,
    /// 中断短信模板
    pub template: String,
}
impl UserType {
    /// 完整创建器
    ///
    /// # 参数说明
    ///
    /// * id 类型编号
    /// * name 类型名称
    /// * template 中断短信模板
    pub fn new(id: u32, name: &str, template: &str) -> Self {
        UserType {
            id: Some(id),
            name: name.into(),
            template: template.into(),
        }
    }
}
#[derive(Debug, PartialEq, Eq)]
pub struct Organization {
    /// 行号
    pub id: String,
    /// 机构名称
    pub name: String,
}

#[allow(dead_code)]
#[derive(TinyOrm, TinyOrmQuery, Debug, PartialEq, Eq)]
// 自动生成表格名称时附加前缀,生成表名称为:core_test_user
//#[orm_table_name_pref = "core"]
// 指定表名称,未指定则自动生成,规则结构名称转换为蛇形命名,如:test_user
#[orm_table_name = "user"]
/// 自动生成orm_query_with查询方法,生成后的函数定义如下
/// /// 根据姓名和手机号查询用户
/// pub async fn orm_query_with_name_and_tel(pool: &TinyOrmDbPool, name:&str,mobile_phone:&str) ->AnyhowResult<Vec<Self>> {
///     let sql = Self::DB_META.build_select_sql("name = ? and mobile_phone = ?");
///     let query = Self::db_query(&sql)
///         .bind(name)
///         .bind(mobile_phone);
///     Self::db_fetch_all(pool,query,Self::orm_row_map).await
/// }
#[orm_query(
    name = "name_and_tel",
    sql_where = "user.name = ? and mobile_phone = ?",
    args = "name:&str,mobile_phone:&str",
    doc = "根据姓名和手机号查询用户"
)]
/// 自动生成orm_delete_with删除方法,生成后的函数定义如下
/// /// 根据姓名和手机号删除用户
/// pub async fn orm_delete_with_name_and_tel(pool: &TinyOrmDbPool, name:&str,mobile_phone:&str) ->AnyhowResult<Vec<Self>> {
///     let sql = Self::DB_META.build_delete_sql("name = ? and mobile_phone = ?");
///     let query = Self::db_query(&sql)
///         .bind(name)
///         .bind(mobile_phone);
///     Self::db_execute(pool, query).await
/// }
#[orm_delete(
    name = "name_and_tel",
    sql_where = "user.name = ? and mobile_phone = ?",
    args = "name:&str,mobile_phone:&str",
    doc = "根据姓名和手机号删除用户"
)]
/// 生成orm_exist_with_name方法
#[orm_exist(
    name = "name",
    sql_where = "user.name like ?",
    args = "name:&str",
    doc = "根据姓名查询用户是否存在"
)]
/// 生成orm_update_with_name方法
#[orm_update(
    name = "name_and_tel",
    sql_set = "name = ? , mobile_phone = ?",
    sql_where = "id = ?",
    args = "name:&str,mobile_phone:&str,id:u32",
    doc = "根据id更新姓名和手机号"
)]
pub struct TestUser {
    /// 类型编号
    /// 生成的orm_get_by_pk函数参数中,id转换为u32
    /// 会自动将多个pk字段合并为一个方法的参数,生成如下方法
    /// /// orm_pk自动实现:通过主键获取记录
    /// pub async fn orm_get_by_pk(pool: &TinyOrmDbPool, id:u32,mobile_phone:&str) -> AnyhowResult<Self>{
    ///   let sql = Self::DB_META.build_select_sql(#pk_where_sql);
    ///   let query = Self::db_query(&sql)
    ///      .bind(id)
    ///      .bind(mobile_phone);
    ///   Self::db_fetch_one(pool, query, Self::orm_row_map).await
    ///      .with_context(|| "根据主键获取记录出错!")
    /// }
    /// /// orm_pk自动实现:通过主键删除记录
    /// pub async fn orm_delete_by_pk(pool: &TinyOrmDbPool,id:u32,mobile_phone:&str) -> AnyhowResult<()>{
    ///   let sql = Self::DB_META.build_delete_sql(#pk_where_sql);
    ///   let query = Self::db_query(&sql)
    ///      .bind(id)
    ///      .bind(mobile_phone);
    ///   Self::db_execute(pool, query).await
    ///     .with_context(|| "根据主键删除记录出错!")?;
    ///   Ok(())
    /// }
    /// /// orm_pk自动实现:通过主键删除当前记录
    /// pub async fn orm_delete(&self,pool: &TinyOrmDbPool) -> AnyhowResult<()>{
    ///     Self::orm_delete_by_pk(pool,self.id.unwrap(),self.mobile_phone.as_ref()).await
    /// }
    /// /// orm_pk自动实现:通过主键查询记录是否存在
    /// pub async fn orm_exist_with_pk(pool: &TinyOrmDbPool,,id:u32,mobile_phone:&str) -> AnyhowResult<bool>{
    ///     let sql = Self::DB_META.build_exist_sql(Self::DB_META.pk_where_sql);
    ///     let row: (i64,) = sqlx::query_as(&sql)
    ///         .bind(id)
    ///         .bind(mobile_phone)
    ///         .fetch_one(pool)
    ///         .await
    ///         .with_context(|| "通过主键查询记录是否存在时出错!")?;
    ///     Ok(row.0 > 0)
    /// }
    /// /// orm_pk自动实现:通过主键查询当前记录是否存在
    /// pub async fn orm_exist(&self,pool: &TinyOrmDbPool) -> AnyhowResult<()>{
    ///     Self::orm_exist_with_pk(pool,self.id.unwrap(),self.mobile_phone.as_ref()).await
    /// }
    /// 只能设置一个auto主键
    #[orm_pk(name = "id", auto = "true")]
    /// insert时自动生成id
    pub id: Option<u32>,
    /// 类型名称
    // 生成orm_query_with_name方法
    #[orm_query]
    pub name: String,
    /// 手机号码
    /// 生成的orm_get_by_pk函数参数中,mobile_phone会自动把String转换为&str
    #[orm_pk]
    pub mobile_phone: String,
    /// 密码
    /// 重命名数表字段名称,否则与字段名称一致
    #[orm_field(name = "password")]
    // 生成self.orm_update_password方法
    #[orm_update]
    password: String,
    /// 用户类型
    /// 定义join字段和join sql
    /// 自动生成Self::orm_query_join_with_user_type,Self::orm_delete_join_with_user_type,self.orm_update_join_with_user_type方法
    #[orm_join(
        name="user_type_id", // 重命名表字段名称
        select_field="user_type.name as user_type_name, user_type.template",
        join="JOIN user_type ON user_type.id = user_type_id",
        link_id="id",//update 时保存值使用的字段id,如:user_type.id
        link_id_type="u32"
    )]
    #[allow(unused_variables)]
    pub user_type: UserType,
    /// 所属机构
    /// 自动生成Self::orm_query_join_with_org,Self::orm_delete_join_with_org,self.orm_update_join_with_org方法
    #[orm_join(
        name="org_id", // 重命名表字段名称
        select_field="organization.name as org_name",
        join="JOIN organization ON organization.id = org_id",
        link_id="id",//update 时保存值使用的字段id,如:org.id
        link_id_type="&str"
    )]
    pub org: Organization,
    /// 忽略字段,不在数据库中对应
    #[orm_ignore]
    pub ignore_field: u32,
}

impl TestUser {
    /// 完整创建器
    ///
    /// # 参数说明
    ///
    /// * id 编号
    /// * name 姓名
    /// * mobile_phone 手机
    /// * password 密码
    /// * user_type 用户类型
    /// * org 对应机构
    pub fn new(
        id: u32,
        name: &str,
        mobile_phone: &str,
        password: &str,
        user_type: UserType,
        org: Organization,
    ) -> Self {
        Self {
            id: Some(id),
            name: name.into(),
            mobile_phone: mobile_phone.into(),
            password: password.into(),
            user_type,
            org,
            ignore_field: 0,
        }
    }
    /// 完整创建器
    ///
    /// # 参数说明
    ///
    /// * id 编号
    /// * name 姓名
    /// * mobile_phone 手机
    /// * password 密码
    /// * user_type 用户类型
    /// * org 对应机构
    pub fn new_no_id(
        name: &str,
        mobile_phone: &str,
        password: &str,
        user_type: UserType,
        org: Organization,
    ) -> Self {
        Self {
            id: None,
            name: name.into(),
            mobile_phone: mobile_phone.into(),
            password: password.into(),
            user_type,
            org,
            ignore_field: 0,
        }
    }
}
/// 实现数据获取接口
impl TinyOrmData for TestUser {
    /// 将sql返回数据映射为TestUser
    fn orm_row_map(row: TinyOrmSqlRow) -> Self {
        TestUser::new(
            row.get::<u32, _>("id"),
            row.get("name"),
            row.get("mobile_phone"),
            row.get("password"),
            UserType::new(
                row.get::<u32, _>("user_type_id"),
                row.get("user_type_name"),
                row.get("template"),
            ),
            Organization {
                id: row.get("org_id"),
                name: row.get("org_name"),
            },
        )
    }
}

async fn get_pool() -> AnyhowResult<TinyOrmDbPool> {
    let user_name = "net_guard";
    let password = "some pass";
    let ip = "localhost";
    let port = 3306;
    let db_name = "abc_net_guard";
    let pool = MySqlPoolOptions::new()
        .max_connections(1)
        .connect(&format!(
            "mysql://{}:{}@{}:{}/{}",
            user_name, password, ip, port, db_name
        ))
        .await?;
    Ok(pool)
}

/// .测试SQL生成
#[test]
fn test_user() {
    println!("user sql : \n{}", TestUser::DB_META.select_sql);
}

/// .测试SQL生成
#[test]
fn test_db_query() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let data = TestUser::orm_get_all(&pool).await.unwrap();
            dbg!(data);
        });
}

/// 测试根据姓名和手机获取用户
#[test]
fn test_orm_query_with_name_and_mobile() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let user = TestUser::orm_query_with_name_and_tel(&pool, "张三", "1850703xxxx")
                .await
                .unwrap();
            dbg!(user);
        });
}
/// 测试根据主键获取获取用户
#[test]
fn test_orm_get_with_pk() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let user = TestUser::orm_get_with_pk(&pool, 2, "1387038xxxx")
                .await
                .unwrap();
            dbg!(user);
        });
}

/// 测试根据主键删除用户
#[test]
fn test_orm_delete() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let user = TestUser::orm_get_with_pk(&pool, 15, "1867930xxxx")
                .await
                .unwrap();
            user.orm_delete(&pool).await.unwrap();
            dbg!(user);
        });
}

/// 测试根据个性删除,根据名称和手机号删除用户
#[test]
fn test_orm_delete_with_name_and_tel() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            TestUser::orm_delete_with_name_and_tel(&pool, "盛XX", "1507031xxxx")
                .await
                .unwrap();
        });
}
/// 测试根据姓名和手机获取用户
#[test]
fn test_orm_query_with_name() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let user = TestUser::orm_query_with_name(&pool, "张三").await.unwrap();
            dbg!(user);
        });
}
/// 测试是否存在
#[test]
fn test_orm_exist_with_pk() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let is_exist = TestUser::orm_exist_with_pk(&pool, 8, "18607031111")
                .await
                .unwrap();
            dbg!(is_exist);
        });
}

/// 测试是否存在
#[test]
fn test_orm_exist_with_name() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let is_exist = TestUser::orm_exist_with_name(&pool, "王xx")
                .await
                .unwrap();
            dbg!(is_exist);
        });
}
/// 测试根据姓名和手机获取用户
#[test]
fn test_orm_update_with_name_and_tel() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let user = TestUser::orm_update_with_name_and_tel(&pool, "张三", "18507032200", 4)
                .await
                .unwrap();
            dbg!(user);
        });
}
/// 测试filter
#[test]
fn test_db_filter() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let sql = TestUser::DB_META.build_select_sql("user.name like  ? ");
            println!("{sql}");
            let key = String::from("%李%");
            let data = TestUser::orm_filter_with_sql(&pool, &sql, &key)
                .await
                .unwrap();
            dbg!(data);
        });
}

/// 测试join字段查询和更新
#[test]
fn test_orm_query_join_with_user_type() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let user_type = UserType::new(1, "支行", "");
            let mut data = TestUser::orm_query_join_with_user_type(&pool, user_type)
                .await
                .unwrap();
            let mut ygl = data.pop().unwrap();
            let user_type_2 = UserType::new(2, "", "");
            ygl.user_type = user_type_2;
            ygl.orm_update_join_with_user_type(&pool).await.unwrap();
            dbg!(ygl);
        });
}

/// 测试join字段查询和更新
#[test]
fn test_orm_query_join_with_org_id() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let data = TestUser::orm_query_join_with_org_id(&pool, "14H700")
                .await
                .unwrap();
            dbg!(data);
        });
}
/// 测试join字段查询和更新
#[test]
fn test_orm_delete_join_with_user_type() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let user_type = UserType::new(2, "支行", "");
            TestUser::orm_delete_join_with_user_type(&pool, user_type)
                .await
                .unwrap();
        });
}
/// 测试更新所有信息
#[test]
fn test_orm_update_all() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let mut user = TestUser::orm_get_with_pk(&pool, 2, "13807931111")
                .await
                .unwrap();
            let user_type = UserType::new(2, "支行", "");
            user.name = "王2".into();
            user.user_type = user_type;
            user.orm_update_all(&pool).await.unwrap();
            dbg!(user);
        });
}

/// 测试插入信息
#[test]
fn test_orm_insert() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let org = Organization {
                id: "14H700".into(),
                name: "上饶分行".into(),
            };
            let user_type = UserType::new(2, "管理员", "");
            let mut user = TestUser::new_no_id("李四", "1850703xxxx", "sss", user_type, org);
            user.orm_insert(&pool).await.unwrap();
            dbg!(user);
        });
}
/// 测试根据姓名和手机获取用户
#[test]
fn test_orm_update_password() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let pool = get_pool().await.unwrap();
            let mut user:TestUser = TestUser::orm_query_with_name(&pool, "张三").await.unwrap().pop().unwrap();
            user.password="this is a pass".into();
            user.orm_update_password(&pool).await.unwrap();
            dbg!(user);
        });
}

```