Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cat3306 committed Dec 6, 2023
1 parent e21308f commit 2b40e8e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 33 deletions.
12 changes: 6 additions & 6 deletions tools/goctl/model/sql/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,17 @@ var commonMysqlDataTypeMapString = map[string]string{
}

// ConvertDataType converts mysql column type into golang type
func ConvertDataType(dataBaseType int, isDefaultNull, unsigned, strict bool) (string, error) {
func ConvertDataType(dataBaseType int, isDefaultNull, unsigned, strict bool, args ...bool) (string, error) {
tp, ok := commonMysqlDataTypeMapInt[dataBaseType]
if !ok {
return "", fmt.Errorf("unsupported database type: %v", dataBaseType)
}

return mayConvertNullType(tp, isDefaultNull, unsigned, strict), nil
return mayConvertNullType(tp, isDefaultNull, unsigned, strict, args...), nil
}

// ConvertStringDataType converts mysql column type into golang type
func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict bool) (
func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict bool, args ...bool) (
goType string, isPQArray bool, err error) {
tp, ok := commonMysqlDataTypeMapString[strings.ToLower(dataBaseType)]
if !ok {
Expand All @@ -165,11 +165,11 @@ func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict
return tp, true, nil
}

return mayConvertNullType(tp, isDefaultNull, unsigned, strict), false, nil
return mayConvertNullType(tp, isDefaultNull, unsigned, strict, args...), false, nil
}

func mayConvertNullType(goDataType string, isDefaultNull, unsigned, strict bool) string {
if !isDefaultNull {
func mayConvertNullType(goDataType string, isDefaultNull, unsigned, strict bool, args ...bool) string {
if !isDefaultNull || len(args) != 0 { //gorm mode len(args)!=0
if unsigned && strict {
ret, ok := unsignedTypeMap[goDataType]
if ok {
Expand Down
45 changes: 27 additions & 18 deletions tools/goctl/model/sql/gen/gen_gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,24 @@ func (g *GormGenerator) genFromDDL(filename string, withCache, strict bool, data
map[string]*codeTuple, error,
) {
m := make(map[string]*codeTuple)
tables, err := parser.Parse(filename, database, strict)
tables, err := parser.Parse(filename, database, strict, true) //true gorm mode
if err != nil {
return nil, err
}

for _, e := range tables {
code, err := g.genModel(*e, withCache)
code, err := g.genModel(*e)
if err != nil {
return nil, err
}
customCode, err := g.genModelCustom(*e)
if err != nil {
return nil, err
}
//customCode, err := g.genModelCustom(*e, withCache)
//if err != nil {
// return nil, err
//}

m[e.Name.Source()] = &codeTuple{
modelCode: code,
modelCode: code,
modelCustomCode: customCode,
}
}

Expand All @@ -81,16 +82,16 @@ func (g *GormGenerator) genFromDDL(filename string, withCache, strict bool, data
func (g *GormGenerator) StartFromInformationSchema(tables map[string]*model.Table, withCache, strict bool) error {
m := make(map[string]*codeTuple)
for _, each := range tables {
table, err := parser.ConvertDataType(each, strict)
table, err := parser.ConvertDataType(each, strict, true) //true gorm mode
if err != nil {
return err
}

code, err := g.genModel(*table, withCache)
code, err := g.genModel(*table)
if err != nil {
return err
}
customCode, err := g.genModelCustom(*table, withCache)
customCode, err := g.genModelCustom(*table)
if err != nil {
return err
}
Expand Down Expand Up @@ -139,6 +140,17 @@ func (g *GormGenerator) createFile(modelList map[string]*codeTuple) error {
if err != nil {
return err
}

name = util.SafeString(modelFilename) + ".go"
filename = filepath.Join(dirAbs, name)
if pathx.FileExists(filename) {
g.Warning("%s already exists, ignored.", name)
continue
}
err = os.WriteFile(filename, []byte(codes.modelCustomCode), os.ModePerm)
if err != nil {
return err
}
}

// generate error file
Expand Down Expand Up @@ -204,7 +216,7 @@ func (g *GormGenerator) GenMethod(table Table) (string, error) {
}
return buffer.String(), nil
}
func (g *GormGenerator) genModel(in parser.Table, withCache bool) (string, error) {
func (g *GormGenerator) genModel(in parser.Table) (string, error) {
if len(in.PrimaryKey.Name.Source()) == 0 {
return "", fmt.Errorf("table %s: missing primary key", in.Name.Source())
}
Expand Down Expand Up @@ -244,20 +256,17 @@ func (g *GormGenerator) genModel(in parser.Table, withCache bool) (string, error
return output.String(), nil
}

func (g *GormGenerator) genModelCustom(in parser.Table, withCache bool) (string, error) {
text, err := pathx.LoadTemplate(category, modelCustomTemplateFile, template.ModelCustom)
func (g *GormGenerator) genModelCustom(in parser.Table) (string, error) {
text, err := pathx.LoadTemplate(category, modelGormCustomTemplateFile, template.GormModelCustom)
if err != nil {
return "", err
}

t := util.With("model-custom").
t := util.With("gorm-model-custom").
Parse(text).
GoFmt(true)
output, err := t.Execute(map[string]any{
"pkg": g.pkg,
"withCache": withCache,
"upperStartCamelObject": in.Name.ToCamel(),
"lowerStartCamelObject": stringx.From(in.Name.ToCamel()).Untitle(),
"pkg": g.pkg,
})
if err != nil {
return "", err
Expand Down
2 changes: 2 additions & 0 deletions tools/goctl/model/sql/gen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
errGormTemplateFile = "gorm-err.tpl"
methodGormTemplateFile = "gorm-method.tpl"
findGormOneByFieldTemplateFile = "gorm-find-one-by-field.tpl"
modelGormCustomTemplateFile = "gorm-custom-model.tpl"
)

var templates = map[string]string{
Expand Down Expand Up @@ -70,6 +71,7 @@ var templates = map[string]string{
methodGormTemplateFile: template.GormMethod,
findGormOneByFieldTemplateFile: template.GormFindOneByField,
typesGormTemplateFile: template.GormTypes,
modelGormCustomTemplateFile: template.GormModelCustom,
}

// Category returns model const value
Expand Down
18 changes: 9 additions & 9 deletions tools/goctl/model/sql/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func parseNameOriginal(ts []*parser.Table) (nameOriginals [][]string) {
}

// Parse parses ddl into golang structure
func Parse(filename, database string, strict bool) ([]*Table, error) {
func Parse(filename, database string, strict bool, args ...bool) ([]*Table, error) {
p := parser.NewParser()
tables, err := p.From(filename)
if err != nil {
Expand Down Expand Up @@ -128,7 +128,7 @@ func Parse(filename, database string, strict bool) ([]*Table, error) {
return nil, fmt.Errorf("%s: unexpected join primary key", prefix)
}

primaryKey, fieldM, err := convertColumns(columns, primaryColumn, strict)
primaryKey, fieldM, err := convertColumns(columns, primaryColumn, strict, args...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func checkDuplicateUniqueIndex(uniqueIndex map[string][]*Field, tableName string
}
}

func convertColumns(columns []*parser.Column, primaryColumn string, strict bool) (Primary, map[string]*Field, error) {
func convertColumns(columns []*parser.Column, primaryColumn string, strict bool, args ...bool) (Primary, map[string]*Field, error) {
var (
primaryKey Primary
fieldM = make(map[string]*Field)
Expand Down Expand Up @@ -227,7 +227,7 @@ func convertColumns(columns []*parser.Column, primaryColumn string, strict bool)
}
}

dataType, err := converter.ConvertDataType(column.DataType.Type(), isDefaultNull, column.DataType.Unsigned(), strict)
dataType, err := converter.ConvertDataType(column.DataType.Type(), isDefaultNull, column.DataType.Unsigned(), strict, args...)
if err != nil {
return Primary{}, nil, err
}
Expand Down Expand Up @@ -272,10 +272,10 @@ func (t *Table) ContainsTime() bool {
}

// ConvertDataType converts mysql data type into golang data type
func ConvertDataType(table *model.Table, strict bool) (*Table, error) {
func ConvertDataType(table *model.Table, strict bool, args ...bool) (*Table, error) {
isPrimaryDefaultNull := table.PrimaryKey.ColumnDefault == nil && table.PrimaryKey.IsNullAble == "YES"
isPrimaryUnsigned := strings.Contains(table.PrimaryKey.DbColumn.ColumnType, "unsigned")
primaryDataType, containsPQ, err := converter.ConvertStringDataType(table.PrimaryKey.DataType, isPrimaryDefaultNull, isPrimaryUnsigned, strict)
primaryDataType, containsPQ, err := converter.ConvertStringDataType(table.PrimaryKey.DataType, isPrimaryDefaultNull, isPrimaryUnsigned, strict, args...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func ConvertDataType(table *model.Table, strict bool) (*Table, error) {
AutoIncrement: strings.Contains(table.PrimaryKey.Extra, "auto_increment"),
}

fieldM, err := getTableFields(table, strict)
fieldM, err := getTableFields(table, strict, args...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -358,12 +358,12 @@ func ConvertDataType(table *model.Table, strict bool) (*Table, error) {
return &reply, nil
}

func getTableFields(table *model.Table, strict bool) (map[string]*Field, error) {
func getTableFields(table *model.Table, strict bool, args ...bool) (map[string]*Field, error) {
fieldM := make(map[string]*Field)
for _, each := range table.Columns {
isDefaultNull := each.ColumnDefault == nil && each.IsNullAble == "YES"
isPrimaryUnsigned := strings.Contains(each.ColumnType, "unsigned")
dt, containsPQ, err := converter.ConvertStringDataType(each.DataType, isDefaultNull, isPrimaryUnsigned, strict)
dt, containsPQ, err := converter.ConvertStringDataType(each.DataType, isDefaultNull, isPrimaryUnsigned, strict, args...)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions tools/goctl/model/sql/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,6 @@ var GormMethod string

//go:embed tpl/gorm-find-one-by-field.tpl
var GormFindOneByField string

//go:embed tpl/gorm-custom-model.tpl
var GormModelCustom string
1 change: 1 addition & 0 deletions tools/goctl/model/sql/template/tpl/gorm-custom-model.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package {{.pkg}}

0 comments on commit 2b40e8e

Please sign in to comment.