dao_internal.template 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // ==========================================================================
  2. // GFast自动生成dao internal操作代码,无需手动修改,重新生成会自动覆盖.
  3. // 生成日期:{{.table.CreateTime}}
  4. // 生成路径: {{.table.PackageName}}/dao/internal/{{.table.TableName}}.go
  5. // 生成人:{{.table.FunctionAuthor}}
  6. // ==========================================================================
  7. ////
  8. package internal
  9. import (
  10. "context"
  11. "github.com/gogf/gf/database/gdb"
  12. "github.com/gogf/gf/frame/g"
  13. )
  14. // {{.table.ClassName}}Dao is the manager for logic model data accessing and custom defined data operations functions management.
  15. type {{.table.ClassName}}Dao struct {
  16. Table string // Table is the underlying table name of the DAO.
  17. Group string // Group is the database configuration group name of current DAO.
  18. Columns {{.table.ClassName}}Columns // Columns is the short type for Columns, which contains all the column names of Table for convenient usage.
  19. }
  20. // {{.table.ClassName}}Columns defines and stores column names for table {{.table.TableName}}.
  21. type {{.table.ClassName}}Columns struct {
  22. {{range $index, $column := .table.Columns}}
  23. {{$column.GoField}} string // {{$column.ColumnComment}}
  24. {{end}}
  25. }
  26. var {{.table.BusinessName | CaseCamelLower}}Columns = {{.table.ClassName}}Columns{
  27. {{range $index, $column := .table.Columns}}
  28. {{$column.GoField}}: "{{$column.ColumnName}}",
  29. {{end}}
  30. }
  31. // New{{.table.ClassName}}Dao creates and returns a new DAO object for table data access.
  32. func New{{.table.ClassName}}Dao() *{{.table.ClassName}}Dao {
  33. return &{{.table.ClassName}}Dao{
  34. Group: "default",
  35. Table: "{{.table.TableName}}",
  36. Columns:{{.table.BusinessName | CaseCamelLower}}Columns,
  37. }
  38. }
  39. // DB retrieves and returns the underlying raw database management object of current DAO.
  40. func (dao *{{.table.ClassName}}Dao) DB() gdb.DB {
  41. return g.DB(dao.Group)
  42. }
  43. // Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
  44. func (dao *{{.table.ClassName}}Dao) Ctx(ctx context.Context) *gdb.Model {
  45. return dao.DB().Model(dao.Table).Safe().Ctx(ctx)
  46. }
  47. // Transaction wraps the transaction logic using function f.
  48. // It rollbacks the transaction and returns the error from function f if it returns non-nil error.
  49. // It commits the transaction and returns nil if function f returns nil.
  50. //
  51. // Note that, you should not Commit or Rollback the transaction in function f
  52. // as it is automatically handled by this function.
  53. func (dao *{{.table.ClassName}}Dao) Transaction(ctx context.Context, f func(ctx context.Context, tx *gdb.TX) error) (err error) {
  54. return dao.Ctx(ctx).Transaction(ctx, f)
  55. }