2021-10-25 18:07:12 +08:00
|
|
|
// Package sql2go...
|
|
|
|
//
|
|
|
|
// Description : sql2go...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2021-10-25 4:49 下午
|
|
|
|
package sql2go
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-25 20:38:21 +08:00
|
|
|
"strings"
|
2021-10-25 18:07:12 +08:00
|
|
|
|
|
|
|
"github.com/xwb1989/sqlparser"
|
|
|
|
)
|
|
|
|
|
2021-10-25 20:38:21 +08:00
|
|
|
// ParseCreateTableSql 解析建表sql
|
2021-10-25 18:07:12 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 4:49 下午 2021/10/25
|
2021-10-25 20:38:21 +08:00
|
|
|
func ParseCreateTableSql(sql string) {
|
2021-10-25 18:07:12 +08:00
|
|
|
var (
|
|
|
|
stmt sqlparser.Statement
|
|
|
|
err error
|
|
|
|
)
|
2021-10-25 20:38:21 +08:00
|
|
|
sql = strings.ReplaceAll(sql, " ", "")
|
|
|
|
if stmt, err = sqlparser.ParseStrictDDL(sql); nil != err {
|
|
|
|
fmt.Println(err.Error(), string(sql[786:]))
|
2021-10-25 18:07:12 +08:00
|
|
|
return
|
|
|
|
}
|
2021-10-25 20:38:21 +08:00
|
|
|
|
|
|
|
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)
|
2021-10-25 18:07:12 +08:00
|
|
|
}
|