Conditional expression functions.
This commit is contained in:
parent
ca5a30983e
commit
dca028295d
7 changed files with 89 additions and 15 deletions
|
|
@ -503,3 +503,27 @@ func LOCALTIMESTAMP(precision ...int) TimestampExpression {
|
|||
func NOW() TimestampzExpression {
|
||||
return newTimestampzFunc("NOW")
|
||||
}
|
||||
|
||||
// --------------- Conditional Expressions Functions -------------//
|
||||
|
||||
func COALESCE(value expression, values ...expression) expression {
|
||||
var allValues = []expression{value}
|
||||
allValues = append(allValues, values...)
|
||||
return newFunc("COALESCE", allValues, nil)
|
||||
}
|
||||
|
||||
func NULLIF(value1, value2 expression) expression {
|
||||
return newFunc("NULLIF", []expression{value1, value2}, nil)
|
||||
}
|
||||
|
||||
func GREATEST(value expression, values ...expression) expression {
|
||||
var allValues = []expression{value}
|
||||
allValues = append(allValues, values...)
|
||||
return newFunc("GREATEST", allValues, nil)
|
||||
}
|
||||
|
||||
func LEAST(value expression, values ...expression) expression {
|
||||
var allValues = []expression{value}
|
||||
allValues = append(allValues, values...)
|
||||
return newFunc("LEAST", allValues, nil)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -118,23 +118,24 @@ func TestFuncLOG(t *testing.T) {
|
|||
assertExpressionSerialize(t, LOG(Float(11.2222)), "LOG($1)", float64(11.2222))
|
||||
}
|
||||
|
||||
func TestCase1(t *testing.T) {
|
||||
query := CASE().
|
||||
WHEN(table3Col1.EQ(Int(1))).THEN(table3Col1.ADD(Int(1))).
|
||||
WHEN(table3Col1.EQ(Int(2))).THEN(table3Col1.ADD(Int(2)))
|
||||
|
||||
assertExpressionSerialize(t, query, `(CASE WHEN table3.col1 = $1 THEN table3.col1 + $2 WHEN table3.col1 = $3 THEN table3.col1 + $4 END)`,
|
||||
int64(1), int64(1), int64(2), int64(2))
|
||||
func TestFuncCOALESCE(t *testing.T) {
|
||||
assertExpressionSerialize(t, COALESCE(table1ColFloat), "COALESCE(table1.colFloat)")
|
||||
assertExpressionSerialize(t, COALESCE(Float(11.2222), NULL, String("str")), "COALESCE($1, NULL, $2)", float64(11.2222), "str")
|
||||
}
|
||||
|
||||
func TestCase2(t *testing.T) {
|
||||
query := CASE(table3Col1).
|
||||
WHEN(Int(1)).THEN(table3Col1.ADD(Int(1))).
|
||||
WHEN(Int(2)).THEN(table3Col1.ADD(Int(2))).
|
||||
ELSE(Int(0))
|
||||
func TestFuncNULLIF(t *testing.T) {
|
||||
assertExpressionSerialize(t, NULLIF(table1ColFloat, table2ColInt), "NULLIF(table1.colFloat, table2.colInt)")
|
||||
assertExpressionSerialize(t, NULLIF(Float(11.2222), NULL), "NULLIF($1, NULL)", float64(11.2222))
|
||||
}
|
||||
|
||||
assertExpressionSerialize(t, query, `(CASE table3.col1 WHEN $1 THEN table3.col1 + $2 WHEN $3 THEN table3.col1 + $4 ELSE $5 END)`,
|
||||
int64(1), int64(1), int64(2), int64(2), int64(0))
|
||||
func TestFuncGREATEST(t *testing.T) {
|
||||
assertExpressionSerialize(t, GREATEST(table1ColFloat), "GREATEST(table1.colFloat)")
|
||||
assertExpressionSerialize(t, GREATEST(Float(11.2222), NULL, String("str")), "GREATEST($1, NULL, $2)", float64(11.2222), "str")
|
||||
}
|
||||
|
||||
func TestFuncLEAST(t *testing.T) {
|
||||
assertExpressionSerialize(t, LEAST(table1ColFloat), "LEAST(table1.colFloat)")
|
||||
assertExpressionSerialize(t, LEAST(Float(11.2222), NULL, String("str")), "LEAST($1, NULL, $2)", float64(11.2222), "str")
|
||||
}
|
||||
|
||||
func TestInterval(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,10 @@ const (
|
|||
DEFAULT keywordClause = "DEFAULT"
|
||||
)
|
||||
|
||||
var (
|
||||
NULL = newNullExpression()
|
||||
)
|
||||
|
||||
type keywordClause string
|
||||
|
||||
func (k keywordClause) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
||||
|
|
|
|||
|
|
@ -175,3 +175,21 @@ func Date(year, month, day int) DateExpression {
|
|||
|
||||
return dateLiteral.CAST_TO_DATE()
|
||||
}
|
||||
|
||||
//--------------------------------------------------//
|
||||
type nullExpression struct {
|
||||
expressionInterfaceImpl
|
||||
}
|
||||
|
||||
func newNullExpression() expression {
|
||||
nullExpression := &nullExpression{}
|
||||
|
||||
nullExpression.expressionInterfaceImpl.parent = nullExpression
|
||||
|
||||
return nullExpression
|
||||
}
|
||||
|
||||
func (n *nullExpression) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
||||
out.writeString("NULL")
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ type caseOperatorImpl struct {
|
|||
func CASE(expression ...expression) caseOperatorExpression {
|
||||
caseExp := &caseOperatorImpl{}
|
||||
|
||||
if len(expression) == 1 {
|
||||
if len(expression) > 0 {
|
||||
caseExp.expression = expression[0]
|
||||
}
|
||||
|
||||
|
|
|
|||
22
sqlbuilder/operators_test.go
Normal file
22
sqlbuilder/operators_test.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package sqlbuilder
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCase1(t *testing.T) {
|
||||
query := CASE().
|
||||
WHEN(table3Col1.EQ(Int(1))).THEN(table3Col1.ADD(Int(1))).
|
||||
WHEN(table3Col1.EQ(Int(2))).THEN(table3Col1.ADD(Int(2)))
|
||||
|
||||
assertExpressionSerialize(t, query, `(CASE WHEN table3.col1 = $1 THEN table3.col1 + $2 WHEN table3.col1 = $3 THEN table3.col1 + $4 END)`,
|
||||
int64(1), int64(1), int64(2), int64(2))
|
||||
}
|
||||
|
||||
func TestCase2(t *testing.T) {
|
||||
query := CASE(table3Col1).
|
||||
WHEN(Int(1)).THEN(table3Col1.ADD(Int(1))).
|
||||
WHEN(Int(2)).THEN(table3Col1.ADD(Int(2))).
|
||||
ELSE(Int(0))
|
||||
|
||||
assertExpressionSerialize(t, query, `(CASE table3.col1 WHEN $1 THEN table3.col1 + $2 WHEN $3 THEN table3.col1 + $4 ELSE $5 END)`,
|
||||
int64(1), int64(1), int64(2), int64(2), int64(0))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue