@@ -34,6 +34,17 @@ var ErrFailedChecks = errors.New("failed checks")
34
34
const RuleDbPrepare = "sqlc/db-prepare"
35
35
const QueryFlagSqlcVetDisable = "@sqlc-vet-disable"
36
36
37
+ type emptyProgram struct {
38
+ }
39
+
40
+ func (e * emptyProgram ) Eval (any ) (ref.Val , * cel.EvalDetails , error ) {
41
+ return nil , nil , fmt .Errorf ("unimplemented" )
42
+ }
43
+
44
+ func (e * emptyProgram ) ContextEval (ctx context.Context , a any ) (ref.Val , * cel.EvalDetails , error ) {
45
+ return e .Eval (a )
46
+ }
47
+
37
48
func NewCmdVet () * cobra.Command {
38
49
return & cobra.Command {
39
50
Use : "vet" ,
@@ -53,17 +64,6 @@ func NewCmdVet() *cobra.Command {
53
64
}
54
65
}
55
66
56
- type emptyProgram struct {
57
- }
58
-
59
- func (e * emptyProgram ) Eval (any ) (ref.Val , * cel.EvalDetails , error ) {
60
- return nil , nil , fmt .Errorf ("unimplemented" )
61
- }
62
-
63
- func (e * emptyProgram ) ContextEval (ctx context.Context , a any ) (ref.Val , * cel.EvalDetails , error ) {
64
- return e .Eval (a )
65
- }
66
-
67
67
func Vet (ctx context.Context , e Env , dir , filename string , stderr io.Writer ) error {
68
68
configPath , conf , err := readConfig (stderr , dir , filename )
69
69
if err != nil {
@@ -100,7 +100,7 @@ func Vet(ctx context.Context, e Env, dir, filename string, stderr io.Writer) err
100
100
}
101
101
102
102
checks := map [string ]cel.Program {
103
- RuleDbPrepare : & emptyProgram {},
103
+ RuleDbPrepare : & emptyProgram {}, // Keep this to trigger the name conflict error below
104
104
}
105
105
msgs := map [string ]string {}
106
106
@@ -198,7 +198,8 @@ type dbPreparer struct {
198
198
}
199
199
200
200
func (p * dbPreparer ) Prepare (ctx context.Context , name , query string ) error {
201
- _ , err := p .db .PrepareContext (ctx , query )
201
+ s , err := p .db .PrepareContext (ctx , query )
202
+ s .Close ()
202
203
return err
203
204
}
204
205
@@ -316,12 +317,15 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
316
317
continue
317
318
}
318
319
original := result .Queries [i ]
319
- if prepareable (s , original .RawStmt ) {
320
- name := fmt .Sprintf ("sqlc_vet_%d_%d" , time .Now ().Unix (), i )
321
- if err := prep .Prepare (ctx , name , query .Text ); err != nil {
322
- fmt .Fprintf (c .Stderr , "%s: %s: %s: error preparing query: %s\n " , query .Filename , q .Name , name , err )
323
- errored = true
324
- }
320
+ if ! prepareable (s , original .RawStmt ) {
321
+ fmt .Fprintf (c .Stderr , "%s: %s: %s: error preparing query: %s\n " , query .Filename , q .Name , name , "query type is unpreparable" )
322
+ errored = true
323
+ continue
324
+ }
325
+ name := fmt .Sprintf ("sqlc_vet_%d_%d" , time .Now ().Unix (), i )
326
+ if err := prep .Prepare (ctx , name , query .Text ); err != nil {
327
+ fmt .Fprintf (c .Stderr , "%s: %s: %s: error preparing query: %s\n " , query .Filename , q .Name , name , err )
328
+ errored = true
325
329
}
326
330
continue
327
331
}
0 commit comments