sqlx-paginated 0.3.1

A flexible, type-safe SQLx query builder for dynamic web APIs, offering seamless pagination, searching, filtering, and sorting.
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
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
use crate::paginated_query_as::internal::{
    get_struct_field_names, QueryDateRangeParams, QueryPaginationParams, QuerySearchParams,
    QuerySortParams, DEFAULT_DATE_RANGE_COLUMN_NAME, DEFAULT_MAX_PAGE_SIZE, DEFAULT_MIN_PAGE_SIZE,
    DEFAULT_PAGE,
};
use crate::paginated_query_as::models::QuerySortDirection;
use crate::paginated_query_as::models::{QueryFilterCondition, QueryFilterOperator};
use crate::QueryParams;
use chrono::{DateTime, Utc};
use serde::Serialize;
use std::collections::HashMap;

pub struct QueryParamsBuilder<'q, T> {
    query: QueryParams<'q, T>,
}

impl<T: Default + Serialize> Default for QueryParamsBuilder<'_, T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'q, T: Default + Serialize> QueryParamsBuilder<'q, T> {
    /// Creates a new `QueryParamsBuilder` with default values.
    ///
    /// Default values include:
    /// - Page: 1
    /// - Page size: 10
    /// - Sort column: "created_at"
    /// - Sort direction: Descending
    ///
    /// # Examples
    ///
    /// ```rust
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder};
    ///
    /// #[derive(Serialize, Default)]
    /// struct UserExample {
    ///     name: String
    /// }
    /// let builder = QueryParamsBuilder::<UserExample>::new();
    /// ```
    pub fn new() -> Self {
        Self {
            query: QueryParams::default(),
        }
    }

    /// Creates a new `QueryParamsBuilder` with default values.
    ///
    /// Default values include:
    /// - Page: 1
    /// - Page size: 10
    /// - Sort column: "created_at"
    /// - Sort direction: Descending
    ///
    /// # Examples
    ///
    /// ```rust
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder};
    ///
    /// #[derive(Serialize, Default)]
    /// struct UserExample {
    ///     name: String
    /// }
    /// let builder = QueryParamsBuilder::<UserExample>::new();
    /// ```
    pub fn with_pagination(mut self, page: i64, page_size: i64) -> Self {
        self.query.pagination = QueryPaginationParams {
            page: page.max(DEFAULT_PAGE),
            page_size: page_size.clamp(DEFAULT_MIN_PAGE_SIZE, DEFAULT_MAX_PAGE_SIZE),
        };
        self
    }

    /// Sets sorting parameters.
    ///
    /// # Arguments
    ///
    /// * `sort_column` - Column name to sort by
    /// * `sort_direction` - Direction of sort (Ascending or Descending)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder, QuerySortDirection};
    ///
    /// #[derive(Serialize, Default)]
    /// struct UserExample {
    ///     name: String
    /// }
    ///
    /// let params = QueryParamsBuilder::<UserExample>::new()
    ///     .with_sort("updated_at", QuerySortDirection::Ascending)
    ///     .build();
    /// ```
    pub fn with_sort(
        mut self,
        sort_column: impl Into<String>,
        sort_direction: QuerySortDirection,
    ) -> Self {
        self.query.sort = QuerySortParams {
            sort_column: sort_column.into(),
            sort_direction,
        };
        self
    }

    /// Sets search parameters with multiple columns support.
    ///
    /// # Arguments
    ///
    /// * `search` - Search term to look for
    /// * `search_columns` - Vector of column names to search in
    ///
    /// # Examples
    ///
    /// ```rust
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder, QuerySortDirection};
    ///
    /// #[derive(Serialize, Default)]
    /// struct UserExample {
    ///     name: String
    /// }
    ///
    /// let params = QueryParamsBuilder::<UserExample>::new()
    ///     .with_search("john", vec!["name", "email", "username"])
    ///     .build();
    /// ```
    pub fn with_search(
        mut self,
        search: impl Into<String>,
        search_columns: Vec<impl Into<String>>,
    ) -> Self {
        self.query.search = QuerySearchParams {
            search: Some(search.into()),
            search_columns: Some(search_columns.into_iter().map(Into::into).collect()),
        };
        self
    }

    /// Sets date range parameters for filtering by date.
    ///
    /// # Arguments
    ///
    /// * `date_after` - Optional start date (inclusive)
    /// * `date_before` - Optional end date (inclusive)
    /// * `column_name` - Optional column name to apply date range filter (defaults to created_at)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chrono::{DateTime, Utc};
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder, QuerySortDirection};
    ///
    /// #[derive(Serialize, Default)]
    /// struct UserExample {
    ///     name: String,
    ///     updated_at: DateTime<Utc>
    /// }
    ///
    /// let start = DateTime::parse_from_rfc3339("2024-01-01T00:00:00Z").unwrap().into();
    /// let end = DateTime::parse_from_rfc3339("2024-12-31T23:59:59Z").unwrap().into();
    ///
    /// let params = QueryParamsBuilder::<UserExample>::new()
    ///     .with_date_range(Some(start), Some(end), Some("updated_at"))
    ///     .build();
    /// ```
    pub fn with_date_range(
        mut self,
        date_after: Option<DateTime<Utc>>,
        date_before: Option<DateTime<Utc>>,
        column_name: Option<impl Into<String>>,
    ) -> Self {
        self.query.date_range = QueryDateRangeParams {
            date_after,
            date_before,
            date_column: column_name.map_or_else(
                || Some(DEFAULT_DATE_RANGE_COLUMN_NAME.to_string()),
                |column_name| Some(column_name.into()),
            ),
        };
        self
    }

    /// Adds a single filter condition with an operator.
    ///
    /// # Arguments
    ///
    /// * `key` - Column name to filter on
    /// * `operator` - The comparison operator to use
    /// * `value` - Value to filter by (required for most operators except IS NULL/IS NOT NULL)
    ///
    /// # Details
    ///
    /// Only adds the filter if the column exists in the model struct.
    /// Logs a warning if tracing is enabled and the column is invalid.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder, QueryFilterOperator};
    ///
    /// #[derive(Serialize, Default)]
    /// struct Product {
    ///     name: String,
    ///     price: f64,
    ///     stock: i32,
    ///     status: String,
    /// }
    ///
    /// let params = QueryParamsBuilder::<Product>::new()
    ///     .with_filter_operator("price", QueryFilterOperator::GreaterThan, "10.00")
    ///     .with_filter_operator("stock", QueryFilterOperator::LessOrEqual, "100")
    ///     .with_filter_operator("status", QueryFilterOperator::NotEqual, "deleted")
    ///     .build();
    /// ```
    pub fn with_filter_operator(
        mut self,
        key: impl Into<String>,
        operator: QueryFilterOperator,
        value: impl Into<String>,
    ) -> Self {
        let key = key.into();
        let valid_fields = get_struct_field_names::<T>();

        if valid_fields.contains(&key) {
            self.query
                .filters
                .insert(key, QueryFilterCondition::new(operator, Some(value)));
        } else {
            #[cfg(feature = "tracing")]
            tracing::warn!(column = %key, "Skipping invalid filter column");
        }
        self
    }

    /// Adds a filter condition for IS NULL or IS NOT NULL checks.
    ///
    /// # Arguments
    ///
    /// * `key` - Column name to filter on
    /// * `is_null` - If true, checks IS NULL; if false, checks IS NOT NULL
    ///
    /// # Examples
    ///
    /// ```rust
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder};
    ///
    /// #[derive(Serialize, Default)]
    /// struct User {
    ///     name: String,
    ///     deleted_at: Option<String>,
    /// }
    ///
    /// let params = QueryParamsBuilder::<User>::new()
    ///     .with_filter_null("deleted_at", true)  // IS NULL
    ///     .build();
    /// ```
    pub fn with_filter_null(mut self, key: impl Into<String>, is_null: bool) -> Self {
        let key = key.into();
        let valid_fields = get_struct_field_names::<T>();

        if valid_fields.contains(&key) {
            let condition = if is_null {
                QueryFilterCondition::is_null()
            } else {
                QueryFilterCondition::is_not_null()
            };
            self.query.filters.insert(key, condition);
        } else {
            #[cfg(feature = "tracing")]
            tracing::warn!(column = %key, "Skipping invalid filter column");
        }
        self
    }

    /// Adds an IN filter condition with multiple values.
    ///
    /// # Arguments
    ///
    /// * `key` - Column name to filter on
    /// * `values` - Vector of values to check against
    ///
    /// # Examples
    ///
    /// ```rust
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder};
    ///
    /// #[derive(Serialize, Default)]
    /// struct User {
    ///     name: String,
    ///     role: String,
    /// }
    ///
    /// let params = QueryParamsBuilder::<User>::new()
    ///     .with_filter_in("role", vec!["admin", "moderator", "user"])
    ///     .build();
    /// ```
    pub fn with_filter_in(
        mut self,
        key: impl Into<String>,
        values: Vec<impl Into<String>>,
    ) -> Self {
        let key = key.into();
        let valid_fields = get_struct_field_names::<T>();

        if valid_fields.contains(&key) {
            self.query
                .filters
                .insert(key, QueryFilterCondition::in_list(values));
        } else {
            #[cfg(feature = "tracing")]
            tracing::warn!(column = %key, "Skipping invalid filter column");
        }
        self
    }

    /// Adds a NOT IN filter condition with multiple values.
    ///
    /// # Arguments
    ///
    /// * `key` - Column name to filter on
    /// * `values` - Vector of values to exclude
    ///
    /// # Examples
    ///
    /// ```rust
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder};
    ///
    /// #[derive(Serialize, Default)]
    /// struct User {
    ///     name: String,
    ///     role: String,
    /// }
    ///
    /// let params = QueryParamsBuilder::<User>::new()
    ///     .with_filter_not_in("role", vec!["banned", "suspended"])
    ///     .build();
    /// ```
    pub fn with_filter_not_in(
        mut self,
        key: impl Into<String>,
        values: Vec<impl Into<String>>,
    ) -> Self {
        let key = key.into();
        let valid_fields = get_struct_field_names::<T>();

        if valid_fields.contains(&key) {
            self.query
                .filters
                .insert(key, QueryFilterCondition::not_in_list(values));
        } else {
            #[cfg(feature = "tracing")]
            tracing::warn!(column = %key, "Skipping invalid filter column");
        }
        self
    }

    /// Adds a simple equality filter condition (backward compatible).
    ///
    /// # Arguments
    ///
    /// * `key` - Column name to filter on
    /// * `value` - Optional value to filter by
    ///
    /// # Details
    ///
    /// Only adds the filter if the column exists in the model struct.
    /// Logs a warning if tracing is enabled and the column is invalid.
    /// This method maintains backward compatibility with the original API.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::any::Any;
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder};
    ///
    /// #[derive(Serialize, Default)]
    /// struct UserExample {
    ///     name: String,
    ///     status: String,
    ///     role: String
    /// }
    ///
    /// let params = QueryParamsBuilder::<UserExample>::new()
    ///     .with_filter("status", Some("active"))
    ///     .with_filter("role", Some("admin"))
    ///     .build();
    /// ```
    pub fn with_filter(mut self, key: impl Into<String>, value: Option<impl Into<String>>) -> Self {
        let key = key.into();
        let valid_fields = get_struct_field_names::<T>();

        if valid_fields.contains(&key) {
            if let Some(val) = value {
                self.query
                    .filters
                    .insert(key, QueryFilterCondition::equal(val));
            }
        } else {
            #[cfg(feature = "tracing")]
            tracing::warn!(column = %key, "Skipping invalid filter column");
        }
        self
    }

    /// Adds multiple filter conditions from a HashMap (backward compatible).
    ///
    /// # Arguments
    ///
    /// * `filters` - HashMap of column names and their filter values (equality only)
    ///
    /// # Details
    ///
    /// Only adds filters for columns that exist in the model struct.
    /// Logs a warning if tracing is enabled and a column is invalid.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::collections::HashMap;
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder};
    ///
    /// #[derive(Serialize, Default)]
    /// struct UserExample {
    ///     name: String,
    ///     status: String,
    ///     role: String
    /// }
    ///
    /// let mut filters = HashMap::new();
    /// filters.insert("status", Some("active"));
    /// filters.insert("role", Some("admin"));
    ///
    /// let params = QueryParamsBuilder::<UserExample>::new()
    ///     .with_filters(filters)
    ///     .build();
    /// ```
    pub fn with_filters(
        mut self,
        filters: HashMap<impl Into<String>, Option<impl Into<String>>>,
    ) -> Self {
        let valid_fields = get_struct_field_names::<T>();

        self.query
            .filters
            .extend(filters.into_iter().filter_map(|(key, value)| {
                let key = key.into();
                if valid_fields.contains(&key) {
                    value.map(|v| (key, QueryFilterCondition::equal(v)))
                } else {
                    #[cfg(feature = "tracing")]
                    tracing::warn!(column = %key, "Skipping invalid filter column");
                    None
                }
            }));

        self
    }

    /// Adds multiple filter conditions with operators from a HashMap.
    ///
    /// # Arguments
    ///
    /// * `filters` - HashMap of column names and their FilterConditions
    ///
    /// # Details
    ///
    /// Only adds filters for columns that exist in the model struct.
    /// Logs a warning if tracing is enabled and a column is invalid.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::collections::HashMap;
    /// use serde::{Serialize};
    /// use sqlx_paginated::{QueryParamsBuilder, QueryFilterCondition, QueryFilterOperator};
    ///
    /// #[derive(Serialize, Default)]
    /// struct Product {
    ///     name: String,
    ///     price: f64,
    ///     stock: i32,
    /// }
    ///
    /// let mut filters = HashMap::new();
    /// filters.insert("price", QueryFilterCondition::greater_than("10.00"));
    /// filters.insert("stock", QueryFilterCondition::less_or_equal("100"));
    ///
    /// let params = QueryParamsBuilder::<Product>::new()
    ///     .with_filter_conditions(filters)
    ///     .build();
    /// ```
    pub fn with_filter_conditions(
        mut self,
        filters: HashMap<impl Into<String>, QueryFilterCondition>,
    ) -> Self {
        let valid_fields = get_struct_field_names::<T>();

        self.query
            .filters
            .extend(filters.into_iter().filter_map(|(key, condition)| {
                let key = key.into();
                if valid_fields.contains(&key) {
                    Some((key, condition))
                } else {
                    #[cfg(feature = "tracing")]
                    tracing::warn!(column = %key, "Skipping invalid filter column");
                    None
                }
            }));

        self
    }

    /// Builds and returns the final QueryParams.
    ///
    /// # Returns
    ///
    /// Returns the constructed `QueryParams<T>` with all the configured parameters.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use chrono::{DateTime, Utc};
    /// use sqlx_paginated::{QueryParamsBuilder, QuerySortDirection};
    /// use serde::{Serialize};
    ///
    /// #[derive(Serialize, Default)]
    /// struct UserExample {
    ///     name: String,
    ///     status: String,
    ///     email: String,
    ///     created_at: DateTime<Utc>
    /// }
    ///
    /// let params = QueryParamsBuilder::<UserExample>::new()
    ///     .with_pagination(1, 20)
    ///     .with_sort("created_at", QuerySortDirection::Descending)
    ///     .with_search("john", vec!["name", "email"])
    ///     .with_filter("status", Some("active"))
    ///     .build();
    /// ```
    pub fn build(self) -> QueryParams<'q, T> {
        self.query
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::paginated_query_as::internal::{
        DEFAULT_SEARCH_COLUMN_NAMES, DEFAULT_SORT_COLUMN_NAME,
    };
    use crate::paginated_query_as::models::QuerySortDirection;
    use chrono::{DateTime, Utc};
    use std::collections::HashMap;

    #[derive(Debug, Default, Serialize)]
    struct TestModel {
        name: String,
        title: String,
        description: String,
        status: String,
        category: String,
        updated_at: DateTime<Utc>,
        created_at: DateTime<Utc>,
    }

    #[test]
    fn test_pagination_defaults() {
        let params = QueryParamsBuilder::<TestModel>::new().build();

        assert_eq!(
            params.pagination.page_size, DEFAULT_MIN_PAGE_SIZE,
            "Default page size should be {}",
            DEFAULT_MIN_PAGE_SIZE
        );
        assert_eq!(
            params.pagination.page, DEFAULT_PAGE,
            "Default page should be {}",
            DEFAULT_PAGE
        );

        // Test page size clamping
        let params = QueryParamsBuilder::<TestModel>::new()
            .with_pagination(1, DEFAULT_MAX_PAGE_SIZE + 10)
            .build();

        assert_eq!(
            params.pagination.page_size, DEFAULT_MAX_PAGE_SIZE,
            "Page size should be clamped to maximum {}",
            DEFAULT_MAX_PAGE_SIZE
        );

        let params = QueryParamsBuilder::<TestModel>::new()
            .with_pagination(1, DEFAULT_MIN_PAGE_SIZE - 5)
            .build();

        assert_eq!(
            params.pagination.page_size, DEFAULT_MIN_PAGE_SIZE,
            "Page size should be clamped to minimum {}",
            DEFAULT_MIN_PAGE_SIZE
        );
    }

    #[test]
    fn test_default_sort_column() {
        let params = QueryParamsBuilder::<TestModel>::new().build();

        assert_eq!(
            params.sort.sort_column, DEFAULT_SORT_COLUMN_NAME,
            "Default sort column should be '{}'",
            DEFAULT_SORT_COLUMN_NAME
        );
    }

    #[test]
    fn test_date_range_defaults() {
        let params = QueryParamsBuilder::<TestModel>::new().build();

        assert_eq!(
            params.date_range.date_column,
            Some(DEFAULT_DATE_RANGE_COLUMN_NAME.to_string()),
            "Default date range column should be '{}'",
            DEFAULT_DATE_RANGE_COLUMN_NAME
        );
        assert!(
            params.date_range.date_after.is_none(),
            "Default date_after should be None"
        );
        assert!(
            params.date_range.date_before.is_none(),
            "Default date_before should be None"
        );
    }

    #[test]
    fn test_search_defaults() {
        let params = QueryParamsBuilder::<TestModel>::new().build();

        // Check if default search columns are set
        assert_eq!(
            params.search.search_columns,
            Some(
                DEFAULT_SEARCH_COLUMN_NAMES
                    .iter()
                    .map(|&s| s.to_string())
                    .collect()
            ),
            "Default search columns should be {:?}",
            DEFAULT_SEARCH_COLUMN_NAMES
        );
        assert!(
            params.search.search.is_none(),
            "Default search term should be None"
        );
    }

    #[test]
    fn test_combined_defaults() {
        let params = QueryParamsBuilder::<TestModel>::new().build();

        // Test all defaults together
        assert_eq!(params.pagination.page, DEFAULT_PAGE);
        assert_eq!(params.pagination.page_size, DEFAULT_MIN_PAGE_SIZE);
        assert_eq!(params.sort.sort_column, DEFAULT_SORT_COLUMN_NAME);
        assert_eq!(params.sort.sort_direction, QuerySortDirection::Descending);
        assert_eq!(
            params.date_range.date_column,
            Some(DEFAULT_DATE_RANGE_COLUMN_NAME.to_string())
        );
        assert_eq!(
            params.search.search_columns,
            Some(
                DEFAULT_SEARCH_COLUMN_NAMES
                    .iter()
                    .map(|&s| s.to_string())
                    .collect()
            )
        );
        assert!(params.search.search.is_none());
        assert!(params.date_range.date_after.is_none());
        assert!(params.date_range.date_before.is_none());
    }

    #[test]
    fn test_empty_params() {
        let params = QueryParamsBuilder::<TestModel>::new().build();

        assert_eq!(params.pagination.page, 1);
        assert_eq!(params.pagination.page_size, 10);
        assert_eq!(params.sort.sort_column, "created_at");
        assert!(matches!(
            params.sort.sort_direction,
            QuerySortDirection::Descending
        ));
    }

    #[test]
    fn test_partial_params() {
        let params = QueryParamsBuilder::<TestModel>::new()
            .with_pagination(2, 10)
            .with_search("test".to_string(), vec!["name".to_string()])
            .build();

        assert_eq!(params.pagination.page, 2);
        assert_eq!(params.search.search, Some("test".to_string()));
        assert_eq!(params.pagination.page_size, 10);
        assert_eq!(params.sort.sort_column, "created_at");
        assert!(matches!(
            params.sort.sort_direction,
            QuerySortDirection::Descending
        ));
    }

    #[test]
    fn test_invalid_params() {
        // For builder pattern, invalid params would be handled at compile time
        // But we can test the defaults
        let params = QueryParamsBuilder::<TestModel>::new()
            .with_pagination(0, 0) // Should be clamped to minimum values
            .build();

        assert_eq!(params.pagination.page, 1);
        assert_eq!(params.pagination.page_size, 10);
    }

    #[test]
    fn test_filters() {
        let mut filters = HashMap::new();
        filters.insert("status".to_string(), Some("active".to_string()));
        filters.insert("category".to_string(), Some("test".to_string()));

        let params = QueryParamsBuilder::<TestModel>::new()
            .with_filters(filters)
            .build();

        assert!(params.filters.contains_key("status"));
        let status_filter = params.filters.get("status").unwrap();
        assert_eq!(status_filter.operator, QueryFilterOperator::Equal);
        assert_eq!(status_filter.value, Some("active".to_string()));

        assert!(params.filters.contains_key("category"));
        let category_filter = params.filters.get("category").unwrap();
        assert_eq!(category_filter.operator, QueryFilterOperator::Equal);
        assert_eq!(category_filter.value, Some("test".to_string()));
    }

    #[test]
    fn test_search_with_columns() {
        let params = QueryParamsBuilder::<TestModel>::new()
            .with_search(
                "test".to_string(),
                vec!["title".to_string(), "description".to_string()],
            )
            .build();

        assert_eq!(params.search.search, Some("test".to_string()));
        assert_eq!(
            params.search.search_columns,
            Some(vec!["title".to_string(), "description".to_string()])
        );
    }

    #[test]
    fn test_full_params() {
        let params = QueryParamsBuilder::<TestModel>::new()
            .with_pagination(2, 20)
            .with_sort("updated_at".to_string(), QuerySortDirection::Ascending)
            .with_search(
                "test".to_string(),
                vec!["title".to_string(), "description".to_string()],
            )
            .with_date_range(Some(Utc::now()), None, None::<String>)
            .build();

        assert_eq!(params.pagination.page, 2);
        assert_eq!(params.pagination.page_size, 20);
        assert_eq!(params.sort.sort_column, "updated_at");
        assert!(matches!(
            params.sort.sort_direction,
            QuerySortDirection::Ascending
        ));
        assert_eq!(params.search.search, Some("test".to_string()));
        assert_eq!(
            params.search.search_columns,
            Some(vec!["title".to_string(), "description".to_string()])
        );
        assert!(params.date_range.date_after.is_some());
        assert!(params.date_range.date_before.is_none());
    }

    #[test]
    fn test_filter_chain() {
        let params = QueryParamsBuilder::<TestModel>::new()
            .with_filter("status", Some("active"))
            .with_filter("category", Some("test"))
            .build();

        let status_filter = params.filters.get("status").unwrap();
        assert_eq!(status_filter.operator, QueryFilterOperator::Equal);
        assert_eq!(status_filter.value, Some("active".to_string()));

        let category_filter = params.filters.get("category").unwrap();
        assert_eq!(category_filter.operator, QueryFilterOperator::Equal);
        assert_eq!(category_filter.value, Some("test".to_string()));
    }

    #[test]
    fn test_mixed_pagination() {
        let params = QueryParamsBuilder::<TestModel>::new()
            .with_pagination(2, 10)
            .with_search("test".to_string(), vec!["title".to_string()])
            .with_filter("status", Some("active"))
            .build();

        assert_eq!(params.pagination.page, 2);
        assert_eq!(params.pagination.page_size, 10);
        assert_eq!(params.search.search, Some("test".to_string()));

        let status_filter = params.filters.get("status").unwrap();
        assert_eq!(status_filter.operator, QueryFilterOperator::Equal);
        assert_eq!(status_filter.value, Some("active".to_string()));
    }

    #[test]
    fn test_filter_operators() {
        let params = QueryParamsBuilder::<TestModel>::new()
            .with_filter_operator("title", QueryFilterOperator::Like, "%test%")
            .with_filter_operator("status", QueryFilterOperator::NotEqual, "deleted")
            .build();

        let title_filter = params.filters.get("title").unwrap();
        assert_eq!(title_filter.operator, QueryFilterOperator::Like);
        assert_eq!(title_filter.value, Some("%test%".to_string()));

        let status_filter = params.filters.get("status").unwrap();
        assert_eq!(status_filter.operator, QueryFilterOperator::NotEqual);
        assert_eq!(status_filter.value, Some("deleted".to_string()));
    }

    #[test]
    fn test_filter_null() {
        let params = QueryParamsBuilder::<TestModel>::new()
            .with_filter_null("description", true)
            .build();

        let filter = params.filters.get("description").unwrap();
        assert_eq!(filter.operator, QueryFilterOperator::IsNull);
        assert_eq!(filter.value, None);
    }

    #[test]
    fn test_filter_in() {
        let params = QueryParamsBuilder::<TestModel>::new()
            .with_filter_in("status", vec!["active", "pending", "approved"])
            .build();

        let filter = params.filters.get("status").unwrap();
        assert_eq!(filter.operator, QueryFilterOperator::In);
        assert_eq!(filter.value, Some("active,pending,approved".to_string()));

        let values = filter.split_values();
        assert_eq!(values, vec!["active", "pending", "approved"]);
    }
}