Skip to content

Commit

Permalink
feat: implement add/sub col expression
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Jul 20, 2022
1 parent 56b9edf commit e6d7c96
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions field/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ func TestExpr_Build(t *testing.T) {
Result: "(UNIX_TIMESTAMP(?))*?",
ExpectedVars: []interface{}{"2005-03-27 03:00:00", uint64(99)},
},
{
Expr: field.NewInt("t1", "id").AddCol(field.NewInt("t2", "num")),
Result: "`t1`.`id` + `t2`.`num`",
},
{
Expr: field.NewInt("t1", "id").AddCol(field.NewInt("t2", "num").Add(1)),
Result: "`t1`.`id` + `t2`.`num`+?",
ExpectedVars: []interface{}{int(1)},
},
{
Expr: field.NewInt("t1", "id").EqCol(field.NewInt("t1", "id").AddCol(field.NewInt("t2", "num").Add(1))),
Result: "`t1`.`id` = `t1`.`id` + `t2`.`num`+?",
ExpectedVars: []interface{}{int(1)},
},
// ======================== integer ========================
{
Expr: field.NewUint("", "id"),
Expand Down
9 changes: 9 additions & 0 deletions field/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ func (e expr) SetCol(col Expr) AssignExpr {
return e.setE(clause.Eq{Column: e.col.Name, Value: col.RawExpr()})
}

// ======================== operate columns ========================
func (e expr) AddCol(col Expr) Expr {
return e.setE(clause.Expr{SQL: "? + ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})
}

func (e expr) SubCol(col Expr) Expr {
return e.setE(clause.Expr{SQL: "? - ?", Vars: []interface{}{e.RawExpr(), col.RawExpr()}})
}

// ======================== keyword ========================
func (e expr) As(alias string) Expr {
if e.e != nil {
Expand Down

0 comments on commit e6d7c96

Please sign in to comment.