-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
60 lines (60 loc) · 1.66 KB
/
Copy pathdoc.go
File metadata and controls
60 lines (60 loc) · 1.66 KB
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
// Package n1detect provides a Go static analysis tool that detects N+1 database
// query patterns in Go source code.
//
// # What is an N+1 Query?
//
// An N+1 query occurs when code executes one query to retrieve a list of N items
// and then executes an additional query for each item, resulting in N+1 total
// queries. This is a common performance anti-pattern.
//
// Example of an N+1 pattern:
//
// rows, _ := db.Query("SELECT id FROM users")
// for rows.Next() {
// var id int
// rows.Scan(&id)
// db.QueryRow("SELECT * FROM orders WHERE user_id = ?", id) // N+1 here!
// }
//
// Instead, prefer a JOIN or an IN clause:
//
// db.Query("SELECT u.id, o.* FROM users u JOIN orders o ON o.user_id = u.id")
//
// # Installation and Usage
//
// Install the standalone binary:
//
// go install github.com/renaldid/n1detect/cmd/n1detect@latest
//
// Run against your packages:
//
// n1detect ./...
//
// # Supported Libraries
//
// The following database libraries are detected out of the box:
//
// - database/sql (DB, Tx, Conn)
// - gorm.io/gorm (DB)
// - github.com/jackc/pgx/v5 (Conn)
// - github.com/jackc/pgx/v5/pgxpool (Pool)
// - github.com/jmoiron/sqlx (DB, Tx)
//
// # Custom Patterns
//
// You can extend detection with your own patterns using WithPatterns:
//
// myPattern := n1detect.Pattern{
// PkgPath: "myorg/mydb",
// TypeName: "Client",
// Methods: []string{"Query", "Exec"},
// }
// analyzer := n1detect.WithPatterns(myPattern)
//
// # Integration with go vet
//
// n1detect uses the golang.org/x/tools/go/analysis framework, which means it
// integrates natively with go vet:
//
// go vet -vettool=$(which n1detect) ./...
package n1detect