Code optimized (#493)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -31,7 +32,20 @@ type (
|
||||
pkg string
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
Option func(generator *defaultGenerator)
|
||||
|
||||
code struct {
|
||||
importsCode string
|
||||
varsCode string
|
||||
typesCode string
|
||||
newCode string
|
||||
insertCode string
|
||||
findCode []string
|
||||
updateCode string
|
||||
deleteCode string
|
||||
cacheExtra string
|
||||
}
|
||||
)
|
||||
|
||||
func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaultGenerator, error) {
|
||||
@@ -186,15 +200,6 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
return "", fmt.Errorf("table %s: missing primary key", in.Name.Source())
|
||||
}
|
||||
|
||||
text, err := util.LoadTemplate(category, modelTemplateFile, template.Model)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
t := util.With("model").
|
||||
Parse(text).
|
||||
GoFmt(true)
|
||||
|
||||
m, err := genCacheKeys(in)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -261,18 +266,19 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
return "", err
|
||||
}
|
||||
|
||||
output, err := t.Execute(map[string]interface{}{
|
||||
"pkg": g.pkg,
|
||||
"imports": importsCode,
|
||||
"vars": varsCode,
|
||||
"types": typesCode,
|
||||
"new": newCode,
|
||||
"insert": insertCode,
|
||||
"find": strings.Join(findCode, "\n"),
|
||||
"update": updateCode,
|
||||
"delete": deleteCode,
|
||||
"extraMethod": ret.cacheExtra,
|
||||
})
|
||||
code := &code{
|
||||
importsCode: importsCode,
|
||||
varsCode: varsCode,
|
||||
typesCode: typesCode,
|
||||
newCode: newCode,
|
||||
insertCode: insertCode,
|
||||
findCode: findCode,
|
||||
updateCode: updateCode,
|
||||
deleteCode: deleteCode,
|
||||
cacheExtra: ret.cacheExtra,
|
||||
}
|
||||
|
||||
output, err := g.executeModel(code)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -280,6 +286,32 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
return output.String(), nil
|
||||
}
|
||||
|
||||
func (g *defaultGenerator) executeModel(code *code) (*bytes.Buffer, error) {
|
||||
text, err := util.LoadTemplate(category, modelTemplateFile, template.Model)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t := util.With("model").
|
||||
Parse(text).
|
||||
GoFmt(true)
|
||||
output, err := t.Execute(map[string]interface{}{
|
||||
"pkg": g.pkg,
|
||||
"imports": code.importsCode,
|
||||
"vars": code.varsCode,
|
||||
"types": code.typesCode,
|
||||
"new": code.newCode,
|
||||
"insert": code.insertCode,
|
||||
"find": strings.Join(code.findCode, "\n"),
|
||||
"update": code.updateCode,
|
||||
"delete": code.deleteCode,
|
||||
"extraMethod": code.cacheExtra,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func wrapWithRawString(v string) string {
|
||||
if v == "`" {
|
||||
return v
|
||||
|
||||
@@ -68,6 +68,71 @@ func Parse(ddl string) (*Table, error) {
|
||||
|
||||
columns := tableSpec.Columns
|
||||
indexes := tableSpec.Indexes
|
||||
keyMap, err := getIndexKeyType(indexes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fields, primaryKey, err := convertFileds(columns, keyMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Table{
|
||||
Name: stringx.From(tableName),
|
||||
PrimaryKey: primaryKey,
|
||||
Fields: fields,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func convertFileds(columns []*sqlparser.ColumnDefinition, keyMap map[string]KeyType) ([]Field, Primary, error) {
|
||||
var fields []Field
|
||||
var primaryKey Primary
|
||||
for _, column := range columns {
|
||||
if column == nil {
|
||||
continue
|
||||
}
|
||||
var comment string
|
||||
if column.Type.Comment != nil {
|
||||
comment = string(column.Type.Comment.Val)
|
||||
}
|
||||
var isDefaultNull = true
|
||||
if column.Type.NotNull {
|
||||
isDefaultNull = false
|
||||
} else {
|
||||
if column.Type.Default == nil {
|
||||
isDefaultNull = false
|
||||
} else if string(column.Type.Default.Val) != "null" {
|
||||
isDefaultNull = false
|
||||
}
|
||||
}
|
||||
dataType, err := converter.ConvertDataType(column.Type.Type, isDefaultNull)
|
||||
if err != nil {
|
||||
return nil, primaryKey, err
|
||||
}
|
||||
|
||||
var field Field
|
||||
field.Name = stringx.From(column.Name.String())
|
||||
field.DataBaseType = column.Type.Type
|
||||
field.DataType = dataType
|
||||
field.Comment = comment
|
||||
key, ok := keyMap[column.Name.String()]
|
||||
if ok {
|
||||
field.IsPrimaryKey = key == primary
|
||||
field.IsUniqueKey = key == unique
|
||||
if field.IsPrimaryKey {
|
||||
primaryKey.Field = field
|
||||
if column.Type.Autoincrement {
|
||||
primaryKey.AutoIncrement = true
|
||||
}
|
||||
}
|
||||
}
|
||||
fields = append(fields, field)
|
||||
}
|
||||
return fields, primaryKey, nil
|
||||
}
|
||||
|
||||
func getIndexKeyType(indexes []*sqlparser.IndexDefinition) (map[string]KeyType, error) {
|
||||
keyMap := make(map[string]KeyType)
|
||||
for _, index := range indexes {
|
||||
info := index.Info
|
||||
@@ -101,56 +166,7 @@ func Parse(ddl string) (*Table, error) {
|
||||
keyMap[columnName] = normal
|
||||
}
|
||||
}
|
||||
|
||||
var fields []Field
|
||||
var primaryKey Primary
|
||||
for _, column := range columns {
|
||||
if column == nil {
|
||||
continue
|
||||
}
|
||||
var comment string
|
||||
if column.Type.Comment != nil {
|
||||
comment = string(column.Type.Comment.Val)
|
||||
}
|
||||
var isDefaultNull = true
|
||||
if column.Type.NotNull {
|
||||
isDefaultNull = false
|
||||
} else {
|
||||
if column.Type.Default == nil {
|
||||
isDefaultNull = false
|
||||
} else if string(column.Type.Default.Val) != "null" {
|
||||
isDefaultNull = false
|
||||
}
|
||||
}
|
||||
dataType, err := converter.ConvertDataType(column.Type.Type, isDefaultNull)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var field Field
|
||||
field.Name = stringx.From(column.Name.String())
|
||||
field.DataBaseType = column.Type.Type
|
||||
field.DataType = dataType
|
||||
field.Comment = comment
|
||||
key, ok := keyMap[column.Name.String()]
|
||||
if ok {
|
||||
field.IsPrimaryKey = key == primary
|
||||
field.IsUniqueKey = key == unique
|
||||
if field.IsPrimaryKey {
|
||||
primaryKey.Field = field
|
||||
if column.Type.Autoincrement {
|
||||
primaryKey.AutoIncrement = true
|
||||
}
|
||||
}
|
||||
}
|
||||
fields = append(fields, field)
|
||||
}
|
||||
|
||||
return &Table{
|
||||
Name: stringx.From(tableName),
|
||||
PrimaryKey: primaryKey,
|
||||
Fields: fields,
|
||||
}, nil
|
||||
return keyMap, nil
|
||||
}
|
||||
|
||||
func (t *Table) ContainsTime() bool {
|
||||
|
||||
Reference in New Issue
Block a user