Skip to content

Commit

Permalink
Merge pull request #425 from xxr3376/empty-bytes
Browse files Browse the repository at this point in the history
Treat []byte{} as empty BLOB instead of NULL.
  • Loading branch information
mattn authored Jun 15, 2017
2 parents 83772a7 + 3fa7ed2 commit 83c59d8
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,8 @@ type bindArg struct {
v driver.Value
}

var placeHolder byte = 0

func (s *SQLiteStmt) bind(args []namedValue) error {
rv := C.sqlite3_reset(s.s)
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
Expand All @@ -755,8 +757,7 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
rv = C.sqlite3_bind_null(s.s, n)
case string:
if len(v) == 0 {
b := []byte{0}
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(0))
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&placeHolder)), C.int(0))
} else {
b := []byte(v)
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
Expand All @@ -772,11 +773,13 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
case float64:
rv = C.sqlite3_bind_double(s.s, n, C.double(v))
case []byte:
var ptr *byte
if len(v) == 0 {
rv = C._sqlite3_bind_blob(s.s, n, nil, 0)
ptr = &placeHolder
} else {
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(len(v)))
ptr = &v[0]
}
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(ptr), C.int(len(v)))
case time.Time:
b := []byte(v.Format(SQLiteTimestampFormats[0]))
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
Expand Down

0 comments on commit 83c59d8

Please sign in to comment.