From 308f5f1b2fae699cb38474136205cbab8f081019 Mon Sep 17 00:00:00 2001 From: Xu Xinran Date: Wed, 14 Jun 2017 19:55:09 +0800 Subject: [PATCH 1/2] Treat []byte{} as empty bytes instead of NULL. --- sqlite3.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index 33b9b9c..c3d5d98 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -772,11 +772,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 = &(make([]byte, 1)[0]) } 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))) From 3fa7ed217682f0f66953e3c37d7a648bbc5d3e5c Mon Sep 17 00:00:00 2001 From: Xu Xinran Date: Wed, 14 Jun 2017 21:22:40 +0800 Subject: [PATCH 2/2] Use global variable for better performance. --- sqlite3.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index c3d5d98..56e55e2 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -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 { @@ -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))) @@ -774,7 +775,7 @@ func (s *SQLiteStmt) bind(args []namedValue) error { case []byte: var ptr *byte if len(v) == 0 { - ptr = &(make([]byte, 1)[0]) + ptr = &placeHolder } else { ptr = &v[0] }