gdb_model_condition.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gdb
  7. import (
  8. "strings"
  9. )
  10. // Where sets the condition statement for the model. The parameter <where> can be type of
  11. // string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times,
  12. // multiple conditions will be joined into where statement using "AND".
  13. // Eg:
  14. // Where("uid=10000")
  15. // Where("uid", 10000)
  16. // Where("money>? AND name like ?", 99999, "vip_%")
  17. // Where("uid", 1).Where("name", "john")
  18. // Where("status IN (?)", g.Slice{1,2,3})
  19. // Where("age IN(?,?)", 18, 50)
  20. // Where(User{ Id : 1, UserName : "john"})
  21. func (m *Model) Where(where interface{}, args ...interface{}) *Model {
  22. model := m.getModel()
  23. if model.whereHolder == nil {
  24. model.whereHolder = make([]*whereHolder, 0)
  25. }
  26. model.whereHolder = append(model.whereHolder, &whereHolder{
  27. operator: whereHolderWhere,
  28. where: where,
  29. args: args,
  30. })
  31. return model
  32. }
  33. // Having sets the having statement for the model.
  34. // The parameters of this function usage are as the same as function Where.
  35. // See Where.
  36. func (m *Model) Having(having interface{}, args ...interface{}) *Model {
  37. model := m.getModel()
  38. model.having = []interface{}{
  39. having, args,
  40. }
  41. return model
  42. }
  43. // WherePri does the same logic as Model.Where except that if the parameter <where>
  44. // is a single condition like int/string/float/slice, it treats the condition as the primary
  45. // key value. That is, if primary key is "id" and given <where> parameter as "123", the
  46. // WherePri function treats the condition as "id=123", but Model.Where treats the condition
  47. // as string "123".
  48. func (m *Model) WherePri(where interface{}, args ...interface{}) *Model {
  49. if len(args) > 0 {
  50. return m.Where(where, args...)
  51. }
  52. newWhere := GetPrimaryKeyCondition(m.getPrimaryKey(), where)
  53. return m.Where(newWhere[0], newWhere[1:]...)
  54. }
  55. // And adds "AND" condition to the where statement.
  56. func (m *Model) And(where interface{}, args ...interface{}) *Model {
  57. model := m.getModel()
  58. if model.whereHolder == nil {
  59. model.whereHolder = make([]*whereHolder, 0)
  60. }
  61. model.whereHolder = append(model.whereHolder, &whereHolder{
  62. operator: whereHolderAnd,
  63. where: where,
  64. args: args,
  65. })
  66. return model
  67. }
  68. // Or adds "OR" condition to the where statement.
  69. func (m *Model) Or(where interface{}, args ...interface{}) *Model {
  70. model := m.getModel()
  71. if model.whereHolder == nil {
  72. model.whereHolder = make([]*whereHolder, 0)
  73. }
  74. model.whereHolder = append(model.whereHolder, &whereHolder{
  75. operator: whereHolderOr,
  76. where: where,
  77. args: args,
  78. })
  79. return model
  80. }
  81. // Group sets the "GROUP BY" statement for the model.
  82. func (m *Model) Group(groupBy string) *Model {
  83. model := m.getModel()
  84. model.groupBy = m.db.QuoteString(groupBy)
  85. return model
  86. }
  87. // GroupBy is alias of Model.Group.
  88. // See Model.Group.
  89. // Deprecated.
  90. func (m *Model) GroupBy(groupBy string) *Model {
  91. return m.Group(groupBy)
  92. }
  93. // Order sets the "ORDER BY" statement for the model.
  94. func (m *Model) Order(orderBy ...string) *Model {
  95. model := m.getModel()
  96. model.orderBy = m.db.QuoteString(strings.Join(orderBy, " "))
  97. return model
  98. }
  99. // OrderBy is alias of Model.Order.
  100. // See Model.Order.
  101. // Deprecated.
  102. func (m *Model) OrderBy(orderBy string) *Model {
  103. return m.Order(orderBy)
  104. }
  105. // Limit sets the "LIMIT" statement for the model.
  106. // The parameter <limit> can be either one or two number, if passed two number is passed,
  107. // it then sets "LIMIT limit[0],limit[1]" statement for the model, or else it sets "LIMIT limit[0]"
  108. // statement.
  109. func (m *Model) Limit(limit ...int) *Model {
  110. model := m.getModel()
  111. switch len(limit) {
  112. case 1:
  113. model.limit = limit[0]
  114. case 2:
  115. model.start = limit[0]
  116. model.limit = limit[1]
  117. }
  118. return model
  119. }
  120. // Offset sets the "OFFSET" statement for the model.
  121. // It only makes sense for some databases like SQLServer, PostgreSQL, etc.
  122. func (m *Model) Offset(offset int) *Model {
  123. model := m.getModel()
  124. model.offset = offset
  125. return model
  126. }
  127. // Page sets the paging number for the model.
  128. // The parameter <page> is started from 1 for paging.
  129. // Note that, it differs that the Limit function starts from 0 for "LIMIT" statement.
  130. func (m *Model) Page(page, limit int) *Model {
  131. model := m.getModel()
  132. if page <= 0 {
  133. page = 1
  134. }
  135. model.start = (page - 1) * limit
  136. model.limit = limit
  137. return model
  138. }
  139. // ForPage is alias of Model.Page.
  140. // See Model.Page.
  141. // Deprecated.
  142. func (m *Model) ForPage(page, limit int) *Model {
  143. return m.Page(page, limit)
  144. }