Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/database/sqlcommon/filter_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func (s *SQLCommon) filterSelect(ctx context.Context, tableName string, sel sq.S
sel = sel.Limit(fi.Limit)
}
}
if fi.Distinct {
sel = sel.Distinct()
}
return sel, fop, fi, err
}

Expand Down
14 changes: 14 additions & 0 deletions internal/database/sqlcommon/filter_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,17 @@ func TestSQLQueryFactoryDefaultSortBadType(t *testing.T) {
s.filterSelect(context.Background(), "", sel, f, nil, []interface{}{100})
})
}

func TestSQLQueryFactorySelectDistinct(t *testing.T) {

s, _ := newMockProvider().init()
sel := squirrel.Select("name").From("mytable")
fb := database.MessageQueryFactory.NewFilter(context.Background())
f := fb.And().Distinct(true)
sel, _, _, err := s.filterSelect(context.Background(), "", sel, f, nil, []interface{}{"name"})
assert.NoError(t, err)

sqlFilter, _, err := sel.ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT DISTINCT name FROM mytable WHERE (1=1) ORDER BY name DESC", sqlFilter)
}
14 changes: 14 additions & 0 deletions pkg/database/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type Filter interface {
// Request a count to be returned on the total number that match the query
Count(c bool) Filter

// Select distinct rows
Distinct(bool) Filter

// Finalize completes the filter, and for the plugin to validated output structure to convert
Finalize() (*FilterInfo, error)

Expand Down Expand Up @@ -156,6 +159,7 @@ type FilterInfo struct {
Skip uint64
Limit uint64
Count bool
Distinct bool
Field string
Op FilterOp
Values []FieldSerialization
Expand Down Expand Up @@ -231,6 +235,9 @@ func (f *FilterInfo) String() string {
if f.Count {
val.WriteString(" count=true")
}
if f.Distinct {
val.WriteString(" distinct=true")
}

return val.String()
}
Expand All @@ -254,6 +261,7 @@ type filterBuilder struct {
count bool
forceAscending bool
forceDescending bool
distinct bool
}

type baseFilter struct {
Expand Down Expand Up @@ -327,6 +335,7 @@ func (f *baseFilter) Finalize() (fi *FilterInfo, err error) {
Skip: f.fb.skip,
Limit: f.fb.limit,
Count: f.fb.count,
Distinct: f.fb.distinct,
}, nil
}

Expand Down Expand Up @@ -372,6 +381,11 @@ func (f *baseFilter) Descending() Filter {
return f
}

func (f *baseFilter) Distinct(d bool) Filter {
f.fb.distinct = true
return f
}

type andFilter struct {
baseFilter
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/database/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ func TestBuildMessageFilter3(t *testing.T) {
assert.Equal(t, "( created IN [1000000000,2000000000,3000000000] ) && ( created NI [1000000000,2000000000,3000000000] ) && ( created < 0 ) && ( created <= 0 ) && ( created >= 0 ) && ( created != 0 ) && ( sequence > 12345 ) && ( topics %= 'abc' ) && ( topics %! 'def' ) && ( topics ^= 'ghi' ) && ( topics ^! 'jkl' ) sort=-created,topics,-sequence", f.String())
}

func TestBuildMessageFilterDistinct(t *testing.T) {
fb := MessageQueryFactory.NewFilter(context.Background())
f, err := fb.And().Distinct(true).Finalize()

assert.NoError(t, err)
assert.Equal(t, " distinct=true", f.String())
}

func TestBuildMessageBadInFilterField(t *testing.T) {
fb := MessageQueryFactory.NewFilter(context.Background())
_, err := fb.And(
Expand Down