goctl model reactor (#15)

* reactor sql generation

* reactor sql generation

* add console & example

* optimize unit test & add document

* modify default config

* remove test file

* Revert "remove test file"

This reverts commit 81041f9e

* fix stringx.go & optimize example

* remove unused code
This commit is contained in:
Keson
2020-08-19 10:41:19 +08:00
committed by GitHub
parent 1252bd9cde
commit d21d770b5b
64 changed files with 1505 additions and 2306 deletions

View File

@@ -1,108 +0,0 @@
package gen
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
)
func TableConvert(outerTable OuterTable) (*InnerTable, error) {
var table InnerTable
table.CreateNotFound = outerTable.CreateNotFound
tableSnakeCase, tableUpperCamelCase, tableLowerCamelCase := util.FormatField(outerTable.Table)
table.SnakeCase = tableSnakeCase
table.UpperCamelCase = tableUpperCamelCase
table.LowerCamelCase = tableLowerCamelCase
fields := make([]*InnerField, 0)
var primaryField *InnerField
conflict := make(map[string]struct{})
var containsCache bool
for _, field := range outerTable.Fields {
if field.Cache && !containsCache {
containsCache = true
}
fieldSnakeCase, fieldUpperCamelCase, fieldLowerCamelCase := util.FormatField(field.Name)
tag, err := genTag(fieldSnakeCase)
if err != nil {
return nil, err
}
var comment string
if field.Comment != "" {
comment = fmt.Sprintf("// %s", field.Comment)
}
withFields := make([]InnerWithField, 0)
unique := make([]string, 0)
unique = append(unique, fmt.Sprintf("%v", field.QueryType))
unique = append(unique, field.Name)
for _, item := range field.WithFields {
unique = append(unique, item.Name)
withFieldSnakeCase, withFieldUpperCamelCase, withFieldLowerCamelCase := util.FormatField(item.Name)
withFields = append(withFields, InnerWithField{
Case: Case{
SnakeCase: withFieldSnakeCase,
LowerCamelCase: withFieldLowerCamelCase,
UpperCamelCase: withFieldUpperCamelCase,
},
DataType: commonMysqlDataTypeMap[item.DataBaseType],
})
}
sort.Strings(unique)
uniqueKey := strings.Join(unique, "#")
if _, ok := conflict[uniqueKey]; ok {
return nil, ErrCircleQuery
} else {
conflict[uniqueKey] = struct{}{}
}
sortFields := make([]InnerSort, 0)
for _, sortField := range field.OuterSort {
sortSnake, sortUpperCamelCase, sortLowerCamelCase := util.FormatField(sortField.Field)
sortFields = append(sortFields, InnerSort{
Field: Case{
SnakeCase: sortSnake,
LowerCamelCase: sortUpperCamelCase,
UpperCamelCase: sortLowerCamelCase,
},
Asc: sortField.Asc,
})
}
innerField := &InnerField{
IsPrimaryKey: field.IsPrimaryKey,
InnerWithField: InnerWithField{
Case: Case{
SnakeCase: fieldSnakeCase,
LowerCamelCase: fieldLowerCamelCase,
UpperCamelCase: fieldUpperCamelCase,
},
DataType: commonMysqlDataTypeMap[field.DataBaseType],
},
DataBaseType: field.DataBaseType,
Tag: tag,
Comment: comment,
Cache: field.Cache,
QueryType: field.QueryType,
WithFields: withFields,
Sort: sortFields,
}
if field.IsPrimaryKey {
primaryField = innerField
}
fields = append(fields, innerField)
}
if primaryField == nil {
return nil, errors.New("please ensure that primary exists")
}
table.ContainsCache = containsCache
primaryField.Cache = containsCache
table.PrimaryField = primaryField
table.Fields = fields
cacheKey, err := genCacheKeys(&table)
if err != nil {
return nil, err
}
table.CacheKey = cacheKey
return &table, nil
}

View File

@@ -1,51 +1,47 @@
package gen
import (
"bytes"
"strings"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genDelete(table *InnerTable) (string, error) {
t, err := template.New("delete").Parse(sqltemplate.Delete)
if err != nil {
return "", nil
}
deleteBuffer := new(bytes.Buffer)
keys := make([]string, 0)
keyValues := make([]string, 0)
for snake, key := range table.CacheKey {
if snake == table.PrimaryField.SnakeCase {
keys = append(keys, key.Key)
func genDelete(table Table, withCache bool) (string, error) {
keySet := collection.NewSet()
keyVariableSet := collection.NewSet()
for fieldName, key := range table.CacheKey {
if fieldName == table.PrimaryKey.Name.Source() {
keySet.AddStr(key.KeyExpression)
} else {
keys = append(keys, key.DataKey)
keySet.AddStr(key.DataKeyExpression)
}
keyValues = append(keyValues, key.KeyVariable)
keyVariableSet.AddStr(key.Variable)
}
var isOnlyPrimaryKeyCache = true
var containsIndexCache = false
for _, item := range table.Fields {
if item.IsPrimaryKey {
continue
}
if item.Cache {
isOnlyPrimaryKeyCache = false
if item.IsKey {
containsIndexCache = true
break
}
}
err = t.Execute(deleteBuffer, map[string]interface{}{
"upperObject": table.UpperCamelCase,
"containsCache": table.ContainsCache,
"isNotPrimaryKey": !isOnlyPrimaryKeyCache,
"lowerPrimaryKey": table.PrimaryField.LowerCamelCase,
"dataType": table.PrimaryField.DataType,
"keys": strings.Join(keys, "\r\n"),
"snakePrimaryKey": table.PrimaryField.SnakeCase,
"keyValues": strings.Join(keyValues, ", "),
})
camel := table.Name.Snake2Camel()
output, err := templatex.With("delete").
Parse(template.Delete).
Execute(map[string]interface{}{
"upperStartCamelObject": camel,
"withCache": withCache,
"containsIndexCache": containsIndexCache,
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.Snake2Camel()).LowerStart(),
"dataType": table.PrimaryKey.DataType,
"keys": strings.Join(keySet.KeysStr(), "\n"),
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
})
if err != nil {
return "", err
}
return deleteBuffer.String(), nil
return output.String(), nil
}

View File

@@ -1,15 +1,15 @@
package gen
import (
"bytes"
"strings"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genFields(fields []*InnerField) (string, error) {
list := make([]string, 0)
func genFields(fields []parser.Field) (string, error) {
var list []string
for _, field := range fields {
result, err := genField(field)
if err != nil {
@@ -17,23 +17,25 @@ func genFields(fields []*InnerField) (string, error) {
}
list = append(list, result)
}
return strings.Join(list, "\r\n"), nil
return strings.Join(list, "\n"), nil
}
func genField(field *InnerField) (string, error) {
t, err := template.New("types").Parse(sqltemplate.Field)
if err != nil {
return "", nil
}
var typeBuffer = new(bytes.Buffer)
err = t.Execute(typeBuffer, map[string]string{
"name": field.UpperCamelCase,
"type": field.DataType,
"tag": field.Tag,
"comment": field.Comment,
})
func genField(field parser.Field) (string, error) {
tag, err := genTag(field.Name.Source())
if err != nil {
return "", err
}
return typeBuffer.String(), nil
output, err := templatex.With("types").
Parse(template.Field).
Execute(map[string]interface{}{
"name": field.Name.Snake2Camel(),
"type": field.DataType,
"tag": tag,
"hasComment": field.Comment != "",
"comment": field.Comment,
})
if err != nil {
return "", err
}
return output.String(), nil
}

View File

@@ -1,55 +0,0 @@
package gen
import (
"bytes"
"strings"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
)
func genFindAllByField(table *InnerTable) (string, error) {
t, err := template.New("findAllByField").Parse(sqltemplate.FindAllByField)
if err != nil {
return "", err
}
list := make([]string, 0)
for _, field := range table.Fields {
if field.IsPrimaryKey {
continue
}
if field.QueryType != QueryAll {
continue
}
fineOneByFieldBuffer := new(bytes.Buffer)
upperFields := make([]string, 0)
in := make([]string, 0)
expressionFields := make([]string, 0)
expressionValuesFields := make([]string, 0)
upperFields = append(upperFields, field.UpperCamelCase)
in = append(in, field.LowerCamelCase+" "+field.DataType)
expressionFields = append(expressionFields, field.SnakeCase+" = ?")
expressionValuesFields = append(expressionValuesFields, field.LowerCamelCase)
for _, withField := range field.WithFields {
upperFields = append(upperFields, withField.UpperCamelCase)
in = append(in, withField.LowerCamelCase+" "+withField.DataType)
expressionFields = append(expressionFields, withField.SnakeCase+" = ?")
expressionValuesFields = append(expressionValuesFields, withField.LowerCamelCase)
}
err = t.Execute(fineOneByFieldBuffer, map[string]interface{}{
"in": strings.Join(in, ","),
"upperObject": table.UpperCamelCase,
"upperFields": strings.Join(upperFields, "And"),
"lowerObject": table.LowerCamelCase,
"snakePrimaryKey": field.SnakeCase,
"expression": strings.Join(expressionFields, " AND "),
"expressionValues": strings.Join(expressionValuesFields, ", "),
"containsCache": table.ContainsCache,
})
if err != nil {
return "", err
}
list = append(list, fineOneByFieldBuffer.String())
}
return strings.Join(list, ""), nil
}

View File

@@ -1,63 +0,0 @@
package gen
import (
"bytes"
"strings"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
)
func genFindLimitByField(table *InnerTable) (string, error) {
t, err := template.New("findLimitByField").Parse(sqltemplate.FindLimitByField)
if err != nil {
return "", err
}
list := make([]string, 0)
for _, field := range table.Fields {
if field.IsPrimaryKey {
continue
}
if field.QueryType != QueryLimit {
continue
}
fineOneByFieldBuffer := new(bytes.Buffer)
upperFields := make([]string, 0)
in := make([]string, 0)
expressionFields := make([]string, 0)
expressionValuesFields := make([]string, 0)
upperFields = append(upperFields, field.UpperCamelCase)
in = append(in, field.LowerCamelCase+" "+field.DataType)
expressionFields = append(expressionFields, field.SnakeCase+" = ?")
expressionValuesFields = append(expressionValuesFields, field.LowerCamelCase)
for _, withField := range field.WithFields {
upperFields = append(upperFields, withField.UpperCamelCase)
in = append(in, withField.LowerCamelCase+" "+withField.DataType)
expressionFields = append(expressionFields, withField.SnakeCase+" = ?")
expressionValuesFields = append(expressionValuesFields, withField.LowerCamelCase)
}
sortList := make([]string, 0)
for _, item := range field.Sort {
var sort = "ASC"
if !item.Asc {
sort = "DESC"
}
sortList = append(sortList, item.Field.SnakeCase+" "+sort)
}
err = t.Execute(fineOneByFieldBuffer, map[string]interface{}{
"in": strings.Join(in, ","),
"upperObject": table.UpperCamelCase,
"upperFields": strings.Join(upperFields, "And"),
"lowerObject": table.LowerCamelCase,
"expression": strings.Join(expressionFields, " AND "),
"expressionValues": strings.Join(expressionValuesFields, ", "),
"sortExpression": strings.Join(sortList, ","),
"containsCache": table.ContainsCache,
})
if err != nil {
return "", err
}
list = append(list, fineOneByFieldBuffer.String())
}
return strings.Join(list, ""), nil
}

View File

@@ -1,30 +1,27 @@
package gen
import (
"bytes"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genFindOne(table *InnerTable) (string, error) {
t, err := template.New("findOne").Parse(sqltemplate.FindOne)
func genFindOne(table Table, withCache bool) (string, error) {
camel := table.Name.Snake2Camel()
output, err := templatex.With("findOne").
Parse(template.FindOne).
Execute(map[string]interface{}{
"withCache": withCache,
"upperStartCamelObject": camel,
"lowerStartCamelObject": stringx.From(camel).LowerStart(),
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.Snake2Camel()).LowerStart(),
"dataType": table.PrimaryKey.DataType,
"cacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].KeyExpression,
"cacheKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
})
if err != nil {
return "", err
}
fineOneBuffer := new(bytes.Buffer)
err = t.Execute(fineOneBuffer, map[string]interface{}{
"withCache": table.PrimaryField.Cache,
"upperObject": table.UpperCamelCase,
"lowerObject": table.LowerCamelCase,
"snakePrimaryKey": table.PrimaryField.SnakeCase,
"lowerPrimaryKey": table.PrimaryField.LowerCamelCase,
"dataType": table.PrimaryField.DataType,
"cacheKey": table.CacheKey[table.PrimaryField.SnakeCase].Key,
"cacheKeyVariable": table.CacheKey[table.PrimaryField.SnakeCase].KeyVariable,
})
if err != nil {
return "", err
}
return fineOneBuffer.String(), nil
return output.String(), nil
}

View File

@@ -1,67 +1,41 @@
package gen
import (
"bytes"
"fmt"
"strings"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genFineOneByField(table *InnerTable) (string, error) {
t, err := template.New("findOneByField").Parse(sqltemplate.FindOneByField)
if err != nil {
return "", err
}
list := make([]string, 0)
func genFineOneByField(table Table, withCache bool) (string, error) {
t := templatex.With("findOneByField").Parse(template.FindOneByField)
var list []string
camelTableName := table.Name.Snake2Camel()
for _, field := range table.Fields {
if field.IsPrimaryKey {
if field.IsPrimaryKey || !field.IsKey {
continue
}
if field.QueryType != QueryOne {
continue
}
fineOneByFieldBuffer := new(bytes.Buffer)
upperFields := make([]string, 0)
in := make([]string, 0)
expressionFields := make([]string, 0)
expressionValuesFields := make([]string, 0)
upperFields = append(upperFields, field.UpperCamelCase)
in = append(in, field.LowerCamelCase+" "+field.DataType)
expressionFields = append(expressionFields, field.SnakeCase+" = ?")
expressionValuesFields = append(expressionValuesFields, field.LowerCamelCase)
for _, withField := range field.WithFields {
upperFields = append(upperFields, withField.UpperCamelCase)
in = append(in, withField.LowerCamelCase+" "+withField.DataType)
expressionFields = append(expressionFields, withField.SnakeCase+" = ?")
expressionValuesFields = append(expressionValuesFields, withField.LowerCamelCase)
}
err = t.Execute(fineOneByFieldBuffer, map[string]interface{}{
"in": strings.Join(in, ","),
"upperObject": table.UpperCamelCase,
"upperFields": strings.Join(upperFields, "And"),
"onlyOneFiled": len(field.WithFields) == 0,
"withCache": field.Cache,
"containsCache": table.ContainsCache,
"lowerObject": table.LowerCamelCase,
"lowerField": field.LowerCamelCase,
"snakeField": field.SnakeCase,
"lowerPrimaryKey": table.PrimaryField.LowerCamelCase,
"UpperPrimaryKey": table.PrimaryField.UpperCamelCase,
"primaryKeyDefine": table.CacheKey[table.PrimaryField.SnakeCase].Define,
"primarySnakeField": table.PrimaryField.SnakeCase,
"primaryDataType": table.PrimaryField.DataType,
"primaryDataTypeString": table.PrimaryField.DataType == "string",
"upperObjectKey": table.PrimaryField.UpperCamelCase,
"cacheKey": table.CacheKey[field.SnakeCase].Key,
"cacheKeyVariable": table.CacheKey[field.SnakeCase].KeyVariable,
"expression": strings.Join(expressionFields, " AND "),
"expressionValues": strings.Join(expressionValuesFields, ", "),
camelFieldName := field.Name.Snake2Camel()
output, err := t.Execute(map[string]interface{}{
"upperStartCamelObject": camelTableName,
"upperField": camelFieldName,
"in": fmt.Sprintf("%s %s", stringx.From(camelFieldName).LowerStart(), field.DataType),
"withCache": withCache,
"cacheKey": table.CacheKey[field.Name.Source()].KeyExpression,
"cacheKeyVariable": table.CacheKey[field.Name.Source()].Variable,
"primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
"lowerStartCamelObject": stringx.From(camelTableName).LowerStart(),
"lowerStartCamelField": stringx.From(camelFieldName).LowerStart(),
"upperStartCamelPrimaryKey": table.PrimaryKey.Name.Snake2Camel(),
"originalField": field.Name.Source(),
"originalPrimaryField": table.PrimaryKey.Name.Source(),
})
if err != nil {
return "", err
}
list = append(list, fineOneByFieldBuffer.String())
list = append(list, output.String())
}
return strings.Join(list, ""), nil
return strings.Join(list, "\n"), nil
}

View File

@@ -0,0 +1,196 @@
package gen
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/console"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
const (
pwd = "."
createTableFlag = `(?m)^(?i)CREATE\s+TABLE` // ignore case
)
type (
defaultGenerator struct {
source string
src string
dir string
console.Console
}
Option func(generator *defaultGenerator)
)
func NewDefaultGenerator(src, dir string, opt ...Option) *defaultGenerator {
if src == "" {
src = pwd
}
if dir == "" {
dir = pwd
}
generator := &defaultGenerator{src: src, dir: dir}
var optionList []Option
optionList = append(optionList, newDefaultOption())
optionList = append(optionList, opt...)
for _, fn := range optionList {
fn(generator)
}
return generator
}
func WithConsoleOption(c console.Console) Option {
return func(generator *defaultGenerator) {
generator.Console = c
}
}
func newDefaultOption() Option {
return func(generator *defaultGenerator) {
generator.Console = console.NewColorConsole()
}
}
func (g *defaultGenerator) Start(withCache bool) error {
fileSrc, err := filepath.Abs(g.src)
if err != nil {
return err
}
dirAbs, err := filepath.Abs(g.dir)
if err != nil {
return err
}
err = util.MkdirIfNotExist(dirAbs)
if err != nil {
return err
}
data, err := ioutil.ReadFile(fileSrc)
if err != nil {
return err
}
g.source = string(data)
modelList, err := g.genFromDDL(withCache)
if err != nil {
return err
}
for tableName, code := range modelList {
name := fmt.Sprintf("%smodel.go", strings.ToLower(stringx.From(tableName).Snake2Camel()))
filename := filepath.Join(dirAbs, name)
if util.FileExists(filename) {
g.Warning("%s already exists,ignored.", name)
continue
}
err = ioutil.WriteFile(filename, []byte(code), os.ModePerm)
if err != nil {
return err
}
}
// generate error file
filename := filepath.Join(dirAbs, "error.go")
if !util.FileExists(filename) {
err = ioutil.WriteFile(filename, []byte(template.Error), os.ModePerm)
if err != nil {
return err
}
}
g.Success("Done.")
return nil
}
// ret1: key-table name,value-code
func (g *defaultGenerator) genFromDDL(withCache bool) (map[string]string, error) {
ddlList := g.split()
m := make(map[string]string)
for _, ddl := range ddlList {
table, err := parser.Parse(ddl)
if err != nil {
return nil, err
}
code, err := g.genModel(*table, withCache)
if err != nil {
return nil, err
}
m[table.Name.Source()] = code
}
return m, nil
}
type (
Table struct {
parser.Table
CacheKey map[string]Key
}
)
func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, error) {
t := templatex.With("model").
Parse(template.Model).
GoFmt(true)
m, err := genCacheKeys(in)
if err != nil {
return "", err
}
importsCode := genImports(withCache)
var table Table
table.Table = in
table.CacheKey = m
varsCode, err := genVars(table, withCache)
if err != nil {
return "", err
}
typesCode, err := genTypes(table, withCache)
if err != nil {
return "", err
}
newCode, err := genNew(table, withCache)
if err != nil {
return "", err
}
insertCode, err := genInsert(table, withCache)
if err != nil {
return "", err
}
var findCode = make([]string, 0)
findOneCode, err := genFindOne(table, withCache)
if err != nil {
return "", err
}
findOneByFieldCode, err := genFineOneByField(table, withCache)
if err != nil {
return "", err
}
findCode = append(findCode, findOneCode, findOneByFieldCode)
updateCode, err := genUpdate(table, withCache)
if err != nil {
return "", err
}
deleteCode, err := genDelete(table, withCache)
if err != nil {
return "", err
}
output, err := t.Execute(map[string]interface{}{
"imports": importsCode,
"vars": varsCode,
"types": typesCode,
"new": newCode,
"insert": insertCode,
"find": strings.Join(findCode, "\r\n"),
"update": updateCode,
"delete": deleteCode,
})
if err != nil {
return "", err
}
return output.String(), nil
}

View File

@@ -1,23 +1,13 @@
package gen
import (
"bytes"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
)
func genImports(table *InnerTable) (string, error) {
t, err := template.New("imports").Parse(sqltemplate.Imports)
if err != nil {
return "", err
func genImports(withCache bool) string {
if withCache {
return template.Imports
} else {
return template.ImportsNoCache
}
importBuffer := new(bytes.Buffer)
err = t.Execute(importBuffer, map[string]interface{}{
"containsCache": table.ContainsCache,
})
if err != nil {
return "", err
}
return importBuffer.String(), nil
}

View File

@@ -1,37 +1,39 @@
package gen
import (
"bytes"
"strings"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genInsert(table *InnerTable) (string, error) {
t, err := template.New("insert").Parse(sqltemplate.Insert)
if err != nil {
return "", nil
}
insertBuffer := new(bytes.Buffer)
func genInsert(table Table, withCache bool) (string, error) {
expressions := make([]string, 0)
expressionValues := make([]string, 0)
for _, filed := range table.Fields {
if filed.SnakeCase == "create_time" || filed.SnakeCase == "update_time" || filed.IsPrimaryKey {
camel := filed.Name.Snake2Camel()
if camel == "CreateTime" || camel == "UpdateTime" {
continue
}
if filed.IsPrimaryKey && table.PrimaryKey.AutoIncrement {
continue
}
expressions = append(expressions, "?")
expressionValues = append(expressionValues, "data."+filed.UpperCamelCase)
expressionValues = append(expressionValues, "data."+camel)
}
err = t.Execute(insertBuffer, map[string]interface{}{
"upperObject": table.UpperCamelCase,
"lowerObject": table.LowerCamelCase,
"expression": strings.Join(expressions, ", "),
"expressionValues": strings.Join(expressionValues, ", "),
"containsCache": table.ContainsCache,
})
camel := table.Name.Snake2Camel()
output, err := templatex.With("insert").
Parse(template.Insert).
Execute(map[string]interface{}{
"withCache": withCache,
"upperStartCamelObject": camel,
"lowerStartCamelObject": stringx.From(camel).LowerStart(),
"expression": strings.Join(expressions, ", "),
"expressionValues": strings.Join(expressionValues, ", "),
})
if err != nil {
return "", err
}
return insertBuffer.String(), nil
return output.String(), nil
}

View File

@@ -1,105 +1,50 @@
package gen
import (
"bytes"
"strings"
"text/template"
)
"fmt"
var (
cacheKeyExpressionTemplate = `cache{{.upperCamelTable}}{{.upperCamelField}}Prefix = "cache#{{.lowerCamelTable}}#{{.lowerCamelField}}#"`
keyTemplate = `{{.lowerCamelField}}Key := fmt.Sprintf("%s%v", {{.define}}, {{.lowerCamelField}})`
keyRespTemplate = `{{.lowerCamelField}}Key := fmt.Sprintf("%s%v", {{.define}}, resp.{{.upperCamelField}})`
keyDataTemplate = `{{.lowerCamelField}}Key := fmt.Sprintf("%s%v", {{.define}}, data.{{.upperCamelField}})`
"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
type (
// tableName:user
// {{prefix}}=cache
// key:id
Key struct {
Define string // cacheKey define,如cacheUserUserIdPrefix
Value string // cacheKey value expression,如cache#user#userId#
Expression string // cacheKey expression如:cacheUserUserIdPrefix="cache#user#userId#"
KeyVariable string // cacheKey 声明变量,如:userIdKey
Key string // 缓存key的代码,如 userIdKey:=fmt.Sprintf("%s%v", cacheUserUserIdPrefix, userId)
DataKey string // 缓存key的代码,如 userIdKey:=fmt.Sprintf("%s%v", cacheUserUserIdPrefix, data.userId)
RespKey string // 缓存key的代码,如 userIdKey:=fmt.Sprintf("%s%v", cacheUserUserIdPrefix, resp.userId)
VarExpression string // cacheUserIdPrefix="cache#user#id#"
Left string // cacheUserIdPrefix
Right string // cache#user#id#
Variable string // userIdKey
KeyExpression string // userIdKey: = fmt.Sprintf("cache#user#id#%v", userId)
DataKeyExpression string // userIdKey: = fmt.Sprintf("cache#user#id#%v", data.userId)
RespKeyExpression string // userIdKey: = fmt.Sprintf("cache#user#id#%v", resp.userId)
}
)
// key-数据库原始字段名,value-缓存key对象
func genCacheKeys(table *InnerTable) (map[string]Key, error) {
// key-数据库原始字段名,value-缓存key相关数据
func genCacheKeys(table parser.Table) (map[string]Key, error) {
fields := table.Fields
var m = make(map[string]Key)
if !table.ContainsCache {
return m, nil
}
m := make(map[string]Key)
camelTableName := table.Name.Snake2Camel()
lowerStartCamelTableName := stringx.From(camelTableName).LowerStart()
for _, field := range fields {
if !field.Cache && !field.IsPrimaryKey {
if !field.IsKey {
continue
}
t, err := template.New("keyExpression").Parse(cacheKeyExpressionTemplate)
if err != nil {
return nil, err
}
var expressionBuffer = new(bytes.Buffer)
err = t.Execute(expressionBuffer, map[string]string{
"upperCamelTable": table.UpperCamelCase,
"lowerCamelTable": table.LowerCamelCase,
"upperCamelField": field.UpperCamelCase,
"lowerCamelField": field.LowerCamelCase,
})
if err != nil {
return nil, err
}
expression := expressionBuffer.String()
expressionAr := strings.Split(expression, "=")
define := strings.TrimSpace(expressionAr[0])
value := strings.TrimSpace(expressionAr[1])
t, err = template.New("key").Parse(keyTemplate)
if err != nil {
return nil, err
}
var keyBuffer = new(bytes.Buffer)
err = t.Execute(keyBuffer, map[string]string{
"lowerCamelField": field.LowerCamelCase,
"define": define,
})
if err != nil {
return nil, err
}
t, err = template.New("keyData").Parse(keyDataTemplate)
if err != nil {
return nil, err
}
var keyDataBuffer = new(bytes.Buffer)
err = t.Execute(keyDataBuffer, map[string]string{
"lowerCamelField": field.LowerCamelCase,
"upperCamelField": field.UpperCamelCase,
"define": define,
})
if err != nil {
return nil, err
}
t, err = template.New("keyResp").Parse(keyRespTemplate)
if err != nil {
return nil, err
}
var keyRespBuffer = new(bytes.Buffer)
err = t.Execute(keyRespBuffer, map[string]string{
"lowerCamelField": field.LowerCamelCase,
"upperCamelField": field.UpperCamelCase,
"define": define,
})
if err != nil {
return nil, err
}
m[field.SnakeCase] = Key{
Define: define,
Value: value,
Expression: expression,
KeyVariable: field.LowerCamelCase + "Key",
Key: keyBuffer.String(),
DataKey: keyDataBuffer.String(),
RespKey: keyRespBuffer.String(),
camelFieldName := field.Name.Snake2Camel()
lowerStartCamelFieldName := stringx.From(camelFieldName).LowerStart()
left := fmt.Sprintf("cache%s%sPrefix", camelTableName, camelFieldName)
right := fmt.Sprintf("cache#%s#%s#", camelTableName, lowerStartCamelFieldName)
variable := fmt.Sprintf("%s%sKey", lowerStartCamelTableName, camelFieldName)
m[field.Name.Source()] = Key{
VarExpression: fmt.Sprintf(`%s = "%s"`, left, right),
Left: left,
Right: right,
Variable: variable,
KeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("%s%s", %s,%s)`, variable, "%s", "%v", left, lowerStartCamelFieldName),
DataKeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("%s%s",%s, data.%s)`, variable, "%s", "%v", left, camelFieldName),
RespKeyExpression: fmt.Sprintf(`%s := fmt.Sprintf("%s%s", %s,resp.%s)`, variable, "%s", "%v", left, camelFieldName),
}
}
return m, nil

View File

@@ -1,100 +0,0 @@
package gen
import (
"log"
"testing"
"github.com/tal-tech/go-zero/core/logx"
)
func TestKeys(t *testing.T) {
var table = OuterTable{
Table: "user_info",
CreateNotFound: true,
Fields: []*OuterFiled{
{
IsPrimaryKey: true,
Name: "user_id",
DataBaseType: "bigint",
Comment: "主键id",
},
{
Name: "campus_id",
DataBaseType: "bigint",
Comment: "整校id",
QueryType: QueryAll,
Cache: false,
},
{
Name: "name",
DataBaseType: "varchar",
Comment: "用户姓名",
QueryType: QueryOne,
},
{
Name: "id_number",
DataBaseType: "varchar",
Comment: "身份证",
Cache: false,
QueryType: QueryNone,
WithFields: []OuterWithField{
{
Name: "name",
DataBaseType: "varchar",
},
},
},
{
Name: "age",
DataBaseType: "int",
Comment: "年龄",
Cache: false,
QueryType: QueryNone,
},
{
Name: "gender",
DataBaseType: "tinyint",
Comment: "性别0-男1-女2-不限",
QueryType: QueryLimit,
WithFields: []OuterWithField{
{
Name: "campus_id",
DataBaseType: "bigint",
},
},
OuterSort: []OuterSort{
{
Field: "create_time",
Asc: false,
},
},
},
{
Name: "mobile",
DataBaseType: "varchar",
Comment: "手机号",
QueryType: QueryOne,
Cache: true,
},
{
Name: "create_time",
DataBaseType: "timestamp",
Comment: "创建时间",
},
{
Name: "update_time",
DataBaseType: "timestamp",
Comment: "更新时间",
},
},
}
innerTable, err := TableConvert(table)
if err != nil {
log.Fatalln(err)
}
tp, err := GenModel(innerTable)
if err != nil {
log.Fatalln(err)
}
logx.Info(tp)
}

View File

@@ -1,86 +0,0 @@
package gen
import (
"bytes"
"go/format"
"strings"
"text/template"
"github.com/tal-tech/go-zero/core/logx"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
)
func GenModel(table *InnerTable) (string, error) {
t, err := template.New("model").Parse(sqltemplate.Model)
if err != nil {
return "", nil
}
modelBuffer := new(bytes.Buffer)
importsCode, err := genImports(table)
if err != nil {
return "", err
}
varsCode, err := genVars(table)
if err != nil {
return "", err
}
typesCode, err := genTypes(table)
if err != nil {
return "", err
}
newCode, err := genNew(table)
if err != nil {
return "", err
}
insertCode, err := genInsert(table)
if err != nil {
return "", err
}
var findCode = make([]string, 0)
findOneCode, err := genFindOne(table)
if err != nil {
return "", err
}
findOneByFieldCode, err := genFineOneByField(table)
if err != nil {
return "", err
}
findAllCode, err := genFindAllByField(table)
if err != nil {
return "", err
}
findLimitCode, err := genFindLimitByField(table)
if err != nil {
return "", err
}
findCode = append(findCode, findOneCode, findOneByFieldCode, findAllCode, findLimitCode)
updateCode, err := genUpdate(table)
if err != nil {
return "", err
}
deleteCode, err := genDelete(table)
if err != nil {
return "", err
}
err = t.Execute(modelBuffer, map[string]interface{}{
"imports": importsCode,
"vars": varsCode,
"types": typesCode,
"new": newCode,
"insert": insertCode,
"find": strings.Join(findCode, "\r\n"),
"update": updateCode,
"delete": deleteCode,
})
if err != nil {
return "", err
}
result := modelBuffer.String()
bts, err := format.Source([]byte(result))
if err != nil {
logx.Errorf("%+v", err)
return "", err
}
return string(bts), nil
}

View File

@@ -1,24 +1,19 @@
package gen
import (
"bytes"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genNew(table *InnerTable) (string, error) {
t, err := template.New("new").Parse(sqltemplate.New)
func genNew(table Table, withCache bool) (string, error) {
output, err := templatex.With("new").
Parse(template.New).
Execute(map[string]interface{}{
"withCache": withCache,
"upperStartCamelObject": table.Name.Snake2Camel(),
})
if err != nil {
return "", err
}
newBuffer := new(bytes.Buffer)
err = t.Execute(newBuffer, map[string]interface{}{
"containsCache": table.ContainsCache,
"upperObject": table.UpperCamelCase,
})
if err != nil {
return "", err
}
return newBuffer.String(), nil
return output.String(), nil
}

View File

@@ -1,99 +0,0 @@
package gen
var (
commonMysqlDataTypeMap = map[string]string{
"tinyint": "int64",
"smallint": "int64",
"mediumint": "int64",
"int": "int64",
"integer": "int64",
"bigint": "int64",
"float": "float64",
"double": "float64",
"decimal": "float64",
"date": "time.Time",
"time": "string",
"year": "int64",
"datetime": "time.Time",
"timestamp": "time.Time",
"char": "string",
"varchar": "string",
"tinyblob": "string",
"tinytext": "string",
"blob": "string",
"text": "string",
"mediumblob": "string",
"mediumtext": "string",
"longblob": "string",
"longtext": "string",
}
)
const (
QueryNone QueryType = 0
QueryOne QueryType = 1 // 仅支持单个字段为查询条件
QueryAll QueryType = 2 // 可支持多个字段为查询条件且关系均为and
QueryLimit QueryType = 3 // 可支持多个字段为查询条件且关系均为and
)
type (
QueryType int
Case struct {
SnakeCase string
LowerCamelCase string
UpperCamelCase string
}
InnerWithField struct {
Case
DataType string
}
InnerTable struct {
Case
ContainsCache bool
CreateNotFound bool
PrimaryField *InnerField
Fields []*InnerField
CacheKey map[string]Key // key-数据库字段
}
InnerField struct {
IsPrimaryKey bool
InnerWithField
DataBaseType string // 数据库中字段类型
Tag string // 标签,格式:`db:"xxx"`
Comment string // 注释,以"// 开头"
Cache bool // 是否缓存模式
QueryType QueryType
WithFields []InnerWithField
Sort []InnerSort
}
InnerSort struct {
Field Case
Asc bool
}
OuterTable struct {
Table string `json:"table"`
CreateNotFound bool `json:"createNotFound,optional"`
Fields []*OuterFiled `json:"fields"`
}
OuterWithField struct {
Name string `json:"name"`
DataBaseType string `json:"dataBaseType"`
}
OuterSort struct {
Field string `json:"fields"`
Asc bool `json:"asc,optional"`
}
OuterFiled struct {
IsPrimaryKey bool `json:"isPrimaryKey,optional"`
Name string `json:"name"`
DataBaseType string `json:"dataBaseType"`
Comment string `json:"comment"`
Cache bool `json:"cache,optional"`
// if IsPrimaryKey==false下面字段有效
QueryType QueryType `json:"queryType"` // 查找类型
WithFields []OuterWithField `json:"withFields,optional"` // 其他字段联合组成条件的字段列表
OuterSort []OuterSort `json:"sort,optional"`
}
)

View File

@@ -0,0 +1,23 @@
package gen
import (
"regexp"
)
func (g *defaultGenerator) split() []string {
reg := regexp.MustCompile(createTableFlag)
index := reg.FindAllStringIndex(g.source, -1)
list := make([]string, 0)
source := g.source
for i := len(index) - 1; i >= 0; i-- {
subIndex := index[i]
if len(subIndex) == 0 {
continue
}
start := subIndex[0]
ddl := source[start:]
list = append(list, ddl)
source = source[:start]
}
return list
}

View File

@@ -1,26 +1,21 @@
package gen
import (
"bytes"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genTag(in string) (string, error) {
if in == "" {
return in, nil
}
t, err := template.New("tag").Parse(sqltemplate.Tag)
output, err := templatex.With("tag").
Parse(template.Tag).
Execute(map[string]interface{}{
"field": in,
})
if err != nil {
return "", err
}
var tagBuffer = new(bytes.Buffer)
err = t.Execute(tagBuffer, map[string]interface{}{
"field": in,
})
if err != nil {
return "", err
}
return tagBuffer.String(), nil
return output.String(), nil
}

View File

@@ -1,30 +1,25 @@
package gen
import (
"bytes"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genTypes(table *InnerTable) (string, error) {
func genTypes(table Table, withCache bool) (string, error) {
fields := table.Fields
t, err := template.New("types").Parse(sqltemplate.Types)
if err != nil {
return "", nil
}
var typeBuffer = new(bytes.Buffer)
fieldsString, err := genFields(fields)
if err != nil {
return "", err
}
err = t.Execute(typeBuffer, map[string]interface{}{
"upperObject": table.UpperCamelCase,
"containsCache": table.ContainsCache,
"fields": fieldsString,
})
output, err := templatex.With("types").
Parse(template.Types).
Execute(map[string]interface{}{
"withCache": withCache,
"upperStartCamelObject": table.Name.Snake2Camel(),
"fields": fieldsString,
})
if err != nil {
return "", err
}
return typeBuffer.String(), nil
return output.String(), nil
}

View File

@@ -1,38 +1,40 @@
package gen
import (
"bytes"
"strings"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genUpdate(table *InnerTable) (string, error) {
t, err := template.New("update").Parse(sqltemplate.Update)
func genUpdate(table Table, withCache bool) (string, error) {
expressionValues := make([]string, 0)
for _, filed := range table.Fields {
camel := filed.Name.Snake2Camel()
if camel == "CreateTime" || camel == "UpdateTime" {
continue
}
if filed.IsPrimaryKey {
continue
}
expressionValues = append(expressionValues, "data."+camel)
}
expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.Snake2Camel())
camelTableName := table.Name.Snake2Camel()
output, err := templatex.With("update").
Parse(template.Update).
Execute(map[string]interface{}{
"withCache": withCache,
"upperStartCamelObject": camelTableName,
"primaryCacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].DataKeyExpression,
"primaryKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
"lowerStartCamelObject": stringx.From(camelTableName).LowerStart(),
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
"expressionValues": strings.Join(expressionValues, ", "),
})
if err != nil {
return "", nil
}
updateBuffer := new(bytes.Buffer)
expressionValues := make([]string, 0)
for _, filed := range table.Fields {
if filed.SnakeCase == "create_time" || filed.SnakeCase == "update_time" || filed.IsPrimaryKey {
continue
}
expressionValues = append(expressionValues, "data."+filed.UpperCamelCase)
}
expressionValues = append(expressionValues, "data."+table.PrimaryField.UpperCamelCase)
err = t.Execute(updateBuffer, map[string]interface{}{
"containsCache": table.ContainsCache,
"upperObject": table.UpperCamelCase,
"primaryCacheKey": table.CacheKey[table.PrimaryField.SnakeCase].DataKey,
"primaryKeyVariable": table.CacheKey[table.PrimaryField.SnakeCase].KeyVariable,
"lowerObject": table.LowerCamelCase,
"primarySnakeCase": table.PrimaryField.SnakeCase,
"expressionValues": strings.Join(expressionValues, ", "),
})
if err != nil {
return "", err
}
return updateBuffer.String(), nil
return output.String(), nil
}

View File

@@ -1,36 +1,33 @@
package gen
import (
"bytes"
"strings"
"text/template"
sqltemplate "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
)
func genVars(table *InnerTable) (string, error) {
t, err := template.New("vars").Parse(sqltemplate.Vars)
if err != nil {
return "", err
}
varBuffer := new(bytes.Buffer)
m, err := genCacheKeys(table)
if err != nil {
return "", err
}
func genVars(table Table, withCache bool) (string, error) {
keys := make([]string, 0)
for _, v := range m {
keys = append(keys, v.Expression)
for _, v := range table.CacheKey {
keys = append(keys, v.VarExpression)
}
err = t.Execute(varBuffer, map[string]interface{}{
"lowerObject": table.LowerCamelCase,
"upperObject": table.UpperCamelCase,
"createNotFound": table.CreateNotFound,
"keysDefine": strings.Join(keys, "\r\n"),
"snakePrimaryKey": table.PrimaryField.SnakeCase,
})
camel := table.Name.Snake2Camel()
output, err := templatex.With("var").
Parse(template.Vars).
GoFmt(true).
Execute(map[string]interface{}{
"lowerStartCamelObject": stringx.From(camel).LowerStart(),
"upperStartCamelObject": camel,
"cacheKeys": strings.Join(keys, "\r\n"),
"autoIncrement": table.PrimaryKey.AutoIncrement,
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
"withCache": withCache,
})
if err != nil {
return "", err
}
return varBuffer.String(), nil
return output.String(), nil
}