@@ -335,6 +335,63 @@ func TestEmptyString(t *testing.T) {
335335 }
336336}
337337
338+ // TestEmptyStringNotNull verifies that binding an empty string results in an
339+ // empty string value, not NULL. This is a regression test for a bug where
340+ // unsafe.StringData("") could return nil, causing sqlite3_bind_text64 to
341+ // receive a NULL pointer and treat the value as NULL instead of "".
342+ func TestEmptyStringNotNull (t * testing.T ) {
343+ db := openTestDB (t )
344+ ctx := t .Context ()
345+
346+ exec (t , db , "CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)" )
347+ exec (t , db , "INSERT INTO t (id, val) VALUES (?, ?)" , 1 , "" )
348+ exec (t , db , "INSERT INTO t (id, val) VALUES (?, ?)" , 2 , nil )
349+ exec (t , db , "INSERT INTO t (id, val) VALUES (?, ?)" , 3 , "ok" )
350+
351+ var val sql.NullString
352+ if err := db .QueryRowContext (ctx , "SELECT val FROM t WHERE id = 1" ).Scan (& val ); err != nil {
353+ t .Fatal (err )
354+ }
355+ if ! val .Valid {
356+ t .Fatal ("empty string was stored as NULL" )
357+ }
358+ if val .String != "" {
359+ t .Fatalf ("val=%q, want empty string" , val .String )
360+ }
361+
362+ if err := db .QueryRowContext (ctx , "SELECT val FROM t WHERE id = 2" ).Scan (& val ); err != nil {
363+ t .Fatal (err )
364+ }
365+ if val .Valid {
366+ t .Fatalf ("NULL was stored as %q" , val .String )
367+ }
368+
369+ var countEmpty , countNull int
370+ if err := db .QueryRowContext (ctx , "SELECT count(*) FROM t WHERE val = ''" ).Scan (& countEmpty ); err != nil {
371+ t .Fatal (err )
372+ }
373+ if countEmpty != 1 {
374+ t .Fatalf ("countEmpty=%d, want 1" , countEmpty )
375+ }
376+ if err := db .QueryRowContext (ctx , "SELECT count(*) FROM t WHERE val IS NULL" ).Scan (& countNull ); err != nil {
377+ t .Fatal (err )
378+ }
379+ if countNull != 1 {
380+ t .Fatalf ("countNull=%d, want 1" , countNull )
381+ }
382+
383+ var length sql.NullInt64
384+ if err := db .QueryRowContext (ctx , "SELECT length(val) FROM t WHERE id = 1" ).Scan (& length ); err != nil {
385+ t .Fatal (err )
386+ }
387+ if ! length .Valid {
388+ t .Fatal ("length of empty string returned NULL" )
389+ }
390+ if length .Int64 != 0 {
391+ t .Fatalf ("length=%d, want 0" , length .Int64 )
392+ }
393+ }
394+
338395func TestExecScript (t * testing.T ) {
339396 db := openTestDB (t )
340397 conn , err := db .Conn (context .Background ())
0 commit comments