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

Add new noSelectStar analyzer #42

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/mattermost/mattermost-govet/v2/inconsistentReceiverName"
"github.com/mattermost/mattermost-govet/v2/license"
"github.com/mattermost/mattermost-govet/v2/mutexLock"
"github.com/mattermost/mattermost-govet/v2/noSelectStar"
"github.com/mattermost/mattermost-govet/v2/openApiSync"
"github.com/mattermost/mattermost-govet/v2/pointerToSlice"
"github.com/mattermost/mattermost-govet/v2/rawSql"
Expand Down Expand Up @@ -47,5 +48,6 @@ func main() {
pointerToSlice.Analyzer,
mutexLock.Analyzer,
wraperrors.Analyzer,
noSelectStar.Analyzer,
)
}
57 changes: 57 additions & 0 deletions noSelectStar/noSelectStar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package noSelectStar

import (
"go/ast"
"go/token"
"strings"

"golang.org/x/tools/go/analysis"
)

var Analyzer = &analysis.Analyzer{
Name: "noSelectStar",
Doc: "checks for SQL queries containing SELECT * which breaks forwards compatibility",
Run: run,
}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := func(node ast.Node) bool {
agnivade marked this conversation as resolved.
Show resolved Hide resolved
lit, ok := node.(*ast.BasicLit)
if !ok || lit.Kind != token.STRING {
return true
}

if strings.Contains(strings.ToUpper(lit.Value), "SELECT") &&
strings.Contains(lit.Value, "*") {
pass.Reportf(lit.Pos(), "do not use SELECT *: explicitly select the needed columns instead")
}
return true
}

inspectCalls := func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}

if fun, ok := call.Fun.(*ast.Ident); ok {
if fun.Name == "Select" || fun.Name == "Columns" {
// Check all arguments for "*"
for _, arg := range call.Args {
if lit, ok := arg.(*ast.BasicLit); ok &&
lit.Kind == token.STRING &&
strings.Contains(lit.Value, "*") {
pass.Reportf(lit.Pos(), "do not use %s with *: explicitly select the needed columns instead", fun.Name)
}
}
}
}
return true
}

for _, f := range pass.Files {
ast.Inspect(f, inspect)
ast.Inspect(f, inspectCalls)
}
return nil, nil
}
13 changes: 13 additions & 0 deletions noSelectStar/noSelectStar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package noSelectStar_test

import (
"testing"

"github.com/mattermost/mattermost-govet/v2/noSelectStar"
"golang.org/x/tools/go/analysis/analysistest"
)

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, noSelectStar.Analyzer, "a")
}
37 changes: 37 additions & 0 deletions noSelectStar/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package a

func Select(args ...string) {}
func Columns(args ...string) {}

func example() {
// These should trigger warnings
_ = "SELECT * FROM users" // want `do not use SELECT \*: explicitly select the needed columns instead`
_ = "select * from table" // want `do not use SELECT \*: explicitly select the needed columns instead`

// These should not trigger warnings
_ = "SELECT id, name FROM users"
_ = "Just a * by itself"
_ = "SELECT"

// These should trigger warnings for Select function
Select("*") // want `do not use Select with \*: explicitly select the needed columns instead`
Select("id, *") // want `do not use Select with \*: explicitly select the needed columns instead`
Select("id", "*", "name") // want `do not use Select with \*: explicitly select the needed columns instead`
Select("id", "name", "*") // want `do not use Select with \*: explicitly select the needed columns instead`

// These should not trigger warnings for Select function
Select("id, name")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a test case like:

query := s.getQueryBuilder().
		Select("*").
		From("Channels").
		Where(sq.Eq{"Id": ids})

to test for completeness?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch: the previous code was entirely incompatible with our actual pattern here. I confess to offloading too much to aider here: I suppose this is analogous to the self-driving car problem where the drivers risk not paying sufficient attention to take over when things don't go right.

I still used aider to fix this, but enhanced the test cases manually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good that we have humans to review code 😂

Select("")
Select("id", "name", "email")

// These should trigger warnings for Columns function
Columns("*") // want `do not use Columns with \*: explicitly select the needed columns instead`
Columns("id, *") // want `do not use Columns with \*: explicitly select the needed columns instead`
Columns("id", "*", "name") // want `do not use Columns with \*: explicitly select the needed columns instead`
Columns("id", "name", "*") // want `do not use Columns with \*: explicitly select the needed columns instead`

// These should not trigger warnings for Columns function
Columns("id, name")
Columns("")
Columns("id", "name", "email")
}
Loading