goqu icon indicating copy to clipboard operation
goqu copied to clipboard

Interpolation fails with BLOB columns in sqlite

Open abdusco opened this issue 2 years ago • 0 comments

Describe the bug goqu fails to escape binary data when inserting new rows in a table that contains a binary BLOB column. Obviously this doesn't happen with every binary input. But it happens every time I try to insert a non-trivial binary blob, such as small JPEG images.

I get a SQL logic error: unrecognized token: "'����" (1) error.

To Reproduce

Here's a test to replicate the issue

package main

import (
	"bytes"
	"database/sql"
	"image"
	"image/jpeg"
	"testing"

	"github.com/doug-martin/goqu/v9"
	_ "modernc.org/sqlite"
)

func TestBlobColumn(t *testing.T) {
	conn, err := sql.Open("sqlite", ":memory:")
	if err != nil {
		t.Fatal(err)
	}
	db := goqu.New("sqlite", conn)
	if _, err := db.Exec(`CREATE TABLE things(data BLOB)`); err != nil {
		t.Fatal(err)
	}

	tests := []struct {
		name string
		data []byte
	}{
		{
			name: "ok",
			data: []byte(`this is ok`),
		},
		{
			name: "not ok",
			data: mustGenerateJPEG(),
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			res, err := db.Insert("things").Rows(map[string]any{
				"data": tt.data,
			}).Executor().Exec()
			if err != nil {
				t.Fatal(err)
			}
			n, err := res.RowsAffected()
			if err != nil {
				t.Fatal(err)
			}
			if n != 1 {
				t.Fatalf("failed to insert: %v", err)
			}
		})
	}
}

func mustGenerateJPEG() []byte {
	img := image.NewRGBA(image.Rect(0, 0, 1, 1))
	var jpegBuf bytes.Buffer
	_ = jpeg.Encode(&jpegBuf, img, &jpeg.Options{Quality: 1})
	return jpegBuf.Bytes()
}

Expected behavior I'd expect goqu to be able to escape binary data, but I'm not sure if that's even possible for an arbitrary blob of bytes.

Dialect:

  • [ ] postgres
  • [ ] mysql
  • [x] sqlite3

Additional context I haven't tested whether this issue exists with other dialects. Also this error doesn't happen when I use prepared statements.

abdusco avatar Sep 12 '22 13:09 abdusco