user_model.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // ==========================================================================
  2. // This is auto-generated by gf cli tool. You may not really want to edit it.
  3. // ==========================================================================
  4. package user
  5. import (
  6. "database/sql"
  7. "github.com/gogf/gf/database/gdb"
  8. "github.com/gogf/gf/frame/g"
  9. "time"
  10. )
  11. // arModel is a active record design model for table user operations.
  12. type arModel struct {
  13. Model *gdb.Model
  14. }
  15. var (
  16. // Table is the table name of user.
  17. Table = "user"
  18. // Model is the model object of user.
  19. Model = &arModel{g.DB("default").Table(Table).Safe()}
  20. )
  21. // TX sets the transaction for current operation.
  22. func (m *arModel) TX(tx *gdb.TX) *arModel {
  23. return &arModel{m.Model.TX(tx)}
  24. }
  25. // Master marks the following operation on master node.
  26. func (m *arModel) Master() *arModel {
  27. return &arModel{m.Model.Master()}
  28. }
  29. // Slave marks the following operation on slave node.
  30. // Note that it makes sense only if there's any slave node configured.
  31. func (m *arModel) Slave() *arModel {
  32. return &arModel{m.Model.Slave()}
  33. }
  34. // LeftJoin does "LEFT JOIN ... ON ..." statement on the model.
  35. func (m *arModel) LeftJoin(joinTable string, on string) *arModel {
  36. return &arModel{m.Model.LeftJoin(joinTable, on)}
  37. }
  38. // RightJoin does "RIGHT JOIN ... ON ..." statement on the model.
  39. func (m *arModel) RightJoin(joinTable string, on string) *arModel {
  40. return &arModel{m.Model.RightJoin(joinTable, on)}
  41. }
  42. // InnerJoin does "INNER JOIN ... ON ..." statement on the model.
  43. func (m *arModel) InnerJoin(joinTable string, on string) *arModel {
  44. return &arModel{m.Model.InnerJoin(joinTable, on)}
  45. }
  46. // Fields sets the operation fields of the model, multiple fields joined using char ','.
  47. func (m *arModel) Fields(fields string) *arModel {
  48. return &arModel{m.Model.Fields(fields)}
  49. }
  50. // FieldsEx sets the excluded operation fields of the model, multiple fields joined using char ','.
  51. func (m *arModel) FieldsEx(fields string) *arModel {
  52. return &arModel{m.Model.FieldsEx(fields)}
  53. }
  54. // Option sets the extra operation option for the model.
  55. func (m *arModel) Option(option int) *arModel {
  56. return &arModel{m.Model.Option(option)}
  57. }
  58. // OmitEmpty sets OPTION_OMITEMPTY option for the model, which automatically filers
  59. // the data and where attributes for empty values.
  60. func (m *arModel) OmitEmpty() *arModel {
  61. return &arModel{m.Model.OmitEmpty()}
  62. }
  63. // Filter marks filtering the fields which does not exist in the fields of the operated table.
  64. func (m *arModel) Filter() *arModel {
  65. return &arModel{m.Model.Filter()}
  66. }
  67. // Where sets the condition statement for the model. The parameter <where> can be type of
  68. // string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times,
  69. // multiple conditions will be joined into where statement using "AND".
  70. // Eg:
  71. // Where("uid=10000")
  72. // Where("uid", 10000)
  73. // Where("money>? AND name like ?", 99999, "vip_%")
  74. // Where("uid", 1).Where("name", "john")
  75. // Where("status IN (?)", g.Slice{1,2,3})
  76. // Where("age IN(?,?)", 18, 50)
  77. // Where(User{ Id : 1, UserName : "john"})
  78. func (m *arModel) Where(where interface{}, args ...interface{}) *arModel {
  79. return &arModel{m.Model.Where(where, args...)}
  80. }
  81. // And adds "AND" condition to the where statement.
  82. func (m *arModel) And(where interface{}, args ...interface{}) *arModel {
  83. return &arModel{m.Model.And(where, args...)}
  84. }
  85. // Or adds "OR" condition to the where statement.
  86. func (m *arModel) Or(where interface{}, args ...interface{}) *arModel {
  87. return &arModel{m.Model.Or(where, args...)}
  88. }
  89. // GroupBy sets the "GROUP BY" statement for the model.
  90. func (m *arModel) GroupBy(groupBy string) *arModel {
  91. return &arModel{m.Model.GroupBy(groupBy)}
  92. }
  93. // OrderBy sets the "ORDER BY" statement for the model.
  94. func (m *arModel) OrderBy(orderBy string) *arModel {
  95. return &arModel{m.Model.OrderBy(orderBy)}
  96. }
  97. // Limit sets the "LIMIT" statement for the model.
  98. // The parameter <limit> can be either one or two number, if passed two number is passed,
  99. // it then sets "LIMIT limit[0],limit[1]" statement for the model, or else it sets "LIMIT limit[0]"
  100. // statement.
  101. func (m *arModel) Limit(limit ...int) *arModel {
  102. return &arModel{m.Model.Limit(limit...)}
  103. }
  104. // Offset sets the "OFFSET" statement for the model.
  105. // It only makes sense for some databases like SQLServer, PostgreSQL, etc.
  106. func (m *arModel) Offset(offset int) *arModel {
  107. return &arModel{m.Model.Offset(offset)}
  108. }
  109. // ForPage sets the paging number for the model.
  110. // The parameter <page> is started from 1 for paging.
  111. // Note that, it differs that the Limit function start from 0 for "LIMIT" statement.
  112. func (m *arModel) ForPage(page, limit int) *arModel {
  113. return &arModel{m.Model.Page(page, limit)}
  114. }
  115. // Batch sets the batch operation number for the model.
  116. func (m *arModel) Batch(batch int) *arModel {
  117. return &arModel{m.Model.Batch(batch)}
  118. }
  119. // Cache sets the cache feature for the model. It caches the result of the sql, which means
  120. // if there's another same sql request, it just reads and returns the result from cache, it
  121. // but not committed and executed into the database.
  122. //
  123. // If the parameter <duration> < 0, which means it clear the cache with given <name>.
  124. // If the parameter <duration> = 0, which means it never expires.
  125. // If the parameter <duration> > 0, which means it expires after <duration>.
  126. //
  127. // The optional parameter <name> is used to bind a name to the cache, which means you can later
  128. // control the cache like changing the <duration> or clearing the cache with specified <name>.
  129. //
  130. // Note that, the cache feature is disabled if the model is operating on a transaction.
  131. func (m *arModel) Cache(expire time.Duration, name ...string) *arModel {
  132. return &arModel{m.Model.Cache(expire, name...)}
  133. }
  134. // Data sets the operation data for the model.
  135. // The parameter <data> can be type of string/map/gmap/slice/struct/*struct, etc.
  136. // Eg:
  137. // Data("uid=10000")
  138. // Data("uid", 10000)
  139. // Data(g.Map{"uid": 10000, "name":"john"})
  140. // Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
  141. func (m *arModel) Data(data ...interface{}) *arModel {
  142. return &arModel{m.Model.Data(data...)}
  143. }
  144. // Insert does "INSERT INTO ..." statement for the model.
  145. func (m *arModel) Insert() (result sql.Result, err error) {
  146. return m.Model.Insert()
  147. }
  148. // Replace does "REPLACE INTO ..." statement for the model.
  149. func (m *arModel) Replace() (result sql.Result, err error) {
  150. return m.Model.Replace()
  151. }
  152. // Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the model.
  153. // It updates the record if there's primary or unique index in the saving data,
  154. // or else it inserts a new record into the table.
  155. func (m *arModel) Save() (result sql.Result, err error) {
  156. return m.Model.Save()
  157. }
  158. // Update does "UPDATE ... " statement for the model.
  159. func (m *arModel) Update() (result sql.Result, err error) {
  160. return m.Model.Update()
  161. }
  162. // Delete does "DELETE FROM ... " statement for the model.
  163. func (m *arModel) Delete() (result sql.Result, err error) {
  164. return m.Model.Delete()
  165. }
  166. // Count does "SELECT COUNT(x) FROM ..." statement for the model.
  167. func (m *arModel) Count() (int, error) {
  168. return m.Model.Count()
  169. }
  170. // All does "SELECT FROM ..." statement for the model.
  171. // It retrieves the records from table and returns the result as []*Entity.
  172. // It returns nil if there's no record retrieved with the given conditions from table.
  173. func (m *arModel) All() ([]*Entity, error) {
  174. all, err := m.Model.All()
  175. if err != nil {
  176. return nil, err
  177. }
  178. var entities []*Entity
  179. if err = all.Structs(&entities); err != nil && err != sql.ErrNoRows {
  180. return nil, err
  181. }
  182. return entities, nil
  183. }
  184. // One retrieves one record from table and returns the result as *Entity.
  185. // It returns nil if there's no record retrieved with the given conditions from table.
  186. func (m *arModel) One() (*Entity, error) {
  187. one, err := m.Model.One()
  188. if err != nil {
  189. return nil, err
  190. }
  191. var entity *Entity
  192. if err = one.Struct(&entity); err != nil && err != sql.ErrNoRows {
  193. return nil, err
  194. }
  195. return entity, nil
  196. }
  197. // Value retrieves a specified record value from table and returns the result as interface type.
  198. // It returns nil if there's no record found with the given conditions from table.
  199. func (m *arModel) Value() (gdb.Value, error) {
  200. return m.Model.Value()
  201. }
  202. // Chunk iterates the table with given size and callback function.
  203. func (m *arModel) Chunk(limit int, callback func(entities []*Entity, err error) bool) {
  204. m.Model.Chunk(limit, func(result gdb.Result, err error) bool {
  205. var entities []*Entity
  206. err = result.Structs(&entities)
  207. if err == sql.ErrNoRows {
  208. return false
  209. }
  210. return callback(entities, err)
  211. })
  212. }