consts_gen_dao_template_dao.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright GoFrame gf Author(https://goframe.org). 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 consts
  7. const TemplateGenDaoIndexContent = `
  8. // =================================================================================
  9. // This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
  10. // =================================================================================
  11. package dao
  12. import (
  13. "{TplImportPrefix}/internal"
  14. )
  15. // internal{TplTableNameCamelCase}Dao is internal type for wrapping internal DAO implements.
  16. type internal{TplTableNameCamelCase}Dao = *internal.{TplTableNameCamelCase}Dao
  17. // {TplTableNameCamelCase}Dao is the data access object for table {TplTableName}.
  18. // You can define custom methods on it to extend its functionality as you wish.
  19. type {TplTableNameCamelCase}Dao struct {
  20. internal{TplTableNameCamelCase}Dao
  21. }
  22. var (
  23. // {TplTableNameCamelCase} is globally public accessible object for table {TplTableName} operations.
  24. {TplTableNameCamelCase} = {TplTableNameCamelCase}Dao{
  25. internal.New{TplTableNameCamelCase}Dao("default"),
  26. }
  27. )
  28. func New{TplTableNameCamelCase}Dao(tenant string) *{TplTableNameCamelCase}Dao {
  29. return &{TplTableNameCamelCase}Dao{
  30. internal.New{TplTableNameCamelCase}Dao(tenant),
  31. }
  32. }
  33. // Fill with you ideas below.
  34. `
  35. const TemplateGenDaoInternalContent = `
  36. // ==========================================================================
  37. // Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. {TplCreatedAtDatetimeStr}
  38. // ==========================================================================
  39. package internal
  40. import (
  41. "context"
  42. "github.com/gogf/gf/v2/database/gdb"
  43. "github.com/gogf/gf/v2/frame/g"
  44. )
  45. // {TplTableNameCamelCase}Dao is the data access object for table {TplTableName}.
  46. type {TplTableNameCamelCase}Dao struct {
  47. table string // table is the underlying table name of the DAO.
  48. group string // group is the database configuration group name of current DAO.
  49. C {TplTableNameCamelCase}Columns // columns contains all the column names of Table for convenient usage.
  50. }
  51. // {TplTableNameCamelCase}Columns defines and stores column names for table {TplTableName}.
  52. type {TplTableNameCamelCase}Columns struct {
  53. {TplColumnDefine}
  54. }
  55. // {TplTableNameCamelLowerCase}Columns holds the columns for table {TplTableName}.
  56. var {TplTableNameCamelLowerCase}Columns = {TplTableNameCamelCase}Columns{
  57. {TplColumnNames}
  58. }
  59. // New{TplTableNameCamelCase}Dao creates and returns a new DAO object for table data access.
  60. func New{TplTableNameCamelCase}Dao(tenant string) *{TplTableNameCamelCase}Dao {
  61. return &{TplTableNameCamelCase}Dao{
  62. group: tenant,
  63. table: "{TplTableName}",
  64. C: {TplTableNameCamelLowerCase}Columns,
  65. }
  66. }
  67. // DB retrieves and returns the underlying raw database management object of current DAO.
  68. func (dao *{TplTableNameCamelCase}Dao) DB() gdb.DB {
  69. return g.DB(dao.group)
  70. }
  71. // Table returns the table name of current dao.
  72. func (dao *{TplTableNameCamelCase}Dao) Table() string {
  73. return dao.table
  74. }
  75. // Columns returns all column names of current dao.
  76. func (dao *{TplTableNameCamelCase}Dao) Columns() {TplTableNameCamelCase}Columns {
  77. return dao.C
  78. }
  79. // Group returns the configuration group name of database of current dao.
  80. func (dao *{TplTableNameCamelCase}Dao) Group() string {
  81. return dao.group
  82. }
  83. // Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
  84. func (dao *{TplTableNameCamelCase}Dao) Ctx(ctx context.Context) *gdb.Model {
  85. return dao.DB().Model(dao.table).Safe().Ctx(ctx)
  86. }
  87. // Transaction wraps the transaction logic using function f.
  88. // It rollbacks the transaction and returns the error from function f if it returns non-nil error.
  89. // It commits the transaction and returns nil if function f returns nil.
  90. //
  91. // Note that, you should not Commit or Rollback the transaction in function f
  92. // as it is automatically handled by this function.
  93. func (dao *{TplTableNameCamelCase}Dao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
  94. return dao.Ctx(ctx).Transaction(ctx, f)
  95. }
  96. `