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

143 lines
3.8 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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"
)
2016-01-17 15:48:17 +00:00
// ExprSep define the expression separation
2013-07-30 12:32:38 +00:00
const (
ExprSep = "__"
)
type condValue struct {
exprs []string
args []interface{}
cond *Condition
isOr bool
isNot bool
isCond bool
}
2015-09-12 13:46:43 +00:00
// Condition struct.
2014-01-17 15:28:54 +00:00
// work for WHERE conditions.
2013-07-30 12:32:38 +00:00
type Condition struct {
params []condValue
}
2015-09-12 13:46:43 +00:00
// NewCondition return new condition struct
2013-07-30 12:32:38 +00:00
func NewCondition() *Condition {
c := &Condition{}
return c
}
2015-09-12 13:46:43 +00:00
// And 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
}
2015-09-12 13:46:43 +00:00
// AndNot 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
}
2015-09-12 13:46:43 +00:00
// AndCond 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
}
// AndNotCond combine a AND NOT condition to current condition
func (c *Condition) AndNotCond(cond *Condition) *Condition {
c = c.clone()
if c == cond {
panic(fmt.Errorf("<Condition.AndNotCond> cannot use self as sub cond"))
}
if cond != nil {
c.params = append(c.params, condValue{cond: cond, isCond: true, isNot: true})
}
return c
}
2015-09-12 13:46:43 +00:00
// Or 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
}
2015-09-12 13:46:43 +00:00
// OrNot 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
}
2015-09-12 13:46:43 +00:00
// OrCond 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
}
// OrNotCond combine a OR NOT condition to current condition
func (c *Condition) OrNotCond(cond *Condition) *Condition {
c = c.clone()
if c == cond {
panic(fmt.Errorf("<Condition.OrNotCond> cannot use self as sub cond"))
}
if cond != nil {
c.params = append(c.params, condValue{cond: cond, isCond: true, isNot: true, isOr: true})
}
return c
}
2015-09-12 13:46:43 +00:00
// IsEmpty 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
}
2015-09-12 13:46:43 +00:00
// clone 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
}