mirror of
https://github.com/jackc/pgx.git
synced 2025-04-27 13:14:32 +00:00
70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package pgx
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"reflect"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var literalPattern *regexp.Regexp = regexp.MustCompile(`\$\d+`)
|
|
|
|
// QuoteString escapes and quotes a string making it safe for interpolation
|
|
// into an SQL string.
|
|
func (c *Connection) QuoteString(input string) (output string) {
|
|
output = "'" + strings.Replace(input, "'", "''", -1) + "'"
|
|
return
|
|
}
|
|
|
|
// QuoteIdentifier escapes and quotes an identifier making it safe for
|
|
// interpolation into an SQL string
|
|
func (c *Connection) QuoteIdentifier(input string) (output string) {
|
|
output = `"` + strings.Replace(input, `"`, `""`, -1) + `"`
|
|
return
|
|
}
|
|
|
|
// SanitizeSql substitutely args positionaly into sql. Placeholder values are
|
|
// $ prefixed integers like $1, $2, $3, etc. args are sanitized and quoted as
|
|
// appropriate.
|
|
func (c *Connection) SanitizeSql(sql string, args ...interface{}) (output string) {
|
|
replacer := func(match string) (replacement string) {
|
|
n, _ := strconv.ParseInt(match[1:], 10, 0)
|
|
switch arg := args[n-1].(type) {
|
|
case string:
|
|
return c.QuoteString(arg)
|
|
case int:
|
|
return strconv.FormatInt(int64(arg), 10)
|
|
case int8:
|
|
return strconv.FormatInt(int64(arg), 10)
|
|
case int16:
|
|
return strconv.FormatInt(int64(arg), 10)
|
|
case int32:
|
|
return strconv.FormatInt(int64(arg), 10)
|
|
case int64:
|
|
return strconv.FormatInt(int64(arg), 10)
|
|
case uint:
|
|
return strconv.FormatUint(uint64(arg), 10)
|
|
case uint8:
|
|
return strconv.FormatUint(uint64(arg), 10)
|
|
case uint16:
|
|
return strconv.FormatUint(uint64(arg), 10)
|
|
case uint32:
|
|
return strconv.FormatUint(uint64(arg), 10)
|
|
case uint64:
|
|
return strconv.FormatUint(uint64(arg), 10)
|
|
case float32:
|
|
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())
|
|
}
|
|
}
|
|
|
|
output = literalPattern.ReplaceAllStringFunc(sql, replacer)
|
|
return
|
|
}
|