Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vet): report an error when a query is unpreparable, remove dead code #2486

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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