where.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "like":
  78. whereSQL += fmt.Sprint(k, " like ?")
  79. vals = append(vals, v)
  80. }
  81. default:
  82. whereSQL += fmt.Sprint(src)
  83. if fmt.Sprintf("%v", v) != "" {
  84. vals = append(vals, v)
  85. }
  86. break
  87. }
  88. }
  89. return
  90. }
  91. func whereBuildOr(where map[string]interface{}) (whereSQL string, vals []interface{}, err error) {
  92. for k, v := range where {
  93. src := k
  94. ks := strings.Split(k, " ")
  95. if whereSQL != "" {
  96. whereSQL += " Or "
  97. }
  98. strings.Join(ks, ",")
  99. switch len(ks) {
  100. case 1:
  101. switch v := v.(type) {
  102. case NullType:
  103. if v == IsNotNull {
  104. whereSQL += fmt.Sprint(k, " IS NOT NULL")
  105. } else {
  106. whereSQL += fmt.Sprint(k, " IS NULL")
  107. }
  108. default:
  109. whereSQL += fmt.Sprint(k, "=?")
  110. vals = append(vals, v)
  111. }
  112. break
  113. case 2:
  114. k = ks[0]
  115. switch ks[1] {
  116. case "=":
  117. whereSQL += fmt.Sprint(k, " = ?")
  118. vals = append(vals, v)
  119. break
  120. case ">":
  121. whereSQL += fmt.Sprint(k, " > ?")
  122. vals = append(vals, v)
  123. break
  124. case ">=":
  125. whereSQL += fmt.Sprint(k, " >= ?")
  126. vals = append(vals, v)
  127. break
  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 "in":
  145. whereSQL += fmt.Sprint(k, " in (?)")
  146. vals = append(vals, v)
  147. break
  148. case "like":
  149. whereSQL += fmt.Sprint(k, " like ?")
  150. vals = append(vals, v)
  151. }
  152. default:
  153. whereSQL += fmt.Sprintf("(%s)", src)
  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. }