-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelete.go
More file actions
146 lines (121 loc) · 3.42 KB
/
delete.go
File metadata and controls
146 lines (121 loc) · 3.42 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
134
135
136
137
138
139
140
141
142
143
144
145
146
package sqlparser
import (
"github.com/viant/parsly"
del "github.com/viant/sqlparser/delete"
"github.com/viant/sqlparser/expr"
"github.com/viant/sqlparser/query"
)
// ParseDelete parses DELETE statement
func ParseDelete(SQL string) (*del.Statement, error) {
aStmt := &del.Statement{}
cursor := parsly.NewCursor("", []byte(SQL), 0)
return aStmt, parseDelete(cursor, aStmt)
}
func parseDelete(cursor *parsly.Cursor, stmt *del.Statement) error {
matched := cursor.MatchAfterOptional(whitespaceMatcher, deleteMatcher)
if matched.Code != deleteCode {
return cursor.NewError(deleteMatcher)
}
var err error
stmt.Items, err = tryParseDeleteItems(cursor)
if err != nil {
return err
}
lastMatchedCode, err := buildDeleteTarget(stmt, cursor)
if err != nil {
return err
}
stmt.Qualify, err = tryParseQualify(cursor, lastMatchedCode)
if err != nil {
return err
}
return nil
}
func buildDeleteTarget(stmt *del.Statement, cursor *parsly.Cursor) (int, error) {
matchable := []*parsly.Token{whereKeywordMatcher, joinMatcher, tableMatcher}
var targetData []string
lastMatched := parsly.Invalid
var matched *parsly.TokenMatch
for {
prevPos := cursor.Pos
switch lastMatched {
case whereKeyword:
stmt.Target = buildTarget(targetData, cursor)
return lastMatched, nil
case tableTokenCode:
targetData = append(targetData, matched.Text(cursor))
if len(targetData) >= 2 {
matchable = matchable[:len(matchable)-1]
}
case joinToken:
join := query.NewJoin(matched.Text(cursor))
if _, err := parseDeleteJoin(cursor, join); err != nil {
return lastMatched, err
}
stmt.Joins = append(stmt.Joins, join)
matchable = matchable[:len(matchable)-1]
case parsly.EOF:
stmt.Target = buildTarget(targetData, cursor)
return lastMatched, nil
default:
if matched != nil {
return lastMatched, cursor.NewError(matchable...)
}
}
if prevPos == cursor.Pos {
matched = cursor.MatchAfterOptional(whitespaceMatcher, matchable...)
}
lastMatched = matched.Code
}
}
func buildTarget(targetData []string, cursor *parsly.Cursor) del.Target {
alias := ""
if len(targetData) >= 2 {
alias = targetData[1]
}
target := del.Target{
X: expr.NewSelector(targetData[0]),
Comments: matchComment(cursor),
Alias: alias,
}
return target
}
func tryParseDeleteItems(cursor *parsly.Cursor) ([]*del.Item, error) {
var items []*del.Item
matchable := []*parsly.Token{fromKeywordMatcher, selectorMatcher}
for cursor.Pos < cursor.InputSize {
matched := cursor.MatchAfterOptional(whitespaceMatcher, matchable...)
switch matched.Code {
case fromKeyword:
return items, nil
case selectorTokenCode:
selName := matched.Text(cursor)
comment := matchComment(cursor)
items = append(items, &del.Item{
Comments: comment,
Raw: selName,
})
matched = cursor.MatchAfterOptional(whitespaceMatcher, nextMatcher)
if matched.Code != nextCode {
matchable = matchable[:1]
}
default:
return nil, cursor.NewError(matchable...)
}
}
return items, nil
}
func matchComment(cursor *parsly.Cursor) string {
match := cursor.MatchAfterOptional(whitespaceMatcher, commentBlockMatcher)
if match.Code == commentBlock {
return match.Text(cursor)
}
return ""
}
func tryParseQualify(cursor *parsly.Cursor, lastMatchedCode int) (*expr.Qualify, error) {
if lastMatchedCode != whereKeyword {
return nil, nil
}
qualify := expr.NewQualify()
return qualify, ParseQualify(cursor, qualify)
}