Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor, types: format output #4372

Merged
merged 4 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/auth"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/format"
"github.com/pingcap/tidb/util/types"
)

Expand Down Expand Up @@ -530,7 +531,7 @@ func (e *ShowExec) fetchShowCreateTable() error {
}

if len(tb.Meta().Comment) > 0 {
buf.WriteString(fmt.Sprintf(" COMMENT='%s'", tb.Meta().Comment))
buf.WriteString(fmt.Sprintf(" COMMENT='%s'", format.OutputFormat(tb.Meta().Comment)))
}

data := types.MakeDatums(tb.Meta().Name.O, buf.String())
Expand Down
21 changes: 21 additions & 0 deletions util/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package format
import (
"fmt"
"io"
"strings"
)

const (
Expand Down Expand Up @@ -171,3 +172,23 @@ func FlatFormatter(w io.Writer) Formatter {
func (f *flatFormatter) Format(format string, args ...interface{}) (n int, errno error) {
return (*indentFormatter)(f).format(true, format, args...)
}

// OutputFormat output escape character with backslash
func OutputFormat(s string) string {
replace := map[string]string{
"'": "''",
"\a": "\\a",
"\b": "\\b",
"\f": "\\f",
"\n": "\\n",
"\r": "\\r",
"\t": "\\t",
"\v": "\\v",
}

for old, new := range replace {
s = strings.Replace(s, old, new, -1)
}

return s
}
3 changes: 2 additions & 1 deletion util/types/field_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/format"
"github.com/pingcap/tidb/util/types/json"
)

Expand Down Expand Up @@ -206,7 +207,7 @@ func (ft *FieldType) CompactStr() string {
// Format is ENUM ('e1', 'e2') or SET ('e1', 'e2')
es := make([]string, 0, len(ft.Elems))
for _, e := range ft.Elems {
e = strings.Replace(e, "'", "''", -1)
e = format.OutputFormat(e)
es = append(es, e)
}
suffix = fmt.Sprintf("('%s')", strings.Join(es, "','"))
Expand Down
16 changes: 16 additions & 0 deletions util/types/field_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func (s *testFieldTypeSuite) TestFieldType(c *C) {
ft.Elems = []string{"'a'", "'b'"}
c.Assert(ft.String(), Equals, "enum('''a''','''b''')")

ft = NewFieldType(mysql.TypeEnum)
ft.Elems = []string{"a\nb", "a\tb", "a\rb"}
c.Assert(ft.String(), Equals, "enum('a\\nb','a\\tb','a\\rb')")

ft = NewFieldType(mysql.TypeEnum)
ft.Elems = []string{"a'\nb", "a'b\tc"}
c.Assert(ft.String(), Equals, "enum('a''\\nb','a''b\\tc')")

ft = NewFieldType(mysql.TypeSet)
ft.Elems = []string{"a", "b"}
c.Assert(ft.String(), Equals, "set('a','b')")
Expand All @@ -105,6 +113,14 @@ func (s *testFieldTypeSuite) TestFieldType(c *C) {
ft.Elems = []string{"'a'", "'b'"}
c.Assert(ft.String(), Equals, "set('''a''','''b''')")

ft = NewFieldType(mysql.TypeSet)
ft.Elems = []string{"a\nb", "a\tb", "a\rb"}
c.Assert(ft.String(), Equals, "set('a\\nb','a\\tb','a\\rb')")

ft = NewFieldType(mysql.TypeSet)
ft.Elems = []string{"a'\nb", "a'b\tc"}
c.Assert(ft.String(), Equals, "set('a''\\nb','a''b\\tc')")

ft = NewFieldType(mysql.TypeTimestamp)
ft.Flen = 8
ft.Decimal = 2
Expand Down