From 214b3203e7d3b1f8313091326791596845d59fab Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Sun, 28 Feb 2021 01:45:34 +0800 Subject: [PATCH] =?UTF-8?q?sql=E8=BD=ACgolang=20sructcode=20=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/openapi.go | 11 +++++- main.go | 5 ++- test/utils_test.go | 2 + utils/helper.go | 85 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 90 insertions(+), 13 deletions(-) diff --git a/controller/openapi.go b/controller/openapi.go index 0a11ad0..c4a463b 100644 --- a/controller/openapi.go +++ b/controller/openapi.go @@ -1,6 +1,7 @@ package controller import ( + "background/utils" "log" "github.com/gin-gonic/gin" @@ -42,7 +43,9 @@ type ReqDDL struct { func (this *OpenApiController) DDL2ORM(c *gin.Context) { var req ReqDDL - var resp RespBase + resp := RespBase{ + Msg: "err", + } defer func() { c.JSON(200, resp) }() @@ -53,6 +56,10 @@ func (this *OpenApiController) DDL2ORM(c *gin.Context) { resp.Msg = "ParaErr" return } - + tbname, fields := utils.DDL2Field(req.Data) + log.Print(tbname, fields) + gocode := utils.CreateGoStruct(tbname,fields) + resp.Data = gocode resp.Msg = "OK" + resp.Status = 0 } diff --git a/main.go b/main.go index 01aa195..6d149a3 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,7 @@ var ( mailContoller = controller.MailController{} fileController = controller.FileController{} planController = controller.PlanController{} + openapiController = controller.OpenApiController{} ) func CORSMiddleware(c *gin.Context) { @@ -36,7 +37,7 @@ func CORSMiddleware(c *gin.Context) { c.Writer.Header().Set("Access-Control-Allow-Origin", "https://testingcloud.club") } else { - c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080") + c.Writer.Header().Set("Access-Control-Allow-Origin", "*") } c.Writer.Header().Set("Access-Control-Max-Age", "86400") c.Writer.Header().Set("Access-Control-Allow-Headers", @@ -187,7 +188,7 @@ func main() { openapi := r.Group("openapi") { openapi.POST("/diff") - openapi.POST("/ddl2orm") + openapi.POST("/ddl2orm",openapiController.DDL2ORM) } e := r.Run(":" + strconv.Itoa(config.GetPort())) if nil != e { diff --git a/test/utils_test.go b/test/utils_test.go index a9ee096..2ed19e1 100644 --- a/test/utils_test.go +++ b/test/utils_test.go @@ -18,4 +18,6 @@ func TestDDL2ORM(t *testing.T) { ` tbname, fields := utils.DDL2Field(ddl) log.Print(tbname, fields) + gocode := utils.CreateGoStruct(tbname,fields) + log.Print(gocode) } diff --git a/utils/helper.go b/utils/helper.go index 343cf73..2b8eb13 100644 --- a/utils/helper.go +++ b/utils/helper.go @@ -1,30 +1,94 @@ package utils import ( + "fmt" "log" "strings" ) +// 蛇形转驼峰 +func toCamelString(s string) string { + data := make([]byte, 0, len(s)) + j := false + k := false + num := len(s) - 1 + for i := 0; i <= num; i++ { + d := s[i] + if k == false && d >= 'A' && d <= 'Z' { + k = true + } + if d >= 'a' && d <= 'z' && (j || k == false) { + d = d - 32 + j = false + k = true + } + if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' { + j = true + continue + } + data = append(data, d) + } + return string(data[:]) +} type FieldDesc struct { FiledName string `json:"field_name"` Type string `json:"type"` Primary bool `json:"primary"` } -func TrimUnWanted(in string) string { - ret := "" - ret = strings.Trim(in, "\t") - ret = strings.Trim(in, "\n") - ret = strings.Trim(in, "\x09") - ret = strings.Trim(in, " ") +func DDLType2Golang(types string)string{ + if strings.Contains(types,"int"){ + return "int32" + } + if strings.Contains(types,"varchar"){ + return "string" + } + if strings.Contains(types,"longblob"){ + return "[]byte" + } + if strings.Contains(types,"date"){ + return "time.Time" + } + return "interface" +} +func CreateGoStruct(name string,doc []FieldDesc) string{ + if name == "" { + return "" + } + if len(doc) == 0{ + return "" + } + ret := fmt.Sprintf("type %s struct{\r\n",toCamelString(name)) + for _,v := range doc{ + filed := "\t\t" + camelField := toCamelString(v.FiledName) + filed += camelField + tag := fmt.Sprintf("`json:\"%s\" gorm:\"column:%s\" sql:\"%s\"`",v.FiledName,v.FiledName,v.FiledName) + filed += " " + DDLType2Golang(v.Type) + " " + tag + " \r\n" + ret += filed + } + ret += "}" return ret } + +func TrimUnWanted(in string) string { + ret := strings.ReplaceAll(in, "\t","") + ret = strings.ReplaceAll(ret, "\n","") + ret = strings.ReplaceAll(ret, "\x09","") + ret = strings.ReplaceAll(ret, "\r","") + ret = strings.ReplaceAll(ret, "\x0a","") + ret = strings.TrimLeft(ret," ") + return ret +} + func DDL2Field(ddl string) (string, []FieldDesc) { if "" == ddl { return "", nil } ret := make([]FieldDesc, 0) + ddl = strings.ReplaceAll(ddl,"`","") + log.Print(ddl) indexStart := strings.Index(ddl, "CREATE TABLE") indexFirstParentheses := strings.Index(ddl, "(") indexLastParentheses := strings.LastIndex(ddl, ")") @@ -34,7 +98,7 @@ func DDL2Field(ddl string) (string, []FieldDesc) { sTbDesc := ddl[indexFirstParentheses+1 : indexLastParentheses] fields := strings.Split(sTbDesc, ",") - log.Print(fields) + primarykey := []string{} for _, v := range fields { if strings.Contains(v, "PRIMARY KEY") { @@ -42,11 +106,14 @@ func DDL2Field(ddl string) (string, []FieldDesc) { end := strings.LastIndex(v, ")") primarykey = append(primarykey, v[start+1:end]) } else { + v = TrimUnWanted(v) + log.Print(v) tmp := strings.Split(v, " ") if len(tmp) > 1 { + log.Print(tmp[0]) ret = append(ret, FieldDesc{ - FiledName: strings.Trim(tmp[0], "\n\x09\x09"), - Type: strings.Trim(tmp[1], " "), + FiledName: TrimUnWanted(tmp[0]), + Type: strings.Trim(tmp[1], " "), Primary: false, }) }