-
Notifications
You must be signed in to change notification settings - Fork 36
/
parse.go
259 lines (223 loc) · 5.77 KB
/
parse.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
// Relation between two tables
type Relation struct {
LeftTableName string
LeftCardinality string
RightTableName string
RightCardinality string
RelationAttributes map[string]string
}
// Index on a column
type Index struct {
Title string
Columns []string
IsUnique bool
}
// Column in a table
type Column struct {
Title string
ColumnAttributes map[string]string
}
// Table in a database
type Table struct {
Name string
Title string
TableAttributes map[string]string
Columns []Column
CurrentColumnID int
PrimaryKeys []int
Connected bool
}
// Title ...
type Title struct {
Title string
TitleAttributes map[string]string
}
// Erd of the database
type Erd struct {
Title Title
Tables map[string]*Table
Relations []Relation
CurrentRelation Relation
TableNames []string // for ordering Isolations
Isolations []string
key string
value string
CurrentTableName string
IsError bool
Colors map[string]string
}
var re = regexp.MustCompile(`[^a-zA-Z0-9\\_]`)
func replaceAllIllegal(text string) string {
return re.ReplaceAllString(text, "_")
}
// Connect marks the table is connected to another
func (t *Table) Connect() {
t.Connected = true
}
// ClearTableAndColumn clears the current table
func (e *Erd) ClearTableAndColumn() {
e.CurrentTableName = ""
}
// AddTitleKeyValue adds the key value pair to the title attributes
func (e *Erd) AddTitleKeyValue() {
if e.Title.TitleAttributes == nil {
e.Title.TitleAttributes = map[string]string{}
}
e.Title.TitleAttributes[e.key] = e.value
}
// AddTable adds a table to the EDR
func (e *Erd) AddTable(text string) {
if e.Tables == nil {
e.Tables = map[string]*Table{}
}
name := replaceAllIllegal(text)
e.Tables[name] = &Table{Name: name, Title: text, TableAttributes: map[string]string{}}
e.TableNames = append(e.TableNames, name)
e.CurrentTableName = name
}
// AddTableKeyValue add a key value pair to the table attributes
func (e *Erd) AddTableKeyValue() {
table := e.Tables[e.CurrentTableName]
if table.TableAttributes == nil {
table.TableAttributes = map[string]string{}
}
val := e.value
if strings.Contains(e.key, "color") {
v, ok := e.Colors[e.value]
if ok {
val = v
}
}
table.TableAttributes[e.key] = val
}
// AddColorDefine stores the named color palette
func (e *Erd) AddColorDefine() {
if e.Colors == nil {
e.Colors = map[string]string{}
}
e.Colors[e.key] = e.value
}
// AddColumn adds a column to the EDR
func (e *Erd) AddColumn(text string) {
if e.CurrentTableName == "" {
e.Error(errors.New("Invalid State"))
}
table := e.Tables[e.CurrentTableName]
table.Columns = append(table.Columns, Column{Title: text, ColumnAttributes: map[string]string{}})
table.CurrentColumnID = len(table.Columns) - 1
}
// AddColumnKeyValue adds a key value pair to the column attributes
func (e *Erd) AddColumnKeyValue() {
table := e.Tables[e.CurrentTableName]
column := table.Columns[table.CurrentColumnID]
if column.ColumnAttributes == nil {
column.ColumnAttributes = map[string]string{}
}
column.ColumnAttributes[e.key] = e.value
e.key = ""
e.value = ""
}
// SetKey sets the current key
func (e *Erd) SetKey(text string) {
e.key = text
if len(e.key) > 0 && e.key[0] == '"' {
e.key = e.unquote(e.key)
}
}
// SetValue sets the current value
func (e *Erd) SetValue(text string) {
e.value = text
if len(e.value) > 0 && e.value[0] == '"' {
e.value = e.unquote(e.value)
}
}
// Connect set the table status as connected, for rendering horizontal isolated nodes later
func (e *Erd) Connect(name string) {
if table, ok := e.Tables[name]; ok {
table.Connect()
}
}
// AddRelation adds the current relation to the EDR
func (e *Erd) AddRelation() {
e.Relations = append(e.Relations, e.CurrentRelation)
e.CurrentRelation = Relation{}
}
// AddRelationKeyValue adds a key value pair to the current relation attributes
func (e *Erd) AddRelationKeyValue() {
if e.CurrentRelation.RelationAttributes == nil {
e.CurrentRelation.RelationAttributes = map[string]string{}
}
e.CurrentRelation.RelationAttributes[e.key] = e.value
}
// SetRelationLeft sets the left side of the current relation
func (e *Erd) SetRelationLeft(text string) {
name := replaceAllIllegal(text)
e.CurrentRelation.LeftTableName = name
e.Connect(name)
}
// SetCardinalityLeft sets the left cardinality of the current relation
func (e *Erd) SetCardinalityLeft(text string) {
e.CurrentRelation.LeftCardinality = text
}
// SetRelationRight sets the right side of the current relation
func (e *Erd) SetRelationRight(text string) {
name := replaceAllIllegal(text)
e.CurrentRelation.RightTableName = name
e.Connect(name)
}
func (e *Erd) CalcIsolated() {
for _, name := range e.TableNames {
if table, ok := e.Tables[name]; ok {
if !table.Connected {
e.Isolations = append(e.Isolations, name)
}
}
}
}
// SetCardinalityRight sets the right cardinality of the current relation
func (e *Erd) SetCardinalityRight(text string) {
e.CurrentRelation.RightCardinality = text
}
func (e *Erd) unquote(str string) string {
s, err := strconv.Unquote(str)
if err != nil {
e.Error(err)
}
return s
}
func (e *Erd) Error(err error) {
panic(err)
}
// Err prints an error
func (e *Erd) Err(pos int, buffer string) {
fmt.Println("")
a := strings.Split(buffer[:pos], "\n")
row := len(a) - 1
column := len(a[row]) - 1
lines := strings.Split(buffer, "\n")
for i := row - 5; i <= row; i++ {
if i < 0 {
i = 0
}
fmt.Println(lines[i])
}
s := ""
for i := 0; i <= column; i++ {
s += " "
}
ln := len(strings.Trim(lines[row], " \r\n"))
for i := column + 1; i < ln; i++ {
s += "~"
}
fmt.Println(s)
fmt.Println("error")
e.IsError = true
}