Add []byte to bytea sanitization
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user