-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
schema.go
345 lines (281 loc) · 6.23 KB
/
schema.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//go:generate stringer -type=RelType -output=./gen_string.go
package sdata
import (
"fmt"
"strings"
"github.com/dosco/graphjin/core/internal/util"
)
type edgeInfo struct {
nodeID int32
edgeIDs []int32
}
type nodeInfo struct {
nodeID int32
}
type DBSchema struct {
typ string // db type
ver int // db version
schema string // db schema
name string // db name
tables []DBTable // tables
vt map[string]VirtualTable // for polymorphic relationships
fm map[string]DBFunction // db functions
tindex map[string]nodeInfo // table index
ai map[string]nodeInfo // table alias index
ei map[string][]edgeInfo // edges index
ae map[int32]TEdge // all edges
rg *util.Graph // relationship graph
}
type RelType int
const (
RelNone RelType = iota
RelOneToOne
RelOneToMany
RelPolymorphic
RelRecursive
RelEmbedded
RelRemote
RelSkip
)
type DBRelLeft struct {
Ti DBTable
Col DBColumn
}
type DBRelRight struct {
VTable string
Ti DBTable
Col DBColumn
}
type DBRel struct {
Type RelType
Left DBRelLeft
Right DBRelRight
}
func NewDBSchema(
info *DBInfo,
aliases map[string][]string) (*DBSchema, error) {
schema := &DBSchema{
typ: info.Type,
ver: info.Version,
schema: info.Schema,
name: info.Name,
vt: make(map[string]VirtualTable),
fm: make(map[string]DBFunction),
tindex: make(map[string]nodeInfo),
ai: make(map[string]nodeInfo),
ei: make(map[string][]edgeInfo),
ae: make(map[int32]TEdge),
rg: util.NewGraph(),
}
for _, t := range info.Tables {
nid := schema.addNode(t)
schema.addAliases(schema.tables[nid], nid, aliases[t.Name])
}
for _, t := range info.VTables {
if err := schema.addVirtual(t); err != nil {
return nil, err
}
}
for _, t := range schema.tables {
err := schema.addRels(t)
if err != nil {
return nil, err
}
}
// Add aliases to edge index by duplicating
for t, al := range aliases {
for _, alias := range al {
if _, ok := schema.ei[alias]; ok {
continue
}
if e, ok := schema.ei[t]; ok {
schema.ei[alias] = e
}
}
}
for k, f := range info.Functions {
if len(f.Params) == 1 {
schema.fm[strings.ToLower(f.Name)] = info.Functions[k]
}
}
return schema, nil
}
func (s *DBSchema) addRels(t DBTable) error {
var err error
switch t.Type {
case "json", "jsonb":
err = s.addJsonRel(t)
case "virtual":
err = s.addPolymorphicRel(t)
case "remote":
err = s.addRemoteRel(t)
}
if err != nil {
return err
}
return s.addColumnRels(t)
}
func (s *DBSchema) addJsonRel(t DBTable) error {
st, err := s.Find(t.SecondaryCol.Schema, t.SecondaryCol.Table)
if err != nil {
return err
}
sc, err := st.GetColumn(t.SecondaryCol.Name)
if err != nil {
return err
}
return s.addToGraph(t, t.PrimaryCol, st, sc, RelEmbedded)
}
func (s *DBSchema) addPolymorphicRel(t DBTable) error {
pt, err := s.Find(t.PrimaryCol.FKeySchema, t.PrimaryCol.FKeyTable)
if err != nil {
return err
}
// pc, err := pt.GetColumn(t.PrimaryCol.FKeyCol)
// if err != nil {
// return err
// }
pc, err := pt.GetColumn(t.SecondaryCol.Name)
if err != nil {
return err
}
return s.addToGraph(t, t.PrimaryCol, pt, pc, RelPolymorphic)
}
func (s *DBSchema) addRemoteRel(t DBTable) error {
pt, err := s.Find(t.PrimaryCol.FKeySchema, t.PrimaryCol.FKeyTable)
if err != nil {
return err
}
pc, err := pt.GetColumn(t.PrimaryCol.FKeyCol)
if err != nil {
return err
}
return s.addToGraph(t, t.PrimaryCol, pt, pc, RelRemote)
}
func (s *DBSchema) addColumnRels(t DBTable) error {
var err error
for i := range t.Columns {
c := t.Columns[i]
if c.FKeyTable == "" {
continue
}
if c.FKeySchema == "" {
c.FKeySchema = t.Schema
}
v, ok := s.tindex[(c.FKeySchema + ":" + c.FKeyTable)]
if !ok {
return fmt.Errorf("foreign key table not found: %s.%s", c.FKeySchema, c.FKeyTable)
}
ft := s.tables[v.nodeID]
if c.FKeyCol == "" {
continue
}
fc, ok := ft.getColumn(c.FKeyCol)
if !ok {
return fmt.Errorf("foreign key column not found: %s.%s", c.FKeyTable, c.FKeyCol)
}
var rt RelType
switch {
case t.Name == c.FKeyTable:
rt = RelRecursive
case fc.UniqueKey:
rt = RelOneToOne
default:
rt = RelOneToMany
}
if err = s.addToGraph(t, c, ft, fc, rt); err != nil {
return err
}
}
return nil
}
func (s *DBSchema) addVirtual(vt VirtualTable) error {
s.vt[vt.Name] = vt
for _, t := range s.tables {
idCol, ok := t.getColumn(vt.IDColumn)
if !ok {
continue
}
typeCol, ok := t.getColumn(vt.TypeColumn)
if !ok {
continue
}
col1 := DBColumn{
ID: -1,
Schema: t.Schema,
Table: t.Name,
Name: idCol.Name,
Type: idCol.Type,
FKeySchema: typeCol.Schema,
FKeyTable: typeCol.Table,
FKeyCol: typeCol.Name,
}
fIDCol, ok := t.getColumn(vt.FKeyColumn)
if !ok {
continue
}
col2 := DBColumn{
ID: -1,
Schema: t.Schema,
Table: t.Name,
Name: fIDCol.Name,
}
pt := DBTable{
Name: vt.Name,
Schema: t.Schema,
Type: "virtual",
PrimaryCol: col1,
SecondaryCol: col2,
}
s.addNode(pt)
}
return nil
}
func (s *DBSchema) GetTables() []DBTable {
return s.tables
}
func (ti *DBTable) getColumn(name string) (DBColumn, bool) {
var c DBColumn
if i, ok := ti.colMap[name]; ok {
return ti.Columns[i], true
}
return c, false
}
func (ti *DBTable) GetColumn(name string) (DBColumn, error) {
c, ok := ti.getColumn(name)
if ok {
return c, nil
}
return c, fmt.Errorf("column: '%s.%s' not found", ti.Name, name)
}
func (s *DBSchema) GetFunctions() map[string]DBFunction {
return s.fm
}
func getRelName(colName string) string {
cn := strings.ToLower(colName)
if strings.HasSuffix(cn, "_id") {
return colName[:len(colName)-3]
}
if strings.HasSuffix(cn, "_ids") {
return colName[:len(colName)-4]
}
if strings.HasPrefix(cn, "id_") {
return colName[3:]
}
if strings.HasPrefix(cn, "ids_") {
return colName[4:]
}
return cn
}
func (s *DBSchema) DBType() string {
return s.typ
}
func (s *DBSchema) DBVersion() int {
return s.ver
}
func (s *DBSchema) DBSchema() string {
return s.schema
}
func (s *DBSchema) DBName() string {
return s.name
}