github.com/astaxie/beego@v1.12.3/orm/orm_conds.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package orm
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  )
    21  
    22  // ExprSep define the expression separation
    23  const (
    24  	ExprSep = "__"
    25  )
    26  
    27  type condValue struct {
    28  	exprs  []string
    29  	args   []interface{}
    30  	cond   *Condition
    31  	isOr   bool
    32  	isNot  bool
    33  	isCond bool
    34  	isRaw  bool
    35  	sql    string
    36  }
    37  
    38  // Condition struct.
    39  // work for WHERE conditions.
    40  type Condition struct {
    41  	params []condValue
    42  }
    43  
    44  // NewCondition return new condition struct
    45  func NewCondition() *Condition {
    46  	c := &Condition{}
    47  	return c
    48  }
    49  
    50  // Raw add raw sql to condition
    51  func (c Condition) Raw(expr string, sql string) *Condition {
    52  	if len(sql) == 0 {
    53  		panic(fmt.Errorf("<Condition.Raw> sql cannot empty"))
    54  	}
    55  	c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), sql: sql, isRaw: true})
    56  	return &c
    57  }
    58  
    59  // And add expression to condition
    60  func (c Condition) And(expr string, args ...interface{}) *Condition {
    61  	if expr == "" || len(args) == 0 {
    62  		panic(fmt.Errorf("<Condition.And> args cannot empty"))
    63  	}
    64  	c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args})
    65  	return &c
    66  }
    67  
    68  // AndNot add NOT expression to condition
    69  func (c Condition) AndNot(expr string, args ...interface{}) *Condition {
    70  	if expr == "" || len(args) == 0 {
    71  		panic(fmt.Errorf("<Condition.AndNot> args cannot empty"))
    72  	}
    73  	c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args, isNot: true})
    74  	return &c
    75  }
    76  
    77  // AndCond combine a condition to current condition
    78  func (c *Condition) AndCond(cond *Condition) *Condition {
    79  	c = c.clone()
    80  	if c == cond {
    81  		panic(fmt.Errorf("<Condition.AndCond> cannot use self as sub cond"))
    82  	}
    83  	if cond != nil {
    84  		c.params = append(c.params, condValue{cond: cond, isCond: true})
    85  	}
    86  	return c
    87  }
    88  
    89  // AndNotCond combine a AND NOT condition to current condition
    90  func (c *Condition) AndNotCond(cond *Condition) *Condition {
    91  	c = c.clone()
    92  	if c == cond {
    93  		panic(fmt.Errorf("<Condition.AndNotCond> cannot use self as sub cond"))
    94  	}
    95  
    96  	if cond != nil {
    97  		c.params = append(c.params, condValue{cond: cond, isCond: true, isNot: true})
    98  	}
    99  	return c
   100  }
   101  
   102  // Or add OR expression to condition
   103  func (c Condition) Or(expr string, args ...interface{}) *Condition {
   104  	if expr == "" || len(args) == 0 {
   105  		panic(fmt.Errorf("<Condition.Or> args cannot empty"))
   106  	}
   107  	c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args, isOr: true})
   108  	return &c
   109  }
   110  
   111  // OrNot add OR NOT expression to condition
   112  func (c Condition) OrNot(expr string, args ...interface{}) *Condition {
   113  	if expr == "" || len(args) == 0 {
   114  		panic(fmt.Errorf("<Condition.OrNot> args cannot empty"))
   115  	}
   116  	c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args, isNot: true, isOr: true})
   117  	return &c
   118  }
   119  
   120  // OrCond combine a OR condition to current condition
   121  func (c *Condition) OrCond(cond *Condition) *Condition {
   122  	c = c.clone()
   123  	if c == cond {
   124  		panic(fmt.Errorf("<Condition.OrCond> cannot use self as sub cond"))
   125  	}
   126  	if cond != nil {
   127  		c.params = append(c.params, condValue{cond: cond, isCond: true, isOr: true})
   128  	}
   129  	return c
   130  }
   131  
   132  // OrNotCond combine a OR NOT condition to current condition
   133  func (c *Condition) OrNotCond(cond *Condition) *Condition {
   134  	c = c.clone()
   135  	if c == cond {
   136  		panic(fmt.Errorf("<Condition.OrNotCond> cannot use self as sub cond"))
   137  	}
   138  
   139  	if cond != nil {
   140  		c.params = append(c.params, condValue{cond: cond, isCond: true, isNot: true, isOr: true})
   141  	}
   142  	return c
   143  }
   144  
   145  // IsEmpty check the condition arguments are empty or not.
   146  func (c *Condition) IsEmpty() bool {
   147  	return len(c.params) == 0
   148  }
   149  
   150  // clone clone a condition
   151  func (c Condition) clone() *Condition {
   152  	return &c
   153  }