From 6517252bc8b07611a4e0212590fa79454b74c11e Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Sat, 27 Feb 2021 16:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=BD=AC=20=E7=BB=93=E6=9E=84=E4=BD=93=E5=92=8Cmarkdown?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E7=9A=84=E5=B8=AE=E5=8A=A9=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/openapi.go | 21 +++++++++++++++ main.go | 1 + test/utils_test.go | 21 +++++++++++++++ utils/helper.go | 63 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 test/utils_test.go create mode 100644 utils/helper.go diff --git a/controller/openapi.go b/controller/openapi.go index 2e31b94..0a11ad0 100644 --- a/controller/openapi.go +++ b/controller/openapi.go @@ -35,3 +35,24 @@ func (this *OpenApiController) OndiffCallback(c *gin.Context) { resp.Msg = "OK" } + +type ReqDDL struct { + Data string `json:"data"` +} + +func (this *OpenApiController) DDL2ORM(c *gin.Context) { + var req ReqDDL + var resp RespBase + defer func() { + c.JSON(200, resp) + }() + + e := c.Bind(&req) + if e != nil { + log.Println(e.Error()) + resp.Msg = "ParaErr" + return + } + + resp.Msg = "OK" +} diff --git a/main.go b/main.go index e34f8f6..01aa195 100644 --- a/main.go +++ b/main.go @@ -187,6 +187,7 @@ func main() { openapi := r.Group("openapi") { openapi.POST("/diff") + openapi.POST("/ddl2orm") } e := r.Run(":" + strconv.Itoa(config.GetPort())) if nil != e { diff --git a/test/utils_test.go b/test/utils_test.go new file mode 100644 index 0000000..a9ee096 --- /dev/null +++ b/test/utils_test.go @@ -0,0 +1,21 @@ +package test + +import ( + "background/utils" + "log" + "testing" +) + +func TestDDL2ORM(t *testing.T) { + ddl := ` + CREATE TABLE project ( + id int(13) NOT NULL AUTO_INCREMENT, + title varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, + content longblob, + contact_info varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, + PRIMARY KEY (id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci + ` + tbname, fields := utils.DDL2Field(ddl) + log.Print(tbname, fields) +} diff --git a/utils/helper.go b/utils/helper.go new file mode 100644 index 0000000..343cf73 --- /dev/null +++ b/utils/helper.go @@ -0,0 +1,63 @@ +package utils + +import ( + "log" + "strings" +) + +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, " ") + + return ret +} +func DDL2Field(ddl string) (string, []FieldDesc) { + if "" == ddl { + return "", nil + } + ret := make([]FieldDesc, 0) + indexStart := strings.Index(ddl, "CREATE TABLE") + indexFirstParentheses := strings.Index(ddl, "(") + indexLastParentheses := strings.LastIndex(ddl, ")") + + sTbNameUnTrim := ddl[indexStart+len("CREATE TABLE") : indexFirstParentheses] + sTableName := strings.Trim(sTbNameUnTrim, " ") + + sTbDesc := ddl[indexFirstParentheses+1 : indexLastParentheses] + fields := strings.Split(sTbDesc, ",") + log.Print(fields) + primarykey := []string{} + for _, v := range fields { + if strings.Contains(v, "PRIMARY KEY") { + start := strings.Index(v, "(") + end := strings.LastIndex(v, ")") + primarykey = append(primarykey, v[start+1:end]) + } else { + tmp := strings.Split(v, " ") + if len(tmp) > 1 { + ret = append(ret, FieldDesc{ + FiledName: strings.Trim(tmp[0], "\n\x09\x09"), + Type: strings.Trim(tmp[1], " "), + Primary: false, + }) + } + } + } + for _, key := range primarykey { + for k := range ret { + if 0 == strings.Compare(ret[k].FiledName, key) { + ret[k].Primary = true + } + } + } + return sTableName, ret +}