cond_notin.go 5.5 KB

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