Skip to content

Commit 17dd4da

Browse files
author
James Cor
committed
better implementation
1 parent f3c89b8 commit 17dd4da

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

sql/expression/function/string.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package function
1616

1717
import (
18+
"bytes"
1819
"encoding/hex"
1920
"fmt"
2021
"math"
@@ -632,19 +633,32 @@ func (q *Quote) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
632633
return nil, err
633634
}
634635

635-
val, _, err := types.Text.Convert(ctx, arg)
636+
val, _, err := types.Blob.Convert(ctx, arg)
636637
if err != nil {
637638
return nil, err
638639
}
639640
if val == nil {
640641
return nil, nil
641642
}
642-
valStr := val.(string)
643-
valStr = strings.Replace(valStr, "\\", "\\\\", -1)
644-
valStr = strings.Replace(valStr, "'", "\\'", -1)
645-
valStr = strings.Replace(valStr, "\000", "\\0", -1)
646-
valStr = strings.Replace(valStr, "\032", "\\\032", -1) // CTRL+Z character
647-
return fmt.Sprintf("'%s'", valStr), nil
643+
valBytes := val.([]byte)
644+
645+
ret := new(bytes.Buffer)
646+
ret.WriteByte('\'')
647+
for _, c := range valBytes {
648+
switch c {
649+
// '\032' is CTRL+Z character
650+
case '\\', '\'', '\032':
651+
ret.WriteByte('\\')
652+
ret.WriteByte(c)
653+
case '\000':
654+
ret.WriteByte('\\')
655+
ret.WriteByte('0')
656+
default:
657+
ret.WriteByte(c)
658+
}
659+
}
660+
ret.WriteByte('\'')
661+
return ret.String(), nil
648662
}
649663

650664
func (q *Quote) WithChildren(children ...sql.Expression) (sql.Expression, error) {

0 commit comments

Comments
 (0)