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

116 lines
3.0 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"
)
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
}