/
parseutil.go
479 lines (440 loc) · 11.6 KB
/
parseutil.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package parseutil
import (
"fmt"
"github.com/lighttiger2505/sqls/ast"
"github.com/lighttiger2505/sqls/ast/astutil"
"github.com/lighttiger2505/sqls/token"
)
type TableInfo struct {
DatabaseSchema string
Name string
Alias string
SubQueryColumns []*SubQueryColumn
}
func (ti *TableInfo) isMatchTableName(name string) bool {
if ti.Name == name {
return true
}
if ti.Alias == name {
return true
}
return false
}
func (ti *TableInfo) hasSubQuery() bool {
if ti.SubQueryColumns != nil && len(ti.SubQueryColumns) > 0 {
return true
}
return false
}
type SubQueryInfo struct {
Name string
Views []*SubQueryView
}
type SubQueryView struct {
SubQueryColumns []*SubQueryColumn
}
type SubQueryColumn struct {
ParentTable *TableInfo
ParentName string
ColumnName string
AliasName string
}
func (sc *SubQueryColumn) DisplayName() string {
name := sc.ColumnName
if sc.AliasName != "" {
name = sc.AliasName
}
return name
}
func extractFocusedStatement(parsed ast.TokenList, pos token.Pos) (ast.TokenList, error) {
nodeWalker := NewNodeWalker(parsed, pos)
matcher := astutil.NodeMatcher{NodeTypes: []ast.NodeType{ast.TypeStatement}}
if !nodeWalker.CurNodeIs(matcher) {
return nil, fmt.Errorf("Not found statement, Node: %q, Position: (%d, %d)", parsed.String(), pos.Line, pos.Col)
}
stmt := nodeWalker.CurNodeTopMatched(matcher).(ast.TokenList)
return stmt, nil
}
func encloseIsSubQuery(stmt ast.TokenList, pos token.Pos) bool {
nodeWalker := NewNodeWalker(stmt, pos)
matcher := astutil.NodeMatcher{NodeTypes: []ast.NodeType{ast.TypeParenthesis}}
if !nodeWalker.CurNodeIs(matcher) {
return false
}
parenthesis := nodeWalker.CurNodeButtomMatched(matcher)
tokenList, ok := parenthesis.(ast.TokenList)
if !ok {
return false
}
return isSubQuery(tokenList)
}
func isSubQuery(tokenList ast.TokenList) bool {
reader := astutil.NewNodeReader(tokenList)
if !reader.NextNode(false) {
return false
}
if !reader.NextNode(false) {
return false
}
if !reader.CurNodeIs(astutil.NodeMatcher{ExpectKeyword: []string{"SELECT"}}) {
return false
}
return true
}
func isSubQueryByNode(node ast.Node) bool {
alias, ok := node.(*ast.Aliased)
if !ok {
return false
}
list, ok := alias.RealName.(ast.TokenList)
if !ok {
return false
}
return isSubQuery(list)
}
func extractFocusedSubQuery(stmt ast.TokenList, pos token.Pos) ast.TokenList {
nodeWalker := NewNodeWalker(stmt, pos)
matcher := astutil.NodeMatcher{NodeTypes: []ast.NodeType{ast.TypeParenthesis}}
if !nodeWalker.CurNodeIs(matcher) {
return nil
}
parenthesis := nodeWalker.CurNodeButtomMatched(matcher)
return parenthesis.(ast.TokenList)
}
func ExtractSubQueryViews(parsed ast.TokenList, pos token.Pos) ([]*SubQueryInfo, error) {
stmt, err := extractFocusedStatement(parsed, pos)
if err != nil {
return nil, err
}
reader := astutil.NewNodeReader(parsed)
matcher := astutil.NodeMatcher{NodeTypes: []ast.NodeType{ast.TypeAliased}}
aliases := reader.FindRecursive(matcher)
var subQueries []*ast.Aliased
var before *ast.Aliased
for _, alias := range aliases {
if token.ComparePos(alias.Pos(), pos) < 0 {
continue
}
if before != nil && token.ComparePos(alias.End(), before.End()) < 0 {
continue
}
alias, ok := alias.(*ast.Aliased)
if !ok {
continue
}
list, ok := alias.RealName.(ast.TokenList)
if !ok {
continue
}
if isSubQuery(list) {
subQueries = append(subQueries, alias)
before = alias
}
}
if len(subQueries) == 0 {
return nil, nil
}
results := []*SubQueryInfo{}
for _, subQuery := range subQueries {
parenthesis, ok := subQuery.RealName.(*ast.Parenthesis)
if !ok {
return nil, fmt.Errorf("is not sub query, query: %q, type: %T", stmt, stmt)
}
subqueryCols, _, err := extractSubQueryColumns(parenthesis.Inner())
if err != nil {
return nil, err
}
cols := make([]string, len(subqueryCols))
for i, subqueryCol := range subqueryCols {
cols[i] = subqueryCol.ColumnName
}
info := &SubQueryInfo{
Name: subQuery.AliasedName.String(),
Views: []*SubQueryView{
{
SubQueryColumns: subqueryCols,
},
},
}
results = append(results, info)
}
return results, nil
}
func ExtractTable(parsed ast.TokenList, pos token.Pos) ([]*TableInfo, error) {
stmt, err := extractFocusedStatement(parsed, pos)
if err != nil {
return nil, err
}
list := stmt
if encloseIsSubQuery(stmt, pos) {
list = extractFocusedSubQuery(stmt, pos)
}
tables, err := extractTableIdentifier(list, false)
if err != nil {
return nil, err
}
tableMap := map[string]*TableInfo{}
for _, table := range tables {
tableMap[table.DatabaseSchema+"\t"+table.Name] = table
}
cleanTables := []*TableInfo{}
for _, table := range tableMap {
cleanTables = append(cleanTables, table)
}
return cleanTables, nil
}
var identifierMatcher = astutil.NodeMatcher{
NodeTypes: []ast.NodeType{
ast.TypeIdentifer,
ast.TypeIdentiferList,
ast.TypeMemberIdentifer,
ast.TypeAliased,
},
}
func extractSubQueryColumns(selectStmt ast.TokenList) ([]*SubQueryColumn, []*TableInfo, error) {
tables, err := extractTableIdentifier(selectStmt, true)
if err != nil {
return nil, nil, err
}
// extract select identifiers
identsObj := selectStmt.GetTokens()[2]
cols, err := parseSubQueryColumns(identsObj, tables)
if err != nil {
return nil, nil, err
}
// check from clause is sub query
fromIdentifier := ExtractTableReferences(selectStmt)
if len(fromIdentifier) == 0 {
return cols, tables, nil
}
alias, ok := fromIdentifier[0].(*ast.Aliased)
if !ok {
return cols, tables, nil
}
parenthesis, ok := alias.RealName.(*ast.Parenthesis)
if !ok {
return cols, tables, nil
}
if !isSubQuery(parenthesis) {
return cols, tables, nil
}
// merge select identiner of inner sub query
innerIdents, innerTables, err := extractSubQueryColumns(parenthesis.Inner())
if err != nil {
return nil, nil, err
}
realIdents := []*SubQueryColumn{}
for _, ident := range cols {
if ident.ColumnName == "*" {
return innerIdents, innerTables, nil
}
for _, innerIdent := range innerIdents {
if ident.ColumnName == innerIdent.ColumnName {
ident.ParentTable.SubQueryColumns = innerIdents
realIdents = append(realIdents, ident)
}
}
}
return realIdents, tables, nil
}
func extractTableIdentifier(list ast.TokenList, isSubQuery bool) ([]*TableInfo, error) {
nodes := []ast.Node{}
nodes = append(nodes, ExtractTableReferences(list)...)
nodes = append(nodes, ExtractTableReference(list)...)
nodes = append(nodes, ExtractTableFactor(list)...)
res := []*TableInfo{}
for _, ident := range nodes {
if !isSubQuery && isSubQueryByNode(ident) {
continue
}
infos, err := parseTableInfo(ident)
if err != nil {
return nil, err
}
res = append(res, infos...)
}
return res, nil
}
func filterTokenList(reader *astutil.NodeReader, matcher astutil.NodeMatcher) ast.TokenList {
var res []ast.Node
for reader.NextNode(false) {
if reader.CurNodeIs(matcher) {
res = append(res, reader.CurNode)
} else if list, ok := reader.CurNode.(ast.TokenList); ok {
newReader := astutil.NewNodeReader(list)
res = append(res, filterTokenList(newReader, matcher).GetTokens()...)
}
}
return &ast.Statement{Toks: res}
}
func parseTableInfo(idents ast.Node) ([]*TableInfo, error) {
res := []*TableInfo{}
switch v := idents.(type) {
case *ast.Identifer:
ti := &TableInfo{Name: v.NoQuateString()}
res = append(res, ti)
case *ast.IdentiferList:
tis, err := identifierListToTableInfo(v)
if err != nil {
return nil, err
}
res = append(res, tis...)
case *ast.MemberIdentifer:
if v.Parent != nil {
ti := &TableInfo{
DatabaseSchema: v.Parent.String(),
Name: v.GetChild().String(),
}
res = append(res, ti)
}
case *ast.Aliased:
tis, err := aliasedToTableInfo(v)
if err != nil {
return nil, err
}
res = append(res, tis)
default:
return nil, fmt.Errorf("unknown node type %T", v)
}
return res, nil
}
func identifierListToTableInfo(il *ast.IdentiferList) ([]*TableInfo, error) {
tis := []*TableInfo{}
for _, ident := range il.GetIdentifers() {
switch v := ident.(type) {
case *ast.Identifer:
ti := &TableInfo{
Name: v.NoQuateString(),
}
tis = append(tis, ti)
case *ast.MemberIdentifer:
ti := &TableInfo{
DatabaseSchema: v.Parent.String(),
Name: v.GetChild().String(),
}
tis = append(tis, ti)
case *ast.Aliased:
// pass
default:
return nil, fmt.Errorf("failed parse table info, unknown node type %T, value %q in %q", ident, ident, il)
}
}
return tis, nil
}
func aliasedToTableInfo(aliased *ast.Aliased) (*TableInfo, error) {
ti := &TableInfo{}
// fetch table schema and name
switch v := aliased.RealName.(type) {
case *ast.Identifer:
ti.Name = v.NoQuateString()
case *ast.MemberIdentifer:
ti.DatabaseSchema = v.Parent.String()
ti.Name = v.GetChild().String()
case *ast.Parenthesis:
tables, err := extractTableIdentifier(v.Inner(), true)
if err != nil {
panic(err)
}
ti.DatabaseSchema = tables[0].DatabaseSchema
ti.Name = tables[0].Name
default:
return nil, fmt.Errorf(
"failed parse real name of alias, unknown node type %T, value %q",
aliased.RealName,
aliased.RealName,
)
}
// fetch table aliased name
switch v := aliased.AliasedName.(type) {
case *ast.Identifer:
ti.Alias = v.NoQuateString()
default:
return nil, fmt.Errorf(
"failed parse aliased name of alias, unknown node type %T, value %q",
aliased.AliasedName,
aliased.AliasedName,
)
}
return ti, nil
}
func parseSubQueryColumns(idents ast.Node, tables []*TableInfo) ([]*SubQueryColumn, error) {
subqueryCols := []*SubQueryColumn{}
switch v := idents.(type) {
case *ast.Identifer:
ident := v.NoQuateString()
if ident == "*" {
for _, table := range tables {
subqueryCol := &SubQueryColumn{
ColumnName: ident,
ParentTable: table,
}
subqueryCols = append(subqueryCols, subqueryCol)
}
} else {
subqueryCol := &SubQueryColumn{ColumnName: ident}
if len(tables) == 1 {
subqueryCol.ParentTable = tables[0]
}
subqueryCols = append(subqueryCols, subqueryCol)
}
case *ast.IdentiferList:
for _, ident := range v.GetIdentifers() {
resSubqueryCols, err := parseSubQueryColumns(ident, tables)
if err != nil {
return nil, err
}
subqueryCols = append(subqueryCols, resSubqueryCols...)
}
case *ast.MemberIdentifer:
subqueryCols = append(
subqueryCols,
&SubQueryColumn{
ParentName: v.GetParentIdent().NoQuateString(),
ColumnName: v.GetChildIdent().NoQuateString(),
},
)
case *ast.Aliased:
subqueryCol, err := aliasedToSubQueryColumn(v)
if err != nil {
return nil, err
}
subqueryCols = append(subqueryCols, subqueryCol)
// TODO Add case of function:
// case *ast.Function:
default:
return nil, fmt.Errorf("failed parse sub query columns, unknown node type %T, value %q", idents, idents)
}
for _, subqueryCol := range subqueryCols {
for _, table := range tables {
if table.isMatchTableName(subqueryCol.ParentName) {
subqueryCol.ParentTable = table
}
}
}
return subqueryCols, nil
}
func aliasedToSubQueryColumn(aliased *ast.Aliased) (*SubQueryColumn, error) {
// fetch table schema and name
aliasedName := aliased.GetAliasedNameIdent().NoQuateString()
switch v := aliased.RealName.(type) {
case *ast.Identifer:
subqueryCol := &SubQueryColumn{
ColumnName: v.NoQuateString(),
AliasName: aliasedName,
}
return subqueryCol, nil
case *ast.MemberIdentifer:
subqueryCol := &SubQueryColumn{
ParentName: v.GetParentIdent().NoQuateString(),
ColumnName: v.GetChildIdent().NoQuateString(),
AliasName: aliasedName,
}
return subqueryCol, nil
default:
return nil, fmt.Errorf(
"failed trans alias to column, unknown node type %T, value %q",
aliased.AliasedName,
aliased.AliasedName,
)
}
}