Skip to content

Commit

Permalink
feat: make it works
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jun 11, 2021
1 parent 829070a commit 0b31eee
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.idea
mall.sql
sqling.png
*.puml
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ Todo:

- [ ] import sql parser
- [ ] align coco struct
- [ ] output json
- [ ] to json
- [ ] output
- [ ] json
- [ ] puml
- [ ] mermaid

License
---
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/inherd/sqling

go 1.16

require github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 // indirect
require (
github.com/iancoleman/strcase v0.1.3
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/iancoleman/strcase v0.1.3 h1:dJBk1m2/qjL1twPLf68JND55vvivMupZ4wIzE8CTdBw=
github.com/iancoleman/strcase v0.1.3/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ=
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2/go.mod h1:hzfGeIUDq/j97IG+FhNqkowIyEcD88LrW6fyU3K3WqY=
92 changes: 87 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,121 @@
package main

import (
"bufio"
"fmt"
"github.com/iancoleman/strcase"
"github.com/xwb1989/sqlparser"
"io/ioutil"
"os"
)

type CocoStruct struct {
name string
fields []string
fields []CocoField
}

type CocoField struct {
name string
fType string
}

func mysqlTypeToJava(typ string) string {
switch typ {
case "bit":
return "Boolean"
case "byte":
return " Byte"
case "short":
return " Short"
case "int":
return "Integer"
case "smallint", "tinyint":
return "Integer"
case "bigint":
return "Long"
case "float":
return "Float"
case "double":
return "Double"
case "decimal", "numeric":
return "BigDecimal"
case "date":
return "Date"
case "datetime", "timestamp":
return "Timestamp"
case "time":
return "Time"
case "year":
return "Short"
case "varchar", "char", "text":
return "String"
}
return typ
}

func check(e error) {
if e != nil {
panic(e)
}
}

func main() {
sql := "CREATE TABLE `cms_help` (\n `id` bigint(20) NOT NULL AUTO_INCREMENT,\n `category_id` bigint(20) DEFAULT NULL,\n `icon` varchar(500) DEFAULT NULL,\n `title` varchar(100) DEFAULT NULL,\n `show_status` int(1) DEFAULT NULL,\n `create_time` datetime DEFAULT NULL,\n `read_count` int(1) DEFAULT NULL,\n `content` text,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='帮助表';\n"
dat, err := ioutil.ReadFile("mall.sql")
check(err)
sql := string(dat)

stmt, err := sqlparser.Parse(sql)
if err != nil {
// Do something with the err
fmt.Println(err)
}

// Otherwise do something with stmt
var structs []CocoStruct
switch stmt := stmt.(type) {
case *sqlparser.DDL:
switch stmt.Action {
case "create":
var fields []string
var fields []CocoField
for _, column := range stmt.TableSpec.Columns {
fields = append(fields, column.Name.String())
fields = append(fields, CocoField{
name: column.Name.String(),
fType: mysqlTypeToJava(column.Type.Type),
})
}

cocoStruct := CocoStruct{
name: stmt.NewName.Name.String(),
fields: fields,
}

fmt.Println(cocoStruct)
structs = append(structs, cocoStruct)
}
}

writePuml(structs)
}

func writePuml(structs []CocoStruct) {
f, err := os.Create("sqling.puml")
check(err)
defer f.Close()
w := bufio.NewWriter(f)

_, err = fmt.Fprintln(w, "@startuml")
check(err)

for _, cocoStruct := range structs {
_, err = fmt.Fprintln(w, "class "+strcase.ToCamel(cocoStruct.name)+" {")

for _, field := range cocoStruct.fields {
_, err = fmt.Fprintln(w, " - "+strcase.ToCamel(field.name)+": "+field.fType)
}

_, err = fmt.Fprintln(w, "}")
}

_, err = fmt.Fprintln(w, "@enduml")

w.Flush()
}

0 comments on commit 0b31eee

Please sign in to comment.