prkorm 0.3.0

A procedural macro that simplifies the creation of mysql queries for fields in your Rust structs. It comes with SELECT, INSERT, UPDATE, DELETE operations with JOINS, SUBQUERIES and other compled clauses
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
use proc_macro::TokenStream;
use quote::quote;
use syn::{
    parse_macro_input,  Data, DeriveInput, Fields,
    Ident, LitStr,
};



#[proc_macro_derive(Table, attributes(table_name, primary_key))]
pub fn table_derive(input: TokenStream) -> TokenStream {
    // Parse the input tokens into a syntax tree.
    let ast = parse_macro_input!(input as DeriveInput);

    let struct_name = &ast.ident;
    let builder = Ident::new(&format!("{}SelectBuilder", struct_name), struct_name.span());
    let insert_builder = Ident::new(&format!("{}InsertBuilder", struct_name), struct_name.span());
    let update_builder = Ident::new(&format!("{}UpdateBuilder", struct_name), struct_name.span());
    let delete_builder = Ident::new(&format!("{}DeleteBuilder", struct_name), struct_name.span());

    let fields = match &ast.data {
        Data::Struct(data) => match &data.fields {
            Fields::Named(named_fields) => &named_fields.named,
            _ => return quote! {}.into(),
        },
        _ => return quote! {}.into(),
    };

    // Extract the "table_name" attribute if present.
    let table_name_attr = ast.attrs.iter().find(|attr| {
        if let Some(ident) = attr.path().get_ident() {
            ident == "table_name"
        } else {
            false
        }
    });

    // Extract the value of the "table_name" attribute, if present.
    let table: Option<String> = if let Some(attr) = table_name_attr {
        if let Ok(lit) = attr.parse_args::<LitStr>() {
            Some(lit.value())
        } else {
            None
        }
    } else {
        None
    };

    // Extract the "primary_key" attribute if present.
    let primary_key_attr = ast.attrs.iter().find(|attr| {
        if let Some(ident) = attr.path().get_ident() {
            ident == "primary_key"
        } else {
            false
        }
    });

    // Extract the value of the "table_name" attribute, if present.
    let primary_key_var = if let Some(attr) = primary_key_attr {
        if let Ok(lit) = attr.parse_args::<LitStr>() {
            lit.value()
        } else {
            String::new()
        }
    } else {
        String::new()
    };

    let table_dot =  match table.clone() { Some(name) => format!("{}.", name), None => format!("")};

    let field_names = fields
        .iter()
        .map(|f| format!("{}{}",&table_dot,  f.ident.as_ref().unwrap()))
        .reduce(|acc, x| format!("{}, {}", acc, x))
        .unwrap_or(String::from("*"));

    let mut field_functions = Vec::new();
    let mut insert_functions = Vec::new();
    let mut update_functions = Vec::new();
    let mut delete_functions = Vec::new();
    let mut derived_functions = Vec::new();

    
    

    if primary_key_var.len() > 0 {
        field_functions.push(quote!(

            pub fn inner_join(mut self, table: &str,  primary_key: &str) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\nINNER JOIN {} ON {}.{} = {}.{}", table, table, primary_key, #table,  self.primary_key,));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }
            pub fn join(mut self,  table: &str, primary_key: &str,) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\nJOIN {} ON {}.{} = {}.{}", table, table, primary_key, #table, self.primary_key));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }
            pub fn left_join(mut self, table: &str,  primary_key: &str,) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\nLEFT JOIN {} ON {}.{} = {}.{}", table, table, primary_key,  #table, self.primary_key));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }
            pub fn right_join(mut self,  table: &str, primary_key: &str,) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\nRIGHT JOIN {} ON {}.{} = {}.{}", table, table, primary_key,  #table, self.primary_key));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }
            pub fn full_join(mut self, table: &str,  primary_key: &str,) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\nRIGHT JOIN {} ON {}.{} = {}.{}", table, table, primary_key,  #table, self.primary_key));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }

        ))
    }


    for field in fields {
       

        let field_name = field.ident.as_ref().unwrap();
        let field_ty = &field.ty;
        let field_name_with_table =format!("{}{}", &table_dot, field_name);
        let field_name_without_table =format!("{}",field_name);
 
        let select_field_name = Ident::new(&format!("select_{}", field_name), field_name.span());

        let insert_into_col = Ident::new(&format!("insert_to_{}", field_name), field_name.span());
        
        let delete_where_col = Ident::new(&format!("delete_where_{}_eq", field_name), field_name.span());

        let update_where_col = Ident::new(&format!("update_where_{}_eq", field_name), field_name.span());
        let update_col_with_value = Ident::new(&format!("update_{}_with_value", field_name), field_name.span());

        let inner_join = Ident::new(&format!("inner_join_by_{}", field_name), field_name.span());
        let join = Ident::new(&format!("join_by_{}", field_name), field_name.span());
        let left_join = Ident::new(&format!("left_join_by_{}", field_name), field_name.span());
        let right_join = Ident::new(&format!("right_join_by_{}", field_name), field_name.span());
        let full_join = Ident::new(&format!("full_join_by_{}", field_name), field_name.span());


        let where_function_name_in = Ident::new(&format!("where_{}_in", field_name), field_name.span());
        let where_function_name = Ident::new(&format!("where_{}", field_name), field_name.span());
        let group_by_function = Ident::new(&format!("group_by_{}", field_name), field_name.span());
        let order_by_function = Ident::new(&format!("order_by_{}", field_name), field_name.span());
        let order_by_asc_function = Ident::new(&format!("order_by_{}_asc", field_name), field_name.span());
        let order_by_desc_function = Ident::new(&format!("order_by_{}_desc", field_name), field_name.span());
        let having_function = Ident::new(&format!("having_{}", field_name), field_name.span());
        let where_function_operator_name = Ident::new(
            &format!("where_{}_condition", field_name),
            field_name.span(),
        );

        delete_functions.push(quote! {
            pub fn #delete_where_col(mut self, value: impl ToString) -> String {
                format!("DELETE FROM {} WHERE {} = '{}'", &self.table, #field_name_without_table, value.to_string())
            }
        });

        update_functions.push(quote! {
              pub fn #update_where_col(mut self, value: impl ToString) -> String {
                let mut set_values = String::new();
                for (i, (k, v)) in self.selected.clone().into_iter().enumerate() {
                    set_values = format!("{}{} = '{}'", set_values, k.clone(), v.clone());
                    if i + 1 != self.selected.len() {
                        set_values = format!("{}, ", set_values);
                    }
                }
                format!("UPDATE {} SET {} \nWHERE {} = '{}'", &self.table, set_values.clone(),  #field_name_without_table.clone(), value.to_string())
              }  

              pub fn #update_col_with_value(mut self, value: impl ToString) -> Self {
                let mut selected =  self.selected.clone();
                 selected.entry(#field_name_without_table.to_string()).or_insert(value.to_string());
                Self {
                    selected: selected,
                    ..self
                }
              }  
            }
        );

        insert_functions.push(quote! {

            pub fn #insert_into_col(mut self, value : impl ToString) -> Self {
                let mut selected =  self.selected.clone();
                 selected.entry(#field_name_without_table.to_string()).or_insert(vec![value.to_string()]);
                Self {
                    selected: selected,
                    ..self
                }
            }

            pub fn #order_by_function(mut self, order : &str) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.order_by);
                conditions.push(format!("{} {}",#field_name_with_table, order));
                Self {
                    order_by: conditions.clone(), 
                    ..self
                }
            }

            pub fn #order_by_asc_function(mut self) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.order_by);
                conditions.push(format!("{} ASC",#field_name_with_table));
                Self {
                    order_by: conditions.clone(), 
                    ..self
                }
            }

            pub fn #order_by_desc_function(mut self) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.order_by);
                conditions.push(format!("{} DESC",#field_name_with_table));
                Self {
                    order_by: conditions.clone(), 
                    ..self
                }
            }

            

            

        });

        derived_functions.push(quote! {
            pub fn #select_field_name() -> #builder {
                #builder {
                    primary_key: Self::table_primary_key(),
                    limit: None,
                    joins: Vec::new(),
                    where_conditions: Vec::new(),
                    group_by: Vec::new(),
                    order_by: Vec::new(),
                    having: Vec::new(),
                    table: #table.into(),
                    selected: format!("{}", #field_name_with_table),
                }
            }
        });
        
        field_functions.push(quote! {

            pub fn #inner_join(mut self, table: &str,  key: &str) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\nINNER JOIN {} ON {}.{} = {}", table,table, key, #field_name_with_table));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }
            pub fn #join(mut self, table: &str,  key: &str) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\nJOIN {} ON {}.{} = {}", table,table, key, #field_name_with_table));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }
            pub fn #left_join(mut self,  table: &str, key: &str,) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\nLEFT JOIN {} ON {}.{} = {}", table,table, key, #field_name_with_table));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }
            pub fn #right_join(mut self, table: &str, key: &str,) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\nRIGHT JOIN {} ON {}.{} = {}", table,table, key, #field_name_with_table));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }
            pub fn #full_join(mut self,  table: &str, key: &str,) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\nFULL JOIN {} ON {}.{} = {}", table,table, key, #field_name_with_table));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }
            
            pub fn #order_by_function(mut self, order : &str) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.order_by);
                conditions.push(format!("{} {}",#field_name_with_table, order));
                Self {
                    order_by: conditions.clone(), 
                    ..self
                }
            }

            pub fn #order_by_asc_function(mut self) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.order_by);
                conditions.push(format!("{} ASC",#field_name_with_table));
                Self {
                    order_by: conditions.clone(), 
                    ..self
                }
            }

            pub fn #order_by_desc_function(mut self) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.order_by);
                conditions.push(format!("{} DESC",#field_name_with_table));
                Self {
                    order_by: conditions.clone(), 
                    ..self
                }
            }
            
            pub fn #group_by_function(mut self) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.group_by);
                conditions.push(format!("{}",#field_name_with_table));
                Self {
                    group_by: conditions.clone(), 
                    ..self
                }
            }

            pub fn #having_function(mut self, #field_name: impl ToString) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.having);
                conditions.push(format!("{} = '{}'",#field_name_with_table, #field_name.to_string() ));
                Self {
                    having: conditions.clone(), 
                    ..self
                }
            }
            pub fn #where_function_name_in(mut self, where_in: impl ToString) -> Self {
                let where_in = where_in.to_string();
                if where_in.trim().is_empty() {
                  return  self;
                }
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.where_conditions);
                conditions.push(format!("{} IN ({})", #field_name_with_table,  where_in ));
                Self {
                    where_conditions: conditions.clone(), 
                    ..self
                }
            }
            pub fn #where_function_name(mut self, #field_name:impl ToString) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.where_conditions);
                conditions.push(format!("{} = '{}'",#field_name_with_table,  #field_name.to_string() ));
                Self {
                    where_conditions: conditions.clone(), 
                    ..self
                }
            }
            pub fn #where_function_operator_name(mut self, operator: &str,  #field_name: impl ToString,) -> Self  {
                // self.#field_name = update_with;
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.where_conditions);
                conditions.push(format!("{} {} '{}'",#field_name_with_table, operator, #field_name.to_string() ));
                Self {
                    where_conditions: conditions.clone(), 
                    ..self
                }
            }
        });
    }



    // Generate the struct and its associated functions.
    let gen = quote! {
       

        #[derive(Debug, Clone, Default)]
        pub struct #delete_builder {
            table: String,
        }

        impl #delete_builder {
            pub fn delete_where_str(mut self, raw: &str) -> String {
                format!("DELETE FROM {} WHERE {}", &self.table, raw)
            } 

            #(#delete_functions)*
        }


        #[derive(Debug, Clone, Default)]
        pub struct #update_builder {
            selected: std::collections::HashMap<String, String>,
            table: String,
        }

        impl #update_builder {

            pub fn where_str(mut self, where_condition: &str) -> String {
                let mut set_values = String::new();
                for (i, (k, v)) in self.selected.clone().into_iter().enumerate() {
                    set_values = format!("{}{} = '{}'", set_values, k.clone(), v.clone());
                    if i + 1 != self.selected.len() {
                        set_values = format!("{}, ", set_values);
                    }
                }
                format!("UPDATE {} SET {} WHERE {}", &self.table, set_values, where_condition)
            }

            #(#update_functions)*

        }

        #[derive(Debug, Clone, Default)]
        pub struct #insert_builder {
            selected: std::collections::HashMap<String, Vec<String>>,
            table: String,
            limit: Option<u32>,
            order_by: Vec<String>,
        }

        impl  #insert_builder {

            pub fn limit(mut self, limit: u32) -> Self {
                Self {
                    limit: Some(limit), 
                    ..self
                }
            }

            #(#insert_functions)*

                     pub fn build(self) -> String {
                let mut keys = String::new();
                let mut values = String::new();
                for (i, (k, v)) in self.selected.clone().into_iter().enumerate() {
                    keys = format!("{}{}", keys, k.clone());
                    if (i + 1 != self.selected.len()) {
                        keys = format!("{}, ", keys);
                    }
                }
                         let mut inputs = Vec::new();
                 let mut results = Vec::new();

                 for (k, v) in self.selected.clone().into_iter() {
                 inputs.push(v);
                 }
                 for i in 0..inputs.first().unwrap().len() {
                 let mut data = Vec::new();
                 for j in 0..inputs.len() {
                     data.push(inputs[j][i].clone());
                 }
                 results.push(data);
                 }
                 for i in 0..results.len() {
                 let item = results[i].clone();
                 let mut value = String::new();
                 for j in 0..item.len() {
            value = format!("{}'{}'", value, item[j]);
            if j + 1 != item.len() {
                value = format!("{}, ", value);
                     }
                 }
                    values = format!("{} ({})", values, value);
                    if i + 1 != results.len() {
            values = format!("{},", values);
                    }
                    }
                format!("INSERT INTO {}\n({}) VALUES {}", &self.table, keys, values)
            }



        }

        #[derive(Debug, Clone)]
        pub struct #builder {
            selected: String,
            joins: Vec<String>,
            primary_key: String,
            table: String,
            limit: Option<u32>,
            where_conditions: Vec<String>,
            group_by: Vec<String>,
            order_by: Vec<String>,
            having: Vec<String>,
        }

        impl #builder {

            pub fn join_str(mut self, join: &str) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.joins);
                conditions.push(format!("\n{}", join));
                Self {
                    joins: conditions.clone(),
                    ..self
                }
            }

            pub fn having_str(mut self, having: &str) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                    conditions.append(&mut self.having);
                    conditions.push(format!("{}", having ));
                    Self {
                        
                        having: conditions.clone(), 
                        ..self
                    }
            }
            pub fn where_str(mut self, where_query: &str) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                    conditions.append(&mut self.where_conditions);
                    conditions.push(format!("{}", where_query ));
                    Self {
                        
                        where_conditions: conditions.clone(), 
                        ..self
                    }
            }
            pub fn group_by_str(mut self, group_by: &str) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                    conditions.append(&mut self.group_by);
                    conditions.push(format!("{}", group_by ));
                    Self {
                        
                        group_by: conditions.clone(), 
                        ..self
                    }
            }

            pub fn order_by_str(mut self, order : &str) -> Self {
                let mut conditions: Vec<String> = Vec::new();
                conditions.append(&mut self.order_by);
                conditions.push(format!("{}", order));
                Self {
                    order_by: conditions.clone(), 
                    ..self
                }
            }

            pub fn select_str(mut self, select: &str) -> Self {
                Self {
                    selected: format!("{}, {}", self.selected, select),
                    ..self
                }
            }

            pub fn limit(mut self, limit: u32) -> Self {
                Self {
                    limit: Some(limit), 
                    ..self
                }
            }

            #(#field_functions)*


            pub fn build(&self) -> String {
                let limit = match self.limit {
                    Some(limit) => format!(" \nLIMIT {}", limit), 
                    None => String::new()
                };
               
                    let mut where_query = String::new();
                    for i in 0..self.where_conditions.len() {
                        if(i ==0) {
                            where_query = format!(" \nWHERE");
                        }
                        where_query = format!("{} {}", where_query, self.where_conditions[i].clone());
                        if (i + 1 != self.where_conditions.len()) {
                            where_query = format!("{} {}", where_query, "AND");
                        }
                    }
                    let mut joins = String::new();
                    for i in 0..self.joins.len() {
                        if(i ==0) {
                            joins = format!(" ");
                        }
                        joins = format!("{} {} ", joins, self.joins[i].clone());
                        
                    }
                    let mut group_by = String::new();
                    for i in 0..self.group_by.len() {
                        if(i ==0) {
                            group_by = format!(" \nGROUP BY");
                        }
                        group_by = format!("{} {}", group_by, self.group_by[i].clone());
                        if (i + 1 != self.group_by.len()) {
                            group_by = format!("{},", group_by);
                        }
                    }
                    let mut order_by = String::new();
                    for i in 0..self.order_by.len() {
                        if(i ==0) {
                            order_by = format!(" \nORDER BY");
                        }
                        order_by = format!("{} {}", order_by, self.order_by[i].clone());
                        if (i + 1 != self.order_by.len()) {
                            order_by = format!("{},", order_by);
                        }
                    }
                    let mut having = String::new();
                    for i in 0..self.having.len() {
                        if(i ==0) {
                            having = format!(" \nHAVING");
                        }
                        having = format!("{} {}", having, self.having[i].clone());
                        if (i + 1 != self.having.len()) {
                            having = format!("{} AND", having);
                        }
                    }
                    format!("SELECT {} \nFROM {}{}{}{}{}{}{}", self.selected, self.table,joins, where_query, group_by, having,order_by, limit)
            }
        }

        impl #struct_name {

            pub fn delete() -> #delete_builder {
                #delete_builder {
                    table: #table.into()
                }
            }

            pub fn update() -> #update_builder {
                #update_builder {
                    table: #table.into(), 
                    ..#update_builder::default()
                }
            }

            pub fn insert() -> #insert_builder {
                #insert_builder {
                    table: #table.into(),
                    ..#insert_builder::default()
                }
            }

            pub fn select() -> #builder {
                #builder {
                    primary_key: Self::table_primary_key(),
                    limit: None,
                    order_by: Vec::new(),
                    joins: Vec::new(),
                    where_conditions: Vec::new(),
                    group_by: Vec::new(),
                    having: Vec::new(),
                    table: #table.into(),
                    selected: format!("{}", #field_names),
                }
            }

            #(#derived_functions)*

            pub fn table() -> &'static str {
                #table
            } 
            pub fn table_name(&self) -> &'static str {
                #table
            }
            pub fn table_primary_key() -> String {
                format!("{}", #primary_key_var)
            }
        }


    };
    gen.into()
}