Skip to content

Distinguish between NULL and zero-length blobs on query #643

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 3 commits into from
Nov 21, 2018
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
2 changes: 1 addition & 1 deletion sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
case C.SQLITE_BLOB:
p := C.sqlite3_column_blob(rc.s.s, C.int(i))
if p == nil {
dest[i] = nil
dest[i] = []byte{}
continue
}
n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
Expand Down
31 changes: 31 additions & 0 deletions sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,7 @@ var tests = []testing.InternalTest{
{Name: "TestResult", F: testResult},
{Name: "TestBlobs", F: testBlobs},
{Name: "TestMultiBlobs", F: testMultiBlobs},
{Name: "TestNullZeroLengthBlobs", F: testNullZeroLengthBlobs},
{Name: "TestManyQueryRow", F: testManyQueryRow},
{Name: "TestTxQuery", F: testTxQuery},
{Name: "TestPreparedStmt", F: testPreparedStmt},
Expand Down Expand Up @@ -1975,6 +1976,36 @@ func testMultiBlobs(t *testing.T) {
}
}

// testBlobs tests that we distinguish between null and zero-length blobs
func testNullZeroLengthBlobs(t *testing.T) {
db.tearDown()
db.mustExec("create table foo (id integer primary key, bar " + db.blobType(16) + ")")
db.mustExec(db.q("insert into foo (id, bar) values(?,?)"), 0, nil)
db.mustExec(db.q("insert into foo (id, bar) values(?,?)"), 1, []byte{})

r0 := db.QueryRow(db.q("select bar from foo where id=0"))
var b0 []byte
err := r0.Scan(&b0)
if err != nil {
t.Fatal(err)
}
if b0 != nil {
t.Errorf("for id=0, got %x; want nil", b0)
}

r1 := db.QueryRow(db.q("select bar from foo where id=1"))
var b1 []byte
err = r1.Scan(&b1)
if err != nil {
t.Fatal(err)
}
if b1 == nil {
t.Error("for id=1, got nil; want zero-length slice")
} else if len(b1) > 0 {
t.Errorf("for id=1, got %x; want zero-length slice", b1)
}
}

// testManyQueryRow is test for many query row
func testManyQueryRow(t *testing.T) {
if testing.Short() {
Expand Down