-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate.go
111 lines (100 loc) · 2.88 KB
/
update.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
package sqlparser
import (
"github.com/viant/parsly"
"github.com/viant/sqlparser/expr"
"github.com/viant/sqlparser/update"
)
// ParseUpdate parses update statement
func ParseUpdate(SQL string) (*update.Statement, error) {
result := &update.Statement{}
cursor := parsly.NewCursor("", []byte(SQL), 0)
return result, parseUpdate(cursor, result)
}
func parseUpdate(cursor *parsly.Cursor, stmt *update.Statement) error {
match := cursor.MatchAfterOptional(whitespaceMatcher, updateKeywordMatcher)
switch match.Code {
case updateKeyword:
match = cursor.MatchAfterOptional(whitespaceMatcher, selectorMatcher)
switch match.Code {
case selectorTokenCode:
stmt.Target = extractTarget(cursor, match)
match = cursor.MatchAfterOptional(whitespaceMatcher, setKeywordMatcher)
if match.Code != setKeyword {
return cursor.NewError(setKeywordMatcher)
}
item, err := expectUpdateSetItem(cursor)
if err != nil {
return err
}
stmt.Set = append(stmt.Set, item)
if err = parseUpdateSetItems(cursor, stmt); err != nil {
return err
}
}
}
return nil
}
func extractTarget(cursor *parsly.Cursor, match *parsly.TokenMatch) update.Target {
sel := match.Text(cursor)
comment := ""
match = cursor.MatchAfterOptional(whitespaceMatcher, commentBlockMatcher)
if match.Code == commentBlock {
comment = match.Text(cursor)
}
return update.Target{
X: expr.NewSelector(sel),
Comments: comment,
}
}
func parseUpdateSetItems(cursor *parsly.Cursor, stmt *update.Statement) error {
match := cursor.MatchAfterOptional(whitespaceMatcher, whereKeywordMatcher, nextMatcher)
switch match.Code {
case whereKeyword:
stmt.Qualify = &expr.Qualify{}
if err := ParseQualify(cursor, stmt.Qualify); err != nil {
return err
}
case nextCode:
item, err := expectUpdateSetItem(cursor)
if err != nil {
return err
}
stmt.Set = append(stmt.Set, item)
return parseUpdateSetItems(cursor, stmt)
case parsly.EOF:
default:
return cursor.NewError(nextMatcher, whereKeywordMatcher)
}
return nil
}
func expectUpdateSetItem(cursor *parsly.Cursor) (*update.Item, error) {
beginPos := cursor.Pos
match := cursor.MatchAfterOptional(whitespaceMatcher, selectorMatcher)
if match.Code != selectorTokenCode {
return nil, cursor.NewError(selectorMatcher)
}
selRaw := match.Text(cursor)
item := &update.Item{Column: expr.NewSelector(selRaw)}
match = cursor.MatchAfterOptional(whitespaceMatcher, assignOperatorMatcher)
if match.Code != assignOperator {
return nil, cursor.NewError(assignOperatorMatcher)
}
operand, err := expectOperand(cursor)
if err != nil {
return nil, err
}
pos := cursor.Pos
binary := &expr.Binary{}
binary.X = operand
item.Expr = operand
if err = parseBinaryExpr(cursor, binary); err == nil {
if binary.Op != "" {
item.Expr = binary
}
} else {
cursor.Pos = pos
}
item.Begin = uint32(beginPos) + 1
item.End = uint32(cursor.Pos)
return item, err
}