query.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package query
  2. import (
  3. "gorm.io/gorm"
  4. "strings"
  5. )
  6. type Query struct {
  7. db *gorm.DB
  8. condition string
  9. fields []string
  10. params []interface{}
  11. limit int
  12. offset int
  13. table string
  14. orderBy []string
  15. groupBy []string
  16. joins []join
  17. }
  18. type join struct {
  19. Table string
  20. Direction string
  21. Conds []*Condition
  22. }
  23. func (query *Query) Select(fields ...string) *Query {
  24. if query.fields == nil {
  25. query.fields = fields
  26. } else {
  27. query.fields = append(query.fields, fields...)
  28. }
  29. return query
  30. }
  31. func (query *Query) From(table string) *Query {
  32. query.table = table
  33. return query
  34. }
  35. //// Joins specify Joins conditions
  36. //// db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu@example.org").Find(&user)
  37. //func (s *DB) Joins(query string, args ...interface{}) *DB {
  38. // return s.clone().search.Joins(query, args...).db
  39. //}
  40. func (query *Query) LeftJoin(table string, conds ...*Condition) *Query {
  41. query.joins = append(query.joins, join{
  42. Table: table,
  43. Direction: "LEFT",
  44. Conds: conds,
  45. })
  46. return query
  47. }
  48. func (query *Query) RightJoin(table string, conds ...*Condition) *Query {
  49. query.joins = append(query.joins, join{
  50. Table: table,
  51. Direction: "RIGHT",
  52. Conds: conds,
  53. })
  54. return query
  55. }
  56. func (query *Query) InnerJoin(table string, conds ...*Condition) *Query {
  57. query.joins = append(query.joins, join{
  58. Table: table,
  59. Direction: "INNER",
  60. Conds: conds,
  61. })
  62. return query
  63. }
  64. func (query *Query) AndFilterWhere(conds ...*Condition) *Query {
  65. length := len(conds)
  66. if length == 0 {
  67. return query
  68. }
  69. cs, ps := buildConditions("AND", true, conds...)
  70. if cs == "" {
  71. return query
  72. }
  73. query.params = append(query.params, ps...)
  74. if query.condition == "" {
  75. query.condition = cs
  76. } else {
  77. query.condition += " AND " + cs
  78. }
  79. return query
  80. }
  81. func (query *Query) AndWhere(conds ...*Condition) *Query {
  82. length := len(conds)
  83. if length == 0 {
  84. return query
  85. }
  86. cs, ps := buildConditions("AND", false, conds...)
  87. if cs == "" {
  88. return query
  89. }
  90. query.params = append(query.params, ps...)
  91. if query.condition == "" {
  92. query.condition = cs
  93. } else {
  94. query.condition += " AND " + cs
  95. }
  96. return query
  97. }
  98. func (query *Query) OrFilterWhere(conds ...*Condition) *Query {
  99. length := len(conds)
  100. if length == 0 {
  101. return query
  102. }
  103. cs, ps := buildConditions("OR", true, conds...)
  104. if cs == "" {
  105. return query
  106. }
  107. query.params = append(query.params, ps...)
  108. if query.condition == "" {
  109. query.condition = cs
  110. } else {
  111. query.condition += " AND (" + cs + ")"
  112. }
  113. return query
  114. }
  115. func (query *Query) OrWhere(conds ...*Condition) *Query {
  116. length := len(conds)
  117. if length == 0 {
  118. return query
  119. }
  120. cs, ps := buildConditions("OR", false, conds...)
  121. if cs == "" {
  122. return query
  123. }
  124. query.params = append(query.params, ps...)
  125. if query.condition == "" {
  126. query.condition = cs
  127. } else {
  128. query.condition += " AND (" + cs + ")"
  129. }
  130. return query
  131. }
  132. func (query *Query) GroupBy(cols ...string) *Query {
  133. query.groupBy = append(query.groupBy, cols...)
  134. return query
  135. }
  136. func (query *Query) OrderBy(col, direction string) *Query {
  137. direction = strings.ToUpper(direction)
  138. if direction == "" || !(direction == "ASC" || direction == "DESC") {
  139. direction = "ASC"
  140. }
  141. query.orderBy = append(query.orderBy, col+" "+direction)
  142. return query
  143. }
  144. func (query *Query) Offset(i int) *Query {
  145. query.offset = i
  146. return query
  147. }
  148. func (query *Query) Limit(i int) *Query {
  149. query.limit = i
  150. return query
  151. }
  152. func (query *Query) prepare() (*gorm.DB, error) {
  153. db := query.db
  154. if query.condition != "" {
  155. db = db.Where(query.condition, query.params...)
  156. }
  157. if query.fields != nil {
  158. db = db.Select(strings.Join(query.fields, ","))
  159. }
  160. if query.table != "" {
  161. db = db.Table(query.table)
  162. }
  163. if query.joins != nil && len(query.joins) > 0 {
  164. for _, joinEntity := range query.joins {
  165. cs, ps := buildConditions("OR", false, joinEntity.Conds...)
  166. db = db.Joins(joinEntity.Direction+" JOIN "+joinEntity.Table+" ON "+cs, ps...)
  167. }
  168. }
  169. if query.orderBy != nil && len(query.orderBy) > 0 {
  170. db = db.Order(strings.Join(query.orderBy, ","))
  171. }
  172. if query.groupBy != nil && len(query.groupBy) > 0 {
  173. db = db.Group(strings.Join(query.groupBy, ","))
  174. }
  175. if query.offset > 0 {
  176. db = db.Offset(query.offset)
  177. }
  178. if query.limit > 0 {
  179. db = db.Limit(query.limit)
  180. }
  181. return db, nil
  182. }
  183. func (query *Query) Count(v interface{}) (i int64) {
  184. var (
  185. db *gorm.DB
  186. err error
  187. )
  188. if db, err = query.prepare(); err != nil {
  189. return
  190. } else {
  191. err = db.Model(v).Count(&i).Error
  192. }
  193. return
  194. }
  195. func (query *Query) One(v interface{}) (err error) {
  196. var (
  197. db *gorm.DB
  198. )
  199. if db, err = query.prepare(); err != nil {
  200. return
  201. } else {
  202. err = db.First(v).Error
  203. }
  204. return
  205. }
  206. func (query *Query) All(v interface{}) (err error) {
  207. var (
  208. db *gorm.DB
  209. )
  210. if db, err = query.prepare(); err != nil {
  211. return
  212. } else {
  213. err = db.Find(v).Error
  214. }
  215. return
  216. }
  217. func New(db *gorm.DB) *Query {
  218. return &Query{
  219. db: db,
  220. params: make([]interface{}, 0),
  221. orderBy: make([]string, 0),
  222. groupBy: make([]string, 0),
  223. joins: make([]join, 0),
  224. }
  225. }