rework QuoteString and QuoteBytes as append-style

pull/2136/head
merlin 2024-10-01 17:04:48 +03:00
parent 39ffc8b7a4
commit 59d6aa87b9
No known key found for this signature in database
GPG Key ID: 7EDDCEA6A90062E0
3 changed files with 10 additions and 18 deletions

View File

@ -70,9 +70,9 @@ func (q *Query) Sanitize(args ...any) (string, error) {
case bool: case bool:
p = strconv.AppendBool(buf.AvailableBuffer(), arg) p = strconv.AppendBool(buf.AvailableBuffer(), arg)
case []byte: case []byte:
p = quoteBytes(buf.AvailableBuffer(), arg) p = QuoteBytes(buf.AvailableBuffer(), arg)
case string: case string:
p = quoteString(buf.AvailableBuffer(), arg) p = QuoteString(buf.AvailableBuffer(), arg)
case time.Time: case time.Time:
p = arg.Truncate(time.Microsecond). p = arg.Truncate(time.Microsecond).
AppendFormat(buf.AvailableBuffer(), "'2006-01-02 15:04:05.999999999Z07:00:00'") AppendFormat(buf.AvailableBuffer(), "'2006-01-02 15:04:05.999999999Z07:00:00'")
@ -135,11 +135,7 @@ func (q *Query) init(sql string) {
q.Parts = l.parts q.Parts = l.parts
} }
func QuoteString(str string) string { func QuoteString(dst []byte, str string) []byte {
return string(quoteString(nil, str))
}
func quoteString(dst []byte, str string) []byte {
const quote = "'" const quote = "'"
n := strings.Count(str, quote) n := strings.Count(str, quote)
@ -166,11 +162,7 @@ func quoteString(dst []byte, str string) []byte {
return dst return dst
} }
func QuoteBytes(buf []byte) string { func QuoteBytes(dst, buf []byte) []byte {
return string(quoteBytes(nil, buf))
}
func quoteBytes(dst, buf []byte) []byte {
dst = append(dst, `'\x`...) dst = append(dst, `'\x`...)
n := hex.EncodedLen(len(buf)) n := hex.EncodedLen(len(buf))

View File

@ -14,10 +14,10 @@ func FuzzQuoteString(f *testing.F) {
f.Add("select 'quoted $42', $1") f.Add("select 'quoted $42', $1")
f.Fuzz(func(t *testing.T, input string) { f.Fuzz(func(t *testing.T, input string) {
got := sanitize.QuoteString(input) got := sanitize.QuoteString(nil, input)
want := oldQuoteString(input) want := oldQuoteString(input)
if want != got { if want != string(got) {
t.Errorf("got %q", got) t.Errorf("got %q", got)
t.Fatalf("want %q", want) t.Fatalf("want %q", want)
} }
@ -32,10 +32,10 @@ func FuzzQuoteBytes(f *testing.F) {
f.Add([]byte("select 'quoted $42', $1")) f.Add([]byte("select 'quoted $42', $1"))
f.Fuzz(func(t *testing.T, input []byte) { f.Fuzz(func(t *testing.T, input []byte) {
got := sanitize.QuoteBytes(input) got := sanitize.QuoteBytes(nil, input)
want := oldQuoteBytes(input) want := oldQuoteBytes(input)
if want != got { if want != string(got) {
t.Errorf("got %q", got) t.Errorf("got %q", got)
t.Fatalf("want %q", want) t.Fatalf("want %q", want)
} }

View File

@ -235,7 +235,7 @@ func TestQuoteString(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
got := sanitize.QuoteString(input) got := string(sanitize.QuoteString(nil, input))
want := oldQuoteString(input) want := oldQuoteString(input)
if got != want { if got != want {
@ -259,7 +259,7 @@ func TestQuoteBytes(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
got := sanitize.QuoteBytes(input) got := string(sanitize.QuoteBytes(nil, input))
want := oldQuoteBytes(input) want := oldQuoteBytes(input)
if got != want { if got != want {