-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjoin.go
More file actions
133 lines (122 loc) · 4.06 KB
/
join.go
File metadata and controls
133 lines (122 loc) · 4.06 KB
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
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 {
if err := parseJoinTarget(cursor, join); err != nil {
return err
}
if join.With == nil {
return cursor.NewError(parenthesesMatcher, selectorMatcher)
}
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 parseJoinTarget(cursor *parsly.Cursor, join *query.Join) error {
pos := cursor.Pos
operand, err := expectOperand(cursor)
if err != nil {
return err
}
if operand != nil {
join.With = operand
return nil
}
cursor.Pos = pos
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)
}
join.With = expr.NewSelector(identityOrAlias)
}
return nil
}
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
}