Commit a4318db8 authored by cznic's avatar cznic

handle properly 0 rows query results, fixes #28

parent a6843eee
......@@ -799,3 +799,30 @@ func TestNoRows(t *testing.T) {
t.Fatal(err)
}
}
// https://gitlab.com/cznic/sqlite/-/issues/28
func TestIssue28(t *testing.T) {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
db, err := sql.Open("sqlite", filepath.Join(tempDir, "test.db"))
if err != nil {
t.Fatalf("test.db open fail: %v", err)
}
defer db.Close()
if _, err := db.Exec(`CREATE TABLE test (foo TEXT)`); err != nil {
t.Fatal(err)
}
row := db.QueryRow(`SELECT foo FROM test`)
var foo string
if err = row.Scan(&foo); err != sql.ErrNoRows {
t.Fatalf("got %T(%[1]v), expected %T(%[2]v)", err, sql.ErrNoRows)
}
}
......@@ -29,6 +29,7 @@ var (
//lint:ignore SA1019 TODO implement QueryerContext
_ driver.Queryer = (*conn)(nil)
_ driver.Result = (*result)(nil)
_ driver.Rows = noRows{}
_ driver.Rows = (*rows)(nil)
_ driver.Stmt = (*stmt)(nil)
_ driver.Tx = (*tx)(nil)
......@@ -496,6 +497,11 @@ func (s *stmt) query(ctx context.Context, args []driver.NamedValue) (r driver.Ro
pstmt = 0
return nil
case sqlite3.SQLITE_DONE:
if r == nil {
pstmt = 0
r = noRows{}
return nil
}
// nop
default:
return s.c.errstr(int32(rc))
......@@ -516,6 +522,12 @@ func (s *stmt) query(ctx context.Context, args []driver.NamedValue) (r driver.Ro
return r, err
}
type noRows struct{}
func (noRows) Columns() []string { return nil }
func (noRows) Close() error { return nil }
func (noRows) Next([]driver.Value) error { return sql.ErrNoRows }
type tx struct {
c *conn
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment