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