|
| 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 | + |
| 7 | +package gendao |
| 8 | + |
| 9 | +import ( |
| 10 | + "bytes" |
| 11 | + "context" |
| 12 | + "path/filepath" |
| 13 | + "sort" |
| 14 | + "strconv" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "github.com/gogf/gf/v2/database/gdb" |
| 18 | + "github.com/gogf/gf/v2/os/gfile" |
| 19 | + "github.com/gogf/gf/v2/os/gview" |
| 20 | + "github.com/gogf/gf/v2/text/gstr" |
| 21 | + "github.com/gogf/gf/v2/util/gconv" |
| 22 | + |
| 23 | + "github.com/gogf/gf/cmd/gf/v2/internal/consts" |
| 24 | + "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" |
| 25 | + "github.com/gogf/gf/cmd/gf/v2/internal/utility/utils" |
| 26 | +) |
| 27 | + |
| 28 | +// generateTable generates dao files for given tables. |
| 29 | +func generateTable(ctx context.Context, in CGenDaoInternalInput) { |
| 30 | + dirPathTable := gfile.Join(in.Path, in.TablePath) |
| 31 | + if !in.GenTable { |
| 32 | + if gfile.Exists(dirPathTable) { |
| 33 | + in.genItems.AppendDirPath(dirPathTable) |
| 34 | + } |
| 35 | + return |
| 36 | + } |
| 37 | + in.genItems.AppendDirPath(dirPathTable) |
| 38 | + for i := 0; i < len(in.TableNames); i++ { |
| 39 | + var ( |
| 40 | + realTableName = in.TableNames[i] |
| 41 | + newTableName = in.NewTableNames[i] |
| 42 | + ) |
| 43 | + generateTableSingle(ctx, generateTableSingleInput{ |
| 44 | + CGenDaoInternalInput: in, |
| 45 | + TableName: realTableName, |
| 46 | + NewTableName: newTableName, |
| 47 | + DirPathTable: dirPathTable, |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// generateTableSingleInput is the input parameter for generateTableSingle. |
| 53 | +type generateTableSingleInput struct { |
| 54 | + CGenDaoInternalInput |
| 55 | + // TableName specifies the table name of the table. |
| 56 | + TableName string |
| 57 | + // NewTableName specifies the prefix-stripped or custom edited name of the table. |
| 58 | + NewTableName string |
| 59 | + DirPathTable string |
| 60 | +} |
| 61 | + |
| 62 | +// generateTableSingle generates dao files for a single table. |
| 63 | +func generateTableSingle(ctx context.Context, in generateTableSingleInput) { |
| 64 | + // Generating table data preparing. |
| 65 | + fieldMap, err := in.DB.TableFields(ctx, in.TableName) |
| 66 | + if err != nil { |
| 67 | + mlog.Fatalf(`fetching tables fields failed for table "%s": %+v`, in.TableName, err) |
| 68 | + } |
| 69 | + |
| 70 | + tableNameSnakeCase := gstr.CaseSnake(in.NewTableName) |
| 71 | + fileName := gstr.Trim(tableNameSnakeCase, "-_.") |
| 72 | + if len(fileName) > 5 && fileName[len(fileName)-5:] == "_test" { |
| 73 | + // Add suffix to avoid the table name which contains "_test", |
| 74 | + // which would make the go file a testing file. |
| 75 | + fileName += "_table" |
| 76 | + } |
| 77 | + path := filepath.FromSlash(gfile.Join(in.DirPathTable, fileName+".go")) |
| 78 | + in.genItems.AppendGeneratedFilePath(path) |
| 79 | + if in.OverwriteDao || !gfile.Exists(path) { |
| 80 | + var ( |
| 81 | + ctx = context.Background() |
| 82 | + tplContent = getTemplateFromPathOrDefault( |
| 83 | + in.TplDaoTablePath, consts.TemplateGenTableContent, |
| 84 | + ) |
| 85 | + ) |
| 86 | + tplView.ClearAssigns() |
| 87 | + tplView.Assigns(gview.Params{ |
| 88 | + tplVarGroupName: in.Group, |
| 89 | + tplVarTableName: in.TableName, |
| 90 | + tplVarTableNameCamelCase: formatFieldName(in.NewTableName, FieldNameCaseCamel), |
| 91 | + tplVarPackageName: filepath.Base(in.TablePath), |
| 92 | + tplVarTableFields: generateTableFields(fieldMap), |
| 93 | + }) |
| 94 | + indexContent, err := tplView.ParseContent(ctx, tplContent) |
| 95 | + if err != nil { |
| 96 | + mlog.Fatalf("parsing template content failed: %v", err) |
| 97 | + } |
| 98 | + if err = gfile.PutContents(path, strings.TrimSpace(indexContent)); err != nil { |
| 99 | + mlog.Fatalf("writing content to '%s' failed: %v", path, err) |
| 100 | + } else { |
| 101 | + utils.GoFmt(path) |
| 102 | + mlog.Print("generated:", gfile.RealPath(path)) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// generateTableFields generates and returns the field definition content for specified table. |
| 108 | +func generateTableFields(fields map[string]*gdb.TableField) string { |
| 109 | + var buf bytes.Buffer |
| 110 | + fieldNames := make([]string, 0, len(fields)) |
| 111 | + for fieldName := range fields { |
| 112 | + fieldNames = append(fieldNames, fieldName) |
| 113 | + } |
| 114 | + sort.Slice(fieldNames, func(i, j int) bool { |
| 115 | + return fields[fieldNames[i]].Index < fields[fieldNames[j]].Index // asc |
| 116 | + }) |
| 117 | + for index, fieldName := range fieldNames { |
| 118 | + field := fields[fieldName] |
| 119 | + buf.WriteString(" " + strconv.Quote(field.Name) + ": {\n") |
| 120 | + buf.WriteString(" Index: " + gconv.String(field.Index) + ",\n") |
| 121 | + buf.WriteString(" Name: " + strconv.Quote(field.Name) + ",\n") |
| 122 | + buf.WriteString(" Type: " + strconv.Quote(field.Type) + ",\n") |
| 123 | + buf.WriteString(" Null: " + gconv.String(field.Null) + ",\n") |
| 124 | + buf.WriteString(" Key: " + strconv.Quote(field.Key) + ",\n") |
| 125 | + buf.WriteString(" Default: " + generateDefaultValue(field.Default) + ",\n") |
| 126 | + buf.WriteString(" Extra: " + strconv.Quote(field.Extra) + ",\n") |
| 127 | + buf.WriteString(" Comment: " + strconv.Quote(field.Comment) + ",\n") |
| 128 | + buf.WriteString(" },") |
| 129 | + if index != len(fieldNames)-1 { |
| 130 | + buf.WriteString("\n") |
| 131 | + } |
| 132 | + } |
| 133 | + return buf.String() |
| 134 | +} |
| 135 | + |
| 136 | +// generateDefaultValue generates and returns the default value definition for specified field. |
| 137 | +func generateDefaultValue(value interface{}) string { |
| 138 | + if value == nil { |
| 139 | + return "nil" |
| 140 | + } |
| 141 | + switch v := value.(type) { |
| 142 | + case string: |
| 143 | + return strconv.Quote(v) |
| 144 | + default: |
| 145 | + return gconv.String(v) |
| 146 | + } |
| 147 | +} |
0 commit comments