Skip to content

Commit 83c59d8

Browse files
authored
Merge pull request #425 from xxr3376/empty-bytes
Treat []byte{} as empty BLOB instead of NULL.
2 parents 83772a7 + 3fa7ed2 commit 83c59d8

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

sqlite3.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ type bindArg struct {
734734
v driver.Value
735735
}
736736

737+
var placeHolder byte = 0
738+
737739
func (s *SQLiteStmt) bind(args []namedValue) error {
738740
rv := C.sqlite3_reset(s.s)
739741
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
@@ -755,8 +757,7 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
755757
rv = C.sqlite3_bind_null(s.s, n)
756758
case string:
757759
if len(v) == 0 {
758-
b := []byte{0}
759-
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(0))
760+
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&placeHolder)), C.int(0))
760761
} else {
761762
b := []byte(v)
762763
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
@@ -772,11 +773,13 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
772773
case float64:
773774
rv = C.sqlite3_bind_double(s.s, n, C.double(v))
774775
case []byte:
776+
var ptr *byte
775777
if len(v) == 0 {
776-
rv = C._sqlite3_bind_blob(s.s, n, nil, 0)
778+
ptr = &placeHolder
777779
} else {
778-
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(len(v)))
780+
ptr = &v[0]
779781
}
782+
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(ptr), C.int(len(v)))
780783
case time.Time:
781784
b := []byte(v.Format(SQLiteTimestampFormats[0]))
782785
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))

0 commit comments

Comments
 (0)