Skip to content

Commit

Permalink
patch model&rpc (#207)
Browse files Browse the repository at this point in the history
* change column to read from information_schema

* reactor generate mode from datasource

* reactor generate mode from datasource

* add primary key check logic

* resolve rebase conflicts

* add naming style

* add filename test case

* resolve rebase conflicts

* reactor test

* add test case

* change shell script to makefile

* update rpc new

* update gen_test.go

* format code

* format code

* update test

* generates alias
  • Loading branch information
Keson authored Nov 18, 2020
1 parent 71083b5 commit 24fb29a
Show file tree
Hide file tree
Showing 55 changed files with 678 additions and 1,167 deletions.
10 changes: 9 additions & 1 deletion tools/goctl/goctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ var (
Name: "new",
Usage: `generate rpc demo service`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "style",
Usage: "the file naming style, lower|camel|snake,default is lower",
},
cli.BoolFlag{
Name: "idea",
Usage: "whether the command execution environment is from idea plugin. [optional]",
Expand Down Expand Up @@ -235,6 +239,10 @@ var (
Name: "dir, d",
Usage: `the target path of the code`,
},
cli.StringFlag{
Name: "style",
Usage: "the file naming style, lower|camel|snake,default is lower",
},
cli.BoolFlag{
Name: "idea",
Usage: "whether the command execution environment is from idea plugin. [optional]",
Expand Down Expand Up @@ -266,7 +274,7 @@ var (
},
cli.StringFlag{
Name: "style",
Usage: "the file naming style, lower|camel|underline,default is lower",
Usage: "the file naming style, lower|camel|snake,default is lower",
},
cli.BoolFlag{
Name: "cache, c",
Expand Down
27 changes: 0 additions & 27 deletions tools/goctl/model/sql/builderx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,3 @@ func FieldNames(in interface{}) []string {
}
return out
}
func FieldNamesAlias(in interface{}, alias string) []string {
out := make([]string, 0)
v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
// we only accept structs
if v.Kind() != reflect.Struct {
panic(fmt.Errorf("ToMap only accepts structs; got %T", v))
}
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
// gets us a StructField
fi := typ.Field(i)
tagName := ""
if tagv := fi.Tag.Get(dbTag); tagv != "" {
tagName = tagv
} else {
tagName = fi.Name
}
if len(alias) > 0 {
tagName = alias + "." + tagName
}
out = append(out, tagName)
}
return out
}
64 changes: 38 additions & 26 deletions tools/goctl/model/sql/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/urfave/cli"
)

var errNotMatched = errors.New("sql not matched")

const (
flagSrc = "src"
flagDir = "dir"
Expand All @@ -33,6 +35,20 @@ func MysqlDDL(ctx *cli.Context) error {
cache := ctx.Bool(flagCache)
idea := ctx.Bool(flagIdea)
namingStyle := strings.TrimSpace(ctx.String(flagStyle))
return fromDDl(src, dir, namingStyle, cache, idea)
}

func MyDataSource(ctx *cli.Context) error {
url := strings.TrimSpace(ctx.String(flagUrl))
dir := strings.TrimSpace(ctx.String(flagDir))
cache := ctx.Bool(flagCache)
idea := ctx.Bool(flagIdea)
namingStyle := strings.TrimSpace(ctx.String(flagStyle))
pattern := strings.TrimSpace(ctx.String(flagTable))
return fromDataSource(url, pattern, dir, namingStyle, cache, idea)
}

func fromDDl(src, dir, namingStyle string, cache, idea bool) error {
log := console.NewConsole(idea)
src = strings.TrimSpace(src)
if len(src) == 0 {
Expand All @@ -52,29 +68,29 @@ func MysqlDDL(ctx *cli.Context) error {
return err
}

if len(files) == 0 {
return errNotMatched
}

var source []string
for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}

source = append(source, string(data))
}
generator := gen.NewDefaultGenerator(strings.Join(source, "\n"), dir, namingStyle, gen.WithConsoleOption(log))
err = generator.Start(cache)
generator, err := gen.NewDefaultGenerator(dir, namingStyle, gen.WithConsoleOption(log))
if err != nil {
log.Error("%v", err)
return err
}
return nil

err = generator.StartFromDDL(strings.Join(source, "\n"), cache)
return err
}

func MyDataSource(ctx *cli.Context) error {
url := strings.TrimSpace(ctx.String(flagUrl))
dir := strings.TrimSpace(ctx.String(flagDir))
cache := ctx.Bool(flagCache)
idea := ctx.Bool(flagIdea)
namingStyle := strings.TrimSpace(ctx.String(flagStyle))
pattern := strings.TrimSpace(ctx.String(flagTable))
func fromDataSource(url, pattern, dir, namingStyle string, cache, idea bool) error {
log := console.NewConsole(idea)
if len(url) == 0 {
log.Error("%v", "expected data source of mysql, but nothing found")
Expand All @@ -100,18 +116,16 @@ func MyDataSource(ctx *cli.Context) error {
}

logx.Disable()
conn := sqlx.NewMysql(url)
databaseSource := strings.TrimSuffix(url, "/"+cfg.DBName) + "/information_schema"
db := sqlx.NewMysql(databaseSource)
m := model.NewDDLModel(conn)
im := model.NewInformationSchemaModel(db)

tables, err := im.GetAllTables(cfg.DBName)
if err != nil {
return err
}

var matchTables []string
matchTables := make(map[string][]*model.Column)
for _, item := range tables {
match, err := filepath.Match(pattern, item)
if err != nil {
Expand All @@ -121,24 +135,22 @@ func MyDataSource(ctx *cli.Context) error {
if !match {
continue
}

matchTables = append(matchTables, item)
columns, err := im.FindByTableName(cfg.DBName, item)
if err != nil {
return err
}
matchTables[item] = columns
}

if len(matchTables) == 0 {
return errors.New("no tables matched")
}

ddl, err := m.ShowDDL(matchTables...)
if err != nil {
log.Error("%v", err)
return nil
}

generator := gen.NewDefaultGenerator(strings.Join(ddl, "\n"), dir, namingStyle, gen.WithConsoleOption(log))
err = generator.Start(cache)
generator, err := gen.NewDefaultGenerator(dir, namingStyle, gen.WithConsoleOption(log))
if err != nil {
log.Error("%v", err)
return err
}

return nil
err = generator.StartFromInformationSchema(cfg.DBName, matchTables, cache)
return err
}
75 changes: 75 additions & 0 deletions tools/goctl/model/sql/command/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package command

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
"github.com/tal-tech/go-zero/tools/goctl/util"
)

var sql = "-- 用户表 --\nCREATE TABLE `user` (\n `id` bigint(10) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',\n `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',\n `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',\n `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',\n `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `name_index` (`name`),\n UNIQUE KEY `mobile_index` (`mobile`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;\n\n"

func TestFromDDl(t *testing.T) {
err := fromDDl("./user.sql", t.TempDir(), gen.NamingCamel, true, false)
assert.Equal(t, errNotMatched, err)

// case dir is not exists
unknownDir := filepath.Join(t.TempDir(), "test", "user.sql")
err = fromDDl(unknownDir, t.TempDir(), gen.NamingCamel, true, false)
assert.True(t, func() bool {
switch err.(type) {
case *os.PathError:
return true
default:
return false
}
}())

// case empty src
err = fromDDl("", t.TempDir(), gen.NamingCamel, true, false)
if err != nil {
assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error())
}

// case unknown naming style
tmp := filepath.Join(t.TempDir(), "user.sql")
err = fromDDl(tmp, t.TempDir(), "lower1", true, false)
if err != nil {
assert.Equal(t, "unexpected naming style: lower1", err.Error())
}

tempDir := filepath.Join(t.TempDir(), "test")
err = util.MkdirIfNotExist(tempDir)
if err != nil {
return
}

user1Sql := filepath.Join(tempDir, "user1.sql")
user2Sql := filepath.Join(tempDir, "user2.sql")

err = ioutil.WriteFile(user1Sql, []byte(sql), os.ModePerm)
if err != nil {
return
}

err = ioutil.WriteFile(user2Sql, []byte(sql), os.ModePerm)
if err != nil {
return
}

_, err = os.Stat(user1Sql)
assert.Nil(t, err)

_, err = os.Stat(user2Sql)
assert.Nil(t, err)

err = fromDDl(filepath.Join(tempDir, "user*.sql"), tempDir, gen.NamingLower, true, false)
assert.Nil(t, err)

_, err = os.Stat(filepath.Join(tempDir, "usermodel.go"))
assert.Nil(t, err)
}
11 changes: 0 additions & 11 deletions tools/goctl/model/sql/example/generator.sh

This file was deleted.

15 changes: 15 additions & 0 deletions tools/goctl/model/sql/example/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# generate model with cache from ddl
fromDDL:
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/user" -c


# generate model with cache from data source
user=root
password=password
datasource=127.0.0.1:3306
database=gozero

fromDataSource:
goctl model mysql datasource -url="$(user):$(password)@tcp($(datasource))/$(database)" -table="*" -dir ./model/cache -c -style camel
1 change: 1 addition & 0 deletions tools/goctl/model/sql/gen/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ func genDelete(table Table, withCache bool) (string, error) {
if err != nil {
return "", err
}

return output.String(), nil
}
2 changes: 2 additions & 0 deletions tools/goctl/model/sql/gen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func genFields(fields []parser.Field) (string, error) {
if err != nil {
return "", err
}

list = append(list, result)
}
return strings.Join(list, "\n"), nil
Expand Down Expand Up @@ -43,5 +44,6 @@ func genField(field parser.Field) (string, error) {
if err != nil {
return "", err
}

return output.String(), nil
}
1 change: 1 addition & 0 deletions tools/goctl/model/sql/gen/findone.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ func genFindOne(table Table, withCache bool) (string, error) {
if err != nil {
return "", err
}

return output.String(), nil
}
Loading

0 comments on commit 24fb29a

Please sign in to comment.