Skip to content

Commit

Permalink
πŸ› Fix bug preventing non-utf8 characters being saved in some databases
Browse files Browse the repository at this point in the history
  • Loading branch information
hi019 committed Apr 11, 2021
1 parent 7cfce7a commit 5d232d2
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
11 changes: 11 additions & 0 deletions arangodb/arangodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ func Test_ARANGODB_Reset(t *testing.T) {
utils.AssertEqual(t, true, len(result) == 0)
}

func Test_ARANGODB_Non_UTF8(t *testing.T) {
val := []byte("0xF5")

err := testStore.Set("0xF6", val, 0)
utils.AssertEqual(t, nil, err)

result, err := testStore.Get("0xF6")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, val, result)
}

func Test_ARANGODB_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close())
}
6 changes: 2 additions & 4 deletions mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/gofiber/utils"
)

// Storage interface that is implemented by storage providers
Expand All @@ -27,7 +26,7 @@ var (
initQuery = []string{
`CREATE TABLE IF NOT EXISTS %s (
k VARCHAR(64) NOT NULL DEFAULT '',
v TEXT NOT NULL,
v BLOB NOT NULL,
e BIGINT NOT NULL DEFAULT '0',
PRIMARY KEY (k)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
Expand Down Expand Up @@ -133,8 +132,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
if exp != 0 {
expSeconds = time.Now().Add(exp).Unix()
}
valStr := utils.UnsafeString(val)
_, err := s.db.Exec(s.sqlInsert, key, valStr, expSeconds, valStr, expSeconds)
_, err := s.db.Exec(s.sqlInsert, key, val, expSeconds, val, expSeconds)
return err
}

Expand Down
11 changes: 11 additions & 0 deletions mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ func Test_MYSQL_GC(t *testing.T) {

}

func Test_MYSQL_Non_UTF8(t *testing.T) {
val := []byte("0xF5")

err := testStore.Set("0xF6", val, 0)
utils.AssertEqual(t, nil, err)

result, err := testStore.Get("0xF6")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, val, result)
}

func Test_MYSQL_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close())
}
7 changes: 2 additions & 5 deletions postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/url"
"time"

"github.com/gofiber/utils"
_ "github.com/lib/pq"
)

Expand All @@ -29,7 +28,7 @@ var (
initQuery = []string{
`CREATE TABLE IF NOT EXISTS %s (
k VARCHAR(64) PRIMARY KEY NOT NULL DEFAULT '',
v TEXT NOT NULL,
v BYTEA NOT NULL,
e BIGINT NOT NULL DEFAULT '0'
);`,
`CREATE INDEX IF NOT EXISTS e ON %s (e);`,
Expand Down Expand Up @@ -137,7 +136,6 @@ func (s *Storage) Get(key string) ([]byte, error) {
return data, nil
}

// Set key with value
// Set key with value
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
// Ain't Nobody Got Time For That
Expand All @@ -148,8 +146,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
if exp != 0 {
expSeconds = time.Now().Add(exp).Unix()
}
valStr := utils.UnsafeString(val)
_, err := s.db.Exec(s.sqlInsert, key, valStr, expSeconds, valStr, expSeconds)
_, err := s.db.Exec(s.sqlInsert, key, val, expSeconds, val, expSeconds)
return err
}

Expand Down
11 changes: 11 additions & 0 deletions postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ func Test_Postgres_GC(t *testing.T) {

}

func Test_Postgres_Non_UTF8(t *testing.T) {
val := []byte("0xF5")

err := testStore.Set("0xF6", val, 0)
utils.AssertEqual(t, nil, err)

result, err := testStore.Get("0xF6")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, val, result)
}

func Test_Postgres_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close())
}
6 changes: 2 additions & 4 deletions sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"time"

"github.com/gofiber/utils"

_ "github.com/mattn/go-sqlite3"
)

Expand All @@ -28,7 +26,7 @@ var (
initQuery = []string{
`CREATE TABLE IF NOT EXISTS %s (
k VARCHAR(64) PRIMARY KEY NOT NULL DEFAULT '',
v TEXT NOT NULL,
v BLOB NOT NULL,
e BIGINT NOT NULL DEFAULT '0'
);`,
`CREATE INDEX IF NOT EXISTS e ON %s (e);`,
Expand Down Expand Up @@ -126,7 +124,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
if exp != 0 {
expSeconds = time.Now().Add(exp).Unix()
}
_, err := s.db.Exec(s.sqlInsert, key, utils.UnsafeString(val), expSeconds)
_, err := s.db.Exec(s.sqlInsert, key, val, expSeconds)
return err
}

Expand Down
11 changes: 11 additions & 0 deletions sqlite3/sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ func Test_SQLite3_GC(t *testing.T) {

}

func Test_SQLite3_Non_UTF8(t *testing.T) {
val := []byte("0xF5")

err := testStore.Set("0xF6", val, 0)
utils.AssertEqual(t, nil, err)

result, err := testStore.Get("0xF6")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, val, result)
}

func Test_SQLite3_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close())
}

0 comments on commit 5d232d2

Please sign in to comment.