where.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. }
  80. break
  81. }
  82. }
  83. return
  84. }
  85. func whereBuildOr(where map[string]interface{}) (whereSQL string, vals []interface{}, err error) {
  86. for k, v := range where {
  87. ks := strings.Split(k, " ")
  88. if len(ks) > 2 {
  89. return "", nil, fmt.Errorf("Error in query condition: %s. ", k)
  90. }
  91. if whereSQL != "" {
  92. whereSQL += " or "
  93. }
  94. strings.Join(ks, ",")
  95. switch len(ks) {
  96. case 1:
  97. switch v := v.(type) {
  98. case NullType:
  99. if v == IsNotNull {
  100. whereSQL += fmt.Sprint(k, " IS NOT NULL")
  101. } else {
  102. whereSQL += fmt.Sprint(k, " IS NULL")
  103. }
  104. default:
  105. whereSQL += fmt.Sprint(k, "=?")
  106. vals = append(vals, v)
  107. }
  108. break
  109. case 2:
  110. k = ks[0]
  111. switch ks[1] {
  112. case "=":
  113. whereSQL += fmt.Sprint(k, " = ?")
  114. vals = append(vals, v)
  115. break
  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 "in":
  141. whereSQL += fmt.Sprint(k, " in (?)")
  142. vals = append(vals, v)
  143. break
  144. case "like":
  145. whereSQL += fmt.Sprint(k, " like ?")
  146. vals = append(vals, v)
  147. }
  148. break
  149. }
  150. }
  151. return
  152. }
  153. func whereBuildAndOr(where map[string]interface{}, or map[string]interface{}) (whereSQL string, vals []interface{}, err error) {
  154. if len(where) > 0 {
  155. andSql, andVars, err := whereBuild(where)
  156. if err != nil {
  157. return "", nil, err
  158. }
  159. whereSQL = andSql
  160. vals = andVars
  161. }
  162. if len(or) > 0 {
  163. orSql, orVars, err := whereBuildOr(or)
  164. fmt.Printf("xxxx:yyyy:%s,%v\n", orSql, orVars)
  165. if err != nil {
  166. return "", nil, err
  167. }
  168. if whereSQL != "" {
  169. whereSQL = fmt.Sprintf("%s and (%s)", whereSQL, orSql)
  170. } else {
  171. whereSQL = fmt.Sprintf("%s", orSql)
  172. }
  173. vals = append(vals, orVars...)
  174. }
  175. return
  176. }