Skip to content

Commit

Permalink
feat[db]: allow exclude tables
Browse files Browse the repository at this point in the history
Signed-off-by: rogerogers <[email protected]>
  • Loading branch information
rogerogers committed Nov 3, 2023
1 parent 3480534 commit 62a080b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ output/*
# Vscode files
.vscode

# mac
.DS_Store
3 changes: 3 additions & 0 deletions cmd/dynamic/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ func parsePass(da *config.ModelArgument, pass string) error {
f.BoolVar(&da.FieldWithIndexTag, consts.IndexTag, false, "")
var tables utils.FlagStringSlice
f.Var(&tables, consts.Tables, "")
var excludeTables utils.FlagStringSlice
f.Var(&excludeTables, consts.ExcludeTables, "")
if err := f.Parse(utils.StringSliceSpilt([]string{pass})); err != nil {
return err
}
da.Tables = tables
da.ExcludeTables = excludeTables
return nil
}
1 change: 1 addition & 0 deletions cmd/static/model_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func modelFlags() []cli.Flag {
&cli.StringFlag{Name: consts.OutDir, Usage: "Specify output directory", Value: consts.DefaultDbOutDir, DefaultText: consts.DefaultDbOutDir},
&cli.StringFlag{Name: consts.OutFile, Usage: "Specify output filename", Value: consts.DefaultDbOutFile, DefaultText: consts.DefaultDbOutFile},
&cli.StringSliceFlag{Name: consts.Tables, Usage: "Specify databases tables"},
&cli.StringSliceFlag{Name: consts.ExcludeTables, Usage: "Specify exclude tables"},
&cli.BoolFlag{Name: consts.UnitTest, Usage: "Specify generate unit test", Value: false, DefaultText: "false"},
&cli.BoolFlag{Name: consts.OnlyModel, Usage: "Specify only generate model code", Value: false, DefaultText: "false"},
&cli.StringFlag{Name: consts.ModelPkgName, Usage: "Specify model package name", Value: "", DefaultText: ""},
Expand Down
2 changes: 2 additions & 0 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ModelArgument struct {
DSN string
Type string
Tables []string
ExcludeTables []string
OnlyModel bool
OutPath string
OutFile string
Expand All @@ -49,6 +50,7 @@ func (c *ModelArgument) ParseCli(ctx *cli.Context) error {
c.DSN = ctx.String(consts.DSN)
c.Type = strings.ToLower(ctx.String(consts.DBType))
c.Tables = ctx.StringSlice(consts.Tables)
c.ExcludeTables = ctx.StringSlice(consts.ExcludeTables)
c.OnlyModel = ctx.Bool(consts.OnlyModel)
c.OutPath = ctx.String(consts.OutDir)
c.OutFile = ctx.String(consts.OutFile)
Expand Down
25 changes: 13 additions & 12 deletions pkg/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,17 @@ const (
Pass = "pass"
ProtoSearchPath = "proto_search_path"

DSN = "dsn"
DBType = "db_type"
Tables = "tables"
OnlyModel = "only_model"
OutFile = "out_file"
UnitTest = "unittest"
ModelPkgName = "model_pkg"
Nullable = "nullable"
Signable = "signable"
IndexTag = "index_tag"
TypeTag = "type_tag"
HexTag = "hex"
DSN = "dsn"
DBType = "db_type"
Tables = "tables"
ExcludeTables = "exclude_tables"
OnlyModel = "only_model"
OutFile = "out_file"
UnitTest = "unittest"
ModelPkgName = "model_pkg"
Nullable = "nullable"
Signable = "signable"
IndexTag = "index_tag"
TypeTag = "type_tag"
HexTag = "hex"
)
26 changes: 24 additions & 2 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package model

import (
"fmt"
"strings"

"github.com/cloudwego/cwgo/config"
"github.com/cloudwego/cwgo/pkg/consts"
Expand All @@ -33,15 +34,36 @@ func Model(c *config.ModelArgument) error {
return err
}

g := gen.NewGenerator(gen.Config{
genConfig := gen.Config{
OutPath: c.OutPath,
OutFile: c.OutFile,
ModelPkgPath: c.ModelPkgName,
WithUnitTest: c.WithUnitTest,
FieldNullable: c.FieldNullable,
FieldSignable: c.FieldSignable,
FieldWithIndexTag: c.FieldWithIndexTag,
})
}
fmt.Println(c.ExcludeTables)

if len(c.ExcludeTables) > 0 || c.Type == string(consts.Sqlite) {
genConfig.WithTableNameStrategy(func(tableName string) (targetTableName string) {
if c.Type == string(consts.Sqlite) {
if strings.HasPrefix(tableName, "sqlite") {
return ""
}
}
if len(c.ExcludeTables) > 0 {
for _, table := range c.ExcludeTables {
if tableName == table {
return ""
}
}
}
return tableName
})
}

g := gen.NewGenerator(genConfig)

g.UseDB(db)

Expand Down

0 comments on commit 62a080b

Please sign in to comment.