-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(multifilter): Adding a multi filter to handle multiple filters (#98
- Loading branch information
1 parent
de92993
commit 52ec1ab
Showing
9 changed files
with
242 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Multi-Filter | ||
|
||
The `multifilter` example demonstrates how to use the `multifilter`. The `multifilter` effectively is the same as using | ||
multiple `patcher.WithWhere` filters, but it is more efficient and potentially more readable. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jacobbrewer1/patcher" | ||
) | ||
|
||
type ExampleFilter struct { | ||
Field string | ||
Value any | ||
} | ||
|
||
func (e ExampleFilter) Where() (string, []any) { | ||
return fmt.Sprintf("%s = ?", e.Field), []any{e.Value} | ||
} | ||
|
||
func main() { | ||
// Create example filters | ||
filters := []ExampleFilter{ | ||
{Field: "name", Value: "John"}, | ||
{Field: "age", Value: 30}, | ||
} | ||
|
||
mf := patcher.NewMultiFilter() | ||
|
||
// Append each filter to the WHERE clause | ||
for _, filter := range filters { | ||
mf.Add(filter) | ||
} | ||
|
||
// Print the WHERE clause | ||
sql, args := mf.Where() | ||
fmt.Printf("WHERE SQL:\n%s\n", sql) | ||
fmt.Printf("WHERE Args: %v\n", args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
package patcher | ||
|
||
import "strings" | ||
|
||
// Joiner is an interface that can be used to specify the JOIN clause to use when the SQL is being generated. | ||
type Joiner interface { | ||
Join() (string, []any) | ||
} | ||
|
||
func appendJoin(join Joiner, builder *strings.Builder, args *[]any) { | ||
jSQL, jArgs := join.Join() | ||
if jArgs == nil { | ||
jArgs = make([]any, 0) | ||
} | ||
builder.WriteString(strings.TrimSpace(jSQL)) | ||
builder.WriteString("\n") | ||
*args = append(*args, jArgs...) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package patcher | ||
|
||
import "strings" | ||
|
||
type MultiFilter interface { | ||
Wherer | ||
Add(where Wherer) | ||
} | ||
|
||
type multiFilter struct { | ||
whereSql *strings.Builder | ||
whereArgs []any | ||
} | ||
|
||
func NewMultiFilter() MultiFilter { | ||
return &multiFilter{ | ||
whereSql: new(strings.Builder), | ||
whereArgs: nil, | ||
} | ||
} | ||
|
||
func (m *multiFilter) Add(where Wherer) { | ||
appendWhere(where, m.whereSql, &m.whereArgs) | ||
} | ||
|
||
func (m *multiFilter) Where() (string, []any) { | ||
return m.whereSql.String(), m.whereArgs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package patcher | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type multiFilterSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestMultiFilterSuite(t *testing.T) { | ||
suite.Run(t, new(multiFilterSuite)) | ||
} | ||
|
||
func (s *multiFilterSuite) TestNewMultiFilter_Add_Single() { | ||
mf := NewMultiFilter() | ||
s.NotNil(mf) | ||
|
||
mw := NewMockWherer(s.T()) | ||
mw.On("Where").Return("where", []any{"arg1", "arg2"}) | ||
mf.Add(mw) | ||
|
||
sql, args := mf.Where() | ||
s.Equal("AND where\n", sql) | ||
s.Equal([]any{"arg1", "arg2"}, args) | ||
} | ||
|
||
func (s *multiFilterSuite) TestNewMultiFilter_Add_Multi() { | ||
mf := NewMultiFilter() | ||
s.NotNil(mf) | ||
|
||
mw := NewMockWherer(s.T()) | ||
mw.On("Where").Return("where", []any{"arg1", "arg2"}) | ||
mf.Add(mw) | ||
|
||
mwTwo := NewMockWherer(s.T()) | ||
mwTwo.On("Where").Return("whereTwo", []any{"arg3", "arg4"}) | ||
mf.Add(mwTwo) | ||
|
||
sql, args := mf.Where() | ||
s.Equal("AND where\nAND whereTwo\n", sql) | ||
s.Equal([]any{"arg1", "arg2", "arg3", "arg4"}, args) | ||
} | ||
|
||
func (s *multiFilterSuite) TestNewMultiFilter_Add_WhereTyper() { | ||
mf := NewMultiFilter() | ||
s.NotNil(mf) | ||
|
||
mw := NewMockWherer(s.T()) | ||
mw.On("Where").Return("where", []any{"arg1", "arg2"}) | ||
mf.Add(mw) | ||
|
||
mwt := NewMockWhereTyper(s.T()) | ||
mwt.On("Where").Return("whereTwo", []any{"arg3", "arg4"}) | ||
mwt.On("WhereType").Return(WhereTypeOr) | ||
mf.Add(mwt) | ||
|
||
sql, args := mf.Where() | ||
s.Equal("AND where\nOR whereTwo\n", sql) | ||
s.Equal([]any{"arg1", "arg2", "arg3", "arg4"}, args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters