model.template 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // ==========================================================================
  2. // 生成日期:${.table.CreateTime}
  3. // 生成人:${.table.FunctionAuthor}
  4. // ==========================================================================
  5. package ${.table.BusinessName}
  6. import (
  7. "context"
  8. "database/sql"
  9. "github.com/gogf/gf/database/gdb"
  10. "github.com/gogf/gf/frame/g"
  11. "github.com/gogf/gf/frame/gmvc"
  12. "time"
  13. )
  14. // arModel is a active record design model for table ${.table.TableName} operations.
  15. type arModel struct {
  16. gmvc.M
  17. }
  18. var (
  19. // Table is the table name of ${.table.TableName}.
  20. Table = "${.table.TableName}"
  21. // Model is the model object of ${.table.TableName}.
  22. Model = &arModel{g.DB("default").Model(Table).Safe()}
  23. // Columns defines and stores column names for table ${.table.TableName}.
  24. Columns = struct {
  25. ${range $index, $column := .table.Columns}
  26. ${$column.GoField} string // ${$column.ColumnComment} ${end}
  27. }{
  28. ${range $index, $column := .table.Columns}
  29. ${$column.GoField}: "${$column.ColumnName}", ${end}
  30. }
  31. )
  32. // Ctx is a chaining function, which creates and returns a new DB that is a shallow copy
  33. // of current DB object and with given context in it.
  34. // Note that this returned DB object can be used only once, so do not assign it to
  35. // a global or package variable for long using.
  36. func (m *arModel) Ctx(ctx context.Context) *arModel {
  37. return &arModel{m.M.Ctx(ctx)}
  38. }
  39. // As sets an alias name for current table.
  40. func (m *arModel) As(as string) *arModel {
  41. return &arModel{m.M.As(as)}
  42. }
  43. // TX sets the transaction for current operation.
  44. func (m *arModel) TX(tx *gdb.TX) *arModel {
  45. return &arModel{m.M.TX(tx)}
  46. }
  47. // Master marks the following operation on master node.
  48. func (m *arModel) Master() *arModel {
  49. return &arModel{m.M.Master()}
  50. }
  51. // Slave marks the following operation on slave node.
  52. // Note that it makes sense only if there's any slave node configured.
  53. func (m *arModel) Slave() *arModel {
  54. return &arModel{m.M.Slave()}
  55. }
  56. // LeftJoin does "LEFT JOIN ... ON ..." statement on the model.
  57. // The parameter <table> can be joined table and its joined condition,
  58. // and also with its alias name, like:
  59. // Table("user").LeftJoin("user_detail", "user_detail.uid=user.uid")
  60. // Table("user", "u").LeftJoin("user_detail", "ud", "ud.uid=u.uid")
  61. func (m *arModel) LeftJoin(table ...string) *arModel {
  62. return &arModel{m.M.LeftJoin(table ...)}
  63. }
  64. // RightJoin does "RIGHT JOIN ... ON ..." statement on the model.
  65. // The parameter <table> can be joined table and its joined condition,
  66. // and also with its alias name, like:
  67. // Table("user").RightJoin("user_detail", "user_detail.uid=user.uid")
  68. // Table("user", "u").RightJoin("user_detail", "ud", "ud.uid=u.uid")
  69. func (m *arModel) RightJoin(table ...string) *arModel {
  70. return &arModel{m.M.RightJoin(table ...)}
  71. }
  72. // InnerJoin does "INNER JOIN ... ON ..." statement on the model.
  73. // The parameter <table> can be joined table and its joined condition,
  74. // and also with its alias name, like:
  75. // Table("user").InnerJoin("user_detail", "user_detail.uid=user.uid")
  76. // Table("user", "u").InnerJoin("user_detail", "ud", "ud.uid=u.uid")
  77. func (m *arModel) InnerJoin(table ...string) *arModel {
  78. return &arModel{m.M.InnerJoin(table ...)}
  79. }
  80. // Fields sets the operation fields of the model, multiple fields joined using char ','.
  81. func (m *arModel) Fields(fieldNamesOrMapStruct ...interface{}) *arModel {
  82. return &arModel{m.M.Fields(fieldNamesOrMapStruct...)}
  83. }
  84. // FieldsEx sets the excluded operation fields of the model, multiple fields joined using char ','.
  85. func (m *arModel) FieldsEx(fieldNamesOrMapStruct ...interface{}) *arModel {
  86. return &arModel{m.M.FieldsEx(fieldNamesOrMapStruct...)}
  87. }
  88. // Option sets the extra operation option for the model.
  89. func (m *arModel) Option(option int) *arModel {
  90. return &arModel{m.M.Option(option)}
  91. }
  92. // OmitEmpty sets OPTION_OMITEMPTY option for the model, which automatically filers
  93. // the data and where attributes for empty values.
  94. func (m *arModel) OmitEmpty() *arModel {
  95. return &arModel{m.M.OmitEmpty()}
  96. }
  97. // Filter marks filtering the fields which does not exist in the fields of the operated table.
  98. func (m *arModel) Filter() *arModel {
  99. return &arModel{m.M.Filter()}
  100. }
  101. // Where sets the condition statement for the model. The parameter <where> can be type of
  102. // string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times,
  103. // multiple conditions will be joined into where statement using "AND".
  104. // Eg:
  105. // Where("uid=10000")
  106. // Where("uid", 10000)
  107. // Where("money>? AND name like ?", 99999, "vip_%")
  108. // Where("uid", 1).Where("name", "john")
  109. // Where("status IN (?)", g.Slice{1,2,3})
  110. // Where("age IN(?,?)", 18, 50)
  111. // Where(User{ Id : 1, UserName : "john"})
  112. func (m *arModel) Where(where interface{}, args ...interface{}) *arModel {
  113. return &arModel{m.M.Where(where, args...)}
  114. }
  115. // WherePri does the same logic as Model.Where except that if the parameter <where>
  116. // is a single condition like int/string/float/slice, it treats the condition as the primary
  117. // key value. That is, if primary key is "id" and given <where> parameter as "123", the
  118. // WherePri function treats the condition as "id=123", but Model.Where treats the condition
  119. // as string "123".
  120. func (m *arModel) WherePri(where interface{}, args ...interface{}) *arModel {
  121. return &arModel{m.M.WherePri(where, args...)}
  122. }
  123. // And adds "AND" condition to the where statement.
  124. func (m *arModel) And(where interface{}, args ...interface{}) *arModel {
  125. return &arModel{m.M.And(where, args...)}
  126. }
  127. // Or adds "OR" condition to the where statement.
  128. func (m *arModel) Or(where interface{}, args ...interface{}) *arModel {
  129. return &arModel{m.M.Or(where, args...)}
  130. }
  131. // Group sets the "GROUP BY" statement for the model.
  132. func (m *arModel) Group(groupBy string) *arModel {
  133. return &arModel{m.M.Group(groupBy)}
  134. }
  135. // Order sets the "ORDER BY" statement for the model.
  136. func (m *arModel) Order(orderBy ...string) *arModel {
  137. return &arModel{m.M.Order(orderBy...)}
  138. }
  139. // Limit sets the "LIMIT" statement for the model.
  140. // The parameter <limit> can be either one or two number, if passed two number is passed,
  141. // it then sets "LIMIT limit[0],limit[1]" statement for the model, or else it sets "LIMIT limit[0]"
  142. // statement.
  143. func (m *arModel) Limit(limit ...int) *arModel {
  144. return &arModel{m.M.Limit(limit...)}
  145. }
  146. // Offset sets the "OFFSET" statement for the model.
  147. // It only makes sense for some databases like SQLServer, PostgreSQL, etc.
  148. func (m *arModel) Offset(offset int) *arModel {
  149. return &arModel{m.M.Offset(offset)}
  150. }
  151. // Page sets the paging number for the model.
  152. // The parameter <page> is started from 1 for paging.
  153. // Note that, it differs that the Limit function start from 0 for "LIMIT" statement.
  154. func (m *arModel) Page(page, limit int) *arModel {
  155. return &arModel{m.M.Page(page, limit)}
  156. }
  157. // Batch sets the batch operation number for the model.
  158. func (m *arModel) Batch(batch int) *arModel {
  159. return &arModel{m.M.Batch(batch)}
  160. }
  161. // Cache sets the cache feature for the model. It caches the result of the sql, which means
  162. // if there's another same sql request, it just reads and returns the result from cache, it
  163. // but not committed and executed into the database.
  164. //
  165. // If the parameter <duration> < 0, which means it clear the cache with given <name>.
  166. // If the parameter <duration> = 0, which means it never expires.
  167. // If the parameter <duration> > 0, which means it expires after <duration>.
  168. //
  169. // The optional parameter <name> is used to bind a name to the cache, which means you can later
  170. // control the cache like changing the <duration> or clearing the cache with specified <name>.
  171. //
  172. // Note that, the cache feature is disabled if the model is operating on a transaction.
  173. func (m *arModel) Cache(duration time.Duration, name ...string) *arModel {
  174. return &arModel{m.M.Cache(duration, name...)}
  175. }
  176. // Data sets the operation data for the model.
  177. // The parameter <data> can be type of string/map/gmap/slice/struct/*struct, etc.
  178. // Eg:
  179. // Data("uid=10000")
  180. // Data("uid", 10000)
  181. // Data(g.Map{"uid": 10000, "name":"john"})
  182. // Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
  183. func (m *arModel) Data(data ...interface{}) *arModel {
  184. return &arModel{m.M.Data(data...)}
  185. }
  186. // All does "SELECT FROM ..." statement for the model.
  187. // It retrieves the records from table and returns the result as []*Entity.
  188. // It returns nil if there's no record retrieved with the given conditions from table.
  189. //
  190. // The optional parameter <where> is the same as the parameter of Model.Where function,
  191. // see Model.Where.
  192. func (m *arModel) All(where ...interface{}) ([]*Entity, error) {
  193. all, err := m.M.All(where...)
  194. if err != nil {
  195. return nil, err
  196. }
  197. var entities []*Entity
  198. if err = all.Structs(&entities); err != nil && err != sql.ErrNoRows {
  199. return nil, err
  200. }
  201. return entities, nil
  202. }
  203. // One retrieves one record from table and returns the result as *Entity.
  204. // It returns nil if there's no record retrieved with the given conditions from table.
  205. //
  206. // The optional parameter <where> is the same as the parameter of Model.Where function,
  207. // see Model.Where.
  208. func (m *arModel) One(where ...interface{}) (*Entity, error) {
  209. one, err := m.M.One(where...)
  210. if err != nil {
  211. return nil, err
  212. }
  213. var entity *Entity
  214. if err = one.Struct(&entity); err != nil && err != sql.ErrNoRows {
  215. return nil, err
  216. }
  217. return entity, nil
  218. }
  219. // FindOne retrieves and returns a single Record by Model.WherePri and Model.One.
  220. // Also see Model.WherePri and Model.One.
  221. func (m *arModel) FindOne(where ...interface{}) (*Entity, error) {
  222. one, err := m.M.FindOne(where...)
  223. if err != nil {
  224. return nil, err
  225. }
  226. var entity *Entity
  227. if err = one.Struct(&entity); err != nil && err != sql.ErrNoRows {
  228. return nil, err
  229. }
  230. return entity, nil
  231. }
  232. // FindAll retrieves and returns Result by by Model.WherePri and Model.All.
  233. // Also see Model.WherePri and Model.All.
  234. func (m *arModel) FindAll(where ...interface{}) ([]*Entity, error) {
  235. all, err := m.M.FindAll(where...)
  236. if err != nil {
  237. return nil, err
  238. }
  239. var entities []*Entity
  240. if err = all.Structs(&entities); err != nil && err != sql.ErrNoRows {
  241. return nil, err
  242. }
  243. return entities, nil
  244. }
  245. // Chunk iterates the table with given size and callback function.
  246. func (m *arModel) Chunk(limit int, callback func(entities []*Entity, err error) bool) {
  247. m.M.Chunk(limit, func(result gdb.Result, err error) bool {
  248. var entities []*Entity
  249. err = result.Structs(&entities)
  250. if err == sql.ErrNoRows {
  251. return false
  252. }
  253. return callback(entities, err)
  254. })
  255. }
  256. // LockUpdate sets the lock for update for current operation.
  257. func (m *arModel) LockUpdate() *arModel {
  258. return &arModel{m.M.LockUpdate()}
  259. }
  260. // LockShared sets the lock in share mode for current operation.
  261. func (m *arModel) LockShared() *arModel {
  262. return &arModel{m.M.LockShared()}
  263. }
  264. // Unscoped enables/disables the soft deleting feature.
  265. func (m *arModel) Unscoped() *arModel {
  266. return &arModel{m.M.Unscoped()}
  267. }