From 3e27119ceb86dfb32f35b860e3e18e6c7b369381 Mon Sep 17 00:00:00 2001 From: jiang4869 <1121429190@qq.com> Date: Sat, 5 Mar 2022 21:21:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8A=8Adefault=20null?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=AD=97=E6=AE=B5=E8=AE=BE=E7=BD=AE=E4=B8=BA?= =?UTF-8?q?sql.NullXXX=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yml | 1 + data/config/MyIni.go | 11 ++++++++++- data/view/model/common.go | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/config.yml b/config.yml index 2b293c5..21fe6f6 100644 --- a/config.yml +++ b/config.yml @@ -16,6 +16,7 @@ is_gui : false # 是否ui模式显示 is_table_name : true # 是否直接生成表名 is_column_name : true # 是否直接生成列名 is_null_to_point : false # 数据库默认 'DEFAULT NULL' 时设置结构为指针类型 +is_null_to_sql_null: false # 数据库默认 'DEFAULT NULL' 时设置结构为sql.NULL is_null_to_point如果为true,则is_null_to_sql_null不生效 table_prefix : "" # 表前缀, 如果有则使用, 没有留空(如果表前缀以"-"开头,则表示去掉该前缀,struct、文件名都会去掉该前缀) table_names: "" # 指定表生成,多个表用,隔开 is_out_file_by_table_name: false # 是否根据表名生成多个model diff --git a/data/config/MyIni.go b/data/config/MyIni.go index a53b6db..869fdef 100644 --- a/data/config/MyIni.go +++ b/data/config/MyIni.go @@ -25,7 +25,8 @@ 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"` // 表前缀 + IsNullToSqlNull bool `yaml:"is_null_to_sql_null"` + TablePrefix string `yaml:"table_prefix"` // 表前缀 SelfTypeDef map[string]string `yaml:"self_type_define"` OutFileName string `yaml:"out_file_name"` WebTagType int `yaml:"web_tag_type"` // 默认小驼峰 @@ -226,6 +227,14 @@ func GetIsNullToPoint() bool { return _map.IsNullToPoint } +func SetIsNullToSqlNull(b bool) { + _map.IsNullToSqlNull = b +} + +func GetIsNullToSqlNull() bool { + return _map.IsNullToSqlNull +} + // SetTablePrefix set table prefix func SetTablePrefix(t string) { _map.TablePrefix = t diff --git a/data/view/model/common.go b/data/view/model/common.go index 7467953..5a16c30 100755 --- a/data/view/model/common.go +++ b/data/view/model/common.go @@ -113,6 +113,30 @@ func fixNullToPorint(name string, isNull bool) string { return "*" + name } } + if isNull && config.GetIsNullToSqlNull() { + + if strings.HasPrefix(name, "uint") { + return "sql.NullInt64" + } + if strings.HasPrefix(name, "int") { + return "sql.NullInt32" + } + if strings.HasPrefix(name, "float") { + return "sql.NullFloat64" + } + if strings.HasPrefix(name, "date") { + return "sql.NullTime" + } + if strings.HasPrefix(name, "time") { + return "sql.NullTime" + } + if strings.HasPrefix(name, "bool") { + return "sql.NullBool" + } + if strings.HasPrefix(name, "string") { + return "sql.NullString" + } + } return name }