2
0

Add []byte to bytea sanitization

This commit is contained in:
Jack Christensen
2013-05-07 08:29:33 -05:00
parent e21955a1e4
commit e4e3ce0d4a
2 changed files with 13 additions and 0 deletions
+3
View File
@@ -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())
}
+10
View File
@@ -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")
}
}