cond_notin.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2016 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. import (
  6. "fmt"
  7. "reflect"
  8. "strings"
  9. )
  10. type condNotIn condIn
  11. var _ Cond = condNotIn{}
  12. // NotIn generate NOT IN condition
  13. func NotIn(col string, values ...interface{}) Cond {
  14. return condNotIn{col, values}
  15. }
  16. func (condNotIn condNotIn) handleBlank(w Writer) error {
  17. _, err := fmt.Fprint(w, "0=0")
  18. return err
  19. }
  20. func (condNotIn condNotIn) WriteTo(w Writer) error {
  21. if len(condNotIn.vals) <= 0 {
  22. return condNotIn.handleBlank(w)
  23. }
  24. switch condNotIn.vals[0].(type) {
  25. case []int8:
  26. vals := condNotIn.vals[0].([]int8)
  27. if len(vals) <= 0 {
  28. return condNotIn.handleBlank(w)
  29. }
  30. questionMark := strings.Repeat("?,", len(vals))
  31. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  32. return err
  33. }
  34. for _, val := range vals {
  35. w.Append(val)
  36. }
  37. case []int16:
  38. vals := condNotIn.vals[0].([]int16)
  39. if len(vals) <= 0 {
  40. return condNotIn.handleBlank(w)
  41. }
  42. questionMark := strings.Repeat("?,", len(vals))
  43. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  44. return err
  45. }
  46. for _, val := range vals {
  47. w.Append(val)
  48. }
  49. case []int:
  50. vals := condNotIn.vals[0].([]int)
  51. if len(vals) <= 0 {
  52. return condNotIn.handleBlank(w)
  53. }
  54. questionMark := strings.Repeat("?,", len(vals))
  55. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  56. return err
  57. }
  58. for _, val := range vals {
  59. w.Append(val)
  60. }
  61. case []int32:
  62. vals := condNotIn.vals[0].([]int32)
  63. if len(vals) <= 0 {
  64. return condNotIn.handleBlank(w)
  65. }
  66. questionMark := strings.Repeat("?,", len(vals))
  67. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  68. return err
  69. }
  70. for _, val := range vals {
  71. w.Append(val)
  72. }
  73. case []int64:
  74. vals := condNotIn.vals[0].([]int64)
  75. if len(vals) <= 0 {
  76. return condNotIn.handleBlank(w)
  77. }
  78. questionMark := strings.Repeat("?,", len(vals))
  79. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  80. return err
  81. }
  82. for _, val := range vals {
  83. w.Append(val)
  84. }
  85. case []uint8:
  86. vals := condNotIn.vals[0].([]uint8)
  87. if len(vals) <= 0 {
  88. return condNotIn.handleBlank(w)
  89. }
  90. questionMark := strings.Repeat("?,", len(vals))
  91. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  92. return err
  93. }
  94. for _, val := range vals {
  95. w.Append(val)
  96. }
  97. case []uint16:
  98. vals := condNotIn.vals[0].([]uint16)
  99. if len(vals) <= 0 {
  100. return condNotIn.handleBlank(w)
  101. }
  102. questionMark := strings.Repeat("?,", len(vals))
  103. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  104. return err
  105. }
  106. for _, val := range vals {
  107. w.Append(val)
  108. }
  109. case []uint:
  110. vals := condNotIn.vals[0].([]uint)
  111. if len(vals) <= 0 {
  112. return condNotIn.handleBlank(w)
  113. }
  114. questionMark := strings.Repeat("?,", len(vals))
  115. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  116. return err
  117. }
  118. for _, val := range vals {
  119. w.Append(val)
  120. }
  121. case []uint32:
  122. vals := condNotIn.vals[0].([]uint32)
  123. if len(vals) <= 0 {
  124. return condNotIn.handleBlank(w)
  125. }
  126. questionMark := strings.Repeat("?,", len(vals))
  127. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  128. return err
  129. }
  130. for _, val := range vals {
  131. w.Append(val)
  132. }
  133. case []uint64:
  134. vals := condNotIn.vals[0].([]uint64)
  135. if len(vals) <= 0 {
  136. return condNotIn.handleBlank(w)
  137. }
  138. questionMark := strings.Repeat("?,", len(vals))
  139. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  140. return err
  141. }
  142. for _, val := range vals {
  143. w.Append(val)
  144. }
  145. case []string:
  146. vals := condNotIn.vals[0].([]string)
  147. if len(vals) <= 0 {
  148. return condNotIn.handleBlank(w)
  149. }
  150. questionMark := strings.Repeat("?,", len(vals))
  151. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  152. return err
  153. }
  154. for _, val := range vals {
  155. w.Append(val)
  156. }
  157. case []interface{}:
  158. vals := condNotIn.vals[0].([]interface{})
  159. if len(vals) <= 0 {
  160. return condNotIn.handleBlank(w)
  161. }
  162. questionMark := strings.Repeat("?,", len(vals))
  163. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  164. return err
  165. }
  166. w.Append(vals...)
  167. case expr:
  168. val := condNotIn.vals[0].(expr)
  169. if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
  170. return err
  171. }
  172. if err := val.WriteTo(w); err != nil {
  173. return err
  174. }
  175. if _, err := fmt.Fprintf(w, ")"); err != nil {
  176. return err
  177. }
  178. case *Builder:
  179. val := condNotIn.vals[0].(*Builder)
  180. if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
  181. return err
  182. }
  183. if err := val.WriteTo(w); err != nil {
  184. return err
  185. }
  186. if _, err := fmt.Fprintf(w, ")"); err != nil {
  187. return err
  188. }
  189. default:
  190. v := reflect.ValueOf(condNotIn.vals[0])
  191. if v.Kind() == reflect.Slice {
  192. l := v.Len()
  193. if l == 0 {
  194. return condNotIn.handleBlank(w)
  195. }
  196. questionMark := strings.Repeat("?,", l)
  197. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  198. return err
  199. }
  200. for i := 0; i < l; i++ {
  201. w.Append(v.Index(i).Interface())
  202. }
  203. } else {
  204. questionMark := strings.Repeat("?,", len(condNotIn.vals))
  205. if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
  206. return err
  207. }
  208. w.Append(condNotIn.vals...)
  209. }
  210. }
  211. return nil
  212. }
  213. func (condNotIn condNotIn) And(conds ...Cond) Cond {
  214. return And(condNotIn, And(conds...))
  215. }
  216. func (condNotIn condNotIn) Or(conds ...Cond) Cond {
  217. return Or(condNotIn, Or(conds...))
  218. }
  219. func (condNotIn condNotIn) IsValid() bool {
  220. return len(condNotIn.col) > 0 && len(condNotIn.vals) > 0
  221. }