From 3d90296c6a959a96d01d0342bbc0b5dbb98154f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 25 Oct 2021 20:38:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E8=AF=95=E5=AE=8C=E6=88=90=E8=A7=A3?= =?UTF-8?q?=E6=9E=90create=20sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/sql2go/parser.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tool/sql2go/parser.go b/tool/sql2go/parser.go index adfbb5d..d5e7f36 100644 --- a/tool/sql2go/parser.go +++ b/tool/sql2go/parser.go @@ -9,22 +9,35 @@ package sql2go import ( "fmt" + "strings" "github.com/xwb1989/sqlparser" ) -// ParseSql 解析sql +// ParseCreateTableSql 解析建表sql // // Author : go_developer@163.com<白茶清欢> // // Date : 4:49 下午 2021/10/25 -func ParseSql(sql string) { +func ParseCreateTableSql(sql string) { var ( stmt sqlparser.Statement err error ) - if stmt, err = sqlparser.Parse(sql); nil != err { + sql = strings.ReplaceAll(sql, " ", "") + if stmt, err = sqlparser.ParseStrictDDL(sql); nil != err { + fmt.Println(err.Error(), string(sql[786:])) return } - fmt.Println(stmt) + + r := stmt.(*sqlparser.DDL) + for _, item := range r.TableSpec.Columns { + defaultV := item.Type.Default + defaultVal := "-" + if nil != defaultV { + defaultVal += string(defaultV.Val) + "-" + } + fmt.Println(item.Name.String(), defaultVal, item.Type.NotNull, string(item.Type.Comment.Val)) + } + // fmt.Println(r.TableSpec) }