-
Notifications
You must be signed in to change notification settings - Fork 2
/
join.go
121 lines (109 loc) · 3.84 KB
/
join.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
package sqlparser
import (
"github.com/viant/parsly"
"github.com/viant/sqlparser/expr"
"github.com/viant/sqlparser/query"
)
func parseJoin(cursor *parsly.Cursor, join *query.Join, dest *query.Select, expectOn bool) error {
match := cursor.MatchAfterOptional(whitespaceMatcher, parenthesesMatcher, exprMatcher, selectorMatcher)
switch match.Code {
case parenthesesCode:
join.With = expr.NewRaw(match.Text(cursor))
case selectorTokenCode:
identityOrAlias := match.Text(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, parenthesesMatcher)
if match.Code == parenthesesCode {
identityOrAlias += match.Text(cursor)
}
withSelect := dest.WithSelects.Select(identityOrAlias)
if withSelect != nil {
join.With = expr.NewParenthesis(withSelect.Raw)
join.Alias = identityOrAlias
} else {
join.With = expr.NewSelector(identityOrAlias)
}
}
if join.Alias == "" {
join.Alias = discoverAlias(cursor)
}
match = cursor.MatchAfterOptional(whitespaceMatcher, commentBlockMatcher, onKeywordMatcher)
if match.Code == commentBlock {
join.Comments = match.Text(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, onKeywordMatcher)
}
switch match.Code {
case onKeyword:
binary := &expr.Binary{}
join.On = &expr.Qualify{}
join.On.X = binary
if err := parseBinaryExpr(cursor, binary); err != nil {
return err
}
default:
if expectOn {
return cursor.NewError(onKeywordMatcher)
}
}
match = cursor.MatchAfterOptional(whitespaceMatcher, joinMatcher, groupByMatcher, havingKeywordMatcher, whereKeywordMatcher, orderByKeywordMatcher, windowMatcher, unionMatcher)
if match.Code == parsly.EOF {
return nil
}
if match.Code == commentBlock {
join.Comments = match.Text(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, joinMatcher, groupByMatcher, havingKeywordMatcher, whereKeywordMatcher, orderByKeywordMatcher, windowMatcher, unionMatcher)
if match.Code == parsly.EOF {
return nil
}
}
hasMatch, err := matchPostFrom(cursor, dest, match)
if !hasMatch && err == nil {
err = cursor.NewError(joinMatcher, groupByMatcher, havingKeywordMatcher, whereKeywordMatcher, orderByKeywordMatcher, windowMatcher, unionMatcher)
}
return err
}
func parseDeleteJoin(cursor *parsly.Cursor, join *query.Join) (*parsly.TokenMatch, error) {
match := cursor.MatchAfterOptional(whitespaceMatcher, parenthesesMatcher, selectorMatcher)
switch match.Code {
case parenthesesCode:
join.With = expr.NewRaw(match.Text(cursor))
case selectorTokenCode:
join.With = expr.NewSelector(match.Text(cursor))
}
join.Alias = discoverAlias(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, commentBlockMatcher, onKeywordMatcher)
if match.Code == commentBlock {
join.Comments = match.Text(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, onKeywordMatcher)
}
switch match.Code {
case onKeyword:
default:
return match, cursor.NewError(onKeywordMatcher)
}
binary := &expr.Binary{}
join.On = &expr.Qualify{}
join.On.X = binary
if err := parseBinaryExpr(cursor, binary); err != nil {
return match, err
}
match = cursor.MatchAfterOptional(whitespaceMatcher, joinMatcher, groupByMatcher, havingKeywordMatcher, whereKeywordMatcher, orderByKeywordMatcher, windowMatcher)
if match.Code == parsly.EOF {
return match, nil
}
if match.Code == commentBlock {
join.Comments = match.Text(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, joinMatcher, groupByMatcher, havingKeywordMatcher, whereKeywordMatcher, orderByKeywordMatcher, windowMatcher)
if match.Code == parsly.EOF {
return match, nil
}
}
return match, nil
}
func appendJoin(cursor *parsly.Cursor, match *parsly.TokenMatch, dest *query.Select, expectOn bool) error {
join := query.NewJoin(match.Text(cursor))
dest.Joins = append(dest.Joins, join)
if err := parseJoin(cursor, join, dest, expectOn); err != nil {
return err
}
return nil
}