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
extern crate proc_macro;

use quote::quote;
use std::collections::HashMap;
use syn::parse::{Parse, ParseStream};
use syn::{parse_macro_input, DeriveInput, Result};

struct TableAttr {
    table_name: String,
    primary_key: Vec<String>,
    sql_type: proc_macro2::TokenStream,
}

struct AttrInput {
    paren_token: syn::token::Paren,
    attrs: syn::punctuated::Punctuated<KeyValue, syn::Token![,]>,
}

impl Parse for AttrInput {
    fn parse(input: ParseStream) -> Result<Self> {
        let content;
        Ok(AttrInput {
            paren_token: syn::parenthesized!(content in input),
            attrs: content.parse_terminated(KeyValue::parse)?,
        })
    }
}

impl AttrInput {
    fn to_table_attr(self, table_name: String) -> TableAttr {
        let mut table = TableAttr {
            table_name: table_name,
            primary_key: vec![],
            sql_type: quote! { Vec<u8> },
        };

        for attr in self.attrs.into_iter() {
            match format!("{}", attr.key).as_str() {
                "table_name" => table.table_name = attr.value.as_str().unwrap(),
                "sql_type" => {
                    let sql_type =
                        syn::parse_str::<syn::Type>(&attr.value.as_str().unwrap()).unwrap();
                    table.sql_type = quote! { #sql_type };
                }
                "primary_key" => {
                    table.primary_key = attr
                        .value
                        .as_str()
                        .unwrap()
                        .split(",")
                        .map(|s| s.trim().to_string())
                        .collect();
                }
                d => panic!("unsupported attribute: {}", d),
            }
        }

        table
    }

    fn to_attr_map(self) -> HashMap<String, Universe> {
        let mut result = HashMap::new();

        for attr in self.attrs.into_iter() {
            result.insert(format!("{}", attr.key), attr.value);
        }

        result
    }
}

#[derive(Clone)]
enum Universe {
    VStr(String),
    VI32(i32),
    VBool(bool),
    VType(syn::Type),
}

impl Parse for Universe {
    fn parse(input: ParseStream) -> Result<Self> {
        let lookahead = input.lookahead1();
        if lookahead.peek(syn::LitStr) {
            input
                .parse::<syn::LitStr>()
                .map(|v| Universe::VStr(v.value()))
        } else if lookahead.peek(syn::LitInt) {
            input
                .parse::<syn::LitInt>()
                .map(|v| Universe::VI32(v.base10_parse::<i32>().unwrap()))
        } else if lookahead.peek(syn::LitBool) {
            input
                .parse::<syn::LitBool>()
                .map(|v| Universe::VBool(v.value))
        } else {
            Err(lookahead.error())
        }
    }
}

impl Universe {
    fn as_str(self) -> Option<String> {
        use Universe::*;
        match self {
            VStr(s) => Some(s),
            _ => None,
        }
    }

    fn as_i32(self) -> Option<i32> {
        use Universe::*;
        match self {
            VI32(i) => Some(i),
            _ => None,
        }
    }

    fn as_bool(self) -> Option<bool> {
        use Universe::*;
        match self {
            VBool(b) => Some(b),
            _ => None,
        }
    }
}

struct KeyValue {
    key: proc_macro2::Ident,
    punct: syn::Token![=],
    value: Universe,
}

impl Parse for KeyValue {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(KeyValue {
            key: input.parse()?,
            punct: input.parse()?,
            value: input.parse()?,
        })
    }
}

fn get_fields_from_datastruct(
    data: syn::Data,
) -> Vec<(proc_macro2::Ident, syn::Type, HashMap<String, Universe>)> {
    let mut result = Vec::new();

    match data {
        syn::Data::Struct(st) => match st.fields {
            syn::Fields::Named(fields) => {
                for name in fields.named.iter() {
                    result.push((
                        name.ident.as_ref().unwrap().clone(),
                        name.ty.clone(),
                        if name.attrs.len() == 0 {
                            HashMap::new()
                        } else {
                            // TODO: Only first FieldAttr will be effective
                            syn::parse2::<AttrInput>(name.attrs[0].tokens.clone())
                                .unwrap()
                                .to_attr_map()
                        },
                    ));
                }
            }
            _ => unimplemented!(),
        },
        _ => unimplemented!(),
    }

    result
}

fn option_to_quote<T: quote::ToTokens>(opt: Option<T>) -> proc_macro2::TokenStream {
    if opt.is_some() {
        quote! { Some(#opt) }
    } else {
        quote! { None }
    }
}

#[proc_macro_derive(Table, attributes(sql))]
pub fn derive_record(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let ident = input.ident;
    if input.attrs.is_empty() {
        panic!("Currently, sql(table_name),sql(sql_type) and primary_key_columns(comma_separated_string) are required.");
    }

    let attr_stream = input.attrs[0].tokens.clone();
    let table_attr = syn::parse2::<AttrInput>(attr_stream)
        .unwrap()
        .to_table_attr(format!("{}", ident));
    let table_name = table_attr.table_name;

    let field_struct = get_fields_from_datastruct(input.data);

    let primary_key_columns = table_attr.primary_key;
    if primary_key_columns.len() == 0 {
        panic!("At least one primary key must be specified")
    }
    // checking existince of keys specified as primary key
    for pk_column_name in primary_key_columns.iter() {
        if !field_struct
            .iter()
            .map(|(ident, _, _)| ident.to_string())
            .collect::<Vec<String>>()
            .contains(pk_column_name)
        {
            panic!(
                "primary_key: {} was not found in this table struct",
                pk_column_name
            )
        };
    }

    let push_primary_key_columns = primary_key_columns
        .iter()
        .map(|v| quote! { result.push(#v.to_string()); })
        .collect::<Vec<_>>();
    let push_field_names = field_struct
        .iter()
        .map(|(ident, _, _)| quote! { result.push((stringify!(#ident).to_string(), SQLValue::serialize(self.#ident))); })
        .collect::<Vec<_>>();
    let push_column_schema = field_struct
        .iter()
        .map(move |(ident, ty, attr_map)| {
            let size_opt = attr_map.get("size").map(|v| v.clone().as_i32().unwrap());
            let size = option_to_quote(size_opt);
            let unique = option_to_quote(attr_map.get("unique").map(|v| v.clone().as_bool().unwrap()));
            let not_null = option_to_quote(attr_map.get("not_null").map(|v| v.clone().as_bool().unwrap()));
            let size_unopt = size_opt.unwrap_or(0);

            quote! {
                result.push((stringify!(#ident).to_string(), <Self::ValueType as SQLValue<_>>::column_type(std::marker::PhantomData::<#ty>, #size_unopt), FieldAttribute {
                    size: #size,
                    unique: #unique,
                    not_null: #not_null,
                }));
            }
        })
        .collect::<Vec<_>>();
    let record_fields = field_struct
        .iter()
        .map(|(ident, _, _)| {
            quote! {
                #ident: <Self::ValueType as SQLValue<_>>::deserialize(values.get(stringify!(#ident)).unwrap().clone()),
            }
        })
        .collect::<Vec<_>>();

    let sql_type = table_attr.sql_type;

    let expanded = quote! {
        impl SQLMapper for #ident {
            type ValueType = #sql_type;

            fn map_from_sql(values: std::collections::HashMap<String, Self::ValueType>) -> Self {
                #ident {
                    #( #record_fields )*
                }
            }
        }

        impl SQLTable for #ident {
            fn table_name(_: std::marker::PhantomData<Self>) -> String {
                #table_name.to_string()
            }

            fn schema_of(_: std::marker::PhantomData<Self>) -> Vec<(String, String, FieldAttribute)> {
                let mut result = Vec::new();
                #( #push_column_schema )*
                result
            }

            fn primary_key_columns(_: std::marker::PhantomData<Self>) -> Vec<String> {
                let mut result = Vec::new();
                #( #push_primary_key_columns )*
                result
            }

            fn map_to_sql(self) -> Vec<(String, Self::ValueType)> {
                let mut result = Vec::new();
                #( #push_field_names )*

                result
            }
        }
    };

    proc_macro::TokenStream::from(expanded)
}