-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [ADDED] Support for CASE statements #193
- Loading branch information
1 parent
6b579ea
commit a8f0c36
Showing
9 changed files
with
414 additions
and
0 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
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,75 @@ | ||
package exp | ||
|
||
type ( | ||
caseElse struct { | ||
result interface{} | ||
} | ||
caseWhen struct { | ||
caseElse | ||
condition interface{} | ||
} | ||
caseExpression struct { | ||
value interface{} | ||
whens []CaseWhen | ||
elseCondition CaseElse | ||
} | ||
) | ||
|
||
func NewCaseElse(result interface{}) CaseElse { | ||
return caseElse{result: result} | ||
} | ||
|
||
func (ce caseElse) Result() interface{} { | ||
return ce.result | ||
} | ||
|
||
func NewCaseWhen(condition, result interface{}) CaseWhen { | ||
return caseWhen{caseElse: caseElse{result: result}, condition: condition} | ||
} | ||
|
||
func (cw caseWhen) Condition() interface{} { | ||
return cw.condition | ||
} | ||
|
||
func NewCaseExpression() CaseExpression { | ||
return caseExpression{value: nil, whens: []CaseWhen{}, elseCondition: nil} | ||
} | ||
|
||
func (c caseExpression) Expression() Expression { | ||
return c | ||
} | ||
|
||
func (c caseExpression) Clone() Expression { | ||
return caseExpression{value: c.value, whens: c.whens, elseCondition: c.elseCondition} | ||
} | ||
|
||
func (c caseExpression) As(alias interface{}) AliasedExpression { | ||
return aliased(c, alias) | ||
} | ||
|
||
func (c caseExpression) GetValue() interface{} { | ||
return c.value | ||
} | ||
|
||
func (c caseExpression) GetWhens() []CaseWhen { | ||
return c.whens | ||
} | ||
|
||
func (c caseExpression) GetElse() CaseElse { | ||
return c.elseCondition | ||
} | ||
|
||
func (c caseExpression) Value(value interface{}) CaseExpression { | ||
c.value = value | ||
return c | ||
} | ||
|
||
func (c caseExpression) When(condition, result interface{}) CaseExpression { | ||
c.whens = append(c.whens, NewCaseWhen(condition, result)) | ||
return c | ||
} | ||
|
||
func (c caseExpression) Else(result interface{}) CaseExpression { | ||
c.elseCondition = NewCaseElse(result) | ||
return c | ||
} |
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,88 @@ | ||
package exp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type caseExpressionSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestCaseExpressionSuite(t *testing.T) { | ||
suite.Run(t, &caseExpressionSuite{}) | ||
} | ||
|
||
func (ces *caseExpressionSuite) TestClone() { | ||
ce := NewCaseExpression() | ||
ces.Equal(ce, ce.Clone()) | ||
} | ||
|
||
func (ces *caseExpressionSuite) TestExpression() { | ||
ce := NewCaseExpression() | ||
ces.Equal(ce, ce.Expression()) | ||
} | ||
|
||
func (ces *caseExpressionSuite) TestAs() { | ||
ce := NewCaseExpression() | ||
ces.Equal(aliased(ce, "a"), ce.As("a")) | ||
} | ||
|
||
func (ces *caseExpressionSuite) TestValue() { | ||
ce := NewCaseExpression() | ||
ces.Nil(ce.GetValue()) | ||
|
||
ce = NewCaseExpression().Value(NewIdentifierExpression("", "", "a")) | ||
ces.Equal(NewIdentifierExpression("", "", "a"), ce.GetValue()) | ||
} | ||
|
||
func (ces *caseExpressionSuite) TestWhen() { | ||
condition1 := NewIdentifierExpression("", "", "a").Eq(10) | ||
condition2 := NewIdentifierExpression("", "", "b").Eq(20) | ||
ce := NewCaseExpression() | ||
ces.Equal([]CaseWhen{ | ||
NewCaseWhen(condition1, "a"), | ||
NewCaseWhen(condition2, "b"), | ||
}, ce.When(condition1, "a").When(condition2, "b").GetWhens()) | ||
|
||
ces.Empty(ce.GetWhens()) | ||
} | ||
|
||
func (ces *caseExpressionSuite) TestElse() { | ||
ce := NewCaseExpression() | ||
ces.Equal(NewCaseElse("a"), ce.Else("a").GetElse()) | ||
|
||
ces.Nil(ce.GetElse()) | ||
} | ||
|
||
type caseWhenSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestCaseWhenSuite(t *testing.T) { | ||
suite.Run(t, &caseWhenSuite{}) | ||
} | ||
|
||
func (cws *caseWhenSuite) TestCondition() { | ||
ce := NewCaseWhen(true, false) | ||
cws.Equal(true, ce.Condition()) | ||
} | ||
|
||
func (cws *caseWhenSuite) TestResult() { | ||
ce := NewCaseWhen(true, false) | ||
cws.Equal(false, ce.Result()) | ||
} | ||
|
||
type caseElseSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestCaseElseSuite(t *testing.T) { | ||
suite.Run(t, &caseElseSuite{}) | ||
} | ||
|
||
func (ces *caseElseSuite) TestResult() { | ||
ce := NewCaseElse(false) | ||
ces.Equal(false, ce.Result()) | ||
} |
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
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
Oops, something went wrong.