Skip to content

return non-nil result when calling exec with empty query #973

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

Merged
merged 1 commit into from
Oct 19, 2021
Merged
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
4 changes: 4 additions & 0 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,10 @@ func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue)
tail := s.(*SQLiteStmt).t
s.Close()
if tail == "" {
if res == nil {
// https://github.com/mattn/go-sqlite3/issues/963
res = &SQLiteResult{0, 0}
}
return res, nil
}
query = tail
Expand Down
20 changes: 20 additions & 0 deletions sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,7 @@ var tests = []testing.InternalTest{
{Name: "TestManyQueryRow", F: testManyQueryRow},
{Name: "TestTxQuery", F: testTxQuery},
{Name: "TestPreparedStmt", F: testPreparedStmt},
{Name: "TestExecEmptyQuery", F: testExecEmptyQuery},
}

var benchmarks = []testing.InternalBenchmark{
Expand Down Expand Up @@ -2273,6 +2274,25 @@ func testPreparedStmt(t *testing.T) {
wg.Wait()
}

// testEmptyQuery is test for validating the API in case of empty query
func testExecEmptyQuery(t *testing.T) {
db.tearDown()
res, err := db.Exec(" -- this is just a comment ")
if err != nil {
t.Fatalf("empty query err: %v", err)
}

_, err = res.LastInsertId()
if err != nil {
t.Fatalf("LastInsertId returned an error: %v", err)
}

_, err = res.RowsAffected()
if err != nil {
t.Fatalf("RowsAffected returned an error: %v", err)
}
}

// Benchmarks need to use panic() since b.Error errors are lost when
// running via testing.Benchmark() I would like to run these via go
// test -bench but calling Benchmark() from a benchmark test
Expand Down