add 'DEFAULT NULL' to point

添加默认值为null 转换成指针类型
master
谢小军 4 years ago
parent 19b39a0b41
commit 0706dd0747

@ -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"

@ -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

@ -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

@ -2,7 +2,6 @@ package cmd
import (
"os"
"strings"
"github.com/xxjwxc/public/mylog"
@ -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",
@ -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, "端口号")
}
@ -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)

@ -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 数据库信息
@ -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" {
@ -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
}

@ -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",

@ -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 {

@ -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:
@ -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 {

@ -9,7 +9,7 @@ import (
)
func TestTypeName(t *testing.T) {
fmt.Println(getTypeName("tinyint"))
fmt.Println(getTypeName("tinyint", true))
}
func TestTools(t *testing.T) {

Loading…
Cancel
Save