Skip to content

Commit

Permalink
Add Distinct option for SelectBuilder (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sundava authored and geoffreybauduin committed Oct 30, 2019
1 parent 07197a8 commit 7f7cab7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions filterapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func apply(statement squirrel.SelectBuilder, f yaormfilter.Filter, dbp DBProvide
if dbp.CanSelectForUpdate() {
statement = statement.Suffix(fmt.Sprintf(`FOR UPDATE OF %s`, dbp.EscapeValue(applier.tableName)))
}
case yaormfilter.RequestOptions.SelectDistinct:
statement = statement.Distinct()
}
}
for _, orderBy := range f.GetOrderBy() {
Expand Down
28 changes: 28 additions & 0 deletions filterapply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,31 @@ func TestFilterApplier_LoadColumns(t *testing.T) {
assert.False(t, models[1].(*testdata.Category).UpdatedAt.IsZero())
}
}

func TestFilterApplier_Distinct(t *testing.T) {
killDb, err := testdata.SetupTestDatabase("test")
defer killDb()

assert.NoError(t, err)
dbp, err := yaorm.NewDBProvider(context.TODO(), "test")
assert.NoError(t, err)
post := &testdata.Post{}
saveModel(t, dbp, post)
postChild := &testdata.Post{ParentPostID: post.ID}
saveModel(t, dbp, postChild)
postChild2 := &testdata.Post{ParentPostID: post.ID}
saveModel(t, dbp, postChild2)

f := testdata.NewPostFilter()
f.ChildrenPosts(testdata.NewPostFilter().
ParentPostID(yaormfilter.NewInt64Filter().Equals(post.ID)))
models, err := yaorm.GenericSelectAll(dbp, f)
assert.NoError(t, err)
// We do not set the filter Distinct, so we should have one row per matching children
assert.Equal(t, 2, len(models))

f.Distinct()
models, err = yaorm.GenericSelectAll(dbp, f)
assert.NoError(t, err)
assert.Equal(t, 1, len(models))
}
9 changes: 9 additions & 0 deletions testdata/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ func NewPostFilter() *PostFilter {
return &PostFilter{}
}

func (f *PostFilter) AddOption(opt yaormfilter.RequestOption) yaormfilter.Filter {
f.AddOption_(opt)
return f
}

func (f *PostFilter) Distinct() {
f.AddOption("SelectDistinct")
}

func (f *PostFilter) ID(v yaormfilter.ValueFilter) *PostFilter {
f.FilterID = v
return f
Expand Down
4 changes: 3 additions & 1 deletion yaormfilter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ type RequestOption string
// RequestOptions represents the Enum of Request options
var RequestOptions = struct {
SelectForUpdate RequestOption
SelectDistinct RequestOption
LeftJoin RequestOption
}{
SelectForUpdate: "SelectForUpdate",
SelectDistinct: "SelectDistinct",
LeftJoin: "LeftJoin",
}

Expand Down Expand Up @@ -103,7 +105,7 @@ func (mf *ModelFilter) GetSelectOptions() []RequestOption {
opts := []RequestOption{}
for _, opt := range mf.options {
switch opt {
case RequestOptions.SelectForUpdate:
case RequestOptions.SelectForUpdate, RequestOptions.SelectDistinct:
opts = append(opts, opt)
}
}
Expand Down

0 comments on commit 7f7cab7

Please sign in to comment.