where.go 4.0 KB

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