From 1cc4d13a2b8f7d74b6d022edf5448ef9cf55f572 Mon Sep 17 00:00:00 2001 From: fifsky Date: Thu, 19 Mar 2020 10:26:19 +0800 Subject: [PATCH] fix isNUll and add decimal float double tinyint --- README.md | 4 ++++ generator/util.go | 28 +++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c2b0470..f593692 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,10 @@ genstruct -h localhost -u root -p 3306 -P 123456 * `-u` default `root` * `-p` default `3306` +## online + +https://go.fifsky.com/ + ## gosql The structure can be applied to a [gosql](https://github.com/ilibs/gosql) package diff --git a/generator/util.go b/generator/util.go index 65bca8f..fe92af4 100644 --- a/generator/util.go +++ b/generator/util.go @@ -55,19 +55,41 @@ func GetParams(cmds []string, i int) (string, error) { return strings.TrimSpace(cmds[i]), nil } -func typeFormat(t string) string { - if t == "datetime" || t == "date" { +func typeFormat(t string, isNull string) string { + if t == "datetime" || t == "date" || t == "time" { + if isNull == "YES" { + return "sql.NullTime" + } return "time.Time" } if len(t) >= 6 && t[0:6] == "bigint" { + if isNull == "YES" { + return "sql.NullInt64" + } return "int64" } - if strings.Index(t, "int") != -1 { + if strings.Index(t, "int") != -1 || strings.Index(t, "tinyint") != -1 { + if isNull == "YES" { + return "sql.NullInt64" + } + return "int" } + if strings.Index(t, "decimal") != -1 || strings.Index(t, "float") != -1 || strings.Index(t, "double") != -1 { + if isNull == "YES" { + return "sql.NullFloat64" + } + + return "float64" + } + + if isNull == "YES" { + return "sql.NullString" + } + return "string" }