cond_in.go 5.7 KB

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