Skip to content

Commit

Permalink
add 'DEFAULT NULL' to point
Browse files Browse the repository at this point in the history
添加默认值为null 转换成指针类型
  • Loading branch information
xxjwxc committed Jun 23, 2020
1 parent 19b39a0 commit 0706dd0
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ is_url_tag : true # Whether to mark web or not
is_foreign_key : true # Whether to mark foreign key or not
is_gui : false # Whether to operate on gui
is_table_name : false # Whether to out GetTableName function
is_null_to_point : false # database is 'DEFAULT NULL' then set element type as point
mysql_info :
host : "127.0.0.1"
Expand Down
1 change: 1 addition & 0 deletions README_zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ is_url_tag : true # 是否打web标记
is_foreign_key : true # 是否导出外键关联
is_gui : false # 是否ui模式显示
is_table_name : false # 是否直接生成表名函数
is_null_to_point : false # 数据库默认 'DEFAULT NULL' 时设置结构为指针类型
mysql_info:
host : 127.0.0.1
port : 3306
Expand Down
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ is_web_tag_pk_hidden: true # web标记是否隐藏主键
is_foreign_key : true # 是否导出外键关联
is_gui : false # 是否ui模式显示
is_table_name : false # 是否直接生成表名函数
is_null_to_point : false # 数据库默认 'DEFAULT NULL' 时设置结构为指针类型
mysql_info:
host : 127.0.0.1
port : 3306
Expand Down
10 changes: 0 additions & 10 deletions data/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"os"
"strings"

"github.com/xxjwxc/public/mylog"

Expand All @@ -21,7 +20,6 @@ var foreignKey bool
var funcKey bool
var ui bool
var urlTag string
var outFileName string

var rootCmd = &cobra.Command{
Use: "main",
Expand Down Expand Up @@ -73,8 +71,6 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&urlTag, "url", "l", "", "url标签(json,url)")
rootCmd.MarkFlagRequired("url tag")

rootCmd.Flags().StringVar(&outFileName, "outfilename", "", "输出文件名,默认以数据库名称命名")

rootCmd.Flags().IntVar(&mysqlInfo.Port, "port", 3306, "端口号")
}

Expand Down Expand Up @@ -114,12 +110,6 @@ func MergeMysqlDbInfo() {
if len(urlTag) > 0 {
config.SetURLTag(urlTag)
}
if len(outFileName) > 0 {
if !strings.HasSuffix(outFileName, ".go") {
outFileName += ".go"
}
config.SetOutFileName(outFileName)
}

config.SetMysqlDbInfo(&tmp)

Expand Down
20 changes: 11 additions & 9 deletions data/config/MyIni.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Config struct {
IsOutFunc bool `yaml:"is_out_func"`
IsGUI bool `yaml:"is_gui"` //
IsTableName bool `yaml:"is_table_name"`
OutFileName string `yaml:"-"`
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
}

// MysqlDbInfo mysql database information. mysql 数据库信息
Expand Down Expand Up @@ -150,14 +150,6 @@ func SetIsTableName(b bool) {
_map.IsTableName = b
}

func SetOutFileName(f string) {
_map.OutFileName = f
}

func GetOutFileName() string {
return _map.OutFileName
}

// GetURLTag get url tag.
func GetURLTag() string {
if _map.URLTag != "json" && _map.URLTag != "url" {
Expand Down Expand Up @@ -203,3 +195,13 @@ func GetDBTag() string {
func SetDBTag(s string) {
_map.DbTag = s
}

// SetIsNullToPoint if with null to porint in struct
func SetIsNullToPoint(b bool) {
_map.IsNullToPoint = b
}

// GetIsNullToPoint get if with null to porint in sturct
func GetIsNullToPoint() bool {
return _map.IsNullToPoint
}
4 changes: 2 additions & 2 deletions data/view/cnf/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ var TypeMysqlDicMp = map[string]string{
"bit(1)": "[]uint8",
"tinyint": "int8",
"tinyint unsigned": "uint8",
"tinyint(1)": "bool",
"tinyint(1) unsigned": "bool",
"tinyint(1)": "bool", // tinyint(1) 默认设置成bool
"tinyint(1) unsigned": "bool", // tinyint(1) 默认设置成bool
"json": "string",
"text": "string",
"timestamp": "time.Time",
Expand Down
23 changes: 20 additions & 3 deletions data/view/model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,39 @@ func FilterKeywords(src string) string {
}

// getTypeName Type acquisition filtering.类型获取过滤
func getTypeName(name string) string {
func getTypeName(name string, isNull bool) string {
// Precise matching first.先精确匹配
if v, ok := cnf.TypeMysqlDicMp[name]; ok {
return v
return fixNullToPorint(v, isNull)
}

// Fuzzy Regular Matching.模糊正则匹配
for k, v := range cnf.TypeMysqlMatchMp {
if ok, _ := regexp.MatchString(k, name); ok {
return v
return fixNullToPorint(v, isNull)
}
}

panic(fmt.Sprintf("type (%v) not match in any way.maybe need to add on (https://github.com/xxjwxc/gormt/blob/master/data/view/cnf/def.go)", name))
}

// 过滤null point 类型
func fixNullToPorint(name string, isNull bool) string {
if isNull && config.GetIsNullToPoint() {
if strings.HasPrefix(name, "uint") {
return "*" + name
}
if strings.HasPrefix(name, "int") {
return "*" + name
}
if strings.HasPrefix(name, "float") {
return "*" + name
}
}

return name
}

func getUninStr(left, middle, right string) string {
re := left
if len(right) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions data/view/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
} else {
tmp.SetName(getCamelName(v.Name))
tmp.SetNotes(v.Notes)
tmp.SetType(getTypeName(v.Type))
tmp.SetType(getTypeName(v.Type, v.IsNull))
for _, v1 := range v.Index {
switch v1.Key {
// case ColumnsKeyDefault:
Expand Down Expand Up @@ -243,7 +243,7 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
pkg.AddImport(`"time"`)
buildFList(&primary, ColumnsKeyPrimary, "", "int64", "id")
} else {
typeName := getTypeName(el.Type)
typeName := getTypeName(el.Type, el.IsNull)
isMulti := (len(el.Index) == 0)
for _, v1 := range el.Index {
if v1.Multi {
Expand Down
2 changes: 1 addition & 1 deletion data/view/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestTypeName(t *testing.T) {
fmt.Println(getTypeName("tinyint"))
fmt.Println(getTypeName("tinyint", true))
}

func TestTools(t *testing.T) {
Expand Down

0 comments on commit 0706dd0

Please sign in to comment.