1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-14 18:00:54 +00:00
Beego/orm/orm_conds.go

111 lines
2.7 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @description beego is an open-source, high-performance web framework for the Go programming language.
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @link http://github.com/astaxie/beego for the canonical source repository
2014-07-03 15:40:21 +00:00
//
2014-04-12 05:18:18 +00:00
// @license http://github.com/astaxie/beego/blob/master/LICENSE
2014-07-03 15:40:21 +00:00
//
2014-06-25 02:39:37 +00:00
// @authors astaxie, slene
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
}
2014-01-17 15:28:54 +00:00
// condition struct.
// work for WHERE conditions.
2013-07-30 12:32:38 +00:00
type Condition struct {
params []condValue
}
2014-01-17 15:28:54 +00:00
// return new condition struct
2013-07-30 12:32:38 +00:00
func NewCondition() *Condition {
c := &Condition{}
return c
}
2014-01-17 15:28:54 +00:00
// add expression to condition
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
}
2014-01-17 15:28:54 +00:00
// add NOT expression to condition
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
}
2014-01-17 15:28:54 +00:00
// combine a condition to current condition
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
}
2014-01-17 15:28:54 +00:00
// add OR expression to condition
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
}
2014-01-17 15:28:54 +00:00
// add OR NOT expression to condition
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
}
2014-01-17 15:28:54 +00:00
// combine a OR condition to current condition
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
}
2014-01-17 15:28:54 +00:00
// check the condition arguments are empty or not.
2013-07-30 12:32:38 +00:00
func (c *Condition) IsEmpty() bool {
return len(c.params) == 0
}
2014-01-17 15:28:54 +00:00
// clone a condition
2013-08-07 11:11:44 +00:00
func (c Condition) clone() *Condition {
2013-07-30 12:32:38 +00:00
return &c
}