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
//! Code generation for field modules.
use proc_macro2::TokenStream;
use quote::quote;
use prax_schema::ast::{Field, FieldType, Model, TypeModifier};
use super::{generate_doc_comment, pascal_ident, snake_ident};
use crate::types::field_type_to_rust;
/// Generate the field module with select, order, and set operations.
pub fn generate_field_module(field: &Field, model: &Model) -> TokenStream {
let field_name = snake_ident(field.name());
let field_name_pascal = pascal_ident(field.name());
let field_type = field_type_to_rust(&field.field_type, &TypeModifier::Required);
let _full_field_type = field_type_to_rust(&field.field_type, &field.modifier);
let doc = generate_doc_comment(field.documentation.as_ref().map(|d| d.text.as_str()));
// Get database column name
let col_name = field
.attributes
.iter()
.find(|a| a.name() == "map")
.and_then(|a| a.first_arg())
.and_then(|v| v.as_string())
.map(|s| s.to_string())
.unwrap_or_else(|| field.name().to_string());
let is_optional = field.modifier.is_optional();
let is_list = field.modifier.is_list();
let is_relation = matches!(field.field_type, FieldType::Model(_));
// Generate order by operations
let order_by = if !is_list && !is_relation {
quote! {
/// Order by this field ascending.
pub fn asc() -> super::OrderByParam {
super::OrderByParam::#field_name_pascal(::prax_orm::_prax_prelude::SortOrder::Asc)
}
/// Order by this field descending.
pub fn desc() -> super::OrderByParam {
super::OrderByParam::#field_name_pascal(::prax_orm::_prax_prelude::SortOrder::Desc)
}
}
} else {
TokenStream::new()
};
// Generate set operations for updates
let set_ops = if !is_relation {
let set_type = if is_optional {
quote! { Option<#field_type> }
} else {
field_type.clone()
};
quote! {
/// Set this field to a new value.
pub fn set(value: #set_type) -> super::SetParam {
super::SetParam::#field_name_pascal(value)
}
}
} else {
TokenStream::new()
};
// Increment/decrement helpers intentionally omitted — implementing them
// requires an atomic read-modify-write path (today's execution model is
// `SET col = ?`, not `SET col = col + ?`). The previous codegen emitted
// calls to a phantom `get_current_value()` that never existed, so the
// `pub mod` never compiled when a numeric field was present. Re-add once
// the Client exposes a proper `.increment()/.decrement()` update op.
// Scalar-only members. Relation fields have no `SelectParam` variant
// and no scalar `WhereOp` filters — they are read via the model's
// `IncludeParam` (the `include()` helper below), not selected or
// filtered as columns. Emitting the scalar plumbing for a relation
// references `SelectParam::<Rel>` / `WhereParam::<Rel>` variants and a
// `WhereOp::Equals(<RelatedModel>)` value that don't (and shouldn't)
// exist.
let (select_fn, filters) = if is_relation {
(TokenStream::new(), TokenStream::new())
} else {
let select_fn = quote! {
/// Select this field.
pub fn select() -> super::SelectParam {
super::SelectParam::#field_name_pascal
}
};
let filters = super::filters::generate_field_filters(field, model.name());
(select_fn, filters)
};
// For relation fields, expose a `fetch()` helper returning an
// `IncludeSpec` keyed by the relation's field name. This mirrors the
// derive path's `relation_accessors::emit` output so the two codegen
// paths share the same include API. The prior `include()` -> `IncludeParam`
// helper was divergent from the derive path and exposed a type that
// carries no relation metadata useful to the executor.
let fetch_fn = if is_relation {
let field_name_str = field.name();
quote! {
/// Build an [`::prax_query::relations::IncludeSpec`] for this
/// relation. Used as `include(author::posts::fetch())`.
pub fn fetch() -> ::prax_query::relations::IncludeSpec {
::prax_query::relations::IncludeSpec::new(#field_name_str)
}
}
} else {
TokenStream::new()
};
// COLUMN / IS_OPTIONAL / IS_LIST are meaningful only for scalar fields.
// Emitting them for relation fields is misleading: COLUMN would hold the
// field name (e.g. "posts"), which is not a real database column. Gate
// them behind `!is_relation` so relation field modules contain only the
// fetch() helper (and nothing that implies a backing column).
let scalar_consts = if !is_relation {
quote! {
/// Database column name.
pub const COLUMN: &str = #col_name;
/// Whether this field is optional.
pub const IS_OPTIONAL: bool = #is_optional;
/// Whether this field is a list.
pub const IS_LIST: bool = #is_list;
}
} else {
TokenStream::new()
};
quote! {
#doc
pub mod #field_name {
#scalar_consts
#select_fn
#fetch_fn
#order_by
#set_ops
// Filter operations (WhereOp enum + equals/gt/... constructors)
#filters
}
}
}
/// Generate the select param enum for a model.
pub fn generate_select_param(model: &Model) -> TokenStream {
let variants: Vec<_> = model
.fields
.values()
.filter(|f| !matches!(f.field_type, FieldType::Model(_)))
.map(|f| {
let name = pascal_ident(f.name());
quote! { #name }
})
.collect();
let variant_names: Vec<_> = model
.fields
.values()
.filter(|f| !matches!(f.field_type, FieldType::Model(_)))
.map(|f| {
let name = pascal_ident(f.name());
let col = f
.attributes
.iter()
.find(|a| a.name() == "map")
.and_then(|a| a.first_arg())
.and_then(|v| v.as_string())
.map(|s| s.to_string())
.unwrap_or_else(|| f.name().to_string());
(name, col)
})
.collect();
let column_matches: Vec<_> = variant_names
.iter()
.map(|(name, col)| {
quote! { Self::#name => #col }
})
.collect();
quote! {
/// Fields that can be selected.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SelectParam {
#(#variants,)*
}
impl SelectParam {
/// Get the column name for this field.
pub fn column(&self) -> &'static str {
match self {
#(#column_matches,)*
}
}
}
}
}
/// Generate the order by param enum for a model.
pub fn generate_order_by_param(model: &Model) -> TokenStream {
let variants: Vec<_> = model
.fields
.values()
.filter(|f| !f.modifier.is_list() && !matches!(f.field_type, FieldType::Model(_)))
.map(|f| {
let name = pascal_ident(f.name());
quote! { #name(::prax_orm::_prax_prelude::SortOrder) }
})
.collect();
let variant_names: Vec<_> = model
.fields
.values()
.filter(|f| !f.modifier.is_list() && !matches!(f.field_type, FieldType::Model(_)))
.map(|f| {
let name = pascal_ident(f.name());
let col = f
.attributes
.iter()
.find(|a| a.name() == "map")
.and_then(|a| a.first_arg())
.and_then(|v| v.as_string())
.map(|s| s.to_string())
.unwrap_or_else(|| f.name().to_string());
(name, col)
})
.collect();
let column_matches: Vec<_> = variant_names
.iter()
.map(|(name, col)| {
quote! { Self::#name(order) => (#col, order) }
})
.collect();
quote! {
/// Order by parameters.
#[derive(Debug, Clone, Copy)]
pub enum OrderByParam {
#(#variants,)*
}
impl OrderByParam {
/// Get the column name and sort order.
pub fn column_and_order(&self) -> (&'static str, &::prax_orm::_prax_prelude::SortOrder) {
match self {
#(#column_matches,)*
}
}
/// Generate SQL ORDER BY clause part.
pub fn to_sql(&self) -> String {
let (col, order) = self.column_and_order();
let dir = match order {
::prax_orm::_prax_prelude::SortOrder::Asc => "ASC",
::prax_orm::_prax_prelude::SortOrder::Desc => "DESC",
};
format!("{} {}", col, dir)
}
}
}
}
/// Generate the set param enum for updates.
pub fn generate_set_param(model: &Model) -> TokenStream {
let variants: Vec<_> = model
.fields
.values()
.filter(|f| !matches!(f.field_type, FieldType::Model(_)))
.map(|f| {
let name = pascal_ident(f.name());
let field_type = field_type_to_rust(&f.field_type, &f.modifier);
quote! { #name(#field_type) }
})
.collect();
let variant_names: Vec<_> = model
.fields
.values()
.filter(|f| !matches!(f.field_type, FieldType::Model(_)))
.map(|f| {
let name = pascal_ident(f.name());
let col = f
.attributes
.iter()
.find(|a| a.name() == "map")
.and_then(|a| a.first_arg())
.and_then(|v| v.as_string())
.map(|s| s.to_string())
.unwrap_or_else(|| f.name().to_string());
(name, col)
})
.collect();
let column_matches: Vec<_> = variant_names
.iter()
.map(|(name, col)| {
quote! { Self::#name(_) => #col }
})
.collect();
quote! {
/// Parameters for setting field values in updates.
#[derive(Debug, Clone)]
pub enum SetParam {
#(#variants,)*
}
impl SetParam {
/// Get the column name for this parameter.
pub fn column(&self) -> &'static str {
match self {
#(#column_matches,)*
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use prax_schema::ast::{Ident, ScalarType, Span};
fn make_span() -> Span {
Span::new(0, 0)
}
fn make_ident(name: &str) -> Ident {
Ident::new(name, make_span())
}
fn make_model() -> Model {
let mut model = Model::new(make_ident("User"), make_span());
model.add_field(Field::new(
make_ident("id"),
FieldType::Scalar(ScalarType::Int),
TypeModifier::Required,
vec![],
make_span(),
));
model.add_field(Field::new(
make_ident("name"),
FieldType::Scalar(ScalarType::String),
TypeModifier::Required,
vec![],
make_span(),
));
model.add_field(Field::new(
make_ident("email"),
FieldType::Scalar(ScalarType::String),
TypeModifier::Optional,
vec![],
make_span(),
));
model
}
#[test]
fn test_generate_select_param() {
let model = make_model();
let select = generate_select_param(&model);
let code = select.to_string();
assert!(code.contains("pub enum SelectParam"));
assert!(code.contains("Id"));
assert!(code.contains("Name"));
assert!(code.contains("Email"));
}
#[test]
fn test_generate_order_by_param() {
let model = make_model();
let order_by = generate_order_by_param(&model);
let code = order_by.to_string();
assert!(code.contains("pub enum OrderByParam"));
assert!(code.contains("SortOrder"));
}
#[test]
fn test_generate_set_param() {
let model = make_model();
let set = generate_set_param(&model);
let code = set.to_string();
assert!(code.contains("pub enum SetParam"));
assert!(code.contains("Id"));
assert!(code.contains("Name"));
}
}