-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(gdb): Allow to set table field metadata and allow to generate table fields registration code when generating dao #4460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4c191ca
feat(gendao): 新增表字段生成功能
LanceAdd 46ad634
feat(gf): 添加数据库表字段定义模板
LanceAdd 9d14512
fix(gendao): 使用 strconv.Quote 处理字段名和值的转义
LanceAdd 19c964a
refactor(gdb): 从接口中移除了 SetTableFields 方法定义, 需要时通过GetCore获取core实例调用 S…
LanceAdd 876066c
feat(dao):为表字段注册函数添加上下文参数
LanceAdd 5d2e984
docs(genTable): 添加结构体注释
LanceAdd 90dcab0
refactor(gendao): 优化字段排序逻辑并清理模板依赖
LanceAdd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. | ||
| // | ||
| // This Source Code Form is subject to the terms of the MIT License. | ||
| // If a copy of the MIT was not distributed with this file, | ||
| // You can obtain one at https://github.com/gogf/gf. | ||
|
|
||
| package gendao | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "path/filepath" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/gogf/gf/v2/database/gdb" | ||
| "github.com/gogf/gf/v2/os/gfile" | ||
| "github.com/gogf/gf/v2/os/gview" | ||
| "github.com/gogf/gf/v2/text/gstr" | ||
| "github.com/gogf/gf/v2/util/gconv" | ||
|
|
||
| "github.com/gogf/gf/cmd/gf/v2/internal/consts" | ||
| "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" | ||
| "github.com/gogf/gf/cmd/gf/v2/internal/utility/utils" | ||
| ) | ||
|
|
||
| // generateTable generates dao files for given tables. | ||
| func generateTable(ctx context.Context, in CGenDaoInternalInput) { | ||
| dirPathTable := gfile.Join(in.Path, in.TablePath) | ||
| if !in.GenTable { | ||
| if gfile.Exists(dirPathTable) { | ||
| in.genItems.AppendDirPath(dirPathTable) | ||
| } | ||
| return | ||
| } | ||
| in.genItems.AppendDirPath(dirPathTable) | ||
| for i := 0; i < len(in.TableNames); i++ { | ||
| var ( | ||
| realTableName = in.TableNames[i] | ||
| newTableName = in.NewTableNames[i] | ||
| ) | ||
| generateTableSingle(ctx, generateTableSingleInput{ | ||
| CGenDaoInternalInput: in, | ||
| TableName: realTableName, | ||
| NewTableName: newTableName, | ||
| DirPathTable: dirPathTable, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // generateTableSingleInput is the input parameter for generateTableSingle. | ||
| type generateTableSingleInput struct { | ||
| CGenDaoInternalInput | ||
| // TableName specifies the table name of the table. | ||
| TableName string | ||
| // NewTableName specifies the prefix-stripped or custom edited name of the table. | ||
| NewTableName string | ||
| DirPathTable string | ||
| } | ||
|
|
||
| // generateTableSingle generates dao files for a single table. | ||
| func generateTableSingle(ctx context.Context, in generateTableSingleInput) { | ||
| // Generating table data preparing. | ||
| fieldMap, err := in.DB.TableFields(ctx, in.TableName) | ||
| if err != nil { | ||
| mlog.Fatalf(`fetching tables fields failed for table "%s": %+v`, in.TableName, err) | ||
| } | ||
|
|
||
| tableNameSnakeCase := gstr.CaseSnake(in.NewTableName) | ||
| fileName := gstr.Trim(tableNameSnakeCase, "-_.") | ||
| if len(fileName) > 5 && fileName[len(fileName)-5:] == "_test" { | ||
| // Add suffix to avoid the table name which contains "_test", | ||
| // which would make the go file a testing file. | ||
| fileName += "_table" | ||
| } | ||
| path := filepath.FromSlash(gfile.Join(in.DirPathTable, fileName+".go")) | ||
| in.genItems.AppendGeneratedFilePath(path) | ||
| if in.OverwriteDao || !gfile.Exists(path) { | ||
| var ( | ||
| ctx = context.Background() | ||
| tplContent = getTemplateFromPathOrDefault( | ||
| in.TplDaoTablePath, consts.TemplateGenTableContent, | ||
| ) | ||
| ) | ||
| tplView.ClearAssigns() | ||
| tplView.Assigns(gview.Params{ | ||
| tplVarGroupName: in.Group, | ||
| tplVarTableName: in.TableName, | ||
| tplVarTableNameCamelCase: formatFieldName(in.NewTableName, FieldNameCaseCamel), | ||
| tplVarPackageName: filepath.Base(in.TablePath), | ||
| tplVarTableFields: generateTableFields(fieldMap), | ||
| }) | ||
| indexContent, err := tplView.ParseContent(ctx, tplContent) | ||
| if err != nil { | ||
| mlog.Fatalf("parsing template content failed: %v", err) | ||
| } | ||
| if err = gfile.PutContents(path, strings.TrimSpace(indexContent)); err != nil { | ||
| mlog.Fatalf("writing content to '%s' failed: %v", path, err) | ||
| } else { | ||
| utils.GoFmt(path) | ||
| mlog.Print("generated:", gfile.RealPath(path)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // generateTableFields generates and returns the field definition content for specified table. | ||
| func generateTableFields(fields map[string]*gdb.TableField) string { | ||
| var buf bytes.Buffer | ||
| fieldNames := make([]string, 0, len(fields)) | ||
| for fieldName := range fields { | ||
| fieldNames = append(fieldNames, fieldName) | ||
| } | ||
| sort.Slice(fieldNames, func(i, j int) bool { | ||
| return fields[fieldNames[i]].Index < fields[fieldNames[j]].Index // asc | ||
| }) | ||
| for index, fieldName := range fieldNames { | ||
| field := fields[fieldName] | ||
| buf.WriteString(" " + strconv.Quote(field.Name) + ": {\n") | ||
| buf.WriteString(" Index: " + gconv.String(field.Index) + ",\n") | ||
| buf.WriteString(" Name: " + strconv.Quote(field.Name) + ",\n") | ||
| buf.WriteString(" Type: " + strconv.Quote(field.Type) + ",\n") | ||
| buf.WriteString(" Null: " + gconv.String(field.Null) + ",\n") | ||
| buf.WriteString(" Key: " + strconv.Quote(field.Key) + ",\n") | ||
| buf.WriteString(" Default: " + generateDefaultValue(field.Default) + ",\n") | ||
| buf.WriteString(" Extra: " + strconv.Quote(field.Extra) + ",\n") | ||
| buf.WriteString(" Comment: " + strconv.Quote(field.Comment) + ",\n") | ||
| buf.WriteString(" },") | ||
| if index != len(fieldNames)-1 { | ||
| buf.WriteString("\n") | ||
| } | ||
| } | ||
| return buf.String() | ||
| } | ||
|
|
||
| // generateDefaultValue generates and returns the default value definition for specified field. | ||
| func generateDefaultValue(value interface{}) string { | ||
| if value == nil { | ||
| return "nil" | ||
| } | ||
| switch v := value.(type) { | ||
| case string: | ||
| return strconv.Quote(v) | ||
| default: | ||
| return gconv.String(v) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. | ||
| // | ||
| // This Source Code Form is subject to the terms of the MIT License. | ||
| // If a copy of the MIT was not distributed with this file, | ||
| // You can obtain one at https://github.com/gogf/gf. | ||
|
|
||
| package consts | ||
|
|
||
| const TemplateGenTableContent = ` | ||
| // ================================================================================= | ||
| // This file is auto-generated by the GoFrame CLI tool. You may modify it as needed. | ||
| // ================================================================================= | ||
|
|
||
| package {{.TplPackageName}} | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/gogf/gf/v2/database/gdb" | ||
| ) | ||
|
|
||
| // {{.TplTableNameCamelCase}} defines the fields of table "{{.TplTableName}}" with their properties. | ||
| // This map is used internally by GoFrame ORM to understand table structure. | ||
| var {{.TplTableNameCamelCase}} = map[string]*gdb.TableField{ | ||
| {{.TplTableFields}} | ||
| } | ||
|
|
||
| // Set{{.TplTableNameCamelCase}}TableFields registers the table fields definition to the database instance. | ||
| // db: database instance that implements gdb.DB interface. | ||
| // schema: optional schema/namespace name, especially for databases that support schemas. | ||
| func Set{{.TplTableNameCamelCase}}TableFields(ctx context.Context, db gdb.DB, schema ...string) error { | ||
| return db.GetCore().SetTableFields(ctx, "{{.TplTableName}}", {{.TplTableNameCamelCase}}, schema...) | ||
| } | ||
|
|
||
| ` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.