cond_notin.go 6.2 KB

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