Skip to content

Commit

Permalink
fix(vet): report an error when a query is unpreparable, close prepare…
Browse files Browse the repository at this point in the history
…d statement connection (#2486)
  • Loading branch information
andrewmbenton committed Jul 20, 2023
1 parent dddfe4f commit 5348b09
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions internal/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ var ErrFailedChecks = errors.New("failed checks")
const RuleDbPrepare = "sqlc/db-prepare"
const QueryFlagSqlcVetDisable = "@sqlc-vet-disable"

type emptyProgram struct {
}

func (e *emptyProgram) Eval(any) (ref.Val, *cel.EvalDetails, error) {
return nil, nil, fmt.Errorf("unimplemented")
}

func (e *emptyProgram) ContextEval(ctx context.Context, a any) (ref.Val, *cel.EvalDetails, error) {
return e.Eval(a)
}

func NewCmdVet() *cobra.Command {
return &cobra.Command{
Use: "vet",
Expand All @@ -53,17 +64,6 @@ func NewCmdVet() *cobra.Command {
}
}

type emptyProgram struct {
}

func (e *emptyProgram) Eval(any) (ref.Val, *cel.EvalDetails, error) {
return nil, nil, fmt.Errorf("unimplemented")
}

func (e *emptyProgram) ContextEval(ctx context.Context, a any) (ref.Val, *cel.EvalDetails, error) {
return e.Eval(a)
}

func Vet(ctx context.Context, e Env, dir, filename string, stderr io.Writer) error {
configPath, conf, err := readConfig(stderr, dir, filename)
if err != nil {
Expand Down Expand Up @@ -100,7 +100,7 @@ func Vet(ctx context.Context, e Env, dir, filename string, stderr io.Writer) err
}

checks := map[string]cel.Program{
RuleDbPrepare: &emptyProgram{},
RuleDbPrepare: &emptyProgram{}, // Keep this to trigger the name conflict error below
}
msgs := map[string]string{}

Expand Down Expand Up @@ -198,7 +198,8 @@ type dbPreparer struct {
}

func (p *dbPreparer) Prepare(ctx context.Context, name, query string) error {
_, err := p.db.PrepareContext(ctx, query)
s, err := p.db.PrepareContext(ctx, query)
s.Close()
return err
}

Expand Down Expand Up @@ -316,12 +317,15 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
continue
}
original := result.Queries[i]
if prepareable(s, original.RawStmt) {
name := fmt.Sprintf("sqlc_vet_%d_%d", time.Now().Unix(), i)
if err := prep.Prepare(ctx, name, query.Text); err != nil {
fmt.Fprintf(c.Stderr, "%s: %s: %s: error preparing query: %s\n", query.Filename, q.Name, name, err)
errored = true
}
if !prepareable(s, original.RawStmt) {
fmt.Fprintf(c.Stderr, "%s: %s: %s: error preparing query: %s\n", query.Filename, q.Name, name, "query type is unpreparable")
errored = true
continue
}
name := fmt.Sprintf("sqlc_vet_%d_%d", time.Now().Unix(), i)
if err := prep.Prepare(ctx, name, query.Text); err != nil {
fmt.Fprintf(c.Stderr, "%s: %s: %s: error preparing query: %s\n", query.Filename, q.Name, name, err)
errored = true
}
continue
}
Expand Down

0 comments on commit 5348b09

Please sign in to comment.