aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAviv Klasquin Komissar <avivklas@users.noreply.github.com>2021-10-19 12:18:21 +0300
committerGitHub <noreply@github.com>2021-10-19 18:18:21 +0900
commit3900dc318797f0c829deb42ed9d723a30e3f60b0 (patch)
treecbf346bb4fe0ca9c7297e9bae7afb27d477585ce
parentbump codecov/codecov-action@v2 (#957) (diff)
downloadgolite-3900dc318797f0c829deb42ed9d723a30e3f60b0.tar.gz
golite-3900dc318797f0c829deb42ed9d723a30e3f60b0.tar.xz
return non-nil result when calling exec with empty query (#973)
fixes #963
-rw-r--r--sqlite3.go4
-rw-r--r--sqlite3_test.go20
2 files changed, 24 insertions, 0 deletions
diff --git a/sqlite3.go b/sqlite3.go
index 5ac9570..d72bcb3 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -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
diff --git a/sqlite3_test.go b/sqlite3_test.go
index 878ec49..5eddae9 100644
--- a/sqlite3_test.go
+++ b/sqlite3_test.go
@@ -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{
@@ -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