query.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package rest
  2. import (
  3. "fmt"
  4. "git.nspix.com/golang/micro/helper/utils"
  5. "gorm.io/gorm"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type (
  12. Query struct {
  13. db *gorm.DB
  14. condition string
  15. fields []string
  16. params []interface{}
  17. table string
  18. joins []join
  19. orderBy []string
  20. groupBy []string
  21. limit int
  22. offset int
  23. }
  24. condition struct {
  25. Field string `json:"field"`
  26. Value interface{} `json:"value"`
  27. Expr string `json:"expr"`
  28. }
  29. join struct {
  30. Table string
  31. Direction string
  32. Conds []*condition
  33. }
  34. )
  35. func (cond *condition) WithExpr(v string) *condition {
  36. cond.Expr = v
  37. return cond
  38. }
  39. func (query *Query) compile() (*gorm.DB, error) {
  40. db := query.db
  41. if query.condition != "" {
  42. db = db.Where(query.condition, query.params...)
  43. }
  44. if query.fields != nil {
  45. db = db.Select(strings.Join(query.fields, ","))
  46. }
  47. if query.table != "" {
  48. db = db.Table(query.table)
  49. }
  50. if query.joins != nil && len(query.joins) > 0 {
  51. for _, joinEntity := range query.joins {
  52. cs, ps := query.buildConditions("OR", false, joinEntity.Conds...)
  53. db = db.Joins(joinEntity.Direction+" JOIN "+joinEntity.Table+" ON "+cs, ps...)
  54. }
  55. }
  56. if query.orderBy != nil && len(query.orderBy) > 0 {
  57. db = db.Order(strings.Join(query.orderBy, ","))
  58. }
  59. if query.groupBy != nil && len(query.groupBy) > 0 {
  60. db = db.Group(strings.Join(query.groupBy, ","))
  61. }
  62. if query.offset > 0 {
  63. db = db.Offset(query.offset)
  64. }
  65. if query.limit > 0 {
  66. db = db.Limit(query.limit)
  67. }
  68. return db, nil
  69. }
  70. func (query *Query) decodeValue(v interface{}) string {
  71. refVal := reflect.Indirect(reflect.ValueOf(v))
  72. switch refVal.Kind() {
  73. case reflect.Bool:
  74. if refVal.Bool() {
  75. return "1"
  76. } else {
  77. return "0"
  78. }
  79. case reflect.Int, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint32, reflect.Uint64:
  80. return strconv.FormatInt(refVal.Int(), 10)
  81. case reflect.Float32, reflect.Float64:
  82. return strconv.FormatFloat(refVal.Float(), 'f', -1, 64)
  83. case reflect.String:
  84. return "'" + refVal.String() + "'"
  85. case timeKind:
  86. if tm, ok := refVal.Interface().(time.Time); ok {
  87. return "'" + tm.Format("2006-01-02 15:04:05") + "'"
  88. }
  89. return fmt.Sprint(v)
  90. default:
  91. return fmt.Sprint(v)
  92. }
  93. }
  94. func (query *Query) buildConditions(operator string, filter bool, conds ...*condition) (str string, params []interface{}) {
  95. var (
  96. sb strings.Builder
  97. )
  98. params = make([]interface{}, 0)
  99. for _, cond := range conds {
  100. if filter {
  101. if utils.IsEmpty(cond.Value) {
  102. continue
  103. }
  104. }
  105. if cond.Expr == "" {
  106. cond.Expr = "="
  107. }
  108. switch strings.ToUpper(cond.Expr) {
  109. case "=", "<>", ">", "<", ">=", "<=", "!=":
  110. if sb.Len() > 0 {
  111. sb.WriteString(" " + operator + " ")
  112. }
  113. if cond.Expr == "=" && cond.Value == nil {
  114. sb.WriteString("`" + cond.Field + "` IS NULL")
  115. } else {
  116. sb.WriteString("`" + cond.Field + "` " + cond.Expr + " ?")
  117. params = append(params, cond.Value)
  118. }
  119. case "LIKE":
  120. if sb.Len() > 0 {
  121. sb.WriteString(" " + operator + " ")
  122. }
  123. cond.Value = fmt.Sprintf("%%%s%%", cond.Value)
  124. sb.WriteString("`" + cond.Field + "` LIKE ?")
  125. params = append(params, cond.Value)
  126. case "IN":
  127. if sb.Len() > 0 {
  128. sb.WriteString(" " + operator + " ")
  129. }
  130. refVal := reflect.Indirect(reflect.ValueOf(cond.Value))
  131. switch refVal.Kind() {
  132. case reflect.Slice:
  133. ss := make([]string, refVal.Len())
  134. for i := 0; i < refVal.Len(); i++ {
  135. ss[i] = query.decodeValue(refVal.Index(i))
  136. }
  137. sb.WriteString("`" + cond.Field + "` IN (" + strings.Join(ss, ",") + ")")
  138. case reflect.String:
  139. sb.WriteString("`" + cond.Field + "` IN (" + refVal.String() + ")")
  140. }
  141. case "BETWEEN":
  142. refVal := reflect.ValueOf(cond.Value)
  143. if refVal.Kind() == reflect.Slice && refVal.Len() == 2 {
  144. sb.WriteString("`" + cond.Field + "` BETWEEN ? AND ?")
  145. params = append(params, refVal.Index(0), refVal.Index(1))
  146. }
  147. }
  148. }
  149. str = sb.String()
  150. return
  151. }
  152. func (query *Query) Select(fields ...string) *Query {
  153. if query.fields == nil {
  154. query.fields = fields
  155. } else {
  156. query.fields = append(query.fields, fields...)
  157. }
  158. return query
  159. }
  160. func (query *Query) From(table string) *Query {
  161. query.table = table
  162. return query
  163. }
  164. //// Joins specify Joins conditions
  165. //// db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu@example.org").Find(&user)
  166. //func (s *DB) Joins(query string, args ...interface{}) *DB {
  167. // return s.clone().search.Joins(query, args...).db
  168. //}
  169. func (query *Query) LeftJoin(table string, conds ...*condition) *Query {
  170. query.joins = append(query.joins, join{
  171. Table: table,
  172. Direction: "LEFT",
  173. Conds: conds,
  174. })
  175. return query
  176. }
  177. func (query *Query) RightJoin(table string, conds ...*condition) *Query {
  178. query.joins = append(query.joins, join{
  179. Table: table,
  180. Direction: "RIGHT",
  181. Conds: conds,
  182. })
  183. return query
  184. }
  185. func (query *Query) InnerJoin(table string, conds ...*condition) *Query {
  186. query.joins = append(query.joins, join{
  187. Table: table,
  188. Direction: "INNER",
  189. Conds: conds,
  190. })
  191. return query
  192. }
  193. func (query *Query) AndFilterWhere(conds ...*condition) *Query {
  194. length := len(conds)
  195. if length == 0 {
  196. return query
  197. }
  198. cs, ps := query.buildConditions("AND", true, conds...)
  199. if cs == "" {
  200. return query
  201. }
  202. query.params = append(query.params, ps...)
  203. if query.condition == "" {
  204. query.condition = cs
  205. } else {
  206. query.condition += " AND " + cs
  207. }
  208. return query
  209. }
  210. func (query *Query) AndWhere(conds ...*condition) *Query {
  211. length := len(conds)
  212. if length == 0 {
  213. return query
  214. }
  215. cs, ps := query.buildConditions("AND", false, conds...)
  216. if cs == "" {
  217. return query
  218. }
  219. query.params = append(query.params, ps...)
  220. if query.condition == "" {
  221. query.condition = cs
  222. } else {
  223. query.condition += " AND (" + cs + ")"
  224. }
  225. return query
  226. }
  227. func (query *Query) OrFilterWhere(conds ...*condition) *Query {
  228. length := len(conds)
  229. if length == 0 {
  230. return query
  231. }
  232. cs, ps := query.buildConditions("OR", true, conds...)
  233. if cs == "" {
  234. return query
  235. }
  236. query.params = append(query.params, ps...)
  237. if query.condition == "" {
  238. query.condition = cs
  239. } else {
  240. query.condition += " AND (" + cs + ")"
  241. }
  242. return query
  243. }
  244. func (query *Query) OrWhere(conds ...*condition) *Query {
  245. length := len(conds)
  246. if length == 0 {
  247. return query
  248. }
  249. cs, ps := query.buildConditions("OR", false, conds...)
  250. if cs == "" {
  251. return query
  252. }
  253. query.params = append(query.params, ps...)
  254. if query.condition == "" {
  255. query.condition = cs
  256. } else {
  257. query.condition += " AND (" + cs + ")"
  258. }
  259. return query
  260. }
  261. func (query *Query) GroupBy(cols ...string) *Query {
  262. query.groupBy = append(query.groupBy, cols...)
  263. return query
  264. }
  265. func (query *Query) OrderBy(col, direction string) *Query {
  266. direction = strings.ToUpper(direction)
  267. if direction == "" || !(direction == "ASC" || direction == "DESC") {
  268. direction = "ASC"
  269. }
  270. query.orderBy = append(query.orderBy, col+" "+direction)
  271. return query
  272. }
  273. func (query *Query) Offset(i int) *Query {
  274. query.offset = i
  275. return query
  276. }
  277. func (query *Query) Limit(i int) *Query {
  278. query.limit = i
  279. return query
  280. }
  281. func (query *Query) Count(v interface{}) (i int64) {
  282. var (
  283. db *gorm.DB
  284. err error
  285. )
  286. if db, err = query.compile(); err != nil {
  287. return
  288. } else {
  289. err = db.Model(v).Count(&i).Error
  290. }
  291. return
  292. }
  293. func (query *Query) One(v interface{}) (err error) {
  294. var (
  295. db *gorm.DB
  296. )
  297. if db, err = query.compile(); err != nil {
  298. return
  299. } else {
  300. err = db.First(v).Error
  301. }
  302. return
  303. }
  304. func (query *Query) All(v interface{}) (err error) {
  305. var (
  306. db *gorm.DB
  307. )
  308. if db, err = query.compile(); err != nil {
  309. return
  310. } else {
  311. err = db.Find(v).Error
  312. }
  313. return
  314. }
  315. func NewQuery(db *gorm.DB) *Query {
  316. return &Query{
  317. db: db,
  318. params: make([]interface{}, 0),
  319. orderBy: make([]string, 0),
  320. groupBy: make([]string, 0),
  321. joins: make([]join, 0),
  322. }
  323. }
  324. func NewCond(field string, value interface{}) *condition {
  325. return NewQueryCondition(field, value)
  326. }
  327. func NewQueryCondition(field string, value interface{}) *condition {
  328. return &condition{
  329. Field: field,
  330. Value: value,
  331. Expr: "=",
  332. }
  333. }
  334. func NewQueryConditionWithOperator(operator, field string, value interface{}) *condition {
  335. cond := &condition{
  336. Field: field,
  337. Value: value,
  338. Expr: operator,
  339. }
  340. return cond
  341. }