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
use easy_sqlx_utils::value_parser::{parse_groups, parse_next, Array};
use proc_macro2::Span;
use quote::{quote, ToTokens};
use syn::{parse::Parse, Error, Ident, LitStr, Token};

use super::{column::Column, index::Index};

#[derive(Clone, Debug, Default)]
pub struct TableSchema {
    /// 表名称
    pub name: String,
    /// 表所属 schema
    pub schema: Option<String>,
    /// 说明信息
    pub comment: Option<String>,
    /// 索引
    pub indexes: Option<Vec<Index>>,
    /// 列
    pub columns: Vec<Column>,

    /// [控制字段]
    /// 从另外一个表重命名,
    /// 执行重命名的前提条件:1 该表不存在,2 from 指定的表必须存在
    pub from: Option<String>,
    /// [控制字段]
    /// 删除并重建表,给出一个与该表关联的唯一重建标志
    /// 当 重建标志 标志第一次出现时,该表将会重建
    /// 重建会删除表内所有数据,请慎重使用
    pub recreate: Option<String>,
    /// [控制字段]
    /// 删除没有关联 struct 字段的表中的列
    pub trim_columns: bool,
    /// [控制字段]
    /// 删除未定义的索引
    pub trim_indexes: bool,
}

impl TableSchema {
    pub fn name_with_schema(&self) -> String {
        if let Some(schema) = &self.schema {
            return format!("{schema}.{}", self.name.clone());
        }
        self.name.clone()
    }

    pub fn index_name_with_schema(&self, index_name: &String) -> String {
        if let Some(schema) = &self.schema {
            return format!("{schema}.{index_name}");
        }
        index_name.clone()
    }

    /// 检查索引列是否合法
    /// 有效索引列为表中列的字段名称
    pub fn check_indexes_columns(&self) -> syn::Result<()> {
        if let Some(indexes) = self.indexes.as_ref() {
            for index in indexes.iter() {
                for col in index.columns.iter() {
                    if self.find_column(col).is_none() {
                        return Err(Error::new(
                            Span::call_site(),
                            format!("Index's column '{col}' is not exists in table columns"),
                        ));
                    }
                }
            }
        }
        Ok(())
    }

    pub fn find_column(&self, name: &String) -> Option<Column> {
        self.columns
            .iter()
            .find(|col| {
                if col.column.is_none() {
                    col.name == *name
                } else {
                    col.column.as_ref().unwrap() == name
                }
            })
            .map(|col| (*col).clone())
    }

    /// 合并 source 属性到当前 Table
    pub fn assign(&mut self, source: TableSchema) -> syn::Result<()> {
        if self.name.is_empty() && !source.name.is_empty() {
            self.name = source.name.clone();
        }

        if self.comment.is_none() && !source.comment.is_none() {
            self.comment = source.comment.clone();
        }

        if self.schema.is_none() && !source.schema.is_none() {
            self.schema = source.schema.clone();
        }

        if self.recreate.is_none() {
            self.recreate = source.recreate.clone();
        }
        if self.from.is_none() {
            self.from = source.from.clone();
        }
        if !self.trim_columns {
            self.trim_columns = source.trim_columns;
        }

        if !self.trim_indexes {
            self.trim_indexes = source.trim_indexes;
        }

        if let Some(src_indexes) = source.indexes {
            for idx in src_indexes {
                if self.indexes.is_none() {
                    self.indexes = Some(vec![idx.to_owned()]);
                } else {
                    self.indexes.as_mut().unwrap().push(idx.to_owned());
                }
                // self.add_index(idx.to_owned())?;
            }
        }

        // 列无需复制
        Ok(())
    }

    /// 添加 index,如果 name 冲突则重新命名 name
    pub fn add_index(&mut self, mut index: Index) -> syn::Result<()> {
        // let name = index.get_name();
        let (mut name, setted) = index.get_name(&self.name);
        if self.indexes.is_none() {
            index.name = name;
            self.indexes = Some(vec![index]);
        } else {
            // 获取索引名称
            let mut n = 0;
            loop {
                if self
                    .indexes
                    .as_ref()
                    .unwrap()
                    .iter()
                    .find(|idx| (*idx).name == name) // 索引列表中的索引名称为已经设置,可以直接对比
                    .is_none()
                {
                    break;
                }
                if setted {
                    // 索引名称是设置的,不允许冲突
                    return Err(syn::Error::new(
                        Span::call_site(),
                        format!("索引名称 {} 冲突", name),
                    ));
                }
                n += 1;
                (name, _) = index.get_name_with_index(&self.name, n);
                // name = format!("{}_{}", index.name.clone(), n).to_string();
            }
            // 设置索引名称
            index.name = name;
            self.indexes.as_mut().unwrap().push(index);
        }

        Ok(())
    }
}

impl ToTokens for TableSchema {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let name = self.name.clone();
        let comment = self.comment.clone().unwrap_or("".to_string());
        let has_comment = !comment.is_empty();
        let idxs = self.indexes.clone().unwrap_or(vec![]);
        let has_idxs = !idxs.is_empty();
        let schema = self.schema.clone().unwrap_or("".to_string());
        let has_schema = !schema.is_empty();
        let cols = self.columns.clone();
        let from = self.from.clone().unwrap_or("".to_string());
        let has_from = !from.is_empty();
        let trim_columns = self.trim_columns;
        let trim_indexes = self.trim_indexes;
        let recreate = self.recreate.clone().unwrap_or("".to_string());
        let has_recreate = !recreate.is_empty();
        quote! {
            easy_sqlx_core::sql::schema::table::TableSchema {
                indexes: if #has_idxs { Some([#(#idxs), *].to_vec()) } else { None },
                columns: [#(#cols), *].to_vec(),
                name: #name.to_string(),
                comment: if #has_comment { Some(#comment.to_string()) } else { None },
                schema: if #has_schema { Some(#schema.to_string()) } else { None },
                from: if #has_from { Some(#from.to_string()) } else { None },
                recreate: if #has_recreate { Some(#recreate.to_string()) } else { None },
                trim_columns: #trim_columns,
                trim_indexes: #trim_indexes,
                // raw_indexes: if #has_raw_idxs { Some([#(#raw_idxs), *].to_vec()) } else { None },
            }
        }
        .to_tokens(tokens);
    }
}

impl Parse for TableSchema {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        const EXPECTED_ATTRIBUTE: &str =
            "unexpected attribute, expected any of: name, comment, schema, from, recreate, trim_columns, trim_indexes indexes[]";

        let mut table = TableSchema::default();
        let mut idxes = vec![];
        while !input.is_empty() {
            let ident = input.parse::<Ident>().map_err(|error| {
                Error::new(error.span(), format!("{EXPECTED_ATTRIBUTE}, {error}"))
            })?;
            let attribute = &*ident.to_string();

            match attribute {
                "name" => {
                    table.name = parse_next(input, || input.parse::<LitStr>())
                        .map_err(|err| {
                            Error::new(
                                err.span(),
                                format!("attribute {attribute} parse error, {err}"),
                            )
                        })?
                        .value();
                }
                "from" => {
                    table.from = Some(
                        parse_next(input, || input.parse::<LitStr>())
                            .map_err(|err| {
                                Error::new(
                                    err.span(),
                                    format!("attribute {attribute} parse error, {err}"),
                                )
                            })?
                            .value(),
                    );
                }
                "trim_columns" => {
                    table.trim_columns = true;
                }
                "trim_indexes" => {
                    table.trim_indexes = true;
                }
                "recreate" => {
                    table.recreate = Some(
                        parse_next(input, || input.parse::<LitStr>())
                            .map_err(|err| {
                                Error::new(
                                    err.span(),
                                    format!("attribute {attribute} parse error, {err}"),
                                )
                            })?
                            .value(),
                    );
                }
                "comment" => {
                    table.comment = Some(
                        parse_next(input, || input.parse::<LitStr>())
                            .map_err(|err| {
                                Error::new(
                                    err.span(),
                                    format!("attribute {attribute} parse error, {err}"),
                                )
                            })?
                            .value(),
                    );
                }
                "schema" => {
                    table.schema = Some(
                        parse_next(input, || input.parse::<LitStr>())
                            .map_err(|err| {
                                Error::new(
                                    err.span(),
                                    format!("attribute {attribute} parse error, {err}"),
                                )
                            })?
                            .value(),
                    );
                }
                "indexes" => {
                    // parse_next(input, || input.parse::<LitStr>())
                    //     .map_err(|err| {
                    //         Error::new(
                    //             err.span(),
                    //             format!("attribute {attribute} parse error, {err}"),
                    //         )
                    //     })?
                    //     .value();
                    // parse_punctuated_within_parenthesis::<>(input);
                    // let aaaa = parse_macro_input!(input.to_tokens() with Punctuated::<Index, syn::Token![,]>::parse_terminated);
                    let indexes;
                    syn::bracketed!(indexes in input); // [] 括住 索引

                    // let args_parsed =
                    //     syn::punctuated::Punctuated::<syn::Path, syn::Token![,]>::parse_terminated
                    //         .parse(input.)
                    //         .unwrap();

                    let a: Array<'static, Index> = parse_groups(&indexes)?;

                    for index in a.iter() {
                        idxes.push(index.clone());
                        // table.add_raw_index(index.clone())
                    }

                    // loop {
                    //     if indexes.is_empty() {
                    //         break;
                    //     }
                    // //     let index =
                    // //         parse_next(input, || indexes.parse::<Index>()).map_err(|err| {
                    // //             Error::new(
                    // //                 err.span(),
                    // //                 format!("attribute {attribute} parse error, {err}"),
                    // //             )
                    // //         })?;
                    // //     idxes.push(index);
                    // //     // let value = parser(input)?;
                    // //     // punctuated.push_value(value);
                    //     if indexes.is_empty() {
                    //         break;
                    //     }
                    //     // let punct = input.parse()?;
                    // //     // punctuated.push_punct(punct);
                    // }
                }
                _ => {
                    return Err(Error::new(ident.span(), EXPECTED_ATTRIBUTE));
                }
            }

            if !input.is_empty() {
                input.parse::<Token![,]>()?;
            }
        }

        if !idxes.is_empty() {
            // table.raw_indexes = Some(idxes);
            table.indexes = Some(idxes);
        }

        Ok(table)
    }
}