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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
//! Query struct generation for Model derive macro.
//!
//! This module generates the dynamic query builder (e.g., `ProductQuery`) with:
//! - Column name constants
//! - Filtering methods (eq, ne, gt, gte, lt, lte, like, ilike, etc.)
//! - Ordering methods (order_by_asc, order_by_desc)
//! - Pagination methods (limit, offset, page)
//! - Execution methods (find, find_one, find_one_opt, count)
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::ext::IdentExt;
/// Query field info for generating the Query struct
pub(super) struct QueryFieldInfo {
/// The field name in the struct
pub(super) name: syn::Ident,
/// The column name in the database
pub(super) column: String,
/// Whether this field comes from a joined table (skip in query struct)
pub(super) is_joined: bool,
}
/// Generate the Query struct for dynamic queries.
pub(super) fn generate_query_struct(
model_name: &syn::Ident,
table_name: &str,
fields: &[QueryFieldInfo],
has_joins: bool,
) -> TokenStream {
let query_name = format_ident!("{}Query", model_name);
// Filter out joined table fields for the query struct
let query_fields: Vec<_> = fields.iter().filter(|f| !f.is_joined).collect();
// Generate column constants
let column_consts = gen_column_consts(&query_fields, table_name, has_joins);
// Generate the base SQL depending on whether we have JOINs
let base_sql = if has_joins {
quote! {
::std::format!(
"SELECT {} FROM {} {}",
#model_name::SELECT_LIST,
#model_name::TABLE,
#model_name::JOIN_CLAUSE
)
}
} else {
quote! {
::std::format!(
"SELECT {} FROM {}",
#model_name::SELECT_LIST,
#model_name::TABLE
)
}
};
// Generate filtering methods
let filtering_methods = gen_filtering_methods();
// Generate ordering methods
let ordering_methods = gen_ordering_methods();
// Generate pagination methods
let pagination_methods = gen_pagination_methods();
// Generate execution methods
let execution_methods = gen_execution_methods(model_name, has_joins);
quote! {
/// Dynamic query builder for #model_name.
///
/// Supports flexible filtering with chainable methods and pagination.
///
/// # Example
/// ```ignore
/// // Simple equality
/// let products = Product::query()
/// .eq("category_id", 1_i64)?
/// .find(&client).await?;
///
/// // ILIKE query (case-insensitive pattern match)
/// let products = Product::query()
/// .ilike("name", "%phone%")?
/// .find(&client).await?;
///
/// // Range query
/// let products = Product::query()
/// .gte("price_cents", 1000_i64)?
/// .lt("price_cents", 5000_i64)?
/// .find(&client).await?;
///
/// // IN query
/// let products = Product::query()
/// .in_list("category_id", vec![1_i64, 2, 3])?
/// .find(&client).await?;
///
/// // Pagination + ordering
/// let products = Product::query()
/// .eq("in_stock", true)?
/// .page(1, 10)?
/// .order_by_desc("created_at")?
/// .find(&client).await?;
/// ```
#[derive(Debug, Clone)]
pub struct #query_name {
where_expr: pgorm::WhereExpr,
order_by: pgorm::OrderBy,
pagination: pgorm::Pagination,
}
impl #query_name {
/// Column name constants for type-safe queries.
/// Use these instead of string literals to avoid typos.
#(#column_consts)*
}
impl Default for #query_name {
fn default() -> Self {
Self {
where_expr: pgorm::WhereExpr::And(::std::vec::Vec::new()),
order_by: pgorm::OrderBy::new(),
pagination: pgorm::Pagination::new(),
}
}
}
impl #query_name {
/// Create a new empty query.
pub fn new() -> Self {
Self::default()
}
// ==================== Filtering ====================
#filtering_methods
// ==================== Ordering ====================
#ordering_methods
// ==================== Pagination ====================
#pagination_methods
// ==================== Execution ====================
fn build_base_sql(&self) -> pgorm::Sql {
let mut q = pgorm::sql(#base_sql);
if !self.where_expr.is_trivially_true() {
q.push(" WHERE ");
self.where_expr.append_to_sql(&mut q);
}
q
}
fn build_find_sql(&self) -> pgorm::Sql {
let mut q = self.build_base_sql();
self.order_by.append_to_sql(&mut q);
self.pagination.append_to_sql(&mut q);
q
}
fn build_first_sql(&self) -> pgorm::Sql {
let mut q = self.build_base_sql();
self.order_by.append_to_sql(&mut q);
q.limit(1);
q
}
#execution_methods
}
impl #model_name {
/// Create a new query builder for dynamic queries.
pub fn query() -> #query_name {
#query_name::new()
}
}
}
}
/// Generate column constants for type-safe queries.
///
/// We generate two forms:
/// - `<field_name>` (lowercase) for ergonomics when it doesn't conflict with methods.
/// - `COL_<FIELD_NAME>` (uppercase) as a conflict-free fallback.
fn gen_column_consts(
query_fields: &[&QueryFieldInfo],
table_name: &str,
has_joins: bool,
) -> Vec<TokenStream> {
query_fields
.iter()
.map(|f| {
let field_ident = &f.name;
let field_name = f.name.unraw().to_string();
let col = if has_joins && !f.column.contains('.') {
format!("{}.{}", table_name, f.column)
} else {
f.column.clone()
};
let upper_const_name = format_ident!("COL_{}", field_name.to_uppercase());
let is_reserved = matches!(
field_name.as_str(),
"new"
| "apply_if"
| "apply_if_some"
| "apply_if_ok"
| "eq"
| "eq_str"
| "eq_opt"
| "eq_opt_str"
| "eq_opt_map"
| "ne"
| "gt"
| "gte"
| "gte_opt"
| "lt"
| "lte"
| "lte_opt"
| "range_opt"
| "like"
| "ilike"
| "not_like"
| "not_ilike"
| "is_null"
| "is_not_null"
| "in_list"
| "not_in"
| "between"
| "not_between"
| "and"
| "or"
| "raw"
| "paginate"
| "limit"
| "offset"
| "page"
| "order_by"
| "order_by_asc"
| "order_by_desc"
| "order_by_raw"
| "find"
| "count"
| "find_one"
| "find_one_opt"
);
if is_reserved {
quote! {
pub const #upper_const_name: &'static str = #col;
}
} else {
quote! {
#[allow(non_upper_case_globals)]
pub const #field_ident: &'static str = #col;
pub const #upper_const_name: &'static str = #col;
}
}
})
.collect()
}
/// Generate filtering methods (eq, ne, gt, gte, lt, lte, like, ilike, etc.)
fn gen_filtering_methods() -> TokenStream {
quote! {
/// Apply a transformation to the query only when `cond` is true.
///
/// This is a small ergonomic helper for dynamic queries.
#[inline]
pub fn apply_if(
self,
cond: bool,
f: impl FnOnce(Self) -> pgorm::OrmResult<Self>,
) -> pgorm::OrmResult<Self> {
if cond {
f(self)
} else {
::std::result::Result::Ok(self)
}
}
/// Apply a transformation to the query only when `v` is `Some`.
#[inline]
pub fn apply_if_some<T>(
self,
v: ::std::option::Option<T>,
f: impl FnOnce(Self, T) -> pgorm::OrmResult<Self>,
) -> pgorm::OrmResult<Self> {
match v {
::std::option::Option::Some(v) => f(self, v),
::std::option::Option::None => ::std::result::Result::Ok(self),
}
}
/// Apply a transformation to the query only when `v` is `Ok`.
///
/// The `Err(_)` case is treated as a no-op.
#[inline]
pub fn apply_if_ok<T, E>(
self,
v: ::std::result::Result<T, E>,
f: impl FnOnce(Self, T) -> pgorm::OrmResult<Self>,
) -> pgorm::OrmResult<Self> {
match v {
::std::result::Result::Ok(v) => f(self, v),
::std::result::Result::Err(_) => ::std::result::Result::Ok(self),
}
}
/// Combine the current WHERE expression with another using `AND`.
pub fn and(mut self, expr: pgorm::WhereExpr) -> Self {
let current = self.where_expr;
self.where_expr = current.and_with(expr);
self
}
/// Combine the current WHERE expression with another using `OR`.
pub fn or(mut self, expr: pgorm::WhereExpr) -> Self {
let current = self.where_expr;
self.where_expr = current.or_with(expr);
self
}
/// Filter by equality: column = value
pub fn eq<T>(mut self, column: impl pgorm::IntoIdent, value: T) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::eq(column, value)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by equality: column = value
///
/// This is a convenience method for string-like inputs (e.g. `&str`) since
/// the dynamic query builder requires bind values to be `'static`.
#[inline]
pub fn eq_str(
self,
column: impl pgorm::IntoIdent,
value: impl ::core::convert::Into<::std::string::String>,
) -> pgorm::OrmResult<Self> {
self.eq(column, value.into())
}
/// Filter by equality (optional): only applies when `value` is `Some`.
#[inline]
pub fn eq_opt<T>(
self,
column: impl pgorm::IntoIdent,
value: ::std::option::Option<T>,
) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
self.apply_if_some(value, |q, v| q.eq(column, v))
}
/// Filter by equality (optional): column = value (string-like).
///
/// Useful when your input is `Option<&str>` and you don't want to write
/// `value.map(|s| s.to_string())` everywhere.
#[inline]
pub fn eq_opt_str(
self,
column: impl pgorm::IntoIdent,
value: ::std::option::Option<impl ::core::convert::Into<::std::string::String>>,
) -> pgorm::OrmResult<Self> {
self.apply_if_some(value, |q, v| q.eq(column, v.into()))
}
/// Filter by equality (optional): column = map(value)
///
/// This is a small helper for cases like `Option<&str>` + `parse()`, where
/// a failed conversion should just skip the filter.
#[inline]
pub fn eq_opt_map<S, T>(
self,
column: impl pgorm::IntoIdent,
value: ::std::option::Option<S>,
f: impl FnOnce(S) -> ::std::option::Option<T>,
) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
match value.and_then(f) {
::std::option::Option::Some(v) => self.eq(column, v),
::std::option::Option::None => ::std::result::Result::Ok(self),
}
}
/// Filter by inequality: column != value
pub fn ne<T>(mut self, column: impl pgorm::IntoIdent, value: T) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::ne(column, value)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by greater than: column > value
pub fn gt<T>(mut self, column: impl pgorm::IntoIdent, value: T) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::gt(column, value)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by greater than or equal: column >= value
pub fn gte<T>(mut self, column: impl pgorm::IntoIdent, value: T) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::gte(column, value)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by greater than or equal (optional): only applies when `value` is `Some`.
#[inline]
pub fn gte_opt<T>(
self,
column: impl pgorm::IntoIdent,
value: ::std::option::Option<T>,
) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
self.apply_if_some(value, |q, v| q.gte(column, v))
}
/// Filter by less than: column < value
pub fn lt<T>(mut self, column: impl pgorm::IntoIdent, value: T) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::lt(column, value)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by less than or equal: column <= value
pub fn lte<T>(mut self, column: impl pgorm::IntoIdent, value: T) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::lte(column, value)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by less than or equal (optional): only applies when `value` is `Some`.
#[inline]
pub fn lte_opt<T>(
self,
column: impl pgorm::IntoIdent,
value: ::std::option::Option<T>,
) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
self.apply_if_some(value, |q, v| q.lte(column, v))
}
/// Filter by an optional inclusive range: `column >= from AND column <= to`.
#[inline]
pub fn range_opt<I, T>(
self,
column: I,
from: ::std::option::Option<T>,
to: ::std::option::Option<T>,
) -> pgorm::OrmResult<Self>
where
I: pgorm::IntoIdent + ::core::clone::Clone,
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
self.gte_opt(column.clone(), from)?.lte_opt(column, to)
}
/// Filter by LIKE pattern: column LIKE pattern
pub fn like<T>(mut self, column: impl pgorm::IntoIdent, pattern: T) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::like(column, pattern)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by case-insensitive ILIKE pattern: column ILIKE pattern
pub fn ilike<T>(mut self, column: impl pgorm::IntoIdent, pattern: T) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::ilike(column, pattern)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by NOT LIKE pattern: column NOT LIKE pattern
pub fn not_like<T>(mut self, column: impl pgorm::IntoIdent, pattern: T) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::not_like(column, pattern)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by NOT ILIKE pattern: column NOT ILIKE pattern
pub fn not_ilike<T>(mut self, column: impl pgorm::IntoIdent, pattern: T) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::not_ilike(column, pattern)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by IS NULL: column IS NULL
pub fn is_null(mut self, column: impl pgorm::IntoIdent) -> pgorm::OrmResult<Self> {
let cond = pgorm::Condition::is_null(column)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by IS NOT NULL: column IS NOT NULL
pub fn is_not_null(mut self, column: impl pgorm::IntoIdent) -> pgorm::OrmResult<Self> {
let cond = pgorm::Condition::is_not_null(column)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by IN list: column IN (values...)
pub fn in_list<T>(mut self, column: impl pgorm::IntoIdent, values: ::std::vec::Vec<T>) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::in_list(column, values)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by NOT IN list: column NOT IN (values...)
pub fn not_in<T>(mut self, column: impl pgorm::IntoIdent, values: ::std::vec::Vec<T>) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::not_in(column, values)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by BETWEEN: column BETWEEN from AND to
pub fn between<T>(
mut self,
column: impl pgorm::IntoIdent,
from: T,
to: T,
) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::between(column, from, to)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Filter by NOT BETWEEN: column NOT BETWEEN from AND to
pub fn not_between<T>(
mut self,
column: impl pgorm::IntoIdent,
from: T,
to: T,
) -> pgorm::OrmResult<Self>
where
T: ::tokio_postgres::types::ToSql + ::core::marker::Send + ::core::marker::Sync + 'static,
{
let cond = pgorm::Condition::not_between(column, from, to)?;
let current = self.where_expr;
self.where_expr = current.and_with(cond.into());
::std::result::Result::Ok(self)
}
/// Add a raw WHERE expression (escape hatch).
///
/// # Safety
/// Be careful with SQL injection when using raw expressions.
pub fn raw(mut self, sql: impl ::core::convert::Into<::std::string::String>) -> Self {
let current = self.where_expr;
self.where_expr = current.and_with(pgorm::WhereExpr::raw(sql));
self
}
}
}
/// Generate ordering methods (order_by_asc, order_by_desc, order_by_raw)
fn gen_ordering_methods() -> TokenStream {
quote! {
/// Replace the ORDER BY builder.
pub fn order_by(mut self, order_by: pgorm::OrderBy) -> Self {
self.order_by = order_by;
self
}
/// Add an ascending sort.
pub fn order_by_asc(mut self, column: impl pgorm::IntoIdent) -> pgorm::OrmResult<Self> {
let order = self.order_by;
self.order_by = order.asc(column)?;
::std::result::Result::Ok(self)
}
/// Add a descending sort.
pub fn order_by_desc(mut self, column: impl pgorm::IntoIdent) -> pgorm::OrmResult<Self> {
let order = self.order_by;
self.order_by = order.desc(column)?;
::std::result::Result::Ok(self)
}
/// Add a raw ORDER BY item (escape hatch).
///
/// # Safety
/// Be careful with SQL injection when using raw ORDER BY strings.
pub fn order_by_raw(mut self, sql: impl ::core::convert::Into<::std::string::String>) -> Self {
let order = self.order_by;
self.order_by = order.add(pgorm::OrderItem::raw(sql));
self
}
}
}
/// Generate pagination methods (limit, offset, page, paginate)
fn gen_pagination_methods() -> TokenStream {
quote! {
/// Replace the pagination builder.
pub fn paginate(mut self, pagination: pgorm::Pagination) -> Self {
self.pagination = pagination;
self
}
/// Set LIMIT.
pub fn limit(mut self, limit: i64) -> Self {
self.pagination = self.pagination.limit(limit);
self
}
/// Set OFFSET.
pub fn offset(mut self, offset: i64) -> Self {
self.pagination = self.pagination.offset(offset);
self
}
/// Page-based pagination (page numbers start at 1).
pub fn page(mut self, page: i64, per_page: i64) -> pgorm::OrmResult<Self> {
self.pagination = pgorm::Pagination::page(page, per_page)?;
::std::result::Result::Ok(self)
}
}
}
/// Generate execution methods (find, find_one, find_one_opt, count)
fn gen_execution_methods(model_name: &syn::Ident, has_joins: bool) -> TokenStream {
quote! {
/// Execute the query and return matching records.
pub async fn find(
&self,
conn: &impl pgorm::GenericClient,
) -> pgorm::OrmResult<::std::vec::Vec<#model_name>>
where
#model_name: pgorm::FromRow,
{
let q = self.build_find_sql();
q.fetch_all_as(conn).await
}
/// Count the number of matching records.
pub async fn count(&self, conn: &impl pgorm::GenericClient) -> pgorm::OrmResult<i64> {
let mut q = pgorm::sql(if #has_joins {
::std::format!(
"SELECT COUNT(*) FROM {} {}",
#model_name::TABLE,
#model_name::JOIN_CLAUSE
)
} else {
::std::format!("SELECT COUNT(*) FROM {}", #model_name::TABLE)
});
if !self.where_expr.is_trivially_true() {
q.push(" WHERE ");
self.where_expr.append_to_sql(&mut q);
}
q.fetch_scalar_one(conn).await
}
/// Execute the query and return the first matching record.
pub async fn find_one(
&self,
conn: &impl pgorm::GenericClient,
) -> pgorm::OrmResult<#model_name>
where
#model_name: pgorm::FromRow,
{
let q = self.build_first_sql();
q.fetch_one_as(conn).await
}
/// Execute the query and return the first matching record, or None if not found.
pub async fn find_one_opt(
&self,
conn: &impl pgorm::GenericClient,
) -> pgorm::OrmResult<::std::option::Option<#model_name>>
where
#model_name: pgorm::FromRow,
{
let q = self.build_first_sql();
q.fetch_opt_as(conn).await
}
}
}