diff --git a/sanitize.go b/sanitize.go index a90fdcea..9365c042 100644 --- a/sanitize.go +++ b/sanitize.go @@ -1,6 +1,7 @@ package pgx import ( + "encoding/hex" "reflect" "regexp" "strconv" @@ -44,6 +45,8 @@ func (c *Connection) SanitizeSql(sql string, args ...interface{}) (output string return strconv.FormatFloat(float64(arg), 'f', -1, 32) case float64: return strconv.FormatFloat(arg, 'f', -1, 64) + case []byte: + return `E'\\x` + hex.EncodeToString(arg) + `'` default: panic("Unable to sanitize type: " + reflect.TypeOf(arg).String()) } diff --git a/sanitize_test.go b/sanitize_test.go index 0d405d99..e0e6b441 100644 --- a/sanitize_test.go +++ b/sanitize_test.go @@ -34,4 +34,14 @@ func TestSanitizeSql(t *testing.T) { if conn.SanitizeSql("select $1, $2, $3", "Jack's", 42, 1.23) != "select 'Jack''s', 42, 1.23" { t.Error("Failed to sanitize multiple params") } + + bytea := make([]byte, 4) + bytea[0] = 0 // 0x00 + bytea[1] = 15 // 0x0F + bytea[2] = 255 // 0xFF + bytea[3] = 17 // 0x11 + + if conn.SanitizeSql("select $1", bytea) != `select E'\\x000fff11'` { + t.Error("Failed to sanitize []byte") + } }