goone

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2021 License: MIT Imports: 15 Imported by: 8

README

test_and_lint

go_one

go_one finds N+1(strictly speaking call SQL in a for loop) query in go

Example

package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
)


type Person struct {
	Name string
	JobID int
}

type Job struct {
	JobID int
	Name string
}

func main(){

	cnn, _ := sql.Open("mysql", "user:password@tcp(host:port)/dbname")

	rows, _ := cnn.Query("SELECT name, job_id FROM persons")

	defer rows.Close()

	for rows.Next() {
		var person Person
		if err := rows.Scan(&person.Name,&person.JobID); err != nil {
			log.Fatal(err)
		}

		var job Job

        // This is N+1 query
		if err := cnn.QueryRow("SELECT job_id, name FROM Jobs WHERE job_id = ?",person.JobID).Scan(&job.JobID,&job.Name); err != nil { 
			log.Fatal(err)
		}
		fmt.Println(person.Name,job.Name)
	}

}

output

./hoge.go:38:13: this query is called in a loop

Install

go get github.com/masibw/goone/cmd/go_one

Usage

bash

go vet -vettool=`which go_one` ./...

fish

go vet -vettool=(which go_one) ./...

Library Support

  • sql
  • sqlx
  • gorp
  • gorm

You can add types to detect sql query.

Options

You can use the -go_one.configPath option at runtime to determine if you want to use a specified types.

Example

If go_one.yml exists in the directory where the command was executed

go vet -vettool=`which go_one` -go_one.configPath="$PWD/go_one.yml" ./...

You can also detect the case where an interface is in between by writing below. example project

package:
  - pkgName: 'github.com/masibw/go_todo/cmd/go_todo/infrastructure/api/handler'
    typeNames:
      - typeName: '*todoHandler'

Contribute

You're welcome to build an Issue or create a PR and be proactive!

Caution

This tool does not support calls to functions from other packages in a for loop

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "go_one",
	Doc:  doc,
	Run:  run,

	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
}

Analyzer is analysis files

Functions

This section is empty.

Types

type FuncCache

type FuncCache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewFuncCache

func NewFuncCache() *FuncCache

func (*FuncCache) Exists

func (m *FuncCache) Exists(key token.Pos) bool

func (*FuncCache) Get

func (m *FuncCache) Get(key token.Pos) bool

func (*FuncCache) Set

func (m *FuncCache) Set(key token.Pos, value bool)

type ReportCache

type ReportCache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewReportCache

func NewReportCache() *ReportCache

func (*ReportCache) Get

func (m *ReportCache) Get(pass *analysis.Pass, pos token.Pos) bool

func (*ReportCache) Set

func (m *ReportCache) Set(pass *analysis.Pass, pos token.Pos, value bool)

type SearchCache

type SearchCache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewSearchCache

func NewSearchCache() *SearchCache

func (*SearchCache) Get

func (m *SearchCache) Get(key token.Pos) bool

func (*SearchCache) Set

func (m *SearchCache) Set(key token.Pos, value bool)

type Types

type Types struct {
	Package []struct {
		PkgName   string `yaml:"pkgName"`
		TypeNames []struct {
			TypeName string `yaml:"typeName"`
		} `yaml:"typeNames"`
	} `yaml:"package"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL