Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ func (c *SQLiteConn) exec(ctx context.Context, query string, args []driver.Named
na := s.NumInput()
if len(args)-start < na {
s.Close()
return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args)-start)
}
stmtArgs := stmtArgs(args, start, na)
res, err = s.(*SQLiteStmt).exec(ctx, stmtArgs)
Expand Down
30 changes: 30 additions & 0 deletions sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,36 @@ func TestNamedParamClearBindings(t *testing.T) {
}
}

func TestNotEnoughArgsErrorMessage(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()

const want = "not enough args to execute query: want 1 got 0"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless there are other tests like this in the codebase, this test is brittle and doesn't add much value. The fix is a good idea, but this test is more trouble than it's worth. IMHO.

The right way to test errors is to create type errors and ensure that the right type comes back -- but that is much bigger change and I'm not proposing it.


t.Run("exec", func(t *testing.T) {
_, err := db.Exec("SELECT ?; SELECT ?", "hello")
if err == nil {
t.Fatal("expected error, got nil")
}
if err.Error() != want {
t.Errorf("got %q, want %q", err.Error(), want)
}
})

t.Run("query", func(t *testing.T) {
_, err := db.Query("SELECT ?; SELECT ?", "hello")
if err == nil {
t.Fatal("expected error, got nil")
}
if err.Error() != want {
t.Errorf("got %q, want %q", err.Error(), want)
}
})
}

// https://github.com/mattn/go-sqlite3/issues/1390
// sqlite3_prepare_v2 returns SQLITE_OK with a NULL statement handle when the
// input contains no SQL (only whitespace or comments). Querying such input
Expand Down