Skip to content

Commit

Permalink
support join select (#29)
Browse files Browse the repository at this point in the history
* support join select

* fix outer table link in select

* collect tables from select to inner query schema
  • Loading branch information
valichek authored May 14, 2023
1 parent f887b7a commit 443693f
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 148 deletions.
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ func (s *SQLVet) reportError(format string, a ...interface{}) {
// Vet performs static analysis
func (s *SQLVet) Vet() {
queries, err := vet.CheckDir(
vet.VetContext{
Schema: s.Schema,
},
vet.NewContext(s.Schema.Tables),
s.ProjectRoot,
s.Cfg.BuildFlags,
s.Cfg.SqlFuncMatchers,
Expand Down
71 changes: 35 additions & 36 deletions pkg/schema/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,51 +91,50 @@ func parsePostgresSchema(schemaInput string) (map[string]Table, error) {
continue
}

if resTarget.Name != "" {
table.Columns[resTarget.Name] = Column{
Name: resTarget.Name,
}
continue
if col, ok := GetResTargetColumn(resTarget); ok {
table.Columns[col.Name] = col
}
}

if resTarget.Val == nil {
continue
}
tables[tableName] = table
}
}

colRef := resTarget.Val.GetColumnRef()
if colRef == nil {
// parse only column references when no alias is provided
continue
}
return tables, nil
}

var colField *pg_query.Node
if len(colRef.Fields) > 0 {
colField = colRef.Fields[len(colRef.Fields)-1]
}
func GetResTargetColumn(resTarget *pg_query.ResTarget) (col Column, ok bool) {
if resTarget.Name != "" {
return Column{Name: resTarget.Name}, true
}

if colField == nil {
continue
}
if resTarget.Val == nil {
return
}

if colField.GetAStar() != nil {
// SELECT * - force parsing explicit columns for simplicity
continue
}
colRef := resTarget.Val.GetColumnRef()
if colRef == nil {
// parse only column references when no alias is provided
return
}

if colField.GetString_() == nil {
continue
}
var colField *pg_query.Node
if len(colRef.Fields) > 0 {
colField = colRef.Fields[len(colRef.Fields)-1]
}

colName := colField.GetString_().GetStr()
table.Columns[colName] = Column{
Name: colName,
Type: "", // type not set, never used for validation
}
}
if colField == nil {
return
}

tables[tableName] = table
}
if colField.GetAStar() != nil {
// SELECT * - force parsing explicit columns for simplicity
return
}

return tables, nil
if colField.GetString_() == nil {
return
}

return Column{Name: colField.GetString_().GetStr()}, true
}
2 changes: 1 addition & 1 deletion pkg/vet/gosource.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func handleQuery(ctx VetContext, qs *QuerySite) {
}

var queryParams []QueryParam
queryParams, qs.Err = ValidateSqlQuery(ctx, qs.Query)
queryParams, qs.Err = ValidateSqlQuery(NewContext(ctx.Schema.Tables), qs.Query)

if qs.Err != nil {
return
Expand Down
Loading

0 comments on commit 443693f

Please sign in to comment.