Gozero sqlgen patch (#119)

* merge upstream

* optimize insert logic

* reactor functions
master
Keson 4 years ago committed by GitHub
parent 7a134ec64d
commit 7e61555d42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,6 @@ CREATE TABLE `user` (
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY `name_index` (`name`), UNIQUE KEY `name_index` (`name`),
KEY `mobile_index` (`mobile`) UNIQUE KEY `mobile_index` (`mobile`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

@ -20,20 +20,14 @@ func genDelete(table Table, withCache bool) (string, error) {
} }
keyVariableSet.AddStr(key.Variable) keyVariableSet.AddStr(key.Variable)
} }
var containsIndexCache = false
for _, item := range table.Fields {
if item.IsUniqueKey {
containsIndexCache = true
break
}
}
camel := table.Name.ToCamel() camel := table.Name.ToCamel()
output, err := util.With("delete"). output, err := util.With("delete").
Parse(template.Delete). Parse(template.Delete).
Execute(map[string]interface{}{ Execute(map[string]interface{}{
"upperStartCamelObject": camel, "upperStartCamelObject": camel,
"withCache": withCache, "withCache": withCache,
"containsIndexCache": containsIndexCache, "containsIndexCache": table.ContainsUniqueKey,
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(), "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(),
"dataType": table.PrimaryKey.DataType, "dataType": table.PrimaryKey.DataType,
"keys": strings.Join(keySet.KeysStr(), "\n"), "keys": strings.Join(keySet.KeysStr(), "\n"),

@ -9,7 +9,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/util/stringx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
) )
func genFindOneByField(table Table, withCache bool) (string, error) { func genFindOneByField(table Table, withCache bool) (string, string, error) {
t := util.With("findOneByField").Parse(template.FindOneByField) t := util.With("findOneByField").Parse(template.FindOneByField)
var list []string var list []string
camelTableName := table.Name.ToCamel() camelTableName := table.Name.ToCamel()
@ -30,12 +30,23 @@ func genFindOneByField(table Table, withCache bool) (string, error) {
"lowerStartCamelField": stringx.From(camelFieldName).UnTitle(), "lowerStartCamelField": stringx.From(camelFieldName).UnTitle(),
"upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(), "upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
"originalField": field.Name.Source(), "originalField": field.Name.Source(),
"originalPrimaryField": table.PrimaryKey.Name.Source(),
}) })
if err != nil { if err != nil {
return "", err return "", "", err
} }
list = append(list, output.String()) list = append(list, output.String())
} }
return strings.Join(list, "\n"), nil if withCache {
out, err := util.With("findOneByFieldExtraMethod").Parse(template.FindOneByFieldExtraMethod).Execute(map[string]interface{}{
"upperStartCamelObject": camelTableName,
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
"originalPrimaryField": table.PrimaryKey.Name.Source(),
})
if err != nil {
return "", "", err
}
return strings.Join(list, "\n"), out.String(), nil
}
return strings.Join(list, "\n"), "", nil
} }

@ -114,6 +114,7 @@ type (
Table struct { Table struct {
parser.Table parser.Table
CacheKey map[string]Key CacheKey map[string]Key
ContainsUniqueKey bool
} }
) )
@ -135,6 +136,14 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
var table Table var table Table
table.Table = in table.Table = in
table.CacheKey = m table.CacheKey = m
var containsUniqueCache = false
for _, item := range table.Fields {
if item.IsUniqueKey {
containsUniqueCache = true
break
}
}
table.ContainsUniqueKey = containsUniqueCache
varsCode, err := genVars(table, withCache) varsCode, err := genVars(table, withCache)
if err != nil { if err != nil {
@ -162,7 +171,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
return "", err return "", err
} }
findOneByFieldCode, err := genFindOneByField(table, withCache) findOneByFieldCode, extraMethod, err := genFindOneByField(table, withCache)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -187,6 +196,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
"find": strings.Join(findCode, "\n"), "find": strings.Join(findCode, "\n"),
"update": updateCode, "update": updateCode,
"delete": deleteCode, "delete": deleteCode,
"extraMethod": extraMethod,
}) })
if err != nil { if err != nil {
return "", err return "", err

@ -3,12 +3,23 @@ package gen
import ( import (
"strings" "strings"
"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/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
) )
func genInsert(table Table, withCache bool) (string, error) { func genInsert(table Table, withCache bool) (string, error) {
keySet := collection.NewSet()
keyVariableSet := collection.NewSet()
for fieldName, key := range table.CacheKey {
if fieldName == table.PrimaryKey.Name.Source() {
continue
}
keySet.AddStr(key.DataKeyExpression)
keyVariableSet.AddStr(key.Variable)
}
expressions := make([]string, 0) expressions := make([]string, 0)
expressionValues := make([]string, 0) expressionValues := make([]string, 0)
for _, filed := range table.Fields { for _, filed := range table.Fields {
@ -27,10 +38,13 @@ func genInsert(table Table, withCache bool) (string, error) {
Parse(template.Insert). Parse(template.Insert).
Execute(map[string]interface{}{ Execute(map[string]interface{}{
"withCache": withCache, "withCache": withCache,
"containsIndexCache": table.ContainsUniqueKey,
"upperStartCamelObject": camel, "upperStartCamelObject": camel,
"lowerStartCamelObject": stringx.From(camel).UnTitle(), "lowerStartCamelObject": stringx.From(camel).UnTitle(),
"expression": strings.Join(expressions, ", "), "expression": strings.Join(expressions, ", "),
"expressionValues": strings.Join(expressionValues, ", "), "expressionValues": strings.Join(expressionValues, ", "),
"keys": strings.Join(keySet.KeysStr(), "\n"),
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
}) })
if err != nil { if err != nil {
return "", err return "", err

@ -35,18 +35,13 @@ var FindOneByField = `
func (m *{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) { func (m *{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) {
{{if .withCache}}{{.cacheKey}} {{if .withCache}}{{.cacheKey}}
var resp {{.upperStartCamelObject}} var resp {{.upperStartCamelObject}}
err := m.QueryRowIndex(&resp, {{.cacheKeyVariable}}, func(primary interface{}) string { err := m.QueryRowIndex(&resp, {{.cacheKeyVariable}}, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
return fmt.Sprintf("%s%v", {{.primaryKeyLeft}}, primary)
}, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
query := ` + "`" + `select ` + "`" + ` + {{.lowerStartCamelObject}}Rows + ` + "`" + ` from ` + "` + " + `m.table ` + " + `" + ` where {{.originalField}} = ? limit 1` + "`" + ` query := ` + "`" + `select ` + "`" + ` + {{.lowerStartCamelObject}}Rows + ` + "`" + ` from ` + "` + " + `m.table ` + " + `" + ` where {{.originalField}} = ? limit 1` + "`" + `
if err := conn.QueryRow(&resp, query, {{.lowerStartCamelField}}); err != nil { if err := conn.QueryRow(&resp, query, {{.lowerStartCamelField}}); err != nil {
return nil, err return nil, err
} }
return resp.{{.upperStartCamelPrimaryKey}}, nil return resp.{{.upperStartCamelPrimaryKey}}, nil
}, func(conn sqlx.SqlConn, v, primary interface{}) error { }, m.queryPrimary)
query := ` + "`" + `select ` + "`" + ` + {{.lowerStartCamelObject}}Rows + ` + "`" + ` from ` + "` + " + `m.table ` + " + `" + ` where {{.originalPrimaryField}} = ? limit 1` + "`" + `
return conn.QueryRow(v, query, primary)
})
switch err { switch err {
case nil: case nil:
return &resp, nil return &resp, nil
@ -68,3 +63,13 @@ func (m *{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}({{.in}}) (*{{
} }
}{{end}} }{{end}}
` `
var FindOneByFieldExtraMethod = `
func (m *{{.upperStartCamelObject}}Model) formatPrimary(primary interface{}) string {
return fmt.Sprintf("%s%v", cacheUserIdPrefix, primary)
}
func (m *{{.upperStartCamelObject}}Model) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
query := ` + "`" + `select ` + "`" + ` + {{.lowerStartCamelObject}}Rows + ` + "`" + ` from ` + "` + " + `m.table ` + " + `" + ` where {{.originalPrimaryField}} = ? limit 1` + "`" + `
return conn.QueryRow(v, query, primary)
}
`

@ -15,7 +15,6 @@ var (
) )
` `
ImportsNoCache = `import ( ImportsNoCache = `import (
"database/sql"
"strings" "strings"
{{if .time}}"time"{{end}} {{if .time}}"time"{{end}}

@ -1,8 +1,15 @@
package template package template
var Insert = ` var Insert = `
func (m *{{.upperStartCamelObject}}Model) Insert(data {{.upperStartCamelObject}}) (sql.Result, error) { func (m *{{.upperStartCamelObject}}Model) Insert(data {{.upperStartCamelObject}}) error {
{{if .withCache}}{{if .containsIndexCache}}{{.keys}}
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
query := ` + "`" + `insert into ` + "`" + ` + m.table + ` + "` (` + " + `{{.lowerStartCamelObject}}RowsExpectAutoSet` + " + `) values ({{.expression}})` " + ` query := ` + "`" + `insert into ` + "`" + ` + m.table + ` + "` (` + " + `{{.lowerStartCamelObject}}RowsExpectAutoSet` + " + `) values ({{.expression}})` " + `
return m.{{if .withCache}}ExecNoCache{{else}}conn.Exec{{end}}(query, {{.expressionValues}}) return conn.Exec(query, {{.expressionValues}})
}, {{.keyValues}}){{else}}query := ` + "`" + `insert into ` + "`" + ` + m.table + ` + "` (` + " + `{{.lowerStartCamelObject}}RowsExpectAutoSet` + " + `) values ({{.expression}})` " + `
_,err:=m.ExecNoCache(query, {{.expressionValues}})
{{end}}{{else}}query := ` + "`" + `insert into ` + "`" + ` + m.table + ` + "` (` + " + `{{.lowerStartCamelObject}}RowsExpectAutoSet` + " + `) values ({{.expression}})` " + `
_,err:=m.conn.Exec(query, {{.expressionValues}}){{end}}
return err
} }
` `

@ -9,4 +9,5 @@ var Model = `package model
{{.find}} {{.find}}
{{.update}} {{.update}}
{{.delete}} {{.delete}}
{{.extraMethod}}
` `

@ -1,6 +1,7 @@
package project package project
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -136,5 +137,5 @@ func matchModule(data []byte) (string, error) {
return strings.TrimSpace(target[index+6:]), nil return strings.TrimSpace(target[index+6:]), nil
} }
return "", nil return "", errors.New("module not matched")
} }

Loading…
Cancel
Save