-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_plantuml.go
149 lines (125 loc) · 2.86 KB
/
create_plantuml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bufio"
"database/sql"
"encoding/csv"
"fmt"
"log"
"os"
_ "github.com/go-sql-driver/mysql"
)
const START_UML = "@startuml\n\n"
const END_UML = "\n\n@enduml\n"
type TableStructure struct {
Field string
Type string
Null string
Key string
Default sql.NullString
Extra string
}
type Relation struct {
Parent string
Child string
}
func main() {
var relationFile, outputFile string
var sc = bufio.NewScanner(os.Stdin)
fmt.Print("relation csv file path >> ")
if sc.Scan() {
relationFile = sc.Text()
}
fmt.Print("output file name >> ")
if sc.Scan() {
outputFile = sc.Text()
}
// DB設定 別ファイルで環境変数設定するのがよさそう
db, _ := sql.Open("mysql", "")
tableList := []string{"users", "todos"}
var entityStrings string = START_UML
for _, v := range tableList {
tableInfo := createEntity(db, v)
entityStrings += setEntitySentence(tableInfo, v)
}
relations := readCsv(relationFile)
for _, relation := range relations {
entityStrings += setRelation(relation)
}
entityStrings += END_UML
judge := writeFile(entityStrings, outputFile)
if judge {
fmt.Println("success!")
}
}
func createEntity(db *sql.DB, tableName string) (tableStructs []TableStructure) {
rows, err := db.Query("SHOW COLUMNS FROM " + tableName)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var tableStruct TableStructure
for rows.Next() {
err = rows.Scan(&tableStruct.Field, &tableStruct.Type, &tableStruct.Null, &tableStruct.Key, &tableStruct.Default, &tableStruct.Extra)
if err != nil {
log.Fatal(err)
}
tableStructs = append(tableStructs, tableStruct)
}
return
}
func setEntitySentence(tableStructs []TableStructure, tableName string) (sentence string) {
sentence += "entity " + tableName + " as \"" + tableName + "\" {\n"
for _, v := range tableStructs {
if v.Key == "PRI" {
sentence += " +" + v.Field + " " + v.Type + "\n"
sentence += " --\n"
} else {
sentence += " " + v.Field + " " + v.Type + "\n"
}
}
sentence += "}\n\n"
return
}
func setRelation(relation Relation) string {
return relation.Parent + " <|-- " + relation.Child + "\n"
}
func writeFile(entities string, fileName string) bool {
file, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
return false
}
defer file.Close()
fw := bufio.NewWriter(file)
_, err = fw.Write([]byte(entities))
if err != nil {
log.Fatal(err)
return false
}
err = fw.Flush()
if err != nil {
log.Fatal(err)
return false
}
return true
}
func readCsv(relationFilePath string) (relations []Relation) {
file, err := os.Open(relationFilePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
reader := csv.NewReader(file)
var line []string
for {
line, err = reader.Read()
if err != nil {
break
}
relations = append(relations, Relation{
Parent: line[0],
Child: line[1],
})
}
return
}