-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquery_argument.go
More file actions
79 lines (75 loc) · 2.35 KB
/
Copy pathquery_argument.go
File metadata and controls
79 lines (75 loc) · 2.35 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
package sqlparser
import (
"github.com/viant/parsly"
"github.com/viant/sqlparser/query"
)
func startsQueryArgument(cursor *parsly.Cursor) bool {
lookahead := *cursor
for {
skipExpressionSpace(&lookahead)
identifierSize := selectorMatcher.Match(&lookahead)
match := lookahead.MatchAny(selectKeywordMatcher, withKeywordMatcher, parenthesesMatcher)
switch match.Code {
case selectKeyword, withKeyword:
return match.Size == identifierSize || startsSQLComment(lookahead.Input, lookahead.Pos)
case parenthesesCode:
raw := match.Text(&lookahead)
lookahead = *parsly.NewCursor(cursor.Path, []byte(raw[1:len(raw)-1]), 0)
default:
return false
}
}
}
func parseQueryArgument(cursor *parsly.Cursor) (*query.Select, error) {
// Parentheses may enclose the query repeatedly, but each enclosure must
// contain the entire argument. The enclosing call retains its raw syntax.
for {
skipExpressionSpace(cursor)
match := cursor.MatchOne(parenthesesMatcher)
if match.Code != parenthesesCode {
break
}
raw := match.Text(cursor)
start := cursor.Pos - len(raw)
skipExpressionSpace(cursor)
if cursor.Pos != len(cursor.Input) {
return nil, cursor.NewError(exprMatcher)
}
inner := parsly.NewCursor(cursor.Path, []byte(raw[1:len(raw)-1]), start)
inner.OnError = cursor.OnError
cursor = inner
}
start := cursor.Pos
match := cursor.MatchAny(selectKeywordMatcher, withKeywordMatcher)
if match.Code != selectKeyword && match.Code != withKeyword {
return nil, cursor.NewError(selectKeywordMatcher)
}
cursor.Pos = start
result := &query.Select{}
if err := parseQuery(cursor, result); err != nil {
return nil, err
}
skipExpressionSpace(cursor)
if cursor.Pos != len(cursor.Input) || !completeQueryProjections(result) {
return nil, cursor.NewError(exprMatcher)
}
return result, nil
}
// The general query parser permits incomplete projections for legacy callers.
// Query arguments require complete projections in their main query, CTEs and UNION arms.
func completeQueryProjections(q *query.Select) bool {
if q == nil || len(q.List) == 0 {
return false
}
for _, item := range q.List {
if item == nil || !completeExpression(item.Expr) {
return false
}
}
for _, with := range q.WithSelects {
if with == nil || !completeQueryProjections(with.X) {
return false
}
}
return q.Union == nil || completeQueryProjections(q.Union.X)
}