From e4e3ce0d4a326b5aafc7cc28f51a62953aa4944c Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Tue, 7 May 2013 08:29:33 -0500 Subject: [PATCH] Add []byte to bytea sanitization --- sanitize.go | 3 +++ sanitize_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+) 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") + } }