Skip to content

Commit 9f626b2

Browse files
committed
Fix #255.
1 parent 1f5d8bf commit 9f626b2

File tree

6 files changed

+38
-36
lines changed

6 files changed

+38
-36
lines changed

context.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,26 @@ func (ctx Context) ResultText(value string) {
8989
}
9090

9191
// ResultRawText sets the text result of the function to a []byte.
92-
// Returning a nil slice is the same as calling [Context.ResultNull].
9392
//
9493
// https://sqlite.org/c3ref/result_blob.html
9594
func (ctx Context) ResultRawText(value []byte) {
95+
if len(value) == 0 {
96+
ctx.ResultText("")
97+
return
98+
}
9699
ptr := ctx.c.newBytes(value)
97100
ctx.c.call("sqlite3_result_text_go",
98101
stk_t(ctx.handle), stk_t(ptr), stk_t(len(value)))
99102
}
100103

101104
// ResultBlob sets the result of the function to a []byte.
102-
// Returning a nil slice is the same as calling [Context.ResultNull].
103105
//
104106
// https://sqlite.org/c3ref/result_blob.html
105107
func (ctx Context) ResultBlob(value []byte) {
108+
if len(value) == 0 {
109+
ctx.ResultZeroBlob(0)
110+
return
111+
}
106112
ptr := ctx.c.newBytes(value)
107113
ctx.c.call("sqlite3_result_blob_go",
108114
stk_t(ctx.handle), stk_t(ptr), stk_t(len(value)))

sqlite.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,10 @@ func (sqlt *sqlite) realloc(ptr ptr_t, size int64) ptr_t {
212212
}
213213

214214
func (sqlt *sqlite) newBytes(b []byte) ptr_t {
215-
if (*[0]byte)(b) == nil {
215+
if len(b) == 0 {
216216
return 0
217217
}
218-
size := len(b)
219-
if size == 0 {
220-
size = 1
221-
}
222-
ptr := sqlt.new(int64(size))
218+
ptr := sqlt.new(int64(len(b)))
223219
util.WriteBytes(sqlt.mod, ptr, b)
224220
return ptr
225221
}
@@ -288,7 +284,7 @@ func (a *arena) new(size int64) ptr_t {
288284
}
289285

290286
func (a *arena) bytes(b []byte) ptr_t {
291-
if (*[0]byte)(b) == nil {
287+
if len(b) == 0 {
292288
return 0
293289
}
294290
ptr := a.new(int64(len(b)))

sqlite3/sqlite_opt.h

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
// #define SQLITE_OMIT_DECLTYPE
1717
// #define SQLITE_OMIT_PROGRESS_CALLBACK
1818

19+
// TODO add this:
20+
// #define SQLITE_ENABLE_API_ARMOR
21+
1922
// Other Options
2023

2124
#define SQLITE_ALLOW_URI_AUTHORITY

sqlite_test.go

-5
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ func Test_sqlite_newBytes(t *testing.T) {
125125
if got := util.View(sqlite.mod, ptr, int64(len(want))); !bytes.Equal(got, want) {
126126
t.Errorf("got %q, want %q", got, want)
127127
}
128-
129-
ptr = sqlite.newBytes(buf[:0])
130-
if ptr == 0 {
131-
t.Fatal("got nullptr, want a pointer")
132-
}
133128
}
134129

135130
func Test_sqlite_newString(t *testing.T) {

stmt.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,15 @@ func (s *Stmt) BindText(param int, value string) error {
265265

266266
// BindRawText binds a []byte to the prepared statement as text.
267267
// The leftmost SQL parameter has an index of 1.
268-
// Binding a nil slice is the same as calling [Stmt.BindNull].
269268
//
270269
// https://sqlite.org/c3ref/bind_blob.html
271270
func (s *Stmt) BindRawText(param int, value []byte) error {
272271
if len(value) > _MAX_LENGTH {
273272
return TOOBIG
274273
}
274+
if len(value) == 0 {
275+
return s.BindText(param, "")
276+
}
275277
ptr := s.c.newBytes(value)
276278
rc := res_t(s.c.call("sqlite3_bind_text_go",
277279
stk_t(s.handle), stk_t(param),
@@ -281,13 +283,15 @@ func (s *Stmt) BindRawText(param int, value []byte) error {
281283

282284
// BindBlob binds a []byte to the prepared statement.
283285
// The leftmost SQL parameter has an index of 1.
284-
// Binding a nil slice is the same as calling [Stmt.BindNull].
285286
//
286287
// https://sqlite.org/c3ref/bind_blob.html
287288
func (s *Stmt) BindBlob(param int, value []byte) error {
288289
if len(value) > _MAX_LENGTH {
289290
return TOOBIG
290291
}
292+
if len(value) == 0 {
293+
return s.BindZeroBlob(param, 0)
294+
}
291295
ptr := s.c.newBytes(value)
292296
rc := res_t(s.c.call("sqlite3_bind_blob_go",
293297
stk_t(s.handle), stk_t(param),

tests/stmt_test.go

+18-20
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,21 @@ func TestStmt(t *testing.T) {
8989
t.Fatal(err)
9090
}
9191

92-
if err := stmt.BindBlob(1, []byte("")); err != nil {
92+
if err := stmt.BindRawText(1, nil); err != nil {
9393
t.Fatal(err)
9494
}
9595
if err := stmt.Exec(); err != nil {
9696
t.Fatal(err)
9797
}
9898

99-
if err := stmt.BindBlob(1, []byte("blob")); err != nil {
99+
if err := stmt.BindBlob(1, []byte("")); err != nil {
100100
t.Fatal(err)
101101
}
102102
if err := stmt.Exec(); err != nil {
103103
t.Fatal(err)
104104
}
105105

106-
if err := stmt.BindBlob(1, nil); err != nil {
106+
if err := stmt.BindBlob(1, []byte("blob")); err != nil {
107107
t.Fatal(err)
108108
}
109109
if err := stmt.Exec(); err != nil {
@@ -354,8 +354,8 @@ func TestStmt(t *testing.T) {
354354
}
355355

356356
if stmt.Step() {
357-
if got := stmt.ColumnType(0); got != sqlite3.BLOB {
358-
t.Errorf("got %v, want BLOB", got)
357+
if got := stmt.ColumnType(0); got != sqlite3.TEXT {
358+
t.Errorf("got %v, want TEXT", got)
359359
}
360360
if got := stmt.ColumnBool(0); got != false {
361361
t.Errorf("got %v, want false", got)
@@ -391,11 +391,11 @@ func TestStmt(t *testing.T) {
391391
if got := stmt.ColumnFloat(0); got != 0 {
392392
t.Errorf("got %v, want zero", got)
393393
}
394-
if got := stmt.ColumnText(0); got != "blob" {
395-
t.Errorf(`got %q, want "blob"`, got)
394+
if got := stmt.ColumnText(0); got != "" {
395+
t.Errorf("got %q, want empty", got)
396396
}
397-
if got := stmt.ColumnBlob(0, nil); string(got) != "blob" {
398-
t.Errorf(`got %q, want "blob"`, got)
397+
if got := stmt.ColumnBlob(0, nil); got != nil {
398+
t.Errorf("got %q, want nil", got)
399399
}
400400
var got any
401401
if err := stmt.ColumnJSON(0, &got); err == nil {
@@ -404,8 +404,8 @@ func TestStmt(t *testing.T) {
404404
}
405405

406406
if stmt.Step() {
407-
if got := stmt.ColumnType(0); got != sqlite3.NULL {
408-
t.Errorf("got %v, want NULL", got)
407+
if got := stmt.ColumnType(0); got != sqlite3.BLOB {
408+
t.Errorf("got %v, want BLOB", got)
409409
}
410410
if got := stmt.ColumnBool(0); got != false {
411411
t.Errorf("got %v, want false", got)
@@ -416,17 +416,15 @@ func TestStmt(t *testing.T) {
416416
if got := stmt.ColumnFloat(0); got != 0 {
417417
t.Errorf("got %v, want zero", got)
418418
}
419-
if got := stmt.ColumnText(0); got != "" {
420-
t.Errorf("got %q, want empty", got)
419+
if got := stmt.ColumnText(0); got != "blob" {
420+
t.Errorf(`got %q, want "blob"`, got)
421421
}
422-
if got := stmt.ColumnBlob(0, nil); got != nil {
423-
t.Errorf("got %q, want nil", got)
422+
if got := stmt.ColumnBlob(0, nil); string(got) != "blob" {
423+
t.Errorf(`got %q, want "blob"`, got)
424424
}
425-
var got any = 1
426-
if err := stmt.ColumnJSON(0, &got); err != nil {
427-
t.Error(err)
428-
} else if got != nil {
429-
t.Errorf("got %v, want NULL", got)
425+
var got any
426+
if err := stmt.ColumnJSON(0, &got); err == nil {
427+
t.Errorf("got %v, want error", got)
430428
}
431429
}
432430

0 commit comments

Comments
 (0)