co_orm_macros/
lib.rs

1/*
2 * @Author: plucky
3 * @Date: 2022-09-04 00:01:24
4 * 
5 */
6
7extern crate proc_macro;
8
9use helper::check_attributes;
10use impl_by_field::generate_impl_from_row;
11use proc_macro::TokenStream;
12use impl_crud::generate_crud;
13use syn::{parse_macro_input, DeriveInput};
14
15
16mod util;
17mod impl_by_field;
18mod db_type;
19mod impl_crud;
20mod helper;
21
22
23
24/// `#[derive(Crud)]`
25/// generate method: get, get_by, get_where, query, query_by, query_where, update, update_by, delete, delete_by, delete_where, insert, insert_all, query_page_by, query_page_where
26/// 
27/// attributes:
28/// 
29/// `#[co_orm(id)]`
30/// default first field is primary key or set.
31///  
32/// `#[co_orm(rename="name")]`
33/// rename table name or field name. 
34/// default table name by struct name to_table_case: UserDetail => user_detail. 
35/// default field name by field name to_snake_case: UserDetail => user_detail. 
36/// 
37/// `#[co_orm(skip)]`
38/// ignore field. using sqlx::FromRow, skip need `#[co_orm(skip)]` and `#[sqlx(skip)]`
39/// 
40/// `#[co_orm(update)]`
41/// generate method update_xxx. 
42///  
43/// `#[co_orm(skip_insert)]`
44/// insert will skip this field.
45#[proc_macro_derive(Crud, 
46    attributes(
47        co_orm, // co_orm(id), co_orm(skip_insert), co_orm(rename="name"), co_orm(skip), co_orm(update), co_orm(by),
48        
49    )
50)]
51pub fn sql_derive_crud(input: TokenStream) -> TokenStream{
52    let input = parse_macro_input!(input as DeriveInput);
53    
54    if let Err(e) =  check_attributes(&input.attrs){
55       return e.to_compile_error().into();
56    }
57
58    generate_crud(input)
59    
60}
61
62/// `#[derive(FromRow)]`
63/// generate impl sqlx::FromRow for struct.
64/// 
65/// or use `#[derive(sqlx::FromRow)]`.
66/// 
67#[proc_macro_derive(FromRow)]
68pub fn sql_derive_form_row(input: TokenStream) -> TokenStream{
69    let input = parse_macro_input!(input as DeriveInput);
70
71    generate_impl_from_row(&input).into()
72
73}
74