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
//! Do dynamic-sql query through proc-macro
//! 
//! It bases on [**tokio-postgres**] and [**sqlx**] crate (default feature), you can switch them by setting the features. 
//! It uses [**Ramhorns**] the high performance template engine implementation of [**Mustache**]
//! 
//! ## Example (Sqlx)
//! 
//! ### main.rs
//! ```ignore
//! //...
//! 
//! # #[tokio::main]
//! async fn main() -> dysql::DySqlResult<()> {
//!     let conn = connect_postgres_db().await;
//!     
//!     // fetch all
//!     let dto = UserDto{ id: None, name: None, age: Some(15) };
//!     let rst = fetch_all!(|&dto, &conn| -> User {
//!         r#"SELECT * FROM test_user 
//!         WHERE 1 = 1
//!           {{#name}}AND name = :name{{/name}}
//!           {{#age}}AND age > :age{{/age}}
//!         ORDER BY id"#
//!     });
//!     assert_eq!(
//!         vec![
//!             User { id: 2, name: Some("zhanglan".to_owned()), age: Some(21) }, 
//!             User { id: 3, name: Some("zhangsan".to_owned()), age: Some(35) }
//!         ], 
//!         rst
//!     );
//! 
//!     let rst = fetch_one!(...);
//! 
//!     let rst = fetch_scalar!(...);
//!     
//!     let affected_rows_num = execute!(...);
//!     
//!     let insert_id = insert!(...);
//! }
//! ```
//! 
//! ## Example (tokio-postgres)
//! Full example please see: [Dysql tokio-postgres example](https://github.com/evanzp0/dysql-project/tree/main/examples/with_tokio_postgres)
//! 
//! ## Example (sqlx)
//! Full example please see: [Dysql sqlx example](https://github.com/evanzp0/dysql-project/tree/main/examples/with_sqlx)
#[cfg(not(feature = "tokio-postgres"))]
mod dy_sqlx;
#[cfg(not(feature = "tokio-postgres"))]
use dy_sqlx::expand;

#[cfg(feature = "tokio-postgres")]
mod dy_tokio_postgres;
#[cfg(feature = "tokio-postgres")]
use dy_tokio_postgres::expand;

use dysql::{QueryType, get_dysql_config};
use proc_macro::TokenStream;
use syn::punctuated::Punctuated;

#[allow(dead_code)]
#[derive(Debug)]
struct SqlClosure {
    dto: Option<syn::Ident>,
    is_dto_ref: bool,
    cot: syn::Ident, // database connection or transaction
    is_cot_ref: bool,
    is_cot_ref_mut: bool,
    sql_name: Option<String>,
    ret_type: Option<syn::Path>, // return type
    dialect: syn::Ident,
    body: String,
}

impl syn::parse::Parse for SqlClosure {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        // parse closure parameters

        let mut is_dto_ref = false;
        //// parse dto
        input.parse::<syn::Token!(|)>()?;
        if let Ok(_) = input.parse::<syn::Token!(&)>() {
            is_dto_ref = true;
            input.parse::<syn::Token!(mut)>().ok();
        };
        let dto = match input.parse::<syn::Ident>() {
            Ok(i) => Some(i),
            Err(e) => match input.parse::<syn::Token!(_)>() {
                Ok(_) => None,
                Err(_) => return Err(e),
            },
        };

        //// parse cot
        let mut is_cot_ref = false;
        let mut is_cot_ref_mut = false;
        input.parse::<syn::Token!(,)>()?;
        //////parse ref mut
        let cot: syn::Ident = match input.parse::<syn::Token!(&)>() {
            Ok(_) => {
                is_cot_ref = true;
                match input.parse::<syn::Token!(mut)>() {
                    Ok(_) => {
                        is_cot_ref = false;
                        is_cot_ref_mut = true;
                        input.parse()?
                    },
                    Err(_) => input.parse()?,
                }
            },
            Err(_) => {
                input.parse()?
            },
        };

        // parse sql_name
        let mut sql_name = None;
        match input.parse::<syn::Token!(|)>() {
            Ok(_) => (),
            Err(_) => {
                input.parse::<syn::Token!(,)>()?;
                sql_name = match input.parse::<syn::LitStr>() {
                    Ok(s) => Some(s.value()),
                    Err(_) => None,
                };
                input.parse::<syn::Token!(|)>()?;
            },
        }

        // parses token(->) first ,and then parses the tuple of return type and dialect
        let dialect: syn::Ident;
        let ret_type:Option<syn::Path>;
        match input.parse::<syn::Token!(->)>() {
            Ok(_) => {
                // try to parse return type path: ret_type, or ( ... )
                match input.parse::<syn::Path>() {
                    Ok(p) => {
                        ret_type = Some(p);
                        dialect = get_default_dialect(&input.span());
                    },
                    Err(_) => match parse_return_tuple(input) {
                        // try to parse return tuple : ( ret_type, dialect ) or ( ret_type, _ )
                        Ok(tp) => {
                            match tp.parse::<syn::Path>() {
                                Ok(p) => {
                                    ret_type = Some(p);
                                    tp.parse::<syn::Token!(,)>()?;
                                    if let Err(_) = tp.parse::<syn::Token!(_)>() {
                                        match tp.parse::<syn::Ident>() {
                                            Ok(i) => dialect = i,
                                            Err(_) => return Err(syn::Error::new(proc_macro2::Span::call_site(), "Need specify the dialect")),
                                        }
                                    } else {
                                        dialect = get_default_dialect(&input.span());
                                    }
                                },
                                // try to parse ( _, dialect )
                                Err(_) => match tp.parse::<syn::Token!(_)>() {
                                    Ok(_) => {
                                        ret_type = None;
                                        tp.parse::<syn::Token!(,)>()?;
                                        if let Err(_) = tp.parse::<syn::Token!(_)>() {
                                            match tp.parse::<syn::Ident>() {
                                                Ok(i) => dialect = i,
                                                Err(_) => return Err(syn::Error::new(proc_macro2::Span::call_site(), "Need specify the dialect")),
                                            }
                                        } else {
                                            dialect = get_default_dialect(&input.span());
                                        }
                                    },
                                    Err(_) => return Err(syn::Error::new(proc_macro2::Span::call_site(), "Need specify the return type")),
                                },
                            }
                        },
                        Err(_) => return Err(syn::Error::new(proc_macro2::Span::call_site(), "Need specify the return type and dialect")),
                    },
                }
            },
            Err(_) => {
                ret_type = None;
                dialect = get_default_dialect(&input.span());
            },
        };

        // parse closure sql body
        let body_buf;
        syn::braced!(body_buf in input);
        let body: syn::LitStr = body_buf.parse()?;
        let body = body.value();
        let body:Vec<_> = body.split("\n").map(|f| f.trim()).collect();
        let body = body.join(" ");
        let sc = SqlClosure { dto, is_dto_ref, cot, is_cot_ref, is_cot_ref_mut, sql_name, ret_type, dialect, body };
        // eprintln!("{:#?}", sc);

        Ok(sc)
    }
}

pub(crate) fn gen_path(s: &str) -> syn::Path {
    let seg = syn::PathSegment {
        ident: syn::Ident::new(s, proc_macro2::Span::call_site()),
        arguments: syn::PathArguments::None,
    };
    let mut punct: Punctuated<syn::PathSegment, syn::Token![::]> = Punctuated::new();
    punct.push_value(seg);
    let path = syn::Path{ leading_colon: None, segments: punct };

    path
}


fn parse_return_tuple(input: syn::parse::ParseStream) -> syn::Result<syn::parse::ParseBuffer> {
    let tuple_buf;
    syn::parenthesized!(tuple_buf in input);

    Ok(tuple_buf)
}

fn get_default_dialect(span: &proc_macro2::Span) -> syn::Ident {
    match get_dysql_config().dialect {
        dysql::SqlDialect::postgres => syn::Ident::new(&dysql::SqlDialect::postgres.to_string(), span.clone()),
        dysql::SqlDialect::mysql => syn::Ident::new(&dysql::SqlDialect::mysql.to_string(), span.clone()),
        dysql::SqlDialect::sqlite => syn::Ident::new(&dysql::SqlDialect::sqlite.to_string(), span.clone()),
    }
}

///
/// fetch all datas that filtered by dto
/// 
/// # Examples
///
/// Basic usage:
/// 
/// ```ignore
/// let mut conn = connect_db().await;
/// 
/// let dto = UserDto {id: None, name: None, age: 13};
/// let rst = fetch_all!(|&dto, &conn| -> User {
///     r#"select * from test_user 
///     where 1 = 1
///         {{#name}}and name = :name{{/name}}
///         {{#age}}and age > :age{{/age}}
///     order by id"#
/// }).unwrap();
/// 
/// assert_eq!(
///     vec![
///         User { id: 2, name: Some("zhanglan".to_owned()), age: Some(21) }, 
///         User { id: 3, name: Some("zhangsan".to_owned()), age: Some(35) },
///     ], 
///     rst
/// );
/// ```
#[proc_macro]
pub fn fetch_all(input: TokenStream) -> TokenStream {
    let st = syn::parse_macro_input!(input as SqlClosure);
    if st.ret_type.is_none() { panic!("ret_type can't be null.") }

    match expand(&st, QueryType::FetchAll) {
        Ok(ret) => ret.into(),
        Err(e) => e.into_compile_error().into(),
    }
}

///
/// fetch one data that filtered by dto
/// 
/// # Examples
///
/// Basic usage:
/// 
/// ```ignore
/// let mut conn = connect_db().await;
/// 
/// let dto = UserDto {id: 2, name: None, age: None};
/// let rst = fetch_one!(|&dto, &conn| -> User {
///     r#"select * from test_user 
///     where id = :id
///     order by id"#
/// }).unwrap();
/// 
/// assert_eq!(User { id: 2, name: Some("zhanglan".to_owned()), age: Some(21) }, rst);
/// ```
#[proc_macro]
pub fn fetch_one(input: TokenStream) -> TokenStream {
    let st = syn::parse_macro_input!(input as SqlClosure);
    if st.ret_type.is_none() { panic!("ret_type can't be null.") }

    match expand(&st, QueryType::FetchOne) {
        Ok(ret) => ret.into(),
        Err(e) => e.into_compile_error().into(),
    }
}

///
/// Fetch a scalar value from query
/// 
/// # Examples
///
/// Basic usage:
/// 
/// ```ignore
/// let mut conn = connect_db().await;
/// 
/// let rst = fetch_scalar!(|_, &conn| -> i64 {
///     r#"select count (*) from test_user"#
/// }).unwrap();
/// assert_eq!(3, rst);
/// ```
#[proc_macro]
pub fn fetch_scalar(input: TokenStream) -> TokenStream {
    let st = syn::parse_macro_input!(input as SqlClosure);
    if st.ret_type.is_none() { panic!("ret_type can't be null.") }

    match expand(&st, QueryType::FetchScalar) {
        Ok(ret) => ret.into(),
        Err(e) => e.into_compile_error().into(),
    }
}

///
/// Execute query
/// 
/// # Examples
///
/// Basic usage:
/// 
/// ```ignore
/// let mut tran = get_transaction().await.unwrap();
/// 
/// let dto = UserDto::new(Some(2), None, None);
/// let rst = execute!(|&dto, &mut tran| {
///     r#"delete from test_user where id = :id"#
/// }).unwrap();
/// assert_eq!(1, rst);
/// 
/// tran.rollback().await?;
/// ```
#[proc_macro]
pub fn execute(input: TokenStream) -> TokenStream {
    let st = syn::parse_macro_input!(input as SqlClosure);

    match expand(&st, QueryType::Execute) {
        Ok(ret) => ret.into(),
        Err(e) => e.into_compile_error().into(),
    }
}

///
/// Insert data
/// **Note:** if you use this macro under **postgres** database, you should add "returning id" at the end of sql statement by yourself.
/// 
/// # Examples
///
/// Basic usage:
/// 
/// ```ignore
/// let mut tran = get_transaction().await.unwrap();

/// let dto = UserDto{ id: Some(4), name: Some("lisi".to_owned()), age: Some(50) };
/// let last_insert_id = insert!(|&dto, &mut tran| -> (_, mysql) {
///     r#"insert into test_user (id, name, age) values (4, 'aa', 1)"#  // works for mysql and sqlite
///     // r#"insert into test_user (id, name, age) values (4, 'aa', 1) returning id"#  // works for postgres
/// }).unwrap();
/// assert_eq!(4, last_insert_id);
/// 
/// tran.rollback().await?;
/// ```
#[proc_macro]
pub fn insert(input: TokenStream) -> TokenStream {
    let st = syn::parse_macro_input!(input as SqlClosure);

    match expand(&st, QueryType::Insert) {
        Ok(ret) => ret.into(),
        Err(e) => e.into_compile_error().into(),
    }
}