where.go 3.8 KB

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