entity-derive-impl 0.3.0

Internal proc-macro implementation for entity-derive. Use entity-derive instead.
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
// SPDX-FileCopyrightText: 2025-2026 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT

//! Helper functions for SQL generation.
//!
//! This module contains utility functions used across multiple method
//! generators:
//!
//! - [`join_columns`] — builds column list for SELECT/INSERT
//! - [`insert_bindings`] — builds `.bind()` chain for INSERT
//! - [`update_bindings`] — builds `.bind()` chain for UPDATE
//! - [`generate_where_conditions`] — builds WHERE clause for query method
//! - [`generate_query_bindings`] — builds parameter bindings for query method

use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::entity::parse::{FieldDef, FilterType};

/// Join field names into comma-separated column list.
///
/// # Example
///
/// ```text
/// ["id", "name", "email"] -> "id, name, email"
/// ```
pub fn join_columns(fields: &[FieldDef]) -> String {
    fields
        .iter()
        .map(|f| f.name_str())
        .collect::<Vec<_>>()
        .join(", ")
}

/// Build `.bind(insertable.field)` chain for INSERT.
///
/// # Generated Code
///
/// ```rust,ignore
/// .bind(insertable.id)
/// .bind(insertable.name)
/// .bind(insertable.email)
/// ```
pub fn insert_bindings(fields: &[FieldDef]) -> Vec<TokenStream> {
    fields
        .iter()
        .map(|f| {
            let name = f.name();
            quote! { .bind(insertable.#name) }
        })
        .collect()
}

/// Build `.bind(dto.field)` chain for UPDATE.
///
/// # Generated Code
///
/// ```rust,ignore
/// .bind(dto.name)
/// .bind(dto.email)
/// ```
pub fn update_bindings(fields: &[&FieldDef]) -> Vec<TokenStream> {
    fields
        .iter()
        .map(|f| {
            let name = f.name();
            quote! { .bind(dto.#name) }
        })
        .collect()
}

/// Generate WHERE condition building code for query method.
///
/// Creates runtime code that builds a dynamic WHERE clause based on
/// which filter fields are set in the query struct.
///
/// # Filter Types
///
/// | Type | SQL Generated |
/// |------|---------------|
/// | `Eq` | `field = $n` |
/// | `Like` | `field ILIKE $n` |
/// | `Range` | `field >= $n` and `field <= $n` |
///
/// # Soft Delete
///
/// When `soft_delete` is true, adds `deleted_at IS NULL` condition.
pub fn generate_where_conditions(fields: &[&FieldDef], soft_delete: bool) -> TokenStream {
    let conditions: Vec<TokenStream> = fields
        .iter()
        .flat_map(|f| {
            let name = f.name();
            let name_str = f.name_str();
            let filter = f.filter();

            match filter.filter_type {
                FilterType::Eq => {
                    vec![quote! {
                        if query.#name.is_some() {
                            conditions.push(format!("{} = ${}", #name_str, param_idx));
                            param_idx += 1;
                        }
                    }]
                }
                FilterType::Like => {
                    vec![quote! {
                        if query.#name.is_some() {
                            conditions.push(format!("{} ILIKE ${}", #name_str, param_idx));
                            param_idx += 1;
                        }
                    }]
                }
                FilterType::Range => {
                    let from_name = format_ident!("{}_from", name);
                    let to_name = format_ident!("{}_to", name);
                    vec![
                        quote! {
                            if query.#from_name.is_some() {
                                conditions.push(format!("{} >= ${}", #name_str, param_idx));
                                param_idx += 1;
                            }
                        },
                        quote! {
                            if query.#to_name.is_some() {
                                conditions.push(format!("{} <= ${}", #name_str, param_idx));
                                param_idx += 1;
                            }
                        },
                    ]
                }
                // Skip: filter_fields() should only return fields with filters,
                // but handle gracefully if None slips through
                FilterType::None => vec![]
            }
        })
        .collect();

    let soft_delete_condition = if soft_delete {
        quote! {
            conditions.push("deleted_at IS NULL".to_string());
        }
    } else {
        TokenStream::new()
    };

    quote! {
        #soft_delete_condition
        #(#conditions)*
    }
}

/// Generate query parameter binding code.
///
/// Creates runtime code that binds filter values to the query.
/// Only binds values for fields that are `Some`.
///
/// # LIKE Pattern
///
/// For `Like` filters, wraps the value in `%...%` for substring matching.
pub fn generate_query_bindings(fields: &[&FieldDef]) -> TokenStream {
    let bindings: Vec<TokenStream> = fields
        .iter()
        .flat_map(|f| {
            let name = f.name();
            let filter = f.filter();

            match filter.filter_type {
                FilterType::Eq => {
                    vec![quote! {
                        if let Some(ref v) = query.#name {
                            q = q.bind(v);
                        }
                    }]
                }
                FilterType::Like => {
                    vec![quote! {
                        if let Some(ref v) = query.#name {
                            // Escape SQL LIKE wildcards to prevent injection
                            let escaped = v
                                .replace('\\', "\\\\")
                                .replace('%', "\\%")
                                .replace('_', "\\_");
                            q = q.bind(format!("%{}%", escaped));
                        }
                    }]
                }
                FilterType::Range => {
                    let from_name = format_ident!("{}_from", name);
                    let to_name = format_ident!("{}_to", name);
                    vec![
                        quote! {
                            if let Some(ref v) = query.#from_name {
                                q = q.bind(v);
                            }
                        },
                        quote! {
                            if let Some(ref v) = query.#to_name {
                                q = q.bind(v);
                            }
                        },
                    ]
                }
                // Skip: filter_fields() should only return fields with filters,
                // but handle gracefully if None slips through
                FilterType::None => vec![]
            }
        })
        .collect();

    quote! { #(#bindings)* }
}

#[cfg(test)]
mod tests {
    use syn::{Field, parse_quote};

    use super::*;
    use crate::entity::parse::FieldDef;

    fn parse_field(tokens: proc_macro2::TokenStream) -> FieldDef {
        let field: Field = parse_quote!(#tokens);
        FieldDef::from_field(&field).unwrap()
    }

    #[test]
    fn join_columns_single() {
        let field = parse_field(quote! { pub name: String });
        let result = join_columns(&[field]);
        assert_eq!(result, "name");
    }

    #[test]
    fn join_columns_multiple() {
        let fields = vec![
            parse_field(quote! { pub id: Uuid }),
            parse_field(quote! { pub name: String }),
            parse_field(quote! { pub email: String }),
        ];
        let result = join_columns(&fields);
        assert_eq!(result, "id, name, email");
    }

    #[test]
    fn join_columns_empty() {
        let result = join_columns(&[]);
        assert_eq!(result, "");
    }

    #[test]
    fn insert_bindings_generates_bind_calls() {
        let fields = vec![
            parse_field(quote! { pub id: Uuid }),
            parse_field(quote! { pub name: String }),
        ];
        let bindings = insert_bindings(&fields);
        assert_eq!(bindings.len(), 2);

        let first = bindings[0].to_string();
        assert!(first.contains("bind"), "Expected 'bind' in: {}", first);
        assert!(
            first.contains("insertable"),
            "Expected 'insertable' in: {}",
            first
        );
        assert!(first.contains("id"), "Expected 'id' in: {}", first);

        let second = bindings[1].to_string();
        assert!(second.contains("bind"), "Expected 'bind' in: {}", second);
        assert!(
            second.contains("insertable"),
            "Expected 'insertable' in: {}",
            second
        );
        assert!(second.contains("name"), "Expected 'name' in: {}", second);
    }

    #[test]
    fn insert_bindings_empty() {
        let bindings = insert_bindings(&[]);
        assert!(bindings.is_empty());
    }

    #[test]
    fn update_bindings_generates_bind_calls() {
        let fields = [
            parse_field(quote! { pub name: String }),
            parse_field(quote! { pub email: String })
        ];
        let refs: Vec<&FieldDef> = fields.iter().collect();
        let bindings = update_bindings(&refs);
        assert_eq!(bindings.len(), 2);

        let first = bindings[0].to_string();
        assert!(first.contains("bind"), "Expected 'bind' in: {}", first);
        assert!(first.contains("dto"), "Expected 'dto' in: {}", first);
        assert!(first.contains("name"), "Expected 'name' in: {}", first);
    }

    #[test]
    fn update_bindings_empty() {
        let bindings = update_bindings(&[]);
        assert!(bindings.is_empty());
    }

    #[test]
    fn where_conditions_eq_filter() {
        let field = parse_field(quote! {
            #[filter(eq)]
            pub status: String
        });
        let refs: Vec<&FieldDef> = vec![&field];
        let result = generate_where_conditions(&refs, false);
        let code = result.to_string();
        assert!(code.contains("query . status . is_some"));
        assert!(code.contains("= $"));
    }

    #[test]
    fn where_conditions_like_filter() {
        let field = parse_field(quote! {
            #[filter(like)]
            pub name: String
        });
        let refs: Vec<&FieldDef> = vec![&field];
        let result = generate_where_conditions(&refs, false);
        let code = result.to_string();
        assert!(code.contains("query . name . is_some"));
        assert!(code.contains("ILIKE"));
    }

    #[test]
    fn where_conditions_range_filter() {
        let field = parse_field(quote! {
            #[filter(range)]
            pub age: i32
        });
        let refs: Vec<&FieldDef> = vec![&field];
        let result = generate_where_conditions(&refs, false);
        let code = result.to_string();
        assert!(code.contains("age_from"));
        assert!(code.contains("age_to"));
        assert!(code.contains(">="));
        assert!(code.contains("<="));
    }

    #[test]
    fn where_conditions_none_filter() {
        let field = parse_field(quote! { pub name: String });
        let refs: Vec<&FieldDef> = vec![&field];
        let result = generate_where_conditions(&refs, false);
        let code = result.to_string();
        // No conditions for None filter
        assert!(!code.contains("query"));
    }

    #[test]
    fn where_conditions_with_soft_delete() {
        let result = generate_where_conditions(&[], true);
        let code = result.to_string();
        assert!(code.contains("deleted_at IS NULL"));
    }

    #[test]
    fn where_conditions_without_soft_delete() {
        let result = generate_where_conditions(&[], false);
        let code = result.to_string();
        assert!(!code.contains("deleted_at"));
    }

    #[test]
    fn query_bindings_eq_filter() {
        let field = parse_field(quote! {
            #[filter(eq)]
            pub status: String
        });
        let refs: Vec<&FieldDef> = vec![&field];
        let result = generate_query_bindings(&refs);
        let code = result.to_string();
        assert!(code.contains("if let Some (ref v) = query . status"));
        assert!(code.contains("q = q . bind (v)"));
    }

    #[test]
    fn query_bindings_like_filter() {
        let field = parse_field(quote! {
            #[filter(like)]
            pub name: String
        });
        let refs: Vec<&FieldDef> = vec![&field];
        let result = generate_query_bindings(&refs);
        let code = result.to_string();
        assert!(code.contains("escaped"));
        assert!(code.contains("format !"));
    }

    #[test]
    fn query_bindings_range_filter() {
        let field = parse_field(quote! {
            #[filter(range)]
            pub age: i32
        });
        let refs: Vec<&FieldDef> = vec![&field];
        let result = generate_query_bindings(&refs);
        let code = result.to_string();
        assert!(code.contains("age_from"));
        assert!(code.contains("age_to"));
    }

    #[test]
    fn query_bindings_none_filter() {
        let field = parse_field(quote! { pub name: String });
        let refs: Vec<&FieldDef> = vec![&field];
        let result = generate_query_bindings(&refs);
        let code = result.to_string();
        // No bindings for None filter
        assert!(!code.contains("bind"));
    }

    #[test]
    fn query_bindings_empty() {
        let result = generate_query_bindings(&[]);
        assert!(result.is_empty());
    }

    #[test]
    fn where_conditions_multiple_filters() {
        let fields = [
            parse_field(quote! {
                #[filter(eq)]
                pub status: String
            }),
            parse_field(quote! {
                #[filter(like)]
                pub name: String
            })
        ];
        let refs: Vec<&FieldDef> = fields.iter().collect();
        let result = generate_where_conditions(&refs, false);
        let code = result.to_string();
        assert!(code.contains("status"));
        assert!(code.contains("name"));
        assert!(code.contains("= $"));
        assert!(code.contains("ILIKE"));
    }
}