-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
143 lines (121 loc) · 3.25 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
package sql
import (
"fmt"
"log"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/shyandsy/SqlRelationParser/model"
_ "github.com/pingcap/tidb/parser/test_driver"
)
type RelationParser interface {
ParseRelation(sql string) (*model.Schema, error)
ParseRelationFromBatchSql(sqls []string) (*model.Schema, error)
}
type relationParser struct{}
func NewSqlRelationParser() RelationParser {
return &relationParser{}
}
func (r relationParser) ParseRelation(sql string) (*model.Schema, error) {
p := parser.New()
stmtNodes, _, err := p.Parse(sql, "", "")
if err != nil {
return nil, err
}
fmt.Println(stmtNodes)
result := extractField(&stmtNodes[0])
fmt.Println(result)
result.Show()
return mergeParserResult(result), nil
}
func (r relationParser) ParseRelationFromBatchSql(sqls []string) (*model.Schema, error) {
schemas := []*model.Schema{}
for _, sql := range sqls {
schema, err := r.ParseRelation(sql)
if err != nil {
return nil, err
}
schemas = append(schemas, schema)
}
schema := mergeSchemas(schemas)
fmt.Println(schema)
return schema, nil
}
func extractField(rootNode *ast.StmtNode) *model.ParserResult {
result := &model.ParserResult{}
(*rootNode).Accept(result)
return result
}
func mergeSchemas(items []*model.Schema) *model.Schema {
schema := model.Schema{}
for _, item := range items {
tables := item.GetTables()
relations := item.GetRelations()
// merge table
for _, table := range tables {
t := schema.GetTable(table.GetTableName())
if t == nil {
schema.AddTable(table)
} else {
for _, column := range t.GetColumns() {
t.AddColumn(column)
}
}
}
for _, relation := range relations {
rs := schema.GetRelations()
found := false
for _, r := range rs {
if relation.Equals(r) {
found = true
continue
}
}
if !found {
schema.AddRelation(relation)
}
}
}
return &schema
}
func mergeParserResult(result *model.ParserResult) *model.Schema {
schema := model.Schema{}
tables := result.GetTables()
for _, table := range tables {
schema.AddTable(table)
}
columns := result.GetColumns()
for _, column := range columns {
table := schema.GetTable(column.GetTableName())
if table == nil {
log.Println("ignore column :" + column.String())
continue
}
column.SetTableName(table.GetTableName())
table.AddColumn(column)
}
relations := result.GetRelations()
for _, relation := range relations {
sourceTable := schema.GetTable(relation.GetSourceTable())
if sourceTable == nil {
log.Println("source table doesnt exist, ignore relation :" + relation.String())
continue
}
joinedTable := schema.GetTable(relation.GetJoinedTable())
if joinedTable == nil {
log.Println("joined table doesnt exist, ignore relation :" + relation.String())
continue
}
if !sourceTable.HasColumn(relation.GetSourceColumn()) {
log.Println("source column doesnt exist, ignore relation :" + relation.String())
continue
}
if !joinedTable.HasColumn(relation.GetJoinedColumn()) {
log.Println("joined column doesnt exist, ignore relation :" + relation.String())
continue
}
relation.SetSourceTable(sourceTable.GetTableName())
relation.SetJoinedTable(joinedTable.GetTableName())
schema.AddRelation(relation)
}
return &schema
}