search.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package gorm
  2. import (
  3. "fmt"
  4. )
  5. type search struct {
  6. db *DB
  7. whereConditions []map[string]interface{}
  8. orConditions []map[string]interface{}
  9. notConditions []map[string]interface{}
  10. havingConditions []map[string]interface{}
  11. joinConditions []map[string]interface{}
  12. initAttrs []interface{}
  13. assignAttrs []interface{}
  14. selects map[string]interface{}
  15. omits []string
  16. orders []interface{}
  17. preload []searchPreload
  18. offset interface{}
  19. limit interface{}
  20. group string
  21. tableName string
  22. raw bool
  23. Unscoped bool
  24. ignoreOrderQuery bool
  25. }
  26. type searchPreload struct {
  27. schema string
  28. conditions []interface{}
  29. }
  30. func (s *search) clone() *search {
  31. clone := *s
  32. return &clone
  33. }
  34. func (s *search) Where(query interface{}, values ...interface{}) *search {
  35. s.whereConditions = append(s.whereConditions, map[string]interface{}{"query": query, "args": values})
  36. return s
  37. }
  38. func (s *search) Not(query interface{}, values ...interface{}) *search {
  39. s.notConditions = append(s.notConditions, map[string]interface{}{"query": query, "args": values})
  40. return s
  41. }
  42. func (s *search) Or(query interface{}, values ...interface{}) *search {
  43. s.orConditions = append(s.orConditions, map[string]interface{}{"query": query, "args": values})
  44. return s
  45. }
  46. func (s *search) Attrs(attrs ...interface{}) *search {
  47. s.initAttrs = append(s.initAttrs, toSearchableMap(attrs...))
  48. return s
  49. }
  50. func (s *search) Assign(attrs ...interface{}) *search {
  51. s.assignAttrs = append(s.assignAttrs, toSearchableMap(attrs...))
  52. return s
  53. }
  54. func (s *search) Order(value interface{}, reorder ...bool) *search {
  55. if len(reorder) > 0 && reorder[0] {
  56. s.orders = []interface{}{}
  57. }
  58. if value != nil && value != "" {
  59. s.orders = append(s.orders, value)
  60. }
  61. return s
  62. }
  63. func (s *search) Select(query interface{}, args ...interface{}) *search {
  64. s.selects = map[string]interface{}{"query": query, "args": args}
  65. return s
  66. }
  67. func (s *search) Omit(columns ...string) *search {
  68. s.omits = columns
  69. return s
  70. }
  71. func (s *search) Limit(limit interface{}) *search {
  72. s.limit = limit
  73. return s
  74. }
  75. func (s *search) Offset(offset interface{}) *search {
  76. s.offset = offset
  77. return s
  78. }
  79. func (s *search) Group(query string) *search {
  80. s.group = s.getInterfaceAsSQL(query)
  81. return s
  82. }
  83. func (s *search) Having(query interface{}, values ...interface{}) *search {
  84. if val, ok := query.(*expr); ok {
  85. s.havingConditions = append(s.havingConditions, map[string]interface{}{"query": val.expr, "args": val.args})
  86. } else {
  87. s.havingConditions = append(s.havingConditions, map[string]interface{}{"query": query, "args": values})
  88. }
  89. return s
  90. }
  91. func (s *search) Joins(query string, values ...interface{}) *search {
  92. s.joinConditions = append(s.joinConditions, map[string]interface{}{"query": query, "args": values})
  93. return s
  94. }
  95. func (s *search) Preload(schema string, values ...interface{}) *search {
  96. var preloads []searchPreload
  97. for _, preload := range s.preload {
  98. if preload.schema != schema {
  99. preloads = append(preloads, preload)
  100. }
  101. }
  102. preloads = append(preloads, searchPreload{schema, values})
  103. s.preload = preloads
  104. return s
  105. }
  106. func (s *search) Raw(b bool) *search {
  107. s.raw = b
  108. return s
  109. }
  110. func (s *search) unscoped() *search {
  111. s.Unscoped = true
  112. return s
  113. }
  114. func (s *search) Table(name string) *search {
  115. s.tableName = name
  116. return s
  117. }
  118. func (s *search) getInterfaceAsSQL(value interface{}) (str string) {
  119. switch value.(type) {
  120. case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
  121. str = fmt.Sprintf("%v", value)
  122. default:
  123. s.db.AddError(ErrInvalidSQL)
  124. }
  125. if str == "-1" {
  126. return ""
  127. }
  128. return
  129. }