Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 24fc6bf

Browse files
committed
sql/*: implement show collation
Signed-off-by: Miguel Molina <[email protected]>
1 parent 1bdf44b commit 24fc6bf

File tree

5 files changed

+151
-3
lines changed

5 files changed

+151
-3
lines changed

engine_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,26 @@ var queries = []struct {
800800
{nil},
801801
},
802802
},
803+
{
804+
`SHOW COLLATION`,
805+
[]sql.Row{{"utf8_bin", "utf8mb4", int64(1), "Yes", "Yes", int64(1)}},
806+
},
807+
{
808+
`SHOW COLLATION LIKE 'foo'`,
809+
[]sql.Row{},
810+
},
811+
{
812+
`SHOW COLLATION LIKE 'utf8%'`,
813+
[]sql.Row{{"utf8_bin", "utf8mb4", int64(1), "Yes", "Yes", int64(1)}},
814+
},
815+
{
816+
`SHOW COLLATION WHERE charset = 'foo'`,
817+
[]sql.Row{},
818+
},
819+
{
820+
"SHOW COLLATION WHERE `Default` = 'Yes'",
821+
[]sql.Row{{"utf8_bin", "utf8mb4", int64(1), "Yes", "Yes", int64(1)}},
822+
},
803823
}
804824

805825
func TestQueries(t *testing.T) {

sql/parse/parse.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var (
4343
showCreateRegex = regexp.MustCompile(`^show create\s+\S+\s*`)
4444
showVariablesRegex = regexp.MustCompile(`^show\s+(.*)?variables\s*`)
4545
showWarningsRegex = regexp.MustCompile(`^show\s+warnings\s*`)
46+
showCollationRegex = regexp.MustCompile(`^show\s+collation\s*`)
4647
describeRegex = regexp.MustCompile(`^(describe|desc|explain)\s+(.*)\s+`)
4748
fullProcessListRegex = regexp.MustCompile(`^show\s+(full\s+)?processlist$`)
4849
unlockTablesRegex = regexp.MustCompile(`^unlock\s+tables$`)
@@ -82,6 +83,8 @@ func Parse(ctx *sql.Context, query string) (sql.Node, error) {
8283
return parseShowVariables(ctx, s)
8384
case showWarningsRegex.MatchString(lowerQuery):
8485
return parseShowWarnings(ctx, s)
86+
case showCollationRegex.MatchString(lowerQuery):
87+
return parseShowCollation(s)
8588
case describeRegex.MatchString(lowerQuery):
8689
return parseDescribeQuery(ctx, s)
8790
case fullProcessListRegex.MatchString(lowerQuery):
@@ -1190,6 +1193,63 @@ func parseShowTableStatus(query string) (sql.Node, error) {
11901193
}
11911194
}
11921195

1196+
func parseShowCollation(query string) (sql.Node, error) {
1197+
buf := bufio.NewReader(strings.NewReader(query))
1198+
err := parseFuncs{
1199+
expect("show"),
1200+
skipSpaces,
1201+
expect("collation"),
1202+
skipSpaces,
1203+
}.exec(buf)
1204+
1205+
if err != nil {
1206+
return nil, err
1207+
}
1208+
1209+
if _, err = buf.Peek(1); err == io.EOF {
1210+
return plan.NewShowCollation(), nil
1211+
}
1212+
1213+
var clause string
1214+
if err := readIdent(&clause)(buf); err != nil {
1215+
return nil, err
1216+
}
1217+
1218+
if err := skipSpaces(buf); err != nil {
1219+
return nil, err
1220+
}
1221+
1222+
switch strings.ToUpper(clause) {
1223+
case "WHERE", "LIKE":
1224+
bs, err := ioutil.ReadAll(buf)
1225+
if err != nil {
1226+
return nil, err
1227+
}
1228+
1229+
expr, err := parseExpr(string(bs))
1230+
if err != nil {
1231+
return nil, err
1232+
}
1233+
1234+
var filter sql.Expression
1235+
if strings.ToUpper(clause) == "LIKE" {
1236+
filter = expression.NewLike(
1237+
expression.NewUnresolvedColumn("collation"),
1238+
expr,
1239+
)
1240+
} else {
1241+
filter = expr
1242+
}
1243+
1244+
return plan.NewFilter(
1245+
filter,
1246+
plan.NewShowCollation(),
1247+
), nil
1248+
default:
1249+
return nil, errUnexpectedSyntax.New("one of: LIKE or WHERE", clause)
1250+
}
1251+
}
1252+
11931253
var fixSessionRegex = regexp.MustCompile(`(,\s*|(set|SET)\s+)(SESSION|session)\s+([a-zA-Z0-9_]+)\s*=`)
11941254
var fixGlobalRegex = regexp.MustCompile(`(,\s*|(set|SET)\s+)(GLOBAL|global)\s+([a-zA-Z0-9_]+)\s*=`)
11951255

sql/parse/parse_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,21 @@ var fixtures = map[string]sql.Node{
945945
)},
946946
plan.NewUnresolvedTable("dual", ""),
947947
),
948+
"SHOW COLLATION": plan.NewShowCollation(),
949+
"SHOW COLLATION LIKE 'foo'": plan.NewFilter(
950+
expression.NewLike(
951+
expression.NewUnresolvedColumn("collation"),
952+
expression.NewLiteral("foo", sql.Text),
953+
),
954+
plan.NewShowCollation(),
955+
),
956+
"SHOW COLLATION WHERE Charset = 'foo'": plan.NewFilter(
957+
expression.NewEquals(
958+
expression.NewUnresolvedColumn("charset"),
959+
expression.NewLiteral("foo", sql.Text),
960+
),
961+
plan.NewShowCollation(),
962+
),
948963
}
949964

950965
func TestParse(t *testing.T) {

sql/plan/show_collation.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package plan
2+
3+
import "gopkg.in/src-d/go-mysql-server.v0/sql"
4+
5+
// ShowCollation shows all available collations.
6+
type ShowCollation struct{}
7+
8+
var collationSchema = sql.Schema{
9+
{Name: "Collation", Type: sql.Text},
10+
{Name: "Charset", Type: sql.Text},
11+
{Name: "Id", Type: sql.Int64},
12+
{Name: "Default", Type: sql.Text},
13+
{Name: "Compiled", Type: sql.Text},
14+
{Name: "Sortlen", Type: sql.Int64},
15+
}
16+
17+
// NewShowCollation creates a new ShowCollation node.
18+
func NewShowCollation() ShowCollation {
19+
return ShowCollation{}
20+
}
21+
22+
// Children implements the sql.Node interface.
23+
func (ShowCollation) Children() []sql.Node { return nil }
24+
25+
func (ShowCollation) String() string { return "SHOW COLLATION" }
26+
27+
// Resolved implements the sql.Node interface.
28+
func (ShowCollation) Resolved() bool { return true }
29+
30+
// RowIter implements the sql.Node interface.
31+
func (ShowCollation) RowIter(ctx *sql.Context) (sql.RowIter, error) {
32+
return sql.RowsToRowIter(sql.Row{
33+
defaultCollation,
34+
defaultCharacterSet,
35+
int64(1),
36+
"Yes",
37+
"Yes",
38+
int64(1),
39+
}), nil
40+
}
41+
42+
// Schema implements the sql.Node interface.
43+
func (ShowCollation) Schema() sql.Schema { return collationSchema }
44+
45+
// TransformUp implements the sql.Node interface.
46+
func (ShowCollation) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) {
47+
return f(ShowCollation{})
48+
}
49+
50+
// TransformExpressionsUp implements the sql.Node interface.
51+
func (ShowCollation) TransformExpressionsUp(f sql.TransformExprFunc) (sql.Node, error) {
52+
return ShowCollation{}, nil
53+
}

sql/plan/showwarnings_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ func TestShowWarnings(t *testing.T) {
1212
require := require.New(t)
1313

1414
ctx := sql.NewEmptyContext()
15-
ctx.Session.Warn(&sql.Warning{"l1", "w1", 1})
16-
ctx.Session.Warn(&sql.Warning{"l2", "w2", 2})
17-
ctx.Session.Warn(&sql.Warning{"l4", "w3", 3})
15+
ctx.Session.Warn(&sql.Warning{Level: "l1", Message: "w1", Code: 1})
16+
ctx.Session.Warn(&sql.Warning{Level: "l2", Message: "w2", Code: 2})
17+
ctx.Session.Warn(&sql.Warning{Level: "l4", Message: "w3", Code: 3})
1818

1919
sw := ShowWarnings(ctx.Session.Warnings())
2020
require.True(sw.Resolved())

0 commit comments

Comments
 (0)