Skip to content
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
16 changes: 16 additions & 0 deletions go/sqltypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,22 @@ func encodeBytesSQL(val []byte, b BinWriter) {
b.Write(buf.Bytes())
}

// EncodeStringSQL encodes the string as a SQL string.
func EncodeStringSQL(val string) string {
buf := &bytes2.Buffer{}
buf.WriteByte('\'')
for _, ch := range val {
if encodedChar := SQLEncodeMap[ch]; encodedChar == DontEscape {
buf.WriteByte(byte(ch))
} else {
buf.WriteByte('\\')
buf.WriteByte(encodedChar)
}
}
buf.WriteByte('\'')
return buf.String()
}

func encodeBytesSQLBits(val []byte, b BinWriter) {
fmt.Fprint(b, "b'")
for _, ch := range val {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2525,7 +2525,7 @@ func (node *ShowFilter) Format(buf *TrackedBuffer) {
return
}
if node.Like != "" {
buf.astPrintf(node, " like '%s'", node.Like)
buf.astPrintf(node, " like %s", encodeSQLString(node.Like))
} else {
buf.astPrintf(node, " where %v", node.Filter)
}
Expand Down Expand Up @@ -3196,7 +3196,7 @@ func (node *SelectInto) Format(buf *TrackedBuffer) {
if node == nil {
return
}
buf.astPrintf(node, "%s'%s'", node.Type.ToString(), node.FileName)
buf.astPrintf(node, "%s%s", node.Type.ToString(), node.FileName)
if node.Charset != "" {
buf.astPrintf(node, " character set %s", node.Charset)
}
Expand Down
5 changes: 5 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1355,3 +1355,8 @@ func handleUnaryMinus(expr Expr) Expr {
}
return &UnaryExpr{Operator: UMinusOp, Expr: expr}
}

// encodeSQLString encodes the string as a SQL string.
func encodeSQLString(val string) string {
return sqltypes.EncodeStringSQL(val)
}
14 changes: 12 additions & 2 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2297,12 +2297,16 @@ func TestSelectInto(t *testing.T) {
}{{
input: "select * from t order by name limit 100 into outfile s3 'out_file_name'",
output: "select * from t order by `name` asc limit 100 into outfile s3 'out_file_name'",
}, {
input: `select * from TestPerson into outfile s3 's3://test-bucket/export_import/export/users.csv' fields terminated by ',' enclosed by '\"' escaped by '\\' overwrite on`,
}, {
input: "select * from t into dumpfile 'out_file_name'",
}, {
input: "select * from t into outfile 'out_file_name' character set binary fields terminated by 'term' optionally enclosed by 'c' escaped by 'e' lines starting by 'a' terminated by '\n'",
input: "select * from t into outfile 'out_file_name' character set binary fields terminated by 'term' optionally enclosed by 'c' escaped by 'e' lines starting by 'a' terminated by '\\n'",
}, {
input: "select * from t into outfile s3 'out_file_name' character set binary format csv header fields terminated by 'term' optionally enclosed by 'c' escaped by 'e' lines starting by 'a' terminated by '\\n' manifest on overwrite off",
}, {
input: "select * from t into outfile s3 'out_file_name' character set binary format csv header fields terminated by 'term' optionally enclosed by 'c' escaped by 'e' lines starting by 'a' terminated by '\n' manifest on overwrite off",
input: "select * from t into outfile s3 'out_file_name' character set binary lines terminated by '\\n' starting by 'a' manifest on overwrite off",
}, {
input: "select * from (select * from t union select * from t2) as t3 where t3.name in (select col from t4) into outfile s3 'out_file_name'",
output: "select * from (select * from t union select * from t2) as t3 where t3.`name` in (select col from t4) into outfile s3 'out_file_name'",
Expand All @@ -2311,6 +2315,12 @@ func TestSelectInto(t *testing.T) {
input: "select * from t limit 100 into outfile s3 'out_file_name' union select * from t2",
}, {
input: "select * from (select * from t into outfile s3 'inner_outfile') as t2 into outfile s3 'out_file_name'",
}, {
input: `select * from TestPerson into outfile s3 's3://test-bucket/export_import/export/users.csv' character set 'utf8' overwrite on`,
}, {
input: `select * from t1 into outfile '/tmp/foo.csv' fields escaped by '\\' terminated by '\n'`,
}, {
input: `select * from t1 into outfile '/tmp/foo.csv' fields escaped by 'c' terminated by '\n' enclosed by '\t'`,
}}

for _, tcase := range validSQL {
Expand Down
Loading