Beego/orm/orm_conds.go

90 lines
1.9 KiB
Go
Raw Normal View History

2013-07-30 12:32:38 +00:00
package orm
import (
"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 {
panic("<Condition.And> args cannot empty")
}
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 {
panic("<Condition.AndNot> args cannot empty")
}
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 {
panic("cannot use self as sub cond")
}
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 {
panic("<Condition.Or> args cannot empty")
}
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 {
panic("<Condition.OrNot> args cannot empty")
}
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 {
panic("cannot use self as sub cond")
}
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
}