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:
@@ -26,10 +26,11 @@ type (
|
||||
defaultGenerator struct {
|
||||
console.Console
|
||||
// source string
|
||||
dir string
|
||||
pkg string
|
||||
cfg *config.Config
|
||||
isPostgreSql bool
|
||||
dir string
|
||||
pkg string
|
||||
cfg *config.Config
|
||||
isPostgreSql bool
|
||||
ignoreColumns []string
|
||||
}
|
||||
|
||||
// Option defines a function with argument defaultGenerator
|
||||
@@ -82,14 +83,21 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul
|
||||
return generator, nil
|
||||
}
|
||||
|
||||
// WithConsoleOption creates a console option
|
||||
// WithConsoleOption creates a console option.
|
||||
func WithConsoleOption(c console.Console) Option {
|
||||
return func(generator *defaultGenerator) {
|
||||
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 {
|
||||
return func(generator *defaultGenerator) {
|
||||
generator.isPostgreSql = true
|
||||
@@ -235,6 +243,16 @@ type Table struct {
|
||||
PrimaryCacheKey Key
|
||||
UniqueCacheKey []Key
|
||||
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) {
|
||||
@@ -249,6 +267,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
table.PrimaryCacheKey = primaryKey
|
||||
table.UniqueCacheKey = uniqueKey
|
||||
table.ContainsUniqueCacheKey = len(uniqueKey) > 0
|
||||
table.ignoreColumns = g.ignoreColumns
|
||||
|
||||
importsCode, err := genImports(table, withCache, in.ContainsTime())
|
||||
if err != nil {
|
||||
|
||||
@@ -31,7 +31,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
var count int
|
||||
for _, field := range table.Fields {
|
||||
camel := util.SafeString(field.Name.ToCamel())
|
||||
if camel == "CreateTime" || camel == "UpdateTime" || camel == "CreateAt" || camel == "UpdateAt" {
|
||||
if table.isIgnoreColumns(field.Name.Source()) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (
|
||||
}
|
||||
for _, field := range table.Fields {
|
||||
camel := util.SafeString(field.Name.ToCamel())
|
||||
if camel == "CreateTime" || camel == "UpdateTime" || camel == "CreateAt" || camel == "UpdateAt" {
|
||||
if table.isIgnoreColumns(field.Name.Source()) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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/util"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
@@ -32,6 +34,17 @@ func genVars(table Table, withCache, postgreSql bool) (string, error) {
|
||||
"withCache": withCache,
|
||||
"postgreSql": postgreSql,
|
||||
"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 {
|
||||
return "", err
|
||||
|
||||
Reference in New Issue
Block a user