@@ -577,3 +577,37 @@ func TestEmptySliceFilterWithAnotherFilter(t *testing.T) {
577
577
q .AddFilter ("another_id" , EQ , uuid .New ().String ())
578
578
t .Log (q .SQL ("test" ))
579
579
}
580
+
581
+ func TestQuery_AddORFilters (t * testing.T ) {
582
+ t .Run ("2 OR conditions" , func (t * testing.T ) {
583
+ q := New ().AddFilter ("test" , EQ , "ok" )
584
+ q .AddORFilters (func (query * Query ) {
585
+ query .AddFilter ("firstname" , ILIKE , "*hello*" )
586
+ query .AddFilter ("lastname" , ILIKE , "*hello*" )
587
+ })
588
+ out := q .SQL ("table" )
589
+ t .Log (out )
590
+ assert .Equal (t , `SELECT * FROM table WHERE test = ? AND (firstname ILIKE ? OR lastname ILIKE ?)` , out )
591
+ })
592
+
593
+ t .Run ("3 OR conditions" , func (t * testing.T ) {
594
+ q := New ().AddFilter ("test" , EQ , "ok" )
595
+ q .AddORFilters (func (query * Query ) {
596
+ query .AddFilter ("firstname" , ILIKE , "*hello*" )
597
+ query .AddFilter ("lastname" , ILIKE , "*hello*" )
598
+ query .AddFilter ("email" , ILIKE , "*hello*" )
599
+ })
600
+ out := q .SQL ("table" )
601
+ t .Log (out )
602
+ assert .Equal (t , `SELECT * FROM table WHERE test = ? AND (firstname ILIKE ? OR lastname ILIKE ? OR email ILIKE ?)` , out )
603
+ })
604
+ }
605
+
606
+ func ExampleQuery_AddORFilters () {
607
+ q := New ().AddFilter ("test" , EQ , "ok" )
608
+ q .AddORFilters (func (query * Query ) {
609
+ query .AddFilter ("firstname" , ILIKE , "*hello*" )
610
+ query .AddFilter ("lastname" , ILIKE , "*hello*" )
611
+ })
612
+ q .SQL ("table" ) // SELECT * FROM table WHERE test = ? AND (firstname ILIKE ? OR lastname ILIKE ?)
613
+ }
0 commit comments