wherebuild.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. if whereSQL != "" {
  24. whereSQL += " AND "
  25. }
  26. strings.Join(ks, ",")
  27. switch len(ks) {
  28. case 1:
  29. switch v := v.(type) {
  30. case NullType:
  31. if v == IsNotNull {
  32. whereSQL += fmt.Sprint(k, " IS NOT NULL")
  33. } else {
  34. whereSQL += fmt.Sprint(k, " IS NULL")
  35. }
  36. default:
  37. whereSQL += fmt.Sprint(k, "=?")
  38. vals = append(vals, v)
  39. }
  40. break
  41. case 2:
  42. k = ks[0]
  43. switch ks[1] {
  44. case "=":
  45. whereSQL += fmt.Sprint(k, " = ?")
  46. vals = append(vals, v)
  47. break
  48. case ">":
  49. whereSQL += fmt.Sprint(k, " > ?")
  50. vals = append(vals, v)
  51. break
  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 "in":
  73. whereSQL += fmt.Sprint(k, " in (?)")
  74. vals = append(vals, v)
  75. break
  76. case "like":
  77. whereSQL += fmt.Sprint(k, " like ?")
  78. vals = append(vals, v)
  79. case "is":
  80. whereSQL += fmt.Sprintf("%s is %v", k, v)
  81. }
  82. break
  83. }
  84. }
  85. return
  86. }
  87. func whereBuildOr(where map[string]interface{}) (whereSQL string, vals []interface{}, err error) {
  88. for k, v := range where {
  89. ks := strings.Split(k, " ")
  90. if len(ks) > 2 {
  91. return "", nil, fmt.Errorf("Error in query condition: %s. ", k)
  92. }
  93. if whereSQL != "" {
  94. whereSQL += " Or "
  95. }
  96. strings.Join(ks, ",")
  97. switch len(ks) {
  98. case 1:
  99. switch v := v.(type) {
  100. case NullType:
  101. if v == IsNotNull {
  102. whereSQL += fmt.Sprint(k, " IS NOT NULL")
  103. } else {
  104. whereSQL += fmt.Sprint(k, " IS NULL")
  105. }
  106. default:
  107. whereSQL += fmt.Sprint(k, "=?")
  108. vals = append(vals, v)
  109. }
  110. break
  111. case 2:
  112. k = ks[0]
  113. switch ks[1] {
  114. case "=":
  115. whereSQL += fmt.Sprint(k, " = ?")
  116. vals = append(vals, v)
  117. break
  118. case ">":
  119. whereSQL += fmt.Sprint(k, " > ?")
  120. vals = append(vals, v)
  121. break
  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 "in":
  143. whereSQL += fmt.Sprint(k, " in (?)")
  144. vals = append(vals, v)
  145. break
  146. case "like":
  147. whereSQL += fmt.Sprint(k, " like ?")
  148. vals = append(vals, v)
  149. case "is":
  150. whereSQL += fmt.Sprintf("%s is %v", k, v)
  151. }
  152. break
  153. }
  154. }
  155. return
  156. }
  157. func whereBuildAndOr(where map[string]interface{}, or map[string]interface{}) (whereSQL string, vals []interface{}, err error) {
  158. if len(where) > 0 {
  159. andSql, andVars, err := whereBuild(where)
  160. if err != nil {
  161. return "", nil, err
  162. }
  163. whereSQL = andSql
  164. vals = andVars
  165. }
  166. if len(or) > 0 {
  167. orSql, orVars, err := whereBuildOr(or)
  168. if err != nil {
  169. return "", nil, err
  170. }
  171. if whereSQL != "" {
  172. whereSQL = fmt.Sprintf("%s and (%s)", whereSQL, orSql)
  173. } else {
  174. whereSQL = fmt.Sprintf("%s", orSql)
  175. }
  176. vals = append(vals, orVars...)
  177. }
  178. return
  179. }