feat(goctl):Add ignore-columns flag (#2407)
* fix #2074,#2100 * format code * fix #2397 * format code * Support comma spliter * format code
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// BuildVersion is the version of goctl.
|
// BuildVersion is the version of goctl.
|
||||||
const BuildVersion = "1.4.1"
|
const BuildVersion = "1.4.2"
|
||||||
|
|
||||||
var tag = map[string]int{"pre-alpha": 0, "alpha": 1, "pre-bata": 2, "beta": 3, "released": 4, "": 5}
|
var tag = map[string]int{"pre-alpha": 0, "alpha": 1, "pre-bata": 2, "beta": 3, "released": 4, "": 5}
|
||||||
|
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ func init() {
|
|||||||
mongoCmd.Flags().StringVar(&mongo.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
|
mongoCmd.Flags().StringVar(&mongo.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
|
||||||
|
|
||||||
mysqlCmd.PersistentFlags().BoolVar(&command.VarBoolStrict, "strict", false, "Generate model in strict mode")
|
mysqlCmd.PersistentFlags().BoolVar(&command.VarBoolStrict, "strict", false, "Generate model in strict mode")
|
||||||
|
mysqlCmd.PersistentFlags().StringSliceVarP(&command.VarStringSliceIgnoreColumns, "ignore-columns", "i", []string{"create_at", "created_at", "create_time", "update_at", "updated_at", "update_time"}, "Ignore columns while creating or updating rows")
|
||||||
|
|
||||||
mysqlCmd.AddCommand(datasourceCmd)
|
mysqlCmd.AddCommand(datasourceCmd)
|
||||||
mysqlCmd.AddCommand(ddlCmd)
|
mysqlCmd.AddCommand(ddlCmd)
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ import (
|
|||||||
|
|
||||||
"github.com/go-sql-driver/mysql"
|
"github.com/go-sql-driver/mysql"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/collection"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"github.com/zeromicro/go-zero/core/stores/postgres"
|
"github.com/zeromicro/go-zero/core/stores/postgres"
|
||||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/config"
|
"github.com/zeromicro/go-zero/tools/goctl/config"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/command/migrationnotes"
|
"github.com/zeromicro/go-zero/tools/goctl/model/sql/command/migrationnotes"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/gen"
|
"github.com/zeromicro/go-zero/tools/goctl/model/sql/gen"
|
||||||
@@ -50,6 +51,8 @@ var (
|
|||||||
VarStringBranch string
|
VarStringBranch string
|
||||||
// VarBoolStrict describes whether the strict mode is enabled.
|
// VarBoolStrict describes whether the strict mode is enabled.
|
||||||
VarBoolStrict bool
|
VarBoolStrict bool
|
||||||
|
// VarStringSliceIgnoreColumns represents the columns which are ignored.
|
||||||
|
VarStringSliceIgnoreColumns []string
|
||||||
)
|
)
|
||||||
|
|
||||||
var errNotMatched = errors.New("sql not matched")
|
var errNotMatched = errors.New("sql not matched")
|
||||||
@@ -81,13 +84,14 @@ func MysqlDDL(_ *cobra.Command, _ []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
arg := ddlArg{
|
arg := ddlArg{
|
||||||
src: src,
|
src: src,
|
||||||
dir: dir,
|
dir: dir,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
cache: cache,
|
cache: cache,
|
||||||
idea: idea,
|
idea: idea,
|
||||||
database: database,
|
database: database,
|
||||||
strict: VarBoolStrict,
|
strict: VarBoolStrict,
|
||||||
|
ignoreColumns: mergeColumns(VarStringSliceIgnoreColumns),
|
||||||
}
|
}
|
||||||
return fromDDL(arg)
|
return fromDDL(arg)
|
||||||
}
|
}
|
||||||
@@ -121,17 +125,29 @@ func MySqlDataSource(_ *cobra.Command, _ []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
arg := dataSourceArg{
|
arg := dataSourceArg{
|
||||||
url: url,
|
url: url,
|
||||||
dir: dir,
|
dir: dir,
|
||||||
tablePat: patterns,
|
tablePat: patterns,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
cache: cache,
|
cache: cache,
|
||||||
idea: idea,
|
idea: idea,
|
||||||
strict: VarBoolStrict,
|
strict: VarBoolStrict,
|
||||||
|
ignoreColumns: mergeColumns(VarStringSliceIgnoreColumns),
|
||||||
}
|
}
|
||||||
return fromMysqlDataSource(arg)
|
return fromMysqlDataSource(arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mergeColumns(columns []string) []string {
|
||||||
|
set := collection.NewSet()
|
||||||
|
for _, v := range columns {
|
||||||
|
fields := strings.FieldsFunc(v, func(r rune) bool {
|
||||||
|
return r == ','
|
||||||
|
})
|
||||||
|
set.AddStr(fields...)
|
||||||
|
}
|
||||||
|
return set.KeysStr()
|
||||||
|
}
|
||||||
|
|
||||||
type pattern map[string]struct{}
|
type pattern map[string]struct{}
|
||||||
|
|
||||||
func (p pattern) Match(s string) bool {
|
func (p pattern) Match(s string) bool {
|
||||||
@@ -205,11 +221,12 @@ func PostgreSqlDataSource(_ *cobra.Command, _ []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ddlArg struct {
|
type ddlArg struct {
|
||||||
src, dir string
|
src, dir string
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
cache, idea bool
|
cache, idea bool
|
||||||
database string
|
database string
|
||||||
strict bool
|
strict bool
|
||||||
|
ignoreColumns []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func fromDDL(arg ddlArg) error {
|
func fromDDL(arg ddlArg) error {
|
||||||
@@ -228,7 +245,8 @@ func fromDDL(arg ddlArg) error {
|
|||||||
return errNotMatched
|
return errNotMatched
|
||||||
}
|
}
|
||||||
|
|
||||||
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg, gen.WithConsoleOption(log))
|
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg,
|
||||||
|
gen.WithConsoleOption(log), gen.WithIgnoreColumns(arg.ignoreColumns))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -244,11 +262,12 @@ func fromDDL(arg ddlArg) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type dataSourceArg struct {
|
type dataSourceArg struct {
|
||||||
url, dir string
|
url, dir string
|
||||||
tablePat pattern
|
tablePat pattern
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
cache, idea bool
|
cache, idea bool
|
||||||
strict bool
|
strict bool
|
||||||
|
ignoreColumns []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func fromMysqlDataSource(arg dataSourceArg) error {
|
func fromMysqlDataSource(arg dataSourceArg) error {
|
||||||
@@ -301,7 +320,8 @@ func fromMysqlDataSource(arg dataSourceArg) error {
|
|||||||
return errors.New("no tables matched")
|
return errors.New("no tables matched")
|
||||||
}
|
}
|
||||||
|
|
||||||
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg, gen.WithConsoleOption(log))
|
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg,
|
||||||
|
gen.WithConsoleOption(log), gen.WithIgnoreColumns(arg.ignoreColumns))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ fromDDLWithCache:
|
|||||||
goctl template clean
|
goctl template clean
|
||||||
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache" -cache
|
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache" -cache
|
||||||
|
|
||||||
|
fromDDLWithCacheAndIgnoreColumns:
|
||||||
|
goctl template clean
|
||||||
|
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/ignore_columns/cache" -cache -i 'gmt_create,create_at' -i 'gmt_modified,update_at'
|
||||||
|
|
||||||
fromDDLWithCacheAndDb:
|
fromDDLWithCacheAndDb:
|
||||||
goctl template clean
|
goctl template clean
|
||||||
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache_db" -database="1gozero" -cache
|
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache_db" -database="1gozero" -cache
|
||||||
|
|||||||
@@ -26,10 +26,11 @@ type (
|
|||||||
defaultGenerator struct {
|
defaultGenerator struct {
|
||||||
console.Console
|
console.Console
|
||||||
// source string
|
// source string
|
||||||
dir string
|
dir string
|
||||||
pkg string
|
pkg string
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
isPostgreSql bool
|
isPostgreSql bool
|
||||||
|
ignoreColumns []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option defines a function with argument defaultGenerator
|
// Option defines a function with argument defaultGenerator
|
||||||
@@ -82,14 +83,21 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul
|
|||||||
return generator, nil
|
return generator, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithConsoleOption creates a console option
|
// WithConsoleOption creates a console option.
|
||||||
func WithConsoleOption(c console.Console) Option {
|
func WithConsoleOption(c console.Console) Option {
|
||||||
return func(generator *defaultGenerator) {
|
return func(generator *defaultGenerator) {
|
||||||
generator.Console = c
|
generator.Console = c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithPostgreSql marks defaultGenerator.isPostgreSql true
|
// WithIgnoreColumns ignores the columns while insert or update rows.
|
||||||
|
func WithIgnoreColumns(ignoreColumns []string) Option {
|
||||||
|
return func(generator *defaultGenerator) {
|
||||||
|
generator.ignoreColumns = ignoreColumns
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithPostgreSql marks defaultGenerator.isPostgreSql true.
|
||||||
func WithPostgreSql() Option {
|
func WithPostgreSql() Option {
|
||||||
return func(generator *defaultGenerator) {
|
return func(generator *defaultGenerator) {
|
||||||
generator.isPostgreSql = true
|
generator.isPostgreSql = true
|
||||||
@@ -235,6 +243,16 @@ type Table struct {
|
|||||||
PrimaryCacheKey Key
|
PrimaryCacheKey Key
|
||||||
UniqueCacheKey []Key
|
UniqueCacheKey []Key
|
||||||
ContainsUniqueCacheKey bool
|
ContainsUniqueCacheKey bool
|
||||||
|
ignoreColumns []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Table) isIgnoreColumns(columnName string) bool {
|
||||||
|
for _, v := range t.ignoreColumns {
|
||||||
|
if v == columnName {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, error) {
|
func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, error) {
|
||||||
@@ -249,6 +267,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
|||||||
table.PrimaryCacheKey = primaryKey
|
table.PrimaryCacheKey = primaryKey
|
||||||
table.UniqueCacheKey = uniqueKey
|
table.UniqueCacheKey = uniqueKey
|
||||||
table.ContainsUniqueCacheKey = len(uniqueKey) > 0
|
table.ContainsUniqueCacheKey = len(uniqueKey) > 0
|
||||||
|
table.ignoreColumns = g.ignoreColumns
|
||||||
|
|
||||||
importsCode, err := genImports(table, withCache, in.ContainsTime())
|
importsCode, err := genImports(table, withCache, in.ContainsTime())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
|
|||||||
var count int
|
var count int
|
||||||
for _, field := range table.Fields {
|
for _, field := range table.Fields {
|
||||||
camel := util.SafeString(field.Name.ToCamel())
|
camel := util.SafeString(field.Name.ToCamel())
|
||||||
if camel == "CreateTime" || camel == "UpdateTime" || camel == "CreateAt" || camel == "UpdateAt" {
|
if table.isIgnoreColumns(field.Name.Source()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (
|
|||||||
}
|
}
|
||||||
for _, field := range table.Fields {
|
for _, field := range table.Fields {
|
||||||
camel := util.SafeString(field.Name.ToCamel())
|
camel := util.SafeString(field.Name.ToCamel())
|
||||||
if camel == "CreateTime" || camel == "UpdateTime" || camel == "CreateAt" || camel == "UpdateAt" {
|
if table.isIgnoreColumns(field.Name.Source()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package gen
|
package gen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/collection"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/template"
|
"github.com/zeromicro/go-zero/tools/goctl/model/sql/template"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||||
@@ -32,6 +34,17 @@ func genVars(table Table, withCache, postgreSql bool) (string, error) {
|
|||||||
"withCache": withCache,
|
"withCache": withCache,
|
||||||
"postgreSql": postgreSql,
|
"postgreSql": postgreSql,
|
||||||
"data": table,
|
"data": table,
|
||||||
|
"ignoreColumns": func() string {
|
||||||
|
var set = collection.NewSet()
|
||||||
|
for _, c := range table.ignoreColumns {
|
||||||
|
if postgreSql {
|
||||||
|
set.AddStr(fmt.Sprintf(`"%s"`, c))
|
||||||
|
} else {
|
||||||
|
set.AddStr(fmt.Sprintf("\"`%s`\"", c))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strings.Join(set.KeysStr(), ", ")
|
||||||
|
}(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
@@ -1,20 +1,7 @@
|
|||||||
package template
|
package template
|
||||||
|
|
||||||
import "fmt"
|
import _ "embed"
|
||||||
|
|
||||||
// Vars defines a template for var block in model
|
// Vars defines a template for var block in model
|
||||||
var Vars = fmt.Sprintf(
|
//go:embed vars.tpl
|
||||||
`
|
var Vars string
|
||||||
var (
|
|
||||||
{{.lowerStartCamelObject}}FieldNames = builder.RawFieldNames(&{{.upperStartCamelObject}}{}{{if .postgreSql}},true{{end}})
|
|
||||||
{{.lowerStartCamelObject}}Rows = strings.Join({{.lowerStartCamelObject}}FieldNames, ",")
|
|
||||||
{{.lowerStartCamelObject}}RowsExpectAutoSet = {{if .postgreSql}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s"), ","){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s"), ","){{end}}
|
|
||||||
{{.lowerStartCamelObject}}RowsWithPlaceHolder = {{if .postgreSql}}builder.PostgreSqlJoin(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s")){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s"), "=?,") + "=?"{{end}}
|
|
||||||
|
|
||||||
{{if .withCache}}{{.cacheKeys}}{{end}}
|
|
||||||
)
|
|
||||||
`, "", "", "", "", "", "", "", "", // postgreSql mode
|
|
||||||
"`", "`", "`", "`", "`", "`", "`", "`",
|
|
||||||
"", "", "", "", "", "", "", "", // postgreSql mode
|
|
||||||
"`", "`", "`", "`", "`", "`", "`", "`",
|
|
||||||
)
|
|
||||||
|
|||||||
8
tools/goctl/model/sql/template/vars.tpl
Normal file
8
tools/goctl/model/sql/template/vars.tpl
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
var (
|
||||||
|
{{.lowerStartCamelObject}}FieldNames = builder.RawFieldNames(&{{.upperStartCamelObject}}{}{{if .postgreSql}}, true{{end}})
|
||||||
|
{{.lowerStartCamelObject}}Rows = strings.Join({{.lowerStartCamelObject}}FieldNames, ",")
|
||||||
|
{{.lowerStartCamelObject}}RowsExpectAutoSet = {{if .postgreSql}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}", {{end}} {{.ignoreColumns}}), ","){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}", {{end}} {{.ignoreColumns}}), ","){{end}}
|
||||||
|
{{.lowerStartCamelObject}}RowsWithPlaceHolder = {{if .postgreSql}}builder.PostgreSqlJoin(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", {{.ignoreColumns}})){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", {{.ignoreColumns}}), "=?,") + "=?"{{end}}
|
||||||
|
|
||||||
|
{{if .withCache}}{{.cacheKeys}}{{end}}
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user