Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support join select #29

Merged
merged 3 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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