orm_conds.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. package orm
  15. import (
  16. "fmt"
  17. "strings"
  18. )
  19. // ExprSep define the expression separation
  20. const (
  21. ExprSep = "__"
  22. )
  23. type condValue struct {
  24. exprs []string
  25. args []interface{}
  26. cond *Condition
  27. isOr bool
  28. isNot bool
  29. isCond bool
  30. }
  31. // Condition struct.
  32. // work for WHERE conditions.
  33. type Condition struct {
  34. params []condValue
  35. }
  36. // NewCondition return new condition struct
  37. func NewCondition() *Condition {
  38. c := &Condition{}
  39. return c
  40. }
  41. // And add expression to condition
  42. func (c Condition) And(expr string, args ...interface{}) *Condition {
  43. if expr == "" || len(args) == 0 {
  44. panic(fmt.Errorf("<Condition.And> args cannot empty"))
  45. }
  46. c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args})
  47. return &c
  48. }
  49. // AndNot add NOT expression to condition
  50. func (c Condition) AndNot(expr string, args ...interface{}) *Condition {
  51. if expr == "" || len(args) == 0 {
  52. panic(fmt.Errorf("<Condition.AndNot> args cannot empty"))
  53. }
  54. c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args, isNot: true})
  55. return &c
  56. }
  57. // AndCond combine a condition to current condition
  58. func (c *Condition) AndCond(cond *Condition) *Condition {
  59. c = c.clone()
  60. if c == cond {
  61. panic(fmt.Errorf("<Condition.AndCond> cannot use self as sub cond"))
  62. }
  63. if cond != nil {
  64. c.params = append(c.params, condValue{cond: cond, isCond: true})
  65. }
  66. return c
  67. }
  68. // AndNotCond combine a AND NOT condition to current condition
  69. func (c *Condition) AndNotCond(cond *Condition) *Condition {
  70. c = c.clone()
  71. if c == cond {
  72. panic(fmt.Errorf("<Condition.AndNotCond> cannot use self as sub cond"))
  73. }
  74. if cond != nil {
  75. c.params = append(c.params, condValue{cond: cond, isCond: true, isNot: true})
  76. }
  77. return c
  78. }
  79. // Or add OR expression to condition
  80. func (c Condition) Or(expr string, args ...interface{}) *Condition {
  81. if expr == "" || len(args) == 0 {
  82. panic(fmt.Errorf("<Condition.Or> args cannot empty"))
  83. }
  84. c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args, isOr: true})
  85. return &c
  86. }
  87. // OrNot add OR NOT expression to condition
  88. func (c Condition) OrNot(expr string, args ...interface{}) *Condition {
  89. if expr == "" || len(args) == 0 {
  90. panic(fmt.Errorf("<Condition.OrNot> args cannot empty"))
  91. }
  92. c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), args: args, isNot: true, isOr: true})
  93. return &c
  94. }
  95. // OrCond combine a OR condition to current condition
  96. func (c *Condition) OrCond(cond *Condition) *Condition {
  97. c = c.clone()
  98. if c == cond {
  99. panic(fmt.Errorf("<Condition.OrCond> cannot use self as sub cond"))
  100. }
  101. if cond != nil {
  102. c.params = append(c.params, condValue{cond: cond, isCond: true, isOr: true})
  103. }
  104. return c
  105. }
  106. // OrNotCond combine a OR NOT condition to current condition
  107. func (c *Condition) OrNotCond(cond *Condition) *Condition {
  108. c = c.clone()
  109. if c == cond {
  110. panic(fmt.Errorf("<Condition.OrNotCond> cannot use self as sub cond"))
  111. }
  112. if cond != nil {
  113. c.params = append(c.params, condValue{cond: cond, isCond: true, isNot: true, isOr: true})
  114. }
  115. return c
  116. }
  117. // IsEmpty check the condition arguments are empty or not.
  118. func (c *Condition) IsEmpty() bool {
  119. return len(c.params) == 0
  120. }
  121. // clone clone a condition
  122. func (c Condition) clone() *Condition {
  123. return &c
  124. }