1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-29 00:55:47 +00:00
Beego/orm/orm_conds.go

91 lines
2.0 KiB
Go
Raw Normal View History

2013-07-30 12:32:38 +00:00
package orm
import (
2013-10-09 03:37:16 +00:00
"fmt"
2013-07-30 12:32:38 +00:00
"strings"
)
const (
ExprSep = "__"
)
type condValue struct {
exprs []string
args []interface{}
cond *Condition
isOr bool
isNot bool
isCond bool
}
type Condition struct {
params []condValue
}
func NewCondition() *Condition {
c := &Condition{}
return c
}
2013-08-07 11:11:44 +00:00
func (c Condition) And(expr string, args ...interface{}) *Condition {
2013-07-30 12:32:38 +00:00
if expr == "" || len(args) == 0 {
2013-10-09 03:37:16 +00:00
panic(fmt.Errorf("<Condition.And> args cannot empty"))
2013-07-30 12:32:38 +00:00
}
c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args})
2013-08-07 11:11:44 +00:00
return &c
2013-07-30 12:32:38 +00:00
}
2013-08-07 11:11:44 +00:00
func (c Condition) AndNot(expr string, args ...interface{}) *Condition {
2013-07-30 12:32:38 +00:00
if expr == "" || len(args) == 0 {
2013-10-09 03:37:16 +00:00
panic(fmt.Errorf("<Condition.AndNot> args cannot empty"))
2013-07-30 12:32:38 +00:00
}
c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args, isNot: true})
2013-08-07 11:11:44 +00:00
return &c
2013-07-30 12:32:38 +00:00
}
func (c *Condition) AndCond(cond *Condition) *Condition {
2013-08-07 11:11:44 +00:00
c = c.clone()
2013-07-30 12:32:38 +00:00
if c == cond {
2013-10-09 03:37:16 +00:00
panic(fmt.Errorf("<Condition.AndCond> cannot use self as sub cond"))
2013-07-30 12:32:38 +00:00
}
if cond != nil {
c.params = append(c.params, condValue{cond: cond, isCond: true})
}
return c
}
2013-08-07 11:11:44 +00:00
func (c Condition) Or(expr string, args ...interface{}) *Condition {
2013-07-30 12:32:38 +00:00
if expr == "" || len(args) == 0 {
2013-10-09 03:37:16 +00:00
panic(fmt.Errorf("<Condition.Or> args cannot empty"))
2013-07-30 12:32:38 +00:00
}
c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args, isOr: true})
2013-08-07 11:11:44 +00:00
return &c
2013-07-30 12:32:38 +00:00
}
2013-08-07 11:11:44 +00:00
func (c Condition) OrNot(expr string, args ...interface{}) *Condition {
2013-07-30 12:32:38 +00:00
if expr == "" || len(args) == 0 {
2013-10-09 03:37:16 +00:00
panic(fmt.Errorf("<Condition.OrNot> args cannot empty"))
2013-07-30 12:32:38 +00:00
}
c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args, isNot: true, isOr: true})
2013-08-07 11:11:44 +00:00
return &c
2013-07-30 12:32:38 +00:00
}
func (c *Condition) OrCond(cond *Condition) *Condition {
2013-08-07 11:11:44 +00:00
c = c.clone()
2013-07-30 12:32:38 +00:00
if c == cond {
2013-10-09 03:37:16 +00:00
panic(fmt.Errorf("<Condition.OrCond> cannot use self as sub cond"))
2013-07-30 12:32:38 +00:00
}
if cond != nil {
c.params = append(c.params, condValue{cond: cond, isCond: true, isOr: true})
}
return c
}
func (c *Condition) IsEmpty() bool {
return len(c.params) == 0
}
2013-08-07 11:11:44 +00:00
func (c Condition) clone() *Condition {
2013-07-30 12:32:38 +00:00
return &c
}