-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathsqlite_qualified_refs.go
More file actions
217 lines (194 loc) · 4.18 KB
/
sqlite_qualified_refs.go
File metadata and controls
217 lines (194 loc) · 4.18 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package validate
import (
"fmt"
"reflect"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
)
// ValidateSQLiteQualifiedColumnRefs validates that qualified column references
// only use visible tables/aliases in the current or outer SELECT scopes.
func ValidateSQLiteQualifiedColumnRefs(root ast.Node) error {
return validateNodeSQLite(root, nil)
}
type scope struct {
parent *scope
names map[string]struct{}
}
func newScope(parent *scope) *scope {
return &scope{parent: parent, names: map[string]struct{}{}}
}
func (s *scope) add(name string) {
if name == "" {
return
}
s.names[name] = struct{}{}
}
func (s *scope) has(name string) bool {
for cur := s; cur != nil; cur = cur.parent {
if _, ok := cur.names[name]; ok {
return true
}
}
return false
}
func stringSlice(list *ast.List) []string {
if list == nil {
return nil
}
out := make([]string, 0, len(list.Items))
for _, it := range list.Items {
if s, ok := it.(*ast.String); ok {
out = append(out, s.Str)
}
}
return out
}
func qualifierFromColumnRef(ref *ast.ColumnRef) (string, bool) {
if ref == nil || ref.Fields == nil {
return "", false
}
items := stringSlice(ref.Fields)
switch len(items) {
case 2:
return items[0], true
case 3:
return items[1], true
default:
return "", false
}
}
func addFromItemToScope(sc *scope, n ast.Node) {
switch t := n.(type) {
case *ast.RangeVar:
if t.Relname != nil {
sc.add(*t.Relname)
}
if t.Alias != nil && t.Alias.Aliasname != nil {
sc.add(*t.Alias.Aliasname)
}
case *ast.JoinExpr:
addFromItemToScope(sc, t.Larg)
addFromItemToScope(sc, t.Rarg)
case *ast.RangeSubselect:
if t.Alias != nil && t.Alias.Aliasname != nil {
sc.add(*t.Alias.Aliasname)
}
case *ast.RangeFunction:
if t.Alias != nil && t.Alias.Aliasname != nil {
sc.add(*t.Alias.Aliasname)
}
}
}
func validateNodeSQLite(node ast.Node, parent *scope) error {
switch n := node.(type) {
case *ast.SelectStmt:
sc := newScope(parent)
if n.FromClause != nil {
for _, item := range n.FromClause.Items {
addFromItemToScope(sc, item)
}
}
return walkSQLite(n, sc)
default:
return nil
}
}
func walkSQLite(node ast.Node, sc *scope) error {
if node == nil {
return nil
}
if ref, ok := node.(*ast.ColumnRef); ok {
if qual, ok := qualifierFromColumnRef(ref); ok && !sc.has(qual) {
return &sqlerr.Error{
Code: "42703",
Message: fmt.Sprintf("table alias %q does not exist", qual),
Location: ref.Location,
}
}
}
switch n := node.(type) {
case *ast.SubLink:
if n.Subselect != nil {
return validateNodeSQLite(n.Subselect, sc)
}
return nil
case *ast.RangeSubselect:
if n.Subquery != nil {
return validateNodeSQLite(n.Subquery, sc)
}
return nil
}
return walkSQLiteReflect(node, sc)
}
func walkSQLiteReflect(node ast.Node, sc *scope) error {
v := reflect.ValueOf(node)
if v.Kind() == reflect.Pointer {
if v.IsNil() {
return nil
}
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return nil
}
t := v.Type()
for i := 0; i < v.NumField(); i++ {
if t.Field(i).PkgPath != "" {
continue
}
f := v.Field(i)
if !f.IsValid() {
continue
}
for f.Kind() == reflect.Pointer {
if f.IsNil() {
goto next
}
f = f.Elem()
}
if f.Type() == reflect.TypeOf(ast.List{}) {
list := f.Addr().Interface().(*ast.List)
for _, n := range list.Items {
if err := walkSQLite(n, sc); err != nil {
return err
}
}
continue
}
if f.CanAddr() {
if pl, ok := f.Addr().Interface().(**ast.List); ok && *pl != nil {
for _, n := range (*pl).Items {
if err := walkSQLite(n, sc); err != nil {
return err
}
}
continue
}
}
if f.CanInterface() {
if n, ok := f.Interface().(ast.Node); ok {
if err := walkSQLite(n, sc); err != nil {
return err
}
continue
}
}
if f.Kind() == reflect.Slice {
for j := 0; j < f.Len(); j++ {
elem := f.Index(j)
if elem.Kind() == reflect.Pointer && elem.IsNil() {
continue
}
if elem.CanInterface() {
if n, ok := elem.Interface().(ast.Node); ok {
if err := walkSQLite(n, sc); err != nil {
return err
}
}
}
}
}
next:
}
return nil
}