-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdot.go
143 lines (120 loc) · 3.13 KB
/
dot.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
package main
import (
"bufio"
"fmt"
"html/template"
"io"
"os"
"golang.org/x/exp/slices"
)
func newTemplate(source string) (*template.Template, error) {
tmpl := template.New("")
tmpl.Funcs(map[string]interface{}{
"fk": func(t Table, column Column) string {
i, k := t.Refers(column.Name)
if i == -1 {
return ""
}
return k.ToTable + "." + k.ToColumn
},
"add": func(x, y int) int {
return x + y
},
"sub": func(x, y int) int {
return x - y
},
})
if _, err := tmpl.Parse(source); err != nil {
return nil, err
}
return tmpl, nil
}
func newTemplateFromFile(filename string) (*template.Template, error) {
buf, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return newTemplate(string(buf))
}
// Create GraphViz graph
func graph(w io.Writer, tables Tables, includeFK bool, truncate int) error {
bw := bufio.NewWriter(w)
defer bw.Flush()
bw.WriteString("digraph g {\n")
bw.WriteString("rankdir=LR;\n")
bw.WriteString("edge [ arrowsize=0.5, arrowtail=empty, arrowhead=empty ];\n")
bw.WriteString("node [ shape=plain, height=0.1 ];\n")
bw.WriteString(`fontsize="102pt "` + "\n")
tmpl, err := newTemplate(graphVizTemplate)
if err != nil {
return err
}
slices.SortFunc(tables, func(a, b Table) bool {
return a.Name < b.Name
})
// Draw tables and columns
var ctx = struct {
Table Table
FK bool
Truncate int
}{FK: includeFK, Truncate: truncate}
for i, t := range tables {
ctx.Table = t
nodeName := fmt.Sprintf("table%d", i)
bw.WriteString(nodeName)
bw.WriteString(" [label=<")
if err := tmpl.Execute(bw, ctx); err != nil {
return err
}
bw.WriteString(">]\n")
}
// Draw foreign keys
for i, t := range tables {
sourceName := fmt.Sprintf("table%d", i)
for _, r := range t.References {
tableIndex, destTable := tables.Table(r.ToTable)
if tableIndex == -1 {
continue
}
srcIndex, _ := t.Column(r.FromColumn)
destNode := fmt.Sprintf("table%d", tableIndex)
destIndex, _ := destTable.Column(r.ToColumn)
fmt.Fprintf(bw, "%s:s%d -> %s:e%d;\n", sourceName, srcIndex, destNode, destIndex)
}
}
bw.WriteString("}")
return nil
}
var graphVizTemplate = `
<font point-size="12">
<table {{if eq .Table.Type "view" }} style="rounded" {{end}} border="1" cellpadding="2" cellborder="0" cellspacing="0">
<tr>
<td {{if .FK}} colspan="3" {{end}}>{{.Table.Name}}</td>
</tr>
<HR/>
{{- range $i, $d := .Table.Columns }}
{{- if or (le $.Truncate 0) (lt $i $.Truncate) }}
<tr>
<td align="left" port="e{{$i}}">{{$d.Name}}</td>
<td align="left" {{- if not $.FK }} port="s{{$i}}" {{- end}}>
{{- if $d.Type -}}
<font point-size="10">{{$d.Type}}</font>
{{- end -}}
</td>
{{- if $.FK }}
<td align="left" port="s{{$i}}"><font point-size="10">{{ $fk := fk $.Table $d}}
{{- if eq $fk ""}} {{else}}{{$fk}}{{end -}}
</font></td>
{{end}}
</tr>
{{- else if eq $.Truncate $i }}
<tr>
<td colspan="3">
<font point-size="10"><i>{{sub (len $.Table.Columns) $.Truncate }} other column(s) omitted</i></font>
</td>
</tr>
{{- end }}
{{- end}}
</table>
</font>
`