-
Notifications
You must be signed in to change notification settings - Fork 25
/
vet.go
781 lines (692 loc) · 20.3 KB
/
vet.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
package vet
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
pg_query "github.com/pganalyze/pg_query_go/v2"
"github.com/houqp/sqlvet/pkg/schema"
)
type VetContext struct {
Schema *schema.Db
}
type TableUsed struct {
Name string
Alias string
}
type ColumnUsed struct {
Column string
Table string
Location int32
}
type QueryParam struct {
Number int32
// TODO: also store related column type info for analysis
}
type ParseResult struct {
Columns []ColumnUsed
Params []QueryParam
}
// insert query param based on parameter number and avoid deduplications
func AddQueryParam(target *[]QueryParam, param QueryParam) {
params := *target
for i, p := range params {
if p.Number == param.Number {
// avoid duplicate params
return
} else if p.Number > param.Number {
*target = append(
params[:i],
append(
[]QueryParam{param},
params[i:]...,
)...,
)
return
}
}
*target = append(params, param)
}
func AddQueryParams(target *[]QueryParam, params []QueryParam) {
for _, p := range params {
AddQueryParam(target, p)
}
}
func DebugQuery(q string) {
b, _ := pg_query.ParseToJSON(q)
var pretty bytes.Buffer
json.Indent(&pretty, []byte(b), "\t", " ")
fmt.Println("query: " + q)
fmt.Println("parsed query: " + pretty.String())
}
func rangeVarToTableUsed(r *pg_query.RangeVar) TableUsed {
t := TableUsed{
Name: r.Relname,
}
if r.Alias != nil {
t.Alias = r.Alias.Aliasname
}
return t
}
/* =========================================================================================================
this is where the rewrite to nil checks rather than type refleciton starts
========================================================================================================= */
// return nil if no specific column is being referenced
func columnRefToColumnUsed(colRef *pg_query.ColumnRef) *ColumnUsed {
cu := ColumnUsed{
Location: colRef.Location,
}
var colField *pg_query.Node
if len(colRef.Fields) > 1 {
// in the form of SELECT table.column FROM table
cu.Table = colRef.Fields[0].GetString_().Str
// fmt.Printf("table: %s\n", cu.Table)
colField = colRef.Fields[1]
} else {
// in the form of SELECT column FROM table
colField = colRef.Fields[0]
}
switch {
case colField.GetString_() != nil:
cu.Column = colField.GetString_().GetStr()
case colField.GetAStar() != nil:
// SELECT *
return nil
default:
// FIXME: change to debug logging
panic(fmt.Sprintf("Unsupported ref field type: %s", reflect.TypeOf(colField)))
}
return &cu
}
func getUsedTablesFromJoinArg(arg *pg_query.Node) []TableUsed {
switch {
case arg.GetRangeVar() != nil:
return []TableUsed{rangeVarToTableUsed(arg.GetRangeVar())}
case arg.GetJoinExpr() != nil:
return append(
getUsedTablesFromJoinArg(arg.GetJoinExpr().GetLarg()),
getUsedTablesFromJoinArg(arg.GetJoinExpr().GetRarg())...)
default:
return []TableUsed{}
}
}
// extract used tables from FROM clause and JOIN clauses
func getUsedTablesFromSelectStmt(fromClauseList []*pg_query.Node) []TableUsed {
usedTables := []TableUsed{}
if len(fromClauseList) <= 0 {
// skip because no table is referenced in the query
return usedTables
}
for _, fromItem := range fromClauseList {
switch {
case fromItem.GetRangeVar() != nil:
// SELECT without JOIN
usedTables = append(usedTables, rangeVarToTableUsed(fromItem.GetRangeVar()))
case fromItem.GetJoinExpr() != nil:
// SELECT with one or more JOINs
usedTables = append(usedTables, getUsedTablesFromJoinArg(fromItem.GetJoinExpr().GetLarg())...)
usedTables = append(usedTables, getUsedTablesFromJoinArg(fromItem.GetJoinExpr().GetRarg())...)
}
}
return usedTables
}
func getUsedColumnsFromJoinQuals(quals *pg_query.Node) []ColumnUsed {
usedCols := []ColumnUsed{}
if quals.GetAExpr() != nil {
joinCond := quals.GetAExpr()
if lColRef := joinCond.GetLexpr().GetColumnRef(); lColRef != nil {
cu := columnRefToColumnUsed(lColRef)
if cu != nil {
usedCols = append(usedCols, *cu)
}
}
if rColRef := joinCond.GetRexpr().GetColumnRef(); rColRef != nil {
cu := columnRefToColumnUsed(rColRef)
if cu != nil {
usedCols = append(usedCols, *cu)
}
}
}
return usedCols
}
// todo this rewrite seems especially dubious
func getUsedColumnsFromJoinExpr(expr *pg_query.Node) []ColumnUsed {
usedCols := []ColumnUsed{}
if expr.GetJoinExpr() == nil {
return usedCols
}
joinExpr := expr.GetJoinExpr()
if larg := joinExpr.Larg; larg != nil {
usedCols = append(usedCols, getUsedColumnsFromJoinExpr(larg)...)
}
if rarg := joinExpr.Rarg; rarg != nil {
usedCols = append(usedCols, getUsedColumnsFromJoinExpr(rarg)...)
}
usedCols = append(usedCols, getUsedColumnsFromJoinQuals(joinExpr.Quals)...)
return usedCols
}
func getUsedColumnsFromJoinClauses(fromClauseList []*pg_query.Node) []ColumnUsed {
usedCols := []ColumnUsed{}
if len(fromClauseList) <= 0 {
// skip because no table is referenced in the query, which means there
// is no Join clause
return usedCols
}
for _, fromItem := range fromClauseList {
switch {
case fromItem.GetRangeVar() != nil:
// SELECT without JOIN
continue
case fromItem.GetJoinExpr() != nil:
// SELECT with one or more JOINs
usedCols = append(usedCols, getUsedColumnsFromJoinExpr(fromItem)...)
}
}
return usedCols
}
func getUsedColumnsFromReturningList(returningList []*pg_query.Node) []ColumnUsed {
usedCols := []ColumnUsed{}
for _, node := range returningList {
target := node.GetResTarget()
if target == nil {
continue
}
switch {
// case pg_query.ColumnRef:
case target.Val.GetColumnRef() != nil:
cu := columnRefToColumnUsed(target.Val.GetColumnRef())
if cu == nil {
continue
}
usedCols = append(usedCols, *cu)
default:
// do nothing if no column is referenced
}
}
return usedCols
}
func validateTable(ctx VetContext, tname string) error {
if ctx.Schema == nil {
return nil
}
_, ok := ctx.Schema.Tables[tname]
if !ok {
return fmt.Errorf("invalid table name: %s", tname)
}
return nil
}
func validateTableColumns(ctx VetContext, tables []TableUsed, cols []ColumnUsed) error {
if ctx.Schema == nil {
return nil
}
var ok bool
usedTables := map[string]schema.Table{}
for _, tu := range tables {
usedTables[tu.Name], ok = ctx.Schema.Tables[tu.Name]
if !ok {
return fmt.Errorf("invalid table name: %s", tu.Name)
}
if tu.Alias != "" {
usedTables[tu.Alias] = usedTables[tu.Name]
}
}
for _, col := range cols {
if col.Table != "" {
table, ok := usedTables[col.Table]
if !ok {
return fmt.Errorf("table `%s` not available for query", col.Table)
}
_, ok = table.Columns[col.Column]
if !ok {
return fmt.Errorf("column `%s` is not defined in table `%s`", col.Column, col.Table)
}
} else {
// no table prefix, try all tables
found := false
for _, table := range usedTables {
_, ok = table.Columns[col.Column]
if ok {
found = true
break
}
}
if !found {
if len(tables) == 1 {
// to make error message more useful, if only one table is
// referenced in the query, it's safe to assume user only
// want to use columns from that table.
return fmt.Errorf(
"column `%s` is not defined in table `%s`",
col.Column, tables[0].Name)
} else {
return fmt.Errorf(
"column `%s` is not defined in any of the table available for query",
col.Column)
}
}
}
}
return nil
}
func validateInsertValues(ctx VetContext, cols []ColumnUsed, vals []*pg_query.Node) error {
colCnt := len(cols)
// val could be nodes.ParamRef
valCnt := len(vals)
if colCnt != valCnt {
return fmt.Errorf("column count %d doesn't match value count %d", colCnt, valCnt)
}
return nil
}
func parseWindowDef(ctx VetContext, winDef *pg_query.WindowDef, parseRe *ParseResult) error {
if len(winDef.PartitionClause) > 0 {
// TODO should this be [0]
if err := parseExpression(ctx, winDef.GetPartitionClause()[0], parseRe); err != nil {
return err
}
}
if len(winDef.OrderClause) > 0 {
// TODO should this be [0]
if err := parseExpression(ctx, winDef.OrderClause[0], parseRe); err != nil {
return err
}
}
return nil
}
// recursive function to parse expressions including nested expressions
func parseExpression(ctx VetContext, clause *pg_query.Node, parseRe *ParseResult) error {
switch {
case clause.GetAExpr() != nil:
if clause.GetAExpr().GetLexpr() != nil {
err := parseExpression(ctx, clause.GetAExpr().GetLexpr(), parseRe)
if err != nil {
return err
}
}
if clause.GetAExpr().GetRexpr() != nil {
err := parseExpression(ctx, clause.GetAExpr().GetRexpr(), parseRe)
if err != nil {
return err
}
}
case clause.GetBoolExpr() != nil:
// TODO should this be args or args[0]?
return parseExpression(ctx, clause.GetBoolExpr().Args[0], parseRe)
case clause.GetNullTest() != nil:
nullTest := clause.GetNullTest()
if arg := nullTest.GetArg(); arg != nil {
return parseExpression(ctx, arg, parseRe)
}
case clause.GetColumnRef() != nil:
// cu := columnRefToColumnUsed(expr)
cu := columnRefToColumnUsed(clause.GetColumnRef())
if cu == nil {
return nil
}
parseRe.Columns = append(parseRe.Columns, *cu)
case clause.GetParamRef() != nil:
// WHERE id=$1
AddQueryParam(&parseRe.Params, QueryParam{Number: clause.GetParamRef().GetNumber()})
case clause.GetAConst() != nil:
// WHERE 1
case clause.GetFuncCall() != nil:
// WHERE date=NOW()
// WHERE MAX(id) > 1
// TODO should this be args or args[0]?
funcCall := clause.GetFuncCall()
if len(funcCall.Args) > 0 {
if err := parseExpression(ctx, funcCall.Args[0], parseRe); err != nil {
return err
}
}
// SELECT ROW_NUMBER() OVER (PARTITION BY id)
if clause.GetFuncCall().GetOver() != nil {
// TODO dubious rewrite and should this be [0]
over := clause.GetFuncCall().GetOver()
if len(over.PartitionClause) > 0 {
err := parseExpression(ctx, over.PartitionClause[0], parseRe)
if err != nil {
return err
}
}
}
case clause.GetTypeCast() != nil:
// WHERE foo=True
return parseExpression(ctx, clause.GetTypeCast().Arg, parseRe)
case clause.GetList() != nil:
// WHERE id IN (1, 2, 3)
for _, item := range clause.GetList().Items {
err := parseExpression(ctx, item, parseRe)
if err != nil {
return err
}
}
case clause.GetSubLink() != nil:
// WHERE id IN (SELECT id FROM foo)
subselect := clause.GetSubLink().GetSubselect()
if subselect.GetSelectStmt() == nil {
return fmt.Errorf(
"unsupported subquery type: %v", subselect)
}
queryParams, err := validateSelectStmt(ctx, subselect.GetSelectStmt())
if err != nil {
return err
}
if len(queryParams) > 0 {
AddQueryParams(&parseRe.Params, queryParams)
}
case clause.GetCoalesceExpr() != nil:
// TODO should this be [0]?
return parseExpression(ctx, clause.GetCoalesceExpr().GetArgs()[0], parseRe)
case clause.GetWindowDef() != nil:
return parseWindowDef(ctx, clause.GetWindowDef(), parseRe)
case clause.GetSortBy() != nil:
return parseExpression(ctx, clause.GetSortBy().Node, parseRe)
default:
return fmt.Errorf(
"unsupported expression, found node of type: %v",
reflect.TypeOf(clause),
)
}
return nil
}
// find used column names from where clause
func parseWhereClause(ctx VetContext, clause *pg_query.Node, parseRe *ParseResult) error {
err := parseExpression(ctx, clause, parseRe)
if err != nil {
err = fmt.Errorf("invalid WHERE clause: %w", err)
}
return err
}
func getUsedColumnsFromNodeList(nodelist []*pg_query.Node) []ColumnUsed {
usedCols := []ColumnUsed{}
for _, item := range nodelist {
if item.GetColumnRef() != nil {
cu := columnRefToColumnUsed(item.GetColumnRef())
if cu != nil {
usedCols = append(usedCols, *cu)
}
}
}
return usedCols
}
func getUsedColumnsFromSortClause(sortList []*pg_query.Node) []ColumnUsed {
usedCols := []ColumnUsed{}
for _, item := range sortList {
if item.GetSortBy() != nil {
if colRef := item.GetSortBy().GetNode().GetColumnRef(); colRef != nil {
cu := columnRefToColumnUsed(colRef)
if cu != nil {
usedCols = append(usedCols, *cu)
}
}
}
}
return usedCols
}
func validateSelectStmt(ctx VetContext, stmt *pg_query.SelectStmt) ([]QueryParam, error) {
usedTables := getUsedTablesFromSelectStmt(stmt.FromClause)
usedCols := []ColumnUsed{}
queryParams := []QueryParam{}
for _, item := range stmt.TargetList {
if item.GetResTarget() == nil {
continue
}
re := &ParseResult{}
err := parseExpression(ctx, item.GetResTarget().Val, re)
if err != nil {
return nil, err
}
if len(re.Columns) > 0 {
usedCols = append(usedCols, re.Columns...)
}
if len(re.Params) > 0 {
AddQueryParams(&queryParams, re.Params)
}
}
usedCols = append(usedCols, getUsedColumnsFromJoinClauses(stmt.FromClause)...)
if stmt.WhereClause != nil {
re := &ParseResult{}
err := parseWhereClause(ctx, stmt.WhereClause, re)
if err != nil {
return nil, err
}
if len(re.Columns) > 0 {
usedCols = append(usedCols, re.Columns...)
}
if len(re.Params) > 0 {
AddQueryParams(&queryParams, re.Params)
}
}
if len(stmt.GroupClause) > 0 {
usedCols = append(usedCols, getUsedColumnsFromNodeList(stmt.GroupClause)...)
}
if stmt.HavingClause != nil {
re := &ParseResult{}
err := parseExpression(ctx, stmt.HavingClause, re)
if err != nil {
return nil, err
}
if len(re.Columns) > 0 {
usedCols = append(usedCols, re.Columns...)
}
if len(re.Params) > 0 {
AddQueryParams(&queryParams, re.Params)
}
}
if len(stmt.WindowClause) > 0 {
re := &ParseResult{}
// TODO: should this be [0]?
err := parseExpression(ctx, stmt.WindowClause[0], re)
if err != nil {
return nil, err
}
usedCols = append(usedCols, re.Columns...)
AddQueryParams(&queryParams, re.Params)
}
if len(stmt.SortClause) > 0 {
usedCols = append(usedCols, getUsedColumnsFromSortClause(stmt.SortClause)...)
}
return queryParams, validateTableColumns(ctx, usedTables, usedCols)
}
func validateUpdateStmt(ctx VetContext, stmt *pg_query.UpdateStmt) ([]QueryParam, error) {
tableName := stmt.Relation.Relname
usedTables := []TableUsed{{Name: tableName}}
usedTables = append(usedTables, getUsedTablesFromSelectStmt(stmt.FromClause)...)
usedCols := []ColumnUsed{}
queryParams := []QueryParam{}
for _, item := range stmt.TargetList {
target := item.GetResTarget()
usedCols = append(usedCols, ColumnUsed{
Table: tableName,
Column: target.Name,
Location: target.Location,
})
switch {
case target.Val != nil && target.Val.GetColumnRef() != nil:
cu := columnRefToColumnUsed(target.Val.GetColumnRef())
if cu != nil {
usedCols = append(usedCols, *cu)
}
case target.Val != nil && target.Val.GetParamRef() != nil:
AddQueryParam(&queryParams, QueryParam{Number: target.Val.GetParamRef().Number})
}
}
if stmt.WhereClause != nil {
re := &ParseResult{}
err := parseWhereClause(ctx, stmt.WhereClause, re)
if err != nil {
return nil, err
}
usedCols = append(usedCols, re.Columns...)
AddQueryParams(&queryParams, re.Params)
}
if len(stmt.ReturningList) > 0 {
usedCols = append(usedCols, getUsedColumnsFromReturningList(stmt.ReturningList)...)
}
return queryParams, validateTableColumns(ctx, usedTables, usedCols)
}
func validateInsertStmt(ctx VetContext, stmt *pg_query.InsertStmt) ([]QueryParam, error) {
tableName := stmt.Relation.Relname
usedTables := []TableUsed{{Name: tableName}}
targetCols := []ColumnUsed{}
for _, item := range stmt.Cols {
target := item.GetResTarget()
targetCols = append(targetCols, ColumnUsed{
Table: tableName,
Column: target.Name,
Location: target.Location,
})
}
values := []*pg_query.Node{}
// make a copy of targetCols because we need it to do value count
// validation separately
usedCols := append([]ColumnUsed{}, targetCols...)
queryParams := []QueryParam{}
selectStmt := stmt.GetSelectStmt().GetSelectStmt()
if selectStmt.ValuesLists != nil {
/*
* In the form of:
* INSERT INTO table (col1, col2) VALUES (val1, val2)
*
* In a "leaf" node representing a VALUES list, the above fields are all
* null, and instead this field is set. Note that the elements of the
* sublists are just expressions, without ResTarget decoration. Also note
* that a list element can be DEFAULT (represented as a SetToDefault
* node), regardless of the context of the VALUES list. It's up to parse
* analysis to reject that where not valid.
*/
for _, list := range selectStmt.GetValuesLists() {
for _, node := range list.GetList().Items {
re := &ParseResult{}
err := parseExpression(ctx, node, re)
if err != nil {
return nil, fmt.Errorf("invalid value list: %w", err)
}
if len(re.Columns) > 0 {
usedCols = append(usedCols, re.Columns...)
}
if len(re.Params) > 0 {
AddQueryParams(&queryParams, re.Params)
}
values = append(values, node)
}
}
} else {
/*
* Value from SELECT, in the form of:
* INSERT INTO table (col1, col2) SELECT (col1, col2) FROM table
*/
usedTables = append(usedTables, getUsedTablesFromSelectStmt(selectStmt.FromClause)...)
usedCols = append(
usedCols, getUsedColumnsFromJoinClauses(selectStmt.FromClause)...)
if selectStmt.WhereClause != nil {
re := &ParseResult{}
err := parseWhereClause(ctx, selectStmt.WhereClause, re)
if err != nil {
return nil, err
}
if len(re.Columns) > 0 {
usedCols = append(usedCols, re.Columns...)
}
if len(re.Params) > 0 {
AddQueryParams(&queryParams, re.Params)
}
}
for _, item := range selectStmt.TargetList {
target := item.GetResTarget().Val
values = append(values, target)
switch {
case target.GetColumnRef() != nil:
cu := columnRefToColumnUsed(target.GetColumnRef())
if cu == nil {
continue
}
usedCols = append(usedCols, *cu)
case target.GetSubLink() != nil:
tv := target.GetSubLink().Subselect
if tv.GetSelectStmt() == nil {
return nil, fmt.Errorf(
"unsupported subquery type in value list: %s", reflect.TypeOf(tv))
}
qparams, err := validateSelectStmt(ctx, tv.GetSelectStmt())
if err != nil {
return nil, fmt.Errorf("invalid SELECT query in value list: %w", err)
}
if len(qparams) > 0 {
AddQueryParams(&queryParams, qparams)
}
}
}
}
if len(stmt.ReturningList) > 0 {
usedCols = append(usedCols, getUsedColumnsFromReturningList(stmt.ReturningList)...)
}
if err := validateTableColumns(ctx, usedTables, usedCols); err != nil {
return nil, err
}
if err := validateInsertValues(ctx, targetCols, values); err != nil {
return nil, err
}
return queryParams, nil
}
func validateDeleteStmt(ctx VetContext, stmt *pg_query.DeleteStmt) ([]QueryParam, error) {
tableName := stmt.Relation.Relname
if err := validateTable(ctx, tableName); err != nil {
return nil, err
}
usedCols := []ColumnUsed{}
queryParams := []QueryParam{}
if stmt.WhereClause != nil {
re := &ParseResult{}
err := parseWhereClause(ctx, stmt.WhereClause, re)
if err != nil {
return nil, err
}
if len(re.Columns) > 0 {
usedCols = append(usedCols, re.Columns...)
}
if len(re.Params) > 0 {
queryParams = re.Params
}
}
if len(stmt.ReturningList) > 0 {
usedCols = append(
usedCols, getUsedColumnsFromReturningList(stmt.ReturningList)...)
}
if len(usedCols) > 0 {
usedTables := []TableUsed{{Name: tableName}}
if err := validateTableColumns(ctx, usedTables, usedCols); err != nil {
return nil, err
}
}
return queryParams, nil
}
func ValidateSqlQuery(ctx VetContext, queryStr string) ([]QueryParam, error) {
tree, err := pg_query.Parse(queryStr)
if err != nil {
return nil, err
}
if len(tree.Stmts) == 0 || len(tree.Stmts) > 1 {
return nil, fmt.Errorf("query contained more than one statement")
}
var raw *pg_query.RawStmt = tree.Stmts[0]
switch {
case raw.Stmt.GetSelectStmt() != nil:
return validateSelectStmt(ctx, raw.Stmt.GetSelectStmt())
case raw.Stmt.GetUpdateStmt() != nil:
return validateUpdateStmt(ctx, raw.Stmt.GetUpdateStmt())
case raw.Stmt.GetInsertStmt() != nil:
return validateInsertStmt(ctx, raw.Stmt.GetInsertStmt())
case raw.Stmt.GetDeleteStmt() != nil:
return validateDeleteStmt(ctx, raw.Stmt.GetDeleteStmt())
case raw.Stmt.GetDropStmt() != nil:
case raw.Stmt.GetTruncateStmt() != nil:
case raw.Stmt.GetAlterTableStmt() != nil:
case raw.Stmt.GetCreateSchemaStmt() != nil:
case raw.Stmt.GetVariableSetStmt() != nil:
// TODO: check for invalid pg variables
default:
return nil, fmt.Errorf("unsupported statement: %v", reflect.TypeOf(raw.Stmt))
}
return nil, nil
}