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
use convert_case::{Case, Casing};
use proc_macro::TokenStream;
use quote::quote;
use std::collections::HashMap;
use syn::{parse_macro_input, Data, DeriveInput, Field, Fields, Meta};
use crate::api_model_struct::ApiModel;
pub fn sql_model_impl(attr: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as DeriveInput);
let name = &input.ident;
let repo_name = syn::Ident::new(&format!("{name}Repository"), name.span());
let data = match &input.data {
Data::Struct(data) => data,
_ => panic!("api_mode can only be applied to structs"),
};
let model = ApiModel::new(&input, attr.clone());
if model.database.is_none() {
return quote! {}.into();
}
let fields = &data.fields;
let attrs = parse_sql_model(attr);
let table_name = if let Some(SqlModel::Table(ref table_name)) = attrs.get(&SqlModelKey::Table) {
table_name.to_string()
} else {
format!("{}s", name.to_string().to_case(Case::Lower))
};
let rename = if let SqlModel::Rename(rename) = attrs
.get(&SqlModelKey::Rename)
.unwrap_or(&SqlModel::Rename(Case::Snake))
{
rename
} else {
todo!()
};
let create_table_function = create_table_tokens(&table_name, rename.clone(), &fields);
let drop_table_query = syn::LitStr::new(
&format!("DROP TABLE IF EXISTS {};", table_name),
proc_macro2::Span::call_site(),
);
let insert = model.insert_function();
let find_one = model.find_one_function();
let find = model.find_function();
let output = quote! {
impl #name {
pub fn get_repository(pool: sqlx::Pool<sqlx::Postgres>) -> #repo_name {
#repo_name::new(pool)
}
}
#[derive(Debug, Clone)]
pub struct #repo_name {
pool: sqlx::Pool<sqlx::Postgres>,
}
impl #repo_name {
pub fn new(pool: sqlx::Pool<sqlx::Postgres>) -> Self {
Self { pool }
}
#create_table_function
#insert
#find_one
#find
pub async fn drop_table(&self) -> std::result::Result<(), sqlx::Error> {
sqlx::query(#drop_table_query)
.execute(&self.pool)
.await?;
Ok(())
}
}
};
tracing::debug!("Generated code: {}", output.to_string());
output.into()
}
#[derive(Debug, Eq, PartialEq, Hash)]
pub enum SqlAttributeKey {
PrimaryKey,
SqlType,
ManyToMany,
ManyToOne,
OneToMany,
Unique,
Auto,
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum AutoOperation {
Insert,
Update,
}
#[derive(Debug)]
pub enum SqlAttribute {
PrimaryKey,
SqlType(String),
ManyToMany {
table_name: String,
foreign_table_name: String,
foreign_key: String,
foreign_key_type: String,
},
ManyToOne {
table_name: String,
foreign_key: String,
foreign_key_type: String,
},
OneToMany {
#[allow(dead_code)]
table_name: String,
foreign_key: String,
},
Unique,
Auto(Vec<AutoOperation>),
}
#[derive(Debug)]
enum OpenedOffset {
None,
Type,
ManyToMany,
ManyToOne,
OneToMany,
ForeignTableName,
ForeignKey,
ForeignKeyType,
Auto,
}
#[derive(Debug)]
pub struct SqlAttributes {
pub attrs: HashMap<SqlAttributeKey, SqlAttribute>,
}
impl SqlAttributes {
fn is_primary_key(&self) -> bool {
self.attrs.contains_key(&SqlAttributeKey::PrimaryKey)
}
fn to_primary_type(&self, var_type: &syn::Type) -> Option<String> {
match var_type {
syn::Type::Path(ref type_path) => {
let type_ident = type_path.path.segments.last().unwrap().ident.to_string();
let type_str = if let Some(SqlAttribute::SqlType(type_str)) =
self.attrs.get(&SqlAttributeKey::SqlType)
{
type_str.to_string()
} else {
match type_ident.as_str() {
"u64" | "i64" => "BIGINT".to_string(),
"String" => "TEXT".to_string(),
"bool" => "BOOLEAN".to_string(),
"i32" => "INTEGER".to_string(),
"f64" => "DOUBLE PRECISION".to_string(),
_ => return None,
}
};
let type_str = if self.attrs.contains_key(&SqlAttributeKey::PrimaryKey) {
format!("{} PRIMARY KEY", type_str)
} else {
type_str
};
Some(type_str)
}
_ => {
tracing::debug!("field type: {:?}", var_type);
None
}
}
}
fn to_field_name(&self, var_name: &str, case: Case) -> String {
format!("{}", var_name.to_case(case))
}
fn to_field_type(&self, var_type: &syn::Type) -> Option<String> {
if self.attrs.contains_key(&SqlAttributeKey::ManyToMany)
|| self.attrs.contains_key(&SqlAttributeKey::ManyToOne)
{
return None;
}
match var_type {
syn::Type::Path(ref type_path) => {
let type_ident = type_path.path.segments.last().unwrap().ident.to_string();
let type_str = if let Some(SqlAttribute::SqlType(type_str)) =
self.attrs.get(&SqlAttributeKey::SqlType)
{
type_str.to_string()
} else if self.attrs.contains_key(&SqlAttributeKey::PrimaryKey) {
"BIGINT".to_string()
} else {
match type_ident.as_str() {
"u64" | "i64" => "BIGINT NOT NULL".to_string(),
"String" => "TEXT NOT NULL".to_string(),
"bool" => "BOOLEAN NOT NULL".to_string(),
"u32" | "i32" => "INTEGER NOT NULL".to_string(),
"f64" => "DOUBLE PRECISION NOT NULL".to_string(),
"Option<u64>" | "Option<i64>" => "BIGINT".to_string(),
"Option<String>" => "TEXT".to_string(),
"Option<bool>" => "BOOLEAN".to_string(),
"Option<i32>" => "INTEGER".to_string(),
"Option<f64>" => "DOUBLE PRECISION".to_string(),
_ => return None,
}
};
let type_str = if self.attrs.contains_key(&SqlAttributeKey::PrimaryKey) {
if self.attrs.contains_key(&SqlAttributeKey::SqlType) {
format!("{} PRIMARY KEY", type_str)
} else {
format!("{} PRIMARY KEY GENERATED ALWAYS AS IDENTITY", type_str)
}
} else {
type_str
};
let type_str = if self.attrs.contains_key(&SqlAttributeKey::Unique) {
format!("{} UNIQUE", type_str)
} else {
type_str
};
Some(type_str)
}
_ => {
tracing::debug!("field type: {:?}", var_type);
None
}
}
}
fn get_field_query(&self, var_name: &str, case: Case, var_type: &syn::Type) -> Option<String> {
let name = self.to_field_name(var_name, case);
if let Some(SqlAttribute::ManyToOne {
table_name,
foreign_key,
foreign_key_type,
}) = self.attrs.get(&SqlAttributeKey::ManyToOne)
{
Some(format!(
"{} {} NOT NULL, FOREIGN KEY ({}) REFERENCES {} ({}) ON DELETE CASCADE",
// Foreign field
name,
foreign_key_type,
// Foreign key
name,
table_name,
foreign_key.to_case(case),
))
} else if let Some(SqlAttribute::OneToMany { .. }) =
self.attrs.get(&SqlAttributeKey::OneToMany)
{
None
} else {
self.to_field_type(var_type)
.map(|field_type| format!("{} {}", name, field_type))
}
}
fn to_relation_field_type(&self, var_type: &syn::Type) -> Option<String> {
match var_type {
syn::Type::Path(ref type_path) => {
let type_ident = type_path.path.segments.last().unwrap().ident.to_string();
let type_str = if let Some(SqlAttribute::SqlType(type_str)) =
self.attrs.get(&SqlAttributeKey::SqlType)
{
type_str.to_string()
} else {
match type_ident.as_str() {
"u64" | "i64" => "BIGINT NOT NULL".to_string(),
"String" => "TEXT NOT NULL".to_string(),
"bool" => "BOOLEAN NOT NULL".to_string(),
"i32" => "INTEGER NOT NULL".to_string(),
"f64" => "DOUBLE PRECISION NOT NULL".to_string(),
"Option<u64>" | "Option<i64>" => "BIGINT".to_string(),
"Option<String>" => "TEXT".to_string(),
"Option<bool>" => "BOOLEAN".to_string(),
"Option<i32>" => "INTEGER".to_string(),
"Option<f64>" => "DOUBLE PRECISION".to_string(),
_ => return None,
}
};
Some(type_str)
}
_ => {
tracing::debug!("field type: {:?}", var_type);
None
}
}
}
fn get_additional_query(
&self,
this_table_name: &str,
this_primary_key_name: &str,
this_primary_key_type: &str,
var_name: &str,
var_type: &syn::Type,
case: Case,
) -> Vec<String> {
tracing::debug!("additional query for {var_name}");
let mut query = vec![];
tracing::debug!("attrs {:?}", self.attrs);
let this_primary_key_type = this_primary_key_type.replace("PRIMARY KEY", "");
if let Some(SqlAttribute::ManyToMany {
table_name,
foreign_table_name,
foreign_key: foreign_primary_key,
foreign_key_type,
}) = self.attrs.get(&SqlAttributeKey::ManyToMany)
{
tracing::debug!("additional query for many to many relation: {var_name}");
let this_key = format!("{}_{}", this_table_name, this_primary_key_name).to_case(case);
let foreign_pk =
format!("{}_{}", foreign_table_name, foreign_primary_key).to_case(case);
query.push(format!(
"CREATE TABLE IF NOT EXISTS {} ({} {} NOT NULL, {} {} NOT NULL, {} PRIMARY KEY ({}, {}), FOREIGN KEY ({}) REFERENCES {} ({}) ON DELETE CASCADE, FOREIGN KEY ({}) REFERENCES {} ({}) ON DELETE CASCADE); CREATE INDEX IF NOT EXISTS idx_{}_{} ON {}({}); CREATE INDEX IF NOT EXISTS idx_{}_{} ON {}({});",
// Table name for many to many relation
table_name,
// key for this table
this_key,
this_primary_key_type,
// key for other table
foreign_pk,
foreign_key_type,
// Additionaly fields
match self.to_relation_field_type(var_type) {
Some(field_type) =>
format!("{} {},", self.to_field_name(var_name, case), &field_type),
None => "".to_string(),
},
// Composited primary key
this_key,
foreign_pk,
// Foreign key for this table key
this_key,
this_table_name,
this_primary_key_name.to_case(case),
// Foreign key for other table
foreign_pk,
foreign_table_name,
foreign_primary_key.to_case(case),
// Index for this table key
table_name,
this_key.to_case(Case::Snake),
table_name,
this_key,
// Index for foreign table key
table_name,
foreign_pk.to_case(Case::Snake),
table_name,
foreign_pk
));
}
if let Some(SqlAttribute::ManyToOne {
table_name: foreign_table,
foreign_key,
..
}) = self.attrs.get(&SqlAttributeKey::ManyToOne)
{
tracing::debug!("additional query for many to one relation: {var_name}");
query.push(format!(
"CREATE INDEX idx_{}_{} ON {}({});",
// index name
foreign_table,
foreign_key.to_case(Case::Snake),
// indexing field
foreign_table,
foreign_key.to_case(case),
));
}
if let Some(SqlAttribute::Auto(v)) = self.attrs.get(&SqlAttributeKey::Auto) {
tracing::debug!("additional query for auto: {var_name}");
if v.contains(&AutoOperation::Update) {
query.push(format!(
r#"DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_trigger
WHERE tgname = 'trigger_{}_set_timestamps'
AND tgrelid = '{}'::regclass
) THEN
CREATE TRIGGER trigger_{}_set_timestamps
BEFORE INSERT OR UPDATE ON {}
FOR EACH ROW
EXECUTE FUNCTION set_timestamps();
END IF;
END $$"#,
this_table_name, this_table_name, this_table_name, this_table_name,
));
}
}
query
}
}
pub fn parse_field_attr(field: &Field) -> SqlAttributes {
let mut field_attrs = HashMap::new();
let name = field
.ident
.clone()
.unwrap()
.to_string()
.to_case(Case::Snake);
for attr in &field.attrs {
if let Meta::List(meta_list) = attr.meta.clone() {
if meta_list.path.is_ident("api_model") {
let mut opened = OpenedOffset::None;
let mut relation = None;
for nested in meta_list.tokens.clone() {
if let proc_macro2::TokenTree::Ident(iden) = nested {
let id = iden.to_string();
match id.as_str() {
"primary_key" => {
field_attrs
.insert(SqlAttributeKey::PrimaryKey, SqlAttribute::PrimaryKey);
}
"unique" => {
field_attrs.insert(SqlAttributeKey::Unique, SqlAttribute::Unique);
}
"type" => {
opened = OpenedOffset::Type;
}
"many_to_many" => {
opened = OpenedOffset::ManyToMany;
}
"many_to_one" => {
opened = OpenedOffset::ManyToOne;
}
"one_to_many" => {
opened = OpenedOffset::OneToMany;
}
"foreign_key" => {
opened = OpenedOffset::ForeignKey;
}
"foreign_table_name" => {
opened = OpenedOffset::ForeignTableName;
}
"foreign_key_type" => {
opened = OpenedOffset::ForeignKeyType;
}
"auto" => {
opened = OpenedOffset::Auto;
}
_ => match opened {
OpenedOffset::Type => {
field_attrs.insert(
SqlAttributeKey::SqlType,
SqlAttribute::SqlType(id),
);
}
OpenedOffset::ManyToMany => {
field_attrs.insert(
SqlAttributeKey::ManyToMany,
SqlAttribute::ManyToMany {
table_name: id,
foreign_table_name: "".to_string(),
foreign_key: "id".to_string(),
foreign_key_type: "TEXT".to_string(),
},
);
relation = Some(SqlAttributeKey::ManyToMany);
tracing::debug!("many_to_many: {name}");
}
OpenedOffset::ManyToOne => {
field_attrs.insert(
SqlAttributeKey::ManyToOne,
SqlAttribute::ManyToOne {
table_name: id,
foreign_key: "id".to_string(),
foreign_key_type: "TEXT".to_string(),
},
);
relation = Some(SqlAttributeKey::ManyToOne);
tracing::debug!("many_to_one: {name}");
}
OpenedOffset::OneToMany => {
field_attrs.insert(
SqlAttributeKey::OneToMany,
SqlAttribute::OneToMany {
table_name: id,
foreign_key: "id".to_string(),
},
);
relation = Some(SqlAttributeKey::OneToMany);
tracing::debug!("one_to_many: {name}");
}
OpenedOffset::ForeignKey => match relation {
Some(SqlAttributeKey::ManyToOne) => {
field_attrs.get_mut(&SqlAttributeKey::ManyToOne).map(
|attr| {
if let SqlAttribute::ManyToOne {
ref mut foreign_key,
..
} = attr
{
*foreign_key = id
}
},
);
}
Some(SqlAttributeKey::ManyToMany) => {
field_attrs.get_mut(&SqlAttributeKey::ManyToMany).map(
|attr| {
if let SqlAttribute::ManyToMany {
foreign_key: ref mut foreign_primary_key,
..
} = attr
{
*foreign_primary_key = id
}
},
);
}
Some(SqlAttributeKey::OneToMany) => {
field_attrs.get_mut(&SqlAttributeKey::OneToMany).map(
|attr| {
if let SqlAttribute::OneToMany {
ref mut foreign_key,
..
} = attr
{
*foreign_key = id
}
},
);
}
_ => {
tracing::error!("foreign_key must be defined after many_to_many, one_to_many: {name}");
}
},
OpenedOffset::ForeignTableName => match relation {
Some(SqlAttributeKey::ManyToMany) => {
field_attrs.get_mut(&SqlAttributeKey::ManyToMany).map(
|attr| {
if let SqlAttribute::ManyToMany {
ref mut foreign_table_name,
..
} = attr
{
*foreign_table_name = id
}
},
);
}
_ => {
tracing::error!("foreign_table_name must be defined after many_to_many: {name}");
}
},
OpenedOffset::ForeignKeyType => match relation {
Some(SqlAttributeKey::ManyToOne) => {
field_attrs.get_mut(&SqlAttributeKey::ManyToOne).map(
|attr| {
if let SqlAttribute::ManyToOne {
ref mut foreign_key_type,
..
} = attr
{
*foreign_key_type = id
}
},
);
}
Some(SqlAttributeKey::ManyToMany) => {
field_attrs.get_mut(&SqlAttributeKey::ManyToMany).map(
|attr| {
if let SqlAttribute::ManyToMany {
ref mut foreign_key_type,
..
} = attr
{
*foreign_key_type = id
}
},
);
}
_ => {
tracing::error!("foreign_key_type must be defined after many_to_many, many_to_one: {name}");
}
},
OpenedOffset::Auto => {
let auto = match id.as_str() {
"insert" => AutoOperation::Insert,
"update" => AutoOperation::Update,
_ => {
tracing::error!("invalid auto operation: {id}");
continue;
}
};
field_attrs
.entry(SqlAttributeKey::Auto)
.or_insert_with(|| SqlAttribute::Auto(vec![]));
if let Some(SqlAttribute::Auto(ref mut operations)) =
field_attrs.get_mut(&SqlAttributeKey::Auto)
{
operations.push(auto);
}
}
OpenedOffset::None => {}
},
}
} else if let proc_macro2::TokenTree::Group(group) = nested {
match opened {
OpenedOffset::Auto => {
for nested in group.stream() {
if let proc_macro2::TokenTree::Ident(iden) = nested {
let id = iden.to_string();
field_attrs
.entry(SqlAttributeKey::Auto)
.or_insert_with(|| SqlAttribute::Auto(vec![]));
if let Some(SqlAttribute::Auto(ref mut operations)) =
field_attrs.get_mut(&SqlAttributeKey::Auto)
{
operations.push(match id.as_str() {
"insert" => AutoOperation::Insert,
"update" => AutoOperation::Update,
_ => {
tracing::error!("invalid auto operation: {id}");
continue;
}
});
}
}
}
opened = OpenedOffset::None;
}
_ => {}
}
} else if let proc_macro2::TokenTree::Punct(punct) = nested {
if punct.to_string().as_str() == "," {
opened = OpenedOffset::None;
}
}
}
}
}
}
SqlAttributes { attrs: field_attrs }
}
fn create_table_tokens(table_name: &str, case: Case, fields: &Fields) -> proc_macro2::TokenStream {
let mut create_query_fields = vec![];
let mut additional_queries = vec![];
let fields = if let Fields::Named(named_fields) = fields {
named_fields.named.clone()
} else {
return quote! {};
};
let mut primary_key_name = "id".to_string().to_case(case);
let mut primary_key_type = "TEXT".to_string();
for field in fields {
let field = field.clone();
let attrs = parse_field_attr(&field);
let field_name = field.ident.unwrap();
let field_type = field.ty;
if attrs.is_primary_key() {
primary_key_name = field_name.to_string().to_case(case);
primary_key_type = attrs
.to_primary_type(&field_type)
.unwrap_or_else(|| "TEXT".to_string());
}
match attrs.get_field_query(&field_name.to_string(), case, &field_type) {
Some(query) => {
create_query_fields.push(query);
}
None => {}
}
additional_queries.extend(attrs.get_additional_query(
table_name,
&primary_key_name,
&primary_key_type,
&field_name.to_string(),
&field_type,
case,
));
}
// let additional_queries = additional_queries.join("####");
// let queries = syn::LitStr::new(&additional_queries, proc_macro2::Span::call_site());
let queries: Vec<syn::LitStr> = additional_queries
.iter()
.map(|item| syn::LitStr::new(item, proc_macro2::Span::call_site()))
.collect();
let q = format!(
"CREATE TABLE IF NOT EXISTS {} ({});",
table_name,
create_query_fields.join(","),
);
let create_query_output = syn::LitStr::new(&q, proc_macro2::Span::call_site());
tracing::debug!("create table query: {}", q);
quote! {
pub async fn create_table(&self) -> std::result::Result<(), sqlx::Error> {
sqlx::query(#create_query_output).execute(&self.pool).await?;
for query in [#(#queries),*] {
tracing::debug!("Execute queries: {}", query);
sqlx::query(query).execute(&self.pool).await?;
}
Ok(())
}
}
}
#[derive(Debug, Eq, PartialEq, Hash)]
pub enum SqlModelKey {
Table,
Rename,
}
pub enum SqlModel {
Table(String),
Rename(Case),
}
pub fn parse_sql_model(attr: TokenStream) -> HashMap<SqlModelKey, SqlModel> {
let attr_args = attr.to_string();
let mut models = HashMap::new();
for arg in attr_args.split(',') {
let parts: Vec<&str> = arg.split('=').collect();
if parts.len() == 2 {
let key = parts[0].trim();
let value = parts[1].trim().trim_matches('"');
match key {
"table" => {
models.insert(SqlModelKey::Table, SqlModel::Table(value.to_string()));
}
"rename" => {
models.insert(
SqlModelKey::Rename,
match value {
"upcase" => SqlModel::Rename(Case::UpperSnake),
"camel" => SqlModel::Rename(Case::Camel),
"pascal" | "uppercamel" => SqlModel::Rename(Case::Pascal),
"snake" | "underscore" => SqlModel::Rename(Case::Snake),
"kebab" => SqlModel::Rename(Case::Kebab),
_ => {
panic!("invalid rename value {}", value);
}
},
);
}
_ => {}
}
}
}
models
}