Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add InCond #14

Merged
merged 3 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
62 changes: 62 additions & 0 deletions where.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,68 @@ func (c *NullCond) ToASTWhere() (*ast.Where, error) {
}, nil
}

// InConditionValue is a value expression in IN clauses.
type InConditionValue interface {
ToASTInConditionValue() (ast.InCondition, error)
}

// UnnestInConditionValue is a UNNEST operator in IN clauses.
type UnnestInConditionValue struct {
value interface{}
}

func (v *UnnestInConditionValue) ToASTInConditionValue() (ast.InCondition, error) {
value, err := internal.ToExpr(v.value)
if err != nil {
return nil, err
}
return &ast.UnnestInCondition{
Expr: value,
}, nil
}

// Unnest(v) creates `UNNEST(v)` predicate.
func Unnest(v interface{}) *UnnestInConditionValue {
return &UnnestInConditionValue{
value: v,
}
}

// InCond represents IN or NOT IN predicates.
type InCond struct {
lhs interface{}
rhs InConditionValue
not bool
}

// In(x, y) creates `x IN y` predicate.
func In(x interface{}, y InConditionValue) *InCond {
return &InCond{lhs: x, rhs: y, not: false}
}

// In(x, y) creates `x NOT IN y` predicate.
func NotIn(x interface{}, y InConditionValue) *InCond {
return &InCond{lhs: x, rhs: y, not: true}
}

func (c *InCond) ToASTWhere() (*ast.Where, error) {
lhs, err := internal.ToExpr(c.lhs)
if err != nil {
return nil, err
}
rhs, err := c.rhs.ToASTInConditionValue()
if err != nil {
return nil, err
}
return &ast.Where{
Expr: &ast.InExpr{
Not: c.not,
Left: lhs,
Right: rhs,
},
}, nil
}

// BetweenCond represents BETWEEN or NOT BETWEEN predicates.
type BetweenCond struct {
arg interface{}
Expand Down
10 changes: 10 additions & 0 deletions where_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ func TestIsNullAndIsNotNull(t *testing.T) {
testWhere(t, memeduck.IsNotNull(memeduck.Ident("fuga")), `fuga IS NOT NULL`)
}

func TestIn(t *testing.T) {
testWhere(t, memeduck.In(memeduck.Ident("hoge"), memeduck.Unnest(memeduck.Param("hoge"))), `hoge IN UNNEST(@hoge)`)
testWhere(t, memeduck.In(memeduck.Ident("hoge"), memeduck.Unnest([]string{"foo", "bar"})), `hoge IN UNNEST(ARRAY["foo", "bar"])`)
}

func TestNotIn(t *testing.T) {
testWhere(t, memeduck.NotIn(memeduck.Ident("hoge"), memeduck.Unnest(memeduck.Param("hoge"))), `hoge NOT IN UNNEST(@hoge)`)
testWhere(t, memeduck.NotIn(memeduck.Ident("hoge"), memeduck.Unnest([]string{"foo", "bar"})), `hoge NOT IN UNNEST(ARRAY["foo", "bar"])`)
}

func TestBetweenAndNotBetween(t *testing.T) {
testWhere(t, memeduck.Between(memeduck.Ident("hoge"), 1, 10), `hoge BETWEEN 1 AND 10`)
testWhere(t, memeduck.NotBetween(memeduck.Ident("hoge"), 1, 10), `hoge NOT BETWEEN 1 AND 10`)
Expand Down