add: now, you can set tablePrefix

This commit is contained in:
young
2021-01-07 22:35:25 +08:00
parent 39f7512c11
commit 0c3732eb0f
8 changed files with 32 additions and 1 deletions

View File

@@ -65,6 +65,8 @@ func init() {
rootCmd.MarkFlagRequired("url tag")
rootCmd.Flags().Int("port", 3306, "端口号")
rootCmd.Flags().StringP("table_prefix", "t", "", "表前缀")
}
// initConfig reads in config file and ENV variables if set.
@@ -111,4 +113,8 @@ func MergeMysqlDbInfo() {
ig := config.GetIsGUI()
mycobra.IfReplace(rootCmd, "gui", &ig) // 如果设置了,更新
config.SetIsGUI(ig)
tablePrefix := config.GetTablePrefix()
mycobra.IfReplace(rootCmd, "tablePrefix", &tablePrefix) // 如果设置了,更新
config.SetTablePrefix(tablePrefix)
}

View File

@@ -23,6 +23,7 @@ type Config struct {
IsGUI bool `yaml:"is_gui"` //
IsTableName bool `yaml:"is_table_name"`
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
TablePrefix string `yaml:"table_prefix"` // 表前缀
}
// DBInfo mysql database information. mysql 数据库信息
@@ -205,3 +206,13 @@ func SetIsNullToPoint(b bool) {
func GetIsNullToPoint() bool {
return _map.IsNullToPoint
}
// SetTablePrefix set table prefix
func SetTablePrefix(t string) {
_map.TablePrefix = t
}
// GetTablePrefix get table prefix
func GetTablePrefix() string {
return _map.TablePrefix
}

View File

@@ -43,6 +43,7 @@ var _map = Config{
IsOutSQL: false,
IsOutFunc: true,
IsGUI: false,
TablePrefix: "",
}
var configPath string

View File

@@ -47,9 +47,19 @@ func (m *_Model) GetPackage() genstruct.GenPackage {
if m.pkg == nil {
var pkg genstruct.GenPackage
pkg.SetPackage(m.info.PackageName) //package name
tablePrefix := config.GetTablePrefix()
for _, tab := range m.info.TabList {
var sct genstruct.GenStruct
sct.SetTableName(tab.Name)
//如果设置了表前缀
if tablePrefix != "" {
tab.Name = strings.TrimLeft(tab.Name, tablePrefix)
}
sct.SetStructName(getCamelName(tab.Name)) // Big hump.大驼峰
sct.SetNotes(tab.Notes)
sct.AddElement(m.genTableElement(tab.Em)...) // build element.构造元素
@@ -92,7 +102,7 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
tmp.AddTag(_tagGorm, "unique")
case ColumnsKeyIndex: // index key.复合索引
tmp.AddTag(_tagGorm, getUninStr("index", ":", v1.KeyName))
if v1.KeyType=="FULLTEXT" {
if v1.KeyType == "FULLTEXT" {
tmp.AddTag(_tagGorm, "class:FULLTEXT")
}
case ColumnsKeyUniqueIndex: // unique index key.唯一复合索引