本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
5.9KB

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