Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 873ec57

Browse files
authored
Merge pull request mattn#643 from akalin/zero-length-blob
Distinguish between NULL and zero-length blobs on query
2 parents c880439 + ab4f174 commit 873ec57

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

‎sqlite3.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
20242024
case C.SQLITE_BLOB:
20252025
p := C.sqlite3_column_blob(rc.s.s, C.int(i))
20262026
if p == nil {
2027-
dest[i] = nil
2027+
dest[i] = []byte{}
20282028
continue
20292029
}
20302030
n := C.sqlite3_column_bytes(rc.s.s, C.int(i))

‎sqlite3_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,7 @@ var tests = []testing.InternalTest{
17701770
{Name: "TestResult", F: testResult},
17711771
{Name: "TestBlobs", F: testBlobs},
17721772
{Name: "TestMultiBlobs", F: testMultiBlobs},
1773+
{Name: "TestNullZeroLengthBlobs", F: testNullZeroLengthBlobs},
17731774
{Name: "TestManyQueryRow", F: testManyQueryRow},
17741775
{Name: "TestTxQuery", F: testTxQuery},
17751776
{Name: "TestPreparedStmt", F: testPreparedStmt},
@@ -1975,6 +1976,36 @@ func testMultiBlobs(t *testing.T) {
19751976
}
19761977
}
19771978

1979+
// testBlobs tests that we distinguish between null and zero-length blobs
1980+
func testNullZeroLengthBlobs(t *testing.T) {
1981+
db.tearDown()
1982+
db.mustExec("create table foo (id integer primary key, bar " + db.blobType(16) + ")")
1983+
db.mustExec(db.q("insert into foo (id, bar) values(?,?)"), 0, nil)
1984+
db.mustExec(db.q("insert into foo (id, bar) values(?,?)"), 1, []byte{})
1985+
1986+
r0 := db.QueryRow(db.q("select bar from foo where id=0"))
1987+
var b0 []byte
1988+
err := r0.Scan(&b0)
1989+
if err != nil {
1990+
t.Fatal(err)
1991+
}
1992+
if b0 != nil {
1993+
t.Errorf("for id=0, got %x; want nil", b0)
1994+
}
1995+
1996+
r1 := db.QueryRow(db.q("select bar from foo where id=1"))
1997+
var b1 []byte
1998+
err = r1.Scan(&b1)
1999+
if err != nil {
2000+
t.Fatal(err)
2001+
}
2002+
if b1 == nil {
2003+
t.Error("for id=1, got nil; want zero-length slice")
2004+
} else if len(b1) > 0 {
2005+
t.Errorf("for id=1, got %x; want zero-length slice", b1)
2006+
}
2007+
}
2008+
19782009
// testManyQueryRow is test for many query row
19792010
func testManyQueryRow(t *testing.T) {
19802011
if testing.Short() {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /