2023-06-14 10:17:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-16 10:52:24 +00:00
|
|
|
"database/sql"
|
2023-06-14 10:17:45 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2023-06-16 10:52:24 +00:00
|
|
|
"log"
|
2023-06-14 10:17:45 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
2023-06-21 04:21:56 +00:00
|
|
|
"sort"
|
2023-06-16 10:52:24 +00:00
|
|
|
"strconv"
|
2023-06-14 10:17:45 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/text/cases"
|
|
|
|
"golang.org/x/text/language"
|
2023-06-16 10:52:24 +00:00
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2023-06-14 10:17:45 +00:00
|
|
|
)
|
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
var testName = "fs_auth_item"
|
2023-06-19 02:14:44 +00:00
|
|
|
var testGenDir = "model/gmodel"
|
2023-06-14 10:17:45 +00:00
|
|
|
|
|
|
|
func toPascalCase(s string) string {
|
|
|
|
words := strings.Split(s, "_")
|
|
|
|
for i, word := range words {
|
|
|
|
words[i] = cases.Title(language.English).String(strings.ToLower(word))
|
|
|
|
}
|
|
|
|
return strings.Join(words, "")
|
|
|
|
}
|
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
func GetAllTableNames(uri string) []string {
|
|
|
|
db, err := sql.Open("mysql", uri)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2023-06-15 08:08:43 +00:00
|
|
|
}
|
2023-06-16 10:52:24 +00:00
|
|
|
defer db.Close()
|
2023-06-15 08:08:43 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
rows, err := db.Query("SHOW TABLES")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2023-06-15 08:08:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
var tableNames []string
|
|
|
|
for rows.Next() {
|
|
|
|
var tableName string
|
|
|
|
if err := rows.Scan(&tableName); err != nil {
|
2023-06-15 03:05:33 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2023-06-16 10:52:24 +00:00
|
|
|
tableNames = append(tableNames, tableName)
|
2023-06-14 10:17:45 +00:00
|
|
|
}
|
2023-06-16 10:52:24 +00:00
|
|
|
|
|
|
|
return tableNames
|
2023-06-15 03:05:33 +00:00
|
|
|
}
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
// "fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest"
|
|
|
|
func GetColsFromTable(tname string, db *sql.DB) (result []Column, tableName, tableComment string) {
|
|
|
|
|
|
|
|
var a, ddl string
|
|
|
|
err := db.QueryRow("SHOW CREATE TABLE "+tname).Scan(&a, &ddl)
|
|
|
|
// log.Println(ddl)
|
2023-06-14 10:17:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-06-15 03:05:33 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
return ParserDDL(ddl)
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:29:09 +00:00
|
|
|
var gmodelVarStr = `
|
|
|
|
package gmodel
|
|
|
|
|
|
|
|
import "gorm.io/gorm"
|
|
|
|
|
2023-06-21 04:21:56 +00:00
|
|
|
// AllModelsGen 所有Model集合,修改单行,只要不改字段名,不会根据新的内容修改,需要修改的话手动删除
|
2023-06-21 03:29:09 +00:00
|
|
|
type AllModelsGen struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAllModels(gdb *gorm.DB) *AllModelsGen {
|
|
|
|
models := &AllModelsGen{
|
|
|
|
}
|
|
|
|
return models
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var gmodelVarStrFormat = `
|
|
|
|
package gmodel
|
|
|
|
|
|
|
|
import "gorm.io/gorm"
|
|
|
|
|
2023-06-21 04:21:56 +00:00
|
|
|
// AllModelsGen 所有Model集合,修改单行,只要不改字段名,不会根据新的内容修改,需要修改的话手动删除
|
2023-06-21 03:29:09 +00:00
|
|
|
type AllModelsGen struct {
|
|
|
|
%s
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAllModels(gdb *gorm.DB) *AllModelsGen {
|
|
|
|
models := &AllModelsGen{
|
|
|
|
%s
|
|
|
|
}
|
|
|
|
return models
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
type TableNameComment struct {
|
|
|
|
Name string
|
|
|
|
GoName string
|
|
|
|
Comment string
|
|
|
|
}
|
|
|
|
|
2023-06-21 04:21:56 +00:00
|
|
|
type TMCS []TableNameComment
|
|
|
|
|
|
|
|
func (u TMCS) Len() int {
|
|
|
|
return len(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u TMCS) Less(i, j int) bool {
|
|
|
|
return u[i].Name < u[j].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u TMCS) Swap(i, j int) {
|
|
|
|
u[i], u[j] = u[j], u[i]
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:29:09 +00:00
|
|
|
func GenAllModels(filedir string, tmcs ...TableNameComment) {
|
|
|
|
fileName := filedir + "/var_gen.go"
|
2023-06-21 04:21:56 +00:00
|
|
|
var dupMap map[string]TableNameComment = make(map[string]TableNameComment)
|
|
|
|
for _, tmc := range tmcs {
|
|
|
|
dupMap[tmc.Name] = tmc
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:29:09 +00:00
|
|
|
if _, err := os.Stat(fileName); err == nil {
|
2023-06-21 04:21:56 +00:00
|
|
|
log.Printf("%s exists!", fileName)
|
2023-06-21 03:29:09 +00:00
|
|
|
data, err := os.ReadFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
filestr := string(data)
|
|
|
|
filelines := strings.Split(filestr, "\n")
|
2023-06-21 04:21:56 +00:00
|
|
|
re := regexp.MustCompile(`([A-Za-z0-9_]+) [^/]+ // ([^ ]+) (.+)$`)
|
2023-06-21 03:29:09 +00:00
|
|
|
for _, line := range filelines {
|
|
|
|
result := re.FindStringSubmatch(line)
|
|
|
|
if len(result) > 0 {
|
|
|
|
// key := result[0]
|
2023-06-21 04:21:56 +00:00
|
|
|
if len(result) != 4 {
|
|
|
|
log.Println(result)
|
|
|
|
}
|
2023-06-21 03:29:09 +00:00
|
|
|
log.Println(result)
|
|
|
|
|
2023-06-21 04:21:56 +00:00
|
|
|
tmc := TableNameComment{
|
|
|
|
Name: result[2],
|
|
|
|
GoName: result[1],
|
|
|
|
Comment: result[3],
|
|
|
|
}
|
|
|
|
|
|
|
|
if newTmc, ok := dupMap[tmc.Name]; ok {
|
|
|
|
log.Printf("not change: (old)%v -> (new)%v", tmc, newTmc)
|
|
|
|
}
|
|
|
|
|
|
|
|
dupMap[tmc.Name] = tmc
|
2023-06-21 03:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 04:21:56 +00:00
|
|
|
tmcs = nil
|
|
|
|
|
|
|
|
for _, tmc := range dupMap {
|
|
|
|
tmcs = append(tmcs, tmc)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(TMCS(tmcs))
|
|
|
|
|
2023-06-21 03:29:09 +00:00
|
|
|
structStr := ""
|
|
|
|
newModelsStr := ""
|
|
|
|
for _, tmc := range tmcs {
|
2023-06-21 04:21:56 +00:00
|
|
|
fsline := fmt.Sprintf("%s *%sModel // %s %s\n", tmc.GoName, tmc.GoName, tmc.Name, tmc.Comment)
|
2023-06-21 03:29:09 +00:00
|
|
|
structStr += fsline
|
|
|
|
nmline := fmt.Sprintf("%s: New%sModel(gdb),\n", tmc.GoName, tmc.GoName)
|
|
|
|
newModelsStr += nmline
|
|
|
|
}
|
|
|
|
|
|
|
|
content := fmt.Sprintf(gmodelVarStrFormat, structStr, newModelsStr)
|
|
|
|
f, err := os.OpenFile(fileName, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(content)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if os.IsExist(err) {
|
|
|
|
f, err := os.Create(fileName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(gmodelVarStr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := exec.Command("gofmt", "-w", fileName).Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
func main() {
|
|
|
|
var mysqluri string
|
|
|
|
var name string // 需要序列化的单独文件名
|
|
|
|
var mdir string // 需要修改的序列化路径 model
|
|
|
|
|
|
|
|
flag.StringVar(&mysqluri, "uri", "fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest", "输入需要序列化的ddl文件名, 不需要后缀.ddl")
|
|
|
|
flag.StringVar(&name, "name", "", "输入需要序列化的ddl文件名, 不需要后缀.ddl")
|
|
|
|
flag.StringVar(&mdir, "mdir", "", "输入需要生成model的Go文件所在目录")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if mdir != "" {
|
|
|
|
testGenDir = mdir
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := sql.Open("mysql", mysqluri)
|
2023-06-14 10:17:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-06-16 10:52:24 +00:00
|
|
|
defer db.Close()
|
2023-06-21 03:29:09 +00:00
|
|
|
var tmcs []TableNameComment
|
2023-06-16 10:52:24 +00:00
|
|
|
if name == "-" {
|
|
|
|
tablenames := GetAllTableNames(mysqluri)
|
|
|
|
for _, testName := range tablenames {
|
|
|
|
cols, tname, tcomment := GetColsFromTable(testName, db)
|
|
|
|
GenFromPath(testGenDir, cols, tname, tcomment)
|
2023-06-21 03:29:09 +00:00
|
|
|
|
|
|
|
tmcs = append(tmcs, TableNameComment{
|
|
|
|
Name: tname,
|
|
|
|
GoName: toPascalCase(tname),
|
|
|
|
Comment: tcomment,
|
|
|
|
})
|
2023-06-16 10:52:24 +00:00
|
|
|
}
|
2023-06-21 03:29:09 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
} else {
|
|
|
|
if name != "" {
|
|
|
|
testName = name
|
|
|
|
}
|
|
|
|
|
|
|
|
// log.Println(testName)
|
|
|
|
cols, tname, tcomment := GetColsFromTable(testName, db)
|
|
|
|
GenFromPath(testGenDir, cols, tname, tcomment)
|
2023-06-21 03:29:09 +00:00
|
|
|
|
|
|
|
tmcs = append(tmcs, TableNameComment{
|
|
|
|
Name: tname,
|
|
|
|
GoName: toPascalCase(tname),
|
|
|
|
Comment: tcomment,
|
|
|
|
})
|
2023-06-14 10:17:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 03:29:09 +00:00
|
|
|
GenAllModels(testGenDir, tmcs...)
|
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
// tablenames := GetAllTableNames(mysqluri)
|
|
|
|
// log.Println(tablenames)
|
|
|
|
|
|
|
|
// name
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenFromPath(mdir string, cols []Column, tableName string, tableComment string) {
|
|
|
|
|
2023-06-15 03:05:33 +00:00
|
|
|
var importstr = "import (\"gorm.io/gorm\"\n"
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-15 03:05:33 +00:00
|
|
|
// 匹配到主键定义
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-19 02:12:58 +00:00
|
|
|
fcontent := "package gmodel\n"
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
structstr := "// %s %s\ntype %s struct {%s\n}\n"
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
pTableName := toPascalCase(tableName)
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
fieldstr := ""
|
|
|
|
for _, col := range cols {
|
|
|
|
fieldName := toPascalCase(col.Name)
|
|
|
|
typeName := typeForMysqlToGo[col.GetType()]
|
|
|
|
var defaultString string
|
|
|
|
if col.DefaultValue != nil {
|
2023-06-16 11:27:08 +00:00
|
|
|
switch typeName {
|
|
|
|
case "*int64", "*uint64", "*float64", "*bool":
|
|
|
|
defaultString = "default:" + strings.Trim(*col.DefaultValue, "'") + ";"
|
|
|
|
default:
|
|
|
|
defaultString = "default:" + *col.DefaultValue + ";"
|
|
|
|
}
|
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
switch typeName {
|
|
|
|
case "*string":
|
|
|
|
defaultString = "default:'';"
|
|
|
|
case "*time.Time":
|
|
|
|
defaultString = "default:'0000-00-00 00:00:00';"
|
|
|
|
case "*[]byte":
|
|
|
|
defaultString = "default:'';"
|
|
|
|
case "*int64", "*uint64":
|
2023-06-16 11:27:08 +00:00
|
|
|
defaultString = "default:0;"
|
2023-06-16 10:52:24 +00:00
|
|
|
case "*float64":
|
2023-06-16 11:27:08 +00:00
|
|
|
defaultString = "default: 0.0;"
|
2023-06-16 10:52:24 +00:00
|
|
|
case "*bool":
|
2023-06-16 11:27:08 +00:00
|
|
|
defaultString = "default:0;"
|
2023-06-16 10:52:24 +00:00
|
|
|
default:
|
|
|
|
fieldName = "// " + fieldName + " " + col.Type
|
2023-06-14 10:17:45 +00:00
|
|
|
}
|
2023-06-16 10:52:24 +00:00
|
|
|
}
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
if typeName == "*time.Time" {
|
|
|
|
importstr += "\"time\"\n"
|
|
|
|
}
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
if col.IndexType == "primary_key" {
|
|
|
|
typeName = typeName[1:]
|
|
|
|
}
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
tagstr := "`gorm:"
|
|
|
|
|
|
|
|
gormTag := ""
|
|
|
|
if col.IndexType != "" {
|
|
|
|
gormTag += col.IndexType + ";"
|
2023-06-14 10:17:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
gormTag += defaultString
|
2023-06-19 10:27:31 +00:00
|
|
|
|
|
|
|
if col.AutoIncrement {
|
|
|
|
gormTag += "auto_increment;"
|
|
|
|
}
|
2023-06-16 10:52:24 +00:00
|
|
|
|
|
|
|
tagstr += fmt.Sprintf("\"%s\"", gormTag)
|
|
|
|
|
|
|
|
tagstr += fmt.Sprintf(" json:\"%s\"`", col.Name)
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
fieldColStr := fmt.Sprintf("\n%s %s %s// %s", fieldName, typeName, tagstr, col.Comment)
|
|
|
|
|
|
|
|
fieldstr += fieldColStr
|
|
|
|
|
|
|
|
}
|
2023-06-14 10:17:45 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
fcontent += importstr + ")\n"
|
|
|
|
fcontent += fmt.Sprintf(structstr, tableName, tableComment, pTableName, fieldstr)
|
2023-07-20 02:13:18 +00:00
|
|
|
modelstr := fmt.Sprintf(`type %sModel struct {db *gorm.DB
|
|
|
|
name string}`, pTableName)
|
2023-06-16 10:52:24 +00:00
|
|
|
fcontent += modelstr
|
|
|
|
fcontent += "\n"
|
|
|
|
|
2023-07-20 02:13:18 +00:00
|
|
|
newfuncstr := fmt.Sprintf(`func New%sModel(db *gorm.DB) *%sModel {return &%sModel{db:db,name:"%s"}}`, pTableName, pTableName, pTableName, tableName)
|
2023-06-16 10:52:24 +00:00
|
|
|
fcontent += newfuncstr
|
|
|
|
fcontent += "\n"
|
|
|
|
|
|
|
|
genGoFileName := fmt.Sprintf("%s/%s_gen.go", mdir, tableName)
|
|
|
|
f, err := os.OpenFile(genGoFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.WriteString(fcontent)
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = exec.Command("gofmt", "-w", genGoFileName).Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-06-19 02:12:58 +00:00
|
|
|
fcontent = "package gmodel\n// TODO: 使用model的属性做你想做的"
|
2023-06-16 10:52:24 +00:00
|
|
|
genGoLogicFileName := fmt.Sprintf("%s/%s_logic.go", mdir, tableName)
|
|
|
|
|
|
|
|
// 使用 os.Stat 函数获取文件信息
|
|
|
|
_, err = os.Stat(genGoLogicFileName)
|
|
|
|
// 判断文件是否存在并输出结果
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
f2, err := os.OpenFile(genGoLogicFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
2023-06-14 10:17:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-06-16 10:52:24 +00:00
|
|
|
f2.WriteString(fcontent)
|
|
|
|
err = f2.Close()
|
2023-06-14 10:17:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-06-16 10:52:24 +00:00
|
|
|
fmt.Println(genGoLogicFileName, "create!")
|
|
|
|
} else {
|
|
|
|
fmt.Println(genGoLogicFileName, "exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type Column struct {
|
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
DefaultValue *string
|
|
|
|
Length int
|
|
|
|
Decimal int
|
|
|
|
Unsigned bool
|
|
|
|
NotNull bool
|
|
|
|
AutoIncrement bool
|
|
|
|
Comment string
|
|
|
|
|
|
|
|
IndexType string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (col *Column) GetType() string {
|
|
|
|
content := col.Type
|
|
|
|
if col.Unsigned {
|
|
|
|
return content + " unsigned"
|
|
|
|
}
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
|
|
|
var typeForMysqlToGo = map[string]string{
|
|
|
|
// 整数
|
|
|
|
"int": "*int64",
|
|
|
|
"integer": "*int64",
|
|
|
|
"tinyint": "*int64",
|
|
|
|
"smallint": "*int64",
|
|
|
|
"mediumint": "*int64",
|
|
|
|
"bigint": "*int64",
|
|
|
|
"year": "*int64",
|
|
|
|
|
2023-06-16 11:51:41 +00:00
|
|
|
"int unsigned": "*int64",
|
|
|
|
"integer unsigned": "*int64",
|
|
|
|
"tinyint unsigned": "*int64",
|
|
|
|
"smallint unsigned": "*int64",
|
|
|
|
"mediumint unsigned": "*int64",
|
|
|
|
"bigint unsigned": "*int64",
|
|
|
|
"bit": "*int64",
|
2023-06-16 10:52:24 +00:00
|
|
|
|
|
|
|
// 布尔类型
|
|
|
|
"bool": "*bool",
|
|
|
|
|
|
|
|
// 字符串
|
|
|
|
"enum": "*string",
|
|
|
|
"set": "*string",
|
|
|
|
"varchar": "*string",
|
|
|
|
"char": "*string",
|
|
|
|
"tinytext": "*string",
|
|
|
|
"mediumtext": "*string",
|
|
|
|
"text": "*string",
|
|
|
|
"longtext": "*string",
|
|
|
|
|
|
|
|
// 二进制
|
|
|
|
"binary": "*[]byte",
|
|
|
|
"varbinary": "*[]byte",
|
|
|
|
"blob": "*[]byte",
|
|
|
|
"tinyblob": "*[]byte",
|
|
|
|
"mediumblob": "*[]byte",
|
|
|
|
"longblob": "*[]byte",
|
|
|
|
|
|
|
|
// 日期时间
|
|
|
|
"date": "*time.Time",
|
|
|
|
"datetime": "*time.Time",
|
|
|
|
"timestamp": "*time.Time",
|
|
|
|
"time": "*time.Time",
|
|
|
|
|
|
|
|
// 浮点数
|
|
|
|
"float": "*float64",
|
|
|
|
"double": "*float64",
|
|
|
|
"decimal": "*float64",
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParserDDL(ddl string) (result []Column, tableName, tableComment string) {
|
|
|
|
|
|
|
|
reTable := regexp.MustCompile(`CREATE TABLE +([^ ]+) +\(`)
|
|
|
|
reTableComment := regexp.MustCompile(`.+COMMENT='(.+)'$`)
|
|
|
|
reField := regexp.MustCompile("`([^`]+)` +([^ \n\\(\\,]+)(?:\\(([^)]+)\\))?( +unsigned| +UNSIGNED)?( +not +null| +NOT +NULL)?( +default +\\'[^\\']*'| +DEFAULT +\\'[^\\']*')?( +auto_increment| +AUTO_INCREMENT)?( comment '[^']*'| COMMENT '[^']*')?(,)?")
|
|
|
|
reIndex := regexp.MustCompile(`(?i)(PRIMARY|UNIQUE)?\s*(INDEX|KEY)\s*(` + "`([^`]*)`" + `)?\s*\(([^)]+)\)`)
|
|
|
|
reValue := regexp.MustCompile(` '(.+)'$`)
|
|
|
|
reDefaultValue := regexp.MustCompile(` ('.+')$`)
|
|
|
|
|
|
|
|
var fieldmap map[string]string = make(map[string]string)
|
|
|
|
indexMatches := reIndex.FindAllStringSubmatch(ddl, -1)
|
|
|
|
for _, m := range indexMatches {
|
|
|
|
idxAttr := strings.Trim(m[5], "`")
|
|
|
|
PrefixName := strings.ToUpper(m[1])
|
|
|
|
if PrefixName == "PRIMARY" {
|
|
|
|
fieldmap[idxAttr] = "primary_key"
|
|
|
|
} else if PrefixName == "UNIQUE" {
|
|
|
|
fieldmap[idxAttr] = "unique_key"
|
|
|
|
} else if PrefixName == "" {
|
|
|
|
fieldmap[idxAttr] = "index"
|
|
|
|
} else {
|
|
|
|
log.Fatal(PrefixName)
|
2023-06-14 10:17:45 +00:00
|
|
|
}
|
2023-06-16 10:52:24 +00:00
|
|
|
}
|
2023-06-15 08:08:43 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
tableMatches := reTable.FindStringSubmatch(ddl)
|
|
|
|
tableName = strings.Trim(tableMatches[1], "`")
|
2023-06-15 08:17:42 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
tableCommentMatches := reTableComment.FindStringSubmatch(ddl)
|
|
|
|
if len(tableCommentMatches) > 0 {
|
|
|
|
tableComment = strings.Trim(tableCommentMatches[1], "`")
|
|
|
|
}
|
|
|
|
|
|
|
|
// log.Println(tableName, tableComment)
|
|
|
|
fieldMatches := reField.FindAllStringSubmatch(ddl, -1)
|
|
|
|
for _, m := range fieldMatches {
|
|
|
|
if m[0] == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
col := Column{
|
|
|
|
Name: m[1],
|
|
|
|
Type: strings.ToLower(m[2]),
|
|
|
|
}
|
|
|
|
|
|
|
|
col.IndexType = fieldmap[col.Name]
|
|
|
|
|
|
|
|
if m[3] != "" {
|
|
|
|
maylen := strings.Split(m[3], ",")
|
|
|
|
if len(maylen) >= 1 {
|
|
|
|
clen, err := strconv.ParseInt(maylen[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
col.Length = int(clen)
|
2023-06-15 08:17:42 +00:00
|
|
|
}
|
2023-06-16 10:52:24 +00:00
|
|
|
if len(maylen) >= 2 {
|
|
|
|
clen, err := strconv.ParseInt(maylen[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
col.Decimal = int(clen)
|
2023-06-15 08:17:42 +00:00
|
|
|
}
|
2023-06-15 08:08:43 +00:00
|
|
|
}
|
2023-06-15 08:17:42 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
if len(m[4]) > 0 {
|
|
|
|
col.Unsigned = true
|
|
|
|
}
|
2023-06-15 08:08:43 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
if len(m[5]) > 0 {
|
|
|
|
col.NotNull = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m[6]) > 0 {
|
|
|
|
v := reDefaultValue.FindStringSubmatch(m[6])
|
|
|
|
if len(v) > 0 {
|
|
|
|
dv := string(v[1])
|
|
|
|
col.DefaultValue = &dv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m[7]) > 0 {
|
|
|
|
col.AutoIncrement = true
|
|
|
|
}
|
2023-06-15 08:08:43 +00:00
|
|
|
|
2023-06-16 10:52:24 +00:00
|
|
|
if len(m[8]) > 0 {
|
|
|
|
v := reValue.FindStringSubmatch(m[8])
|
|
|
|
if len(v) > 0 {
|
|
|
|
col.Comment = v[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = append(result, col)
|
|
|
|
// fmt.Println(col)
|
|
|
|
}
|
|
|
|
return result, tableName, tableComment
|
2023-06-15 08:08:43 +00:00
|
|
|
}
|