Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
hailaz committed Jan 25, 2025
1 parent 9f0de3d commit 3a2a73c
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 132 deletions.
8 changes: 8 additions & 0 deletions cmd/gf/internal/cmd/gen/tpl/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
## 功能概述
基于数据库表结构,通过自定义模板生成Go代码的工具。

## 功能设计

生成流程:
1. 读取数据库表结构
2. 解析出表结构信息,包括表名、表注释、字段列表
3. 根据规则裁切表数据,生成模板数据
4. 根据模板生成代码

## 命令参数设计

```shell
Expand Down
18 changes: 9 additions & 9 deletions cmd/gf/internal/cmd/gen/tpl/testdata/dao/dao.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
package dao

import (
"{{.table.PackageName}}/internal/cmd/gen/tpl/output/dao/internal"
"{{.table.PackageName}}/internal"
)

// internal{{.table.CaseCamel}}Dao is internal type for wrapping internal DAO implements.
type internal{{.table.CaseCamel}}Dao = *internal.{{.table.CaseCamel}}Dao
// internal{{.table.NameCaseCamel}}Dao is internal type for wrapping internal DAO implements.
type internal{{.table.NameCaseCamel}}Dao = *internal.{{.table.NameCaseCamel}}Dao

// {{.table.CaseCamelLower}}Dao is the data access object for table {{.table.Name}}.
// {{.table.NameCaseCamelLower}}Dao is the data access object for table {{.table.Name}}.
// You can define custom methods on it to extend its functionality as you wish.
type {{.table.CaseCamelLower}}Dao struct {
internal{{.table.CaseCamel}}Dao
type {{.table.NameCaseCamelLower}}Dao struct {
internal{{.table.NameCaseCamel}}Dao
}

var (
// {{.table.CaseCamel}} is globally public accessible object for table {{.table.Name}} operations.
{{.table.CaseCamel}} = {{.table.CaseCamelLower}}Dao{
internal.New{{.table.CaseCamel}}Dao(),
// {{.table.NameCaseCamel}} is globally public accessible object for table {{.table.Name}} operations.
{{.table.NameCaseCamel}} = {{.table.NameCaseCamelLower}}Dao{
internal.New{{.table.NameCaseCamel}}Dao(),
}
)

Expand Down
38 changes: 19 additions & 19 deletions cmd/gf/internal/cmd/gen/tpl/testdata/dao/internal/dao_internal.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,54 @@ import (
"github.com/gogf/gf/v2/frame/g"
)

// {{.table.CaseCamel}}Dao is the data access object for table {{.table.Name}}.
type {{.table.CaseCamel}}Dao struct {
// {{.table.NameCaseCamel}}Dao is the data access object for table {{.table.Name}}.
type {{.table.NameCaseCamel}}Dao struct {
table string // table is the underlying table name of the DAO.
group string // group is the database configuration group name of current DAO.
columns {{.table.CaseCamel}}Columns // columns contains all the column names of Table for convenient usage.
columns {{.table.NameCaseCamel}}Columns // columns contains all the column names of Table for convenient usage.
}

// {{.table.CaseCamel}}Columns defines and stores column names for table {{.table.Name}}.
type {{.table.CaseCamel}}Columns struct { {{range $i,$v := .table.Fields}}
{{$v.NameCase "Camel"}} string // {{$v.Comment}}{{end}}
// {{.table.NameCaseCamel}}Columns defines and stores column names for table {{.table.Name}}.
type {{.table.NameCaseCamel}}Columns struct { {{range $i,$v := .table.Fields}}
{{$v.NameCaseCamel}} string // {{$v.Comment}}{{end}}
}

// {{.table.CaseCamelLower}}Columns holds the columns for table {{.table.Name}}.
var {{.table.CaseCamelLower}}Columns = {{.table.CaseCamel}}Columns{ {{range $i,$v := .table.Fields}}
{{$v.CaseCamel}}: "{{$v.CaseCamelLower}}",{{end}}
// {{.table.NameCaseCamelLower}}Columns holds the columns for table {{.table.Name}}.
var {{.table.NameCaseCamelLower}}Columns = {{.table.NameCaseCamel}}Columns{ {{range $i,$v := .table.Fields}}
{{$v.NameCaseCamel}}: "{{$v.NameCaseCamelLower}}",{{end}}
}

// New{{.table.CaseCamel}}Dao creates and returns a new DAO object for table data access.
func New{{.table.CaseCamel}}Dao() *{{.table.CaseCamel}}Dao {
return &{{.table.CaseCamel}}Dao{
// New{{.table.NameCaseCamel}}Dao creates and returns a new DAO object for table data access.
func New{{.table.NameCaseCamel}}Dao() *{{.table.NameCaseCamel}}Dao {
return &{{.table.NameCaseCamel}}Dao{
group: "test",
table: "{{.table.Name}}",
columns: {{.table.CaseCamelLower}}Columns,
columns: {{.table.NameCaseCamelLower}}Columns,
}
}

// DB retrieves and returns the underlying raw database management object of current DAO.
func (dao *{{.table.CaseCamel}}Dao) DB() gdb.DB {
func (dao *{{.table.NameCaseCamel}}Dao) DB() gdb.DB {
return g.DB(dao.group)
}

// Table returns the table name of current dao.
func (dao *{{.table.CaseCamel}}Dao) Table() string {
func (dao *{{.table.NameCaseCamel}}Dao) Table() string {
return dao.table
}

// Columns returns all column names of current dao.
func (dao *{{.table.CaseCamel}}Dao) Columns() {{.table.CaseCamel}}Columns {
func (dao *{{.table.NameCaseCamel}}Dao) Columns() {{.table.NameCaseCamel}}Columns {
return dao.columns
}

// Group returns the configuration group name of database of current dao.
func (dao *{{.table.CaseCamel}}Dao) Group() string {
func (dao *{{.table.NameCaseCamel}}Dao) Group() string {
return dao.group
}

// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
func (dao *{{.table.CaseCamel}}Dao) Ctx(ctx context.Context) *gdb.Model {
func (dao *{{.table.NameCaseCamel}}Dao) Ctx(ctx context.Context) *gdb.Model {
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
}

Expand All @@ -64,6 +64,6 @@ func (dao *{{.table.CaseCamel}}Dao) Ctx(ctx context.Context) *gdb.Model {
//
// Note that, you should not Commit or Rollback the transaction in function f
// as it is automatically handled by this function.
func (dao *{{.table.CaseCamel}}Dao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
func (dao *{{.table.NameCaseCamel}}Dao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
return dao.Ctx(ctx).Transaction(ctx, f)
}
Loading

0 comments on commit 3a2a73c

Please sign in to comment.