Skip to content

Commit

Permalink
*: use a const int value instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala committed Oct 30, 2015
1 parent 414ae7b commit 480f7f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
25 changes: 16 additions & 9 deletions expression/date_arith.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ import (
)

const (
add = "ADD"
sub = "SUB"
// DateAdd is to run date_add function option.
// See: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-add
DateAdd = 1
// DateSub is to run date_sub function option.
// See: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-sub
DateSub = 2
)

// DateArith is used for dealing with addition and substraction of time.
// If the Op value is ADD, then do date_add function.
// See: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-add
// If the Op value is SUB, then do date_sub function.
// See: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-sub
type DateArith struct {
Op string
Op int
Unit string
Date Expression
Interval Expression
}

func (da *DateArith) isAdd() bool {
if da.Op == add {
if da.Op == DateAdd {
return true
}

Expand All @@ -67,7 +67,14 @@ func (da *DateArith) Accept(v Visitor) (Expression, error) {

// String implements the Expression String interface.
func (da *DateArith) String() string {
return fmt.Sprintf("DATE_%s(%s, INTERVAL %s %s)", da.Op, da.Date, da.Interval, strings.ToUpper(da.Unit))
var str string
if da.isAdd() {
str = "DATE_ADD"
} else {
str = "DATE_SUB"
}

return fmt.Sprintf("%s(%s, INTERVAL %s %s)", str, da.Date, da.Interval, strings.ToUpper(da.Unit))
}

// Eval implements the Expression Eval interface.
Expand Down
20 changes: 10 additions & 10 deletions expression/date_arith_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type testDateArithSuite struct {
func (t *testDateArithSuite) TestDateArith(c *C) {
input := "2011-11-11 10:10:10"
e := &DateArith{
Op: add,
Op: DateAdd,
Unit: "DAY",
Date: Value{Val: input},
Interval: Value{Val: "1"},
Expand All @@ -36,18 +36,18 @@ func (t *testDateArithSuite) TestDateArith(c *C) {
c.Assert(e.IsStatic(), IsTrue)
_, err := e.Eval(nil, nil)
c.Assert(err, IsNil)
e.Op = sub
e.Op = DateSub
c.Assert(e.String(), Equals, `DATE_SUB("2011-11-11 10:10:10", INTERVAL "1" DAY)`)

// Test null.
nullTbl := []struct {
Op string
Op int
Unit string
Date interface{}
Interval interface{}
}{
{add, "DAY", nil, "1"},
{add, "DAY", input, nil},
{DateAdd, "DAY", nil, "1"},
{DateAdd, "DAY", input, nil},
}
for _, t := range nullTbl {
e := &DateArith{
Expand All @@ -59,7 +59,7 @@ func (t *testDateArithSuite) TestDateArith(c *C) {
v, err := e.Eval(nil, nil)
c.Assert(err, IsNil)
c.Assert(v, IsNil)
e.Op = sub
e.Op = DateSub
v, err = e.Eval(nil, nil)
c.Assert(err, IsNil)
c.Assert(v, IsNil)
Expand Down Expand Up @@ -99,7 +99,7 @@ func (t *testDateArithSuite) TestDateArith(c *C) {
}
for _, t := range tbl {
e := &DateArith{
Op: add,
Op: DateAdd,
Unit: t.Unit,
Date: Value{Val: input},
Interval: Value{Val: t.Interval},
Expand All @@ -110,7 +110,7 @@ func (t *testDateArithSuite) TestDateArith(c *C) {
c.Assert(ok, IsTrue)
c.Assert(value.String(), Equals, t.AddExpect)

e.Op = sub
e.Op = DateSub
v, err = e.Eval(nil, nil)
c.Assert(err, IsNil)
value, ok = v.(mysql.Time)
Expand All @@ -136,7 +136,7 @@ func (t *testDateArithSuite) TestDateArith(c *C) {
}
for _, t := range errTbl {
e := &DateArith{
Op: add,
Op: DateAdd,
Unit: t.Unit,
Date: Value{Val: input},
Interval: Value{Val: t.Interval},
Expand All @@ -148,7 +148,7 @@ func (t *testDateArithSuite) TestDateArith(c *C) {
c.Assert(err, NotNil, Commentf("%s", v))

e = &DateArith{
Op: sub,
Op: DateSub,
Unit: t.Unit,
Date: Value{Val: input},
Interval: Value{Val: t.Interval},
Expand Down
4 changes: 2 additions & 2 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -2287,7 +2287,7 @@ FunctionCallNonKeyword:
| "DATE_ADD" '(' Expression ',' "INTERVAL" Expression TimeUnit ')'
{
$$ = &expression.DateArith{
Op:"ADD",
Op:expression.DateAdd,
Unit: $7.(string),
Date: $3.(expression.Expression),
Interval: $6.(expression.Expression),
Expand All @@ -2296,7 +2296,7 @@ FunctionCallNonKeyword:
| "DATE_SUB" '(' Expression ',' "INTERVAL" Expression TimeUnit ')'
{
$$ = &expression.DateArith{
Op:"SUB",
Op:expression.DateSub,
Unit: $7.(string),
Date: $3.(expression.Expression),
Interval: $6.(expression.Expression),
Expand Down

0 comments on commit 480f7f3

Please sign in to comment.