-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
.idea | ||
mall.sql | ||
sqling.png | ||
*.puml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |