where.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "errors"
  6. "fmt"
  7. "strings"
  8. )
  9. type NullType byte
  10. const (
  11. _ NullType = iota
  12. // IsNull the same as `is null`
  13. IsNull
  14. // IsNotNull the same as `is not null`
  15. IsNotNull
  16. )
  17. func WhereAdd(where [][2]interface{}, key string, value interface{}) [][2]interface{} {
  18. where = append(where, [2]interface{}{key, value})
  19. return where
  20. }
  21. // sql build where
  22. func whereBuild(where [][2]interface{}) (whereSQL string, vals []interface{}, err error) {
  23. for _, keyvalue := range where {
  24. k, ok := keyvalue[0].(string)
  25. if !ok {
  26. return "", nil, errors.New("where参数错误")
  27. }
  28. v := keyvalue[1]
  29. ks := strings.Split(k, " ")
  30. src := k
  31. if whereSQL != "" {
  32. whereSQL += " AND "
  33. }
  34. strings.Join(ks, ",")
  35. switch len(ks) {
  36. case 1:
  37. switch v := v.(type) {
  38. case NullType:
  39. if v == IsNotNull {
  40. whereSQL += fmt.Sprint(k, " IS NOT NULL")
  41. } else {
  42. whereSQL += fmt.Sprint(k, " IS NULL")
  43. }
  44. default:
  45. whereSQL += fmt.Sprint(k, "=?")
  46. vals = append(vals, v)
  47. }
  48. break
  49. case 2:
  50. k = ks[0]
  51. switch ks[1] {
  52. case "=":
  53. whereSQL += fmt.Sprint(k, " = ?")
  54. vals = append(vals, v)
  55. break
  56. case ">":
  57. whereSQL += fmt.Sprint(k, " > ?")
  58. vals = append(vals, v)
  59. break
  60. case ">=":
  61. whereSQL += fmt.Sprint(k, " >= ?")
  62. vals = append(vals, v)
  63. break
  64. case "<":
  65. whereSQL += fmt.Sprint(k, " < ?")
  66. vals = append(vals, v)
  67. break
  68. case "<=":
  69. whereSQL += fmt.Sprint(k, " <= ?")
  70. vals = append(vals, v)
  71. break
  72. case "!=":
  73. whereSQL += fmt.Sprint(k, " != ?")
  74. vals = append(vals, v)
  75. break
  76. case "<>":
  77. whereSQL += fmt.Sprint(k, " != ?")
  78. vals = append(vals, v)
  79. break
  80. case "in":
  81. whereSQL += fmt.Sprint(k, " in (?)")
  82. vals = append(vals, v)
  83. break
  84. case "like":
  85. whereSQL += fmt.Sprint(k, " like ?")
  86. vals = append(vals, v)
  87. }
  88. default:
  89. whereSQL += fmt.Sprint(src)
  90. if fmt.Sprintf("%v", v) != "" {
  91. vals = append(vals, v)
  92. }
  93. break
  94. }
  95. }
  96. return
  97. }
  98. func whereBuildOr(where [][2]interface{}) (whereSQL string, vals []interface{}, err error) {
  99. for _, keyvalue := range where {
  100. k, ok := keyvalue[0].(string)
  101. if !ok {
  102. return "", nil, errors.New("where参数错误")
  103. }
  104. v := keyvalue[1]
  105. ks := strings.Split(k, " ")
  106. src := k
  107. if whereSQL != "" {
  108. whereSQL += " Or "
  109. }
  110. strings.Join(ks, ",")
  111. switch len(ks) {
  112. case 1:
  113. switch v := v.(type) {
  114. case NullType:
  115. if v == IsNotNull {
  116. whereSQL += fmt.Sprint(k, " IS NOT NULL")
  117. } else {
  118. whereSQL += fmt.Sprint(k, " IS NULL")
  119. }
  120. default:
  121. whereSQL += fmt.Sprint(k, "=?")
  122. vals = append(vals, v)
  123. }
  124. break
  125. case 2:
  126. k = ks[0]
  127. switch ks[1] {
  128. case "=":
  129. whereSQL += fmt.Sprint(k, " = ?")
  130. vals = append(vals, v)
  131. break
  132. case ">":
  133. whereSQL += fmt.Sprint(k, " > ?")
  134. vals = append(vals, v)
  135. break
  136. case ">=":
  137. whereSQL += fmt.Sprint(k, " >= ?")
  138. vals = append(vals, v)
  139. break
  140. case "<":
  141. whereSQL += fmt.Sprint(k, " < ?")
  142. vals = append(vals, v)
  143. break
  144. case "<=":
  145. whereSQL += fmt.Sprint(k, " <= ?")
  146. vals = append(vals, v)
  147. break
  148. case "!=":
  149. whereSQL += fmt.Sprint(k, " != ?")
  150. vals = append(vals, v)
  151. break
  152. case "<>":
  153. whereSQL += fmt.Sprint(k, " != ?")
  154. vals = append(vals, v)
  155. break
  156. case "in":
  157. whereSQL += fmt.Sprint(k, " in (?)")
  158. vals = append(vals, v)
  159. break
  160. case "like":
  161. whereSQL += fmt.Sprint(k, " like ?")
  162. vals = append(vals, v)
  163. }
  164. default:
  165. whereSQL += fmt.Sprint(src)
  166. if fmt.Sprintf("%v", v) != "" {
  167. vals = append(vals, v)
  168. }
  169. break
  170. }
  171. }
  172. return
  173. }
  174. func whereBuildAndOr(where [][2]interface{}, or [][2]interface{}) (whereSQL string, vals []interface{}, err error) {
  175. if len(where) > 0 {
  176. andSql, andVars, err := whereBuild(where)
  177. if err != nil {
  178. return "", nil, err
  179. }
  180. whereSQL = andSql
  181. vals = andVars
  182. }
  183. if len(or) > 0 {
  184. orSql, orVars, err := whereBuildOr(or)
  185. if err != nil {
  186. return "", nil, err
  187. }
  188. if whereSQL != "" {
  189. whereSQL = fmt.Sprintf("%s and (%s)", whereSQL, orSql)
  190. } else {
  191. whereSQL = fmt.Sprintf("%s", orSql)
  192. }
  193. vals = append(vals, orVars...)
  194. }
  195. return
  196. }