Skip to content

Commit c7859ec

Browse files
committed
Improve SQL tests
Separate the tests for Squirrel and database/sql. Add tests for database/sql/driver.
1 parent 501bb3e commit c7859ec

File tree

3 files changed

+96
-62
lines changed

3 files changed

+96
-62
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"database/sql/driver"
6+
)
7+
8+
var (
9+
driver1 string
10+
driver2 string
11+
driver3 string
12+
driver4 string
13+
driver5 string
14+
driver6 string
15+
)
16+
17+
func testDriver(ctx context.Context, execer driver.Execer,
18+
execerContext driver.ExecerContext,
19+
conn driver.Conn,
20+
connContext driver.ConnPrepareContext,
21+
queryer driver.Queryer,
22+
queryerContext driver.QueryerContext) {
23+
execer.Exec(driver1, nil) // $ query=driver1
24+
execerContext.ExecContext(ctx, driver2, nil) // $ query=driver2
25+
conn.Prepare(driver3) // $ querystring=driver3
26+
connContext.PrepareContext(ctx, driver4) // $ querystring=driver4
27+
queryer.Query(driver5, nil) // $ query=driver5
28+
queryerContext.QueryContext(ctx, driver6, nil) // $ query=driver6
29+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
//go:generate depstubber -vendor github.com/Masterminds/squirrel DeleteBuilder,InsertBuilder,SelectBuilder,UpdateBuilder Delete,Expr,Insert,Select,Update
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
)
9+
10+
var (
11+
query1 string
12+
query2 string
13+
query3 string
14+
query4 string
15+
query5 string
16+
query6 string
17+
query7 string
18+
query8 string
19+
query11 string
20+
query12 string
21+
query13 string
22+
query14 string
23+
query15 string
24+
query16 string
25+
query17 string
26+
query18 string
27+
query21 string
28+
query22 string
29+
query23 string
30+
)
31+
32+
func test(db *sql.DB, ctx context.Context) {
33+
db.Exec(query1) // $ query=query1
34+
db.ExecContext(ctx, query2) // $ query=query2
35+
db.Prepare(query3) // $ querystring=query3
36+
db.PrepareContext(ctx, query4) // $ querystring=query4
37+
db.Query(query5) // $ query=query5
38+
db.QueryContext(ctx, query6) // $ query=query6
39+
db.QueryRow(query7) // $ query=query7
40+
db.QueryRowContext(ctx, query8) // $ query=query8
41+
}
42+
43+
func test2(tx *sql.Tx, query string, ctx context.Context) {
44+
tx.Exec(query11) // $ query=query11
45+
tx.ExecContext(ctx, query12) // $ query=query12
46+
tx.Prepare(query13) // $ querystring=query13
47+
tx.PrepareContext(ctx, query14) // $ querystring=query14
48+
tx.Query(query15) // $ query=query15
49+
tx.QueryContext(ctx, query16) // $ query=query16
50+
tx.QueryRow(query17) // $ query=query17
51+
tx.QueryRowContext(ctx, query18) // $ query=query18
52+
}
53+
54+
func test3(db *sql.DB, ctx context.Context) {
55+
stmt1, _ := db.Prepare(query21) // $ SPURIOUS: querystring=query21
56+
stmt1.Exec() // $ MISSING: query=query21
57+
stmt2, _ := db.PrepareContext(ctx, query22) // $ SPURIOUS: querystring=query22
58+
stmt2.ExecContext(ctx) // $ MISSING: query=query22
59+
stmt3, _ := db.Prepare(query23) // $ SPURIOUS: querystring=query23
60+
runQuery(stmt3)
61+
}
62+
63+
func runQuery(stmt *sql.Stmt) {
64+
stmt.Exec() // $ MISSING: query=query23
65+
}
66+
67+
func main() {}

go/ql/test/library-tests/semmle/go/frameworks/SQL/main.go renamed to go/ql/test/library-tests/semmle/go/frameworks/SQL/squirrel.go

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,9 @@ package main
33
//go:generate depstubber -vendor github.com/Masterminds/squirrel DeleteBuilder,InsertBuilder,SelectBuilder,UpdateBuilder Delete,Expr,Insert,Select,Update
44

55
import (
6-
"context"
7-
"database/sql"
8-
96
"github.com/Masterminds/squirrel"
107
)
118

12-
var (
13-
query1 string
14-
query2 string
15-
query3 string
16-
query4 string
17-
query5 string
18-
query6 string
19-
query7 string
20-
query8 string
21-
query11 string
22-
query12 string
23-
query13 string
24-
query14 string
25-
query15 string
26-
query16 string
27-
query17 string
28-
query18 string
29-
query21 string
30-
query22 string
31-
query23 string
32-
)
33-
34-
func test(db *sql.DB, ctx context.Context) {
35-
db.Exec(query1) // $ query=query1
36-
db.ExecContext(ctx, query2) // $ query=query2
37-
db.Prepare(query3) // $ querystring=query3
38-
db.PrepareContext(ctx, query4) // $ querystring=query4
39-
db.Query(query5) // $ query=query5
40-
db.QueryContext(ctx, query6) // $ query=query6
41-
db.QueryRow(query7) // $ query=query7
42-
db.QueryRowContext(ctx, query8) // $ query=query8
43-
}
44-
459
func squirrelTest(querypart string) {
4610
squirrel.Expr(querypart) // $ querystring=querypart
4711
deleteBuilder := squirrel.Delete(querypart) // $ querystring=querypart
@@ -81,29 +45,3 @@ func squirrelTest(querypart string) {
8145
updateBuilder.Set(querypart, "") // $ querystring=querypart
8246
updateBuilder.Table(querypart) // $ querystring=querypart
8347
}
84-
85-
func test2(tx *sql.Tx, query string, ctx context.Context) {
86-
tx.Exec(query11) // $ query=query11
87-
tx.ExecContext(ctx, query12) // $ query=query12
88-
tx.Prepare(query13) // $ querystring=query13
89-
tx.PrepareContext(ctx, query14) // $ querystring=query14
90-
tx.Query(query15) // $ query=query15
91-
tx.QueryContext(ctx, query16) // $ query=query16
92-
tx.QueryRow(query17) // $ query=query17
93-
tx.QueryRowContext(ctx, query18) // $ query=query18
94-
}
95-
96-
func test3(db *sql.DB, ctx context.Context) {
97-
stmt1, _ := db.Prepare(query21) // $ SPURIOUS: querystring=query21
98-
stmt1.Exec() // $ MISSING: query=query21
99-
stmt2, _ := db.PrepareContext(ctx, query22) // $ SPURIOUS: querystring=query22
100-
stmt2.ExecContext(ctx) // $ MISSING: query=query22
101-
stmt3, _ := db.Prepare(query23) // $ SPURIOUS: querystring=query23
102-
runQuery(stmt3)
103-
}
104-
105-
func runQuery(stmt *sql.Stmt) {
106-
stmt.Exec() // $ MISSING: query=query23
107-
}
108-
109-
func main() {}

0 commit comments

Comments
 (0)