diff --git a/filterapply_test.go b/filterapply_test.go index 6a3a7d8..2de339c 100644 --- a/filterapply_test.go +++ b/filterapply_test.go @@ -266,3 +266,19 @@ func TestFilterApply_Like(t *testing.T) { assert.Nil(t, err) assert.Len(t, models, 2) } + +func TestFilterApply_ILike(t *testing.T) { + killDb, err := testdata.SetupTestDatabase("test") + defer killDb() + assert.Nil(t, err) + dbp, err := yaorm.NewDBProvider(context.TODO(), "test") + assert.Nil(t, err) + category := &testdata.TwoI{Name: "category"} + saveModel(t, dbp, category) + category2 := &testdata.TwoI{Name: "categoryY"} + saveModel(t, dbp, category2) + + models, err := yaorm.GenericSelectAll(dbp, testdata.NewTwoIFilter().Name(yaormfilter.ILike("categor%"))) + assert.Nil(t, err) + assert.Len(t, models, 2) +} diff --git a/yaormfilter/boolfilter.go b/yaormfilter/boolfilter.go index eb3a832..f59af3d 100644 --- a/yaormfilter/boolfilter.go +++ b/yaormfilter/boolfilter.go @@ -42,6 +42,11 @@ func (f *BoolFilter) Like(v interface{}) ValueFilter { return f } +// ILike is not applicable on bool +func (f *BoolFilter) ILike(v interface{}) ValueFilter { + return f +} + // Nil adds a nil filter func (f *BoolFilter) Nil(v bool) ValueFilter { f.nil(v) diff --git a/yaormfilter/boolfilter_test.go b/yaormfilter/boolfilter_test.go index c4f84b2..87d72f7 100644 --- a/yaormfilter/boolfilter_test.go +++ b/yaormfilter/boolfilter_test.go @@ -33,6 +33,11 @@ func TestBoolFilter_Like(t *testing.T) { assert.Equal(t, filter, filter.Like(true)) } +func TestBoolFilter_ILike(t *testing.T) { + filter := yaormfilter.NewBoolFilter() + assert.Equal(t, filter, filter.ILike(true)) +} + func TestBoolFilter_Nil(t *testing.T) { filter := yaormfilter.NewBoolFilter() assert.Equal(t, filter, filter.Nil(true)) diff --git a/yaormfilter/datefilter.go b/yaormfilter/datefilter.go index 8910824..8d2c2a9 100644 --- a/yaormfilter/datefilter.go +++ b/yaormfilter/datefilter.go @@ -47,6 +47,11 @@ func (f *DateFilter) Like(v interface{}) ValueFilter { return f } +// ILike is not applicable on Date +func (f *DateFilter) ILike(v interface{}) ValueFilter { + return f +} + // Nil applies a nil filter on Date func (f *DateFilter) Nil(v bool) ValueFilter { f.nil(v) diff --git a/yaormfilter/datefilter_test.go b/yaormfilter/datefilter_test.go index efb1c91..9e55812 100644 --- a/yaormfilter/datefilter_test.go +++ b/yaormfilter/datefilter_test.go @@ -34,6 +34,11 @@ func TestDateFilter_Like(t *testing.T) { assert.Equal(t, filter, filter.Like(time.Now())) } +func TestDateFilter_ILike(t *testing.T) { + filter := yaormfilter.NewDateFilter() + assert.Equal(t, filter, filter.ILike(time.Now())) +} + func TestDateFilter_Nil(t *testing.T) { filter := yaormfilter.NewDateFilter() assert.Equal(t, filter, filter.Nil(true)) diff --git a/yaormfilter/int64filter.go b/yaormfilter/int64filter.go index 75ccabe..197195a 100644 --- a/yaormfilter/int64filter.go +++ b/yaormfilter/int64filter.go @@ -42,6 +42,11 @@ func (f *Int64Filter) Like(v interface{}) ValueFilter { return f } +// ILike is not applicable on int64 +func (f *Int64Filter) ILike(v interface{}) ValueFilter { + return f +} + // Nil adds a nil filter func (f *Int64Filter) Nil(v bool) ValueFilter { f.nil(v) diff --git a/yaormfilter/int64filter_test.go b/yaormfilter/int64filter_test.go index 68c4142..304da83 100644 --- a/yaormfilter/int64filter_test.go +++ b/yaormfilter/int64filter_test.go @@ -33,6 +33,11 @@ func TestInt64Filter_Like(t *testing.T) { assert.Equal(t, filter, filter.Like(int64(12))) } +func TestInt64Filter_ILike(t *testing.T) { + filter := yaormfilter.NewInt64Filter() + assert.Equal(t, filter, filter.ILike(int64(12))) +} + func TestInt64Filter_Nil(t *testing.T) { filter := yaormfilter.NewInt64Filter() assert.Equal(t, filter, filter.Nil(true)) diff --git a/yaormfilter/nilfilter.go b/yaormfilter/nilfilter.go index 8952362..2cfe356 100644 --- a/yaormfilter/nilfilter.go +++ b/yaormfilter/nilfilter.go @@ -25,6 +25,11 @@ func (f *NilFilter) Like(v interface{}) ValueFilter { return f } +// ILike is not applicable on Date +func (f *NilFilter) ILike(v interface{}) ValueFilter { + return f +} + // Nil applies a nil filter on Date func (f *NilFilter) Nil(v bool) ValueFilter { f.nil(v) diff --git a/yaormfilter/stringfilter.go b/yaormfilter/stringfilter.go index 88c4b43..3f84a1e 100644 --- a/yaormfilter/stringfilter.go +++ b/yaormfilter/stringfilter.go @@ -48,6 +48,17 @@ func (f *StringFilter) Like(v interface{}) ValueFilter { return f } +// ILike adds a Like filter +func (f *StringFilter) ILike(v interface{}) ValueFilter { + underlyingValue := tools.GetNonPtrValue(v) + // make sure we have a string + if underlyingValue.Kind() != reflect.String { + panic("Value in StringFilter is not a string") + } + f.ilike(underlyingValue.Interface()) + return f +} + // Nil adds a nil filter func (f *StringFilter) Nil(v bool) ValueFilter { f.nil(v) diff --git a/yaormfilter/stringfilter_test.go b/yaormfilter/stringfilter_test.go index a3f10ab..c10e63d 100644 --- a/yaormfilter/stringfilter_test.go +++ b/yaormfilter/stringfilter_test.go @@ -34,6 +34,12 @@ func TestStringFilter_Like(t *testing.T) { assert.Equal(t, filter, filter.Like(str)) } +func TestStringFilter_ILike(t *testing.T) { + filter := yaormfilter.NewStringFilter() + str := "bla" + assert.Equal(t, filter, filter.ILike(str)) +} + func TestStringFilter_Nil(t *testing.T) { filter := yaormfilter.NewStringFilter() assert.Equal(t, filter, filter.Nil(true)) diff --git a/yaormfilter/tools.go b/yaormfilter/tools.go index 6e89346..9361476 100644 --- a/yaormfilter/tools.go +++ b/yaormfilter/tools.go @@ -94,6 +94,16 @@ func Like(v interface{}) ValueFilter { panic(fmt.Errorf("Unknown type: %+v for value %+v in Like filter", underlyingValue.Kind(), v)) } +// ILike returns the correct filter according to the value sent +func ILike(v interface{}) ValueFilter { + underlyingValue := tools.GetNonPtrValue(v) + switch underlyingValue.Kind() { + case reflect.String: + return NewStringFilter().ILike(v) + } + panic(fmt.Errorf("Unknown type: %+v for value %+v in ILike filter", underlyingValue.Kind(), v)) +} + // Lt returns the correct filter according the value sent func Lt(v interface{}) ValueFilter { underlyingValue := tools.GetNonPtrValue(v) diff --git a/yaormfilter/tools_test.go b/yaormfilter/tools_test.go index 5e3f925..f958b8a 100644 --- a/yaormfilter/tools_test.go +++ b/yaormfilter/tools_test.go @@ -56,6 +56,10 @@ func TestLike(t *testing.T) { assert.IsType(t, &yaormfilter.StringFilter{}, yaormfilter.Like("abcdef%")) } +func TestILike(t *testing.T) { + assert.IsType(t, &yaormfilter.StringFilter{}, yaormfilter.ILike("abcdef%")) +} + func TestLt(t *testing.T) { assert.IsType(t, &yaormfilter.Int64Filter{}, yaormfilter.Lt(int64(12))) assert.IsType(t, &yaormfilter.DateFilter{}, yaormfilter.Lt(time.Now())) diff --git a/yaormfilter/valuefilter.go b/yaormfilter/valuefilter.go index 42db710..787bfbd 100644 --- a/yaormfilter/valuefilter.go +++ b/yaormfilter/valuefilter.go @@ -13,6 +13,7 @@ type ValueFilter interface { Equals(v interface{}) ValueFilter NotEquals(v interface{}) ValueFilter Like(v interface{}) ValueFilter + ILike(v interface{}) ValueFilter Lt(v interface{}) ValueFilter Lte(v interface{}) ValueFilter Gt(v interface{}) ValueFilter @@ -69,6 +70,12 @@ func (f *valuefilterimpl) like(e interface{}) *valuefilterimpl { }) } +func (f *valuefilterimpl) ilike(e interface{}) *valuefilterimpl { + return f.raw(func(field string) interface{} { + return squirrel.Expr(fmt.Sprintf("%s ILIKE ?", field), e) + }) +} + func (f *valuefilterimpl) in(e []interface{}) *valuefilterimpl { return f.raw(func(field string) interface{} { return squirrel.Eq{field: e}