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

Commit 65f04e7

Browse files
authored
Merge pull request #587 from erizocosmico/feature/show-collation
sql/*: implement show collation
2 parents 8e113a9 + e380be7 commit 65f04e7

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
@@ -844,6 +844,26 @@ var queries = []struct {
844844
{"mytable"},
845845
},
846846
},
847+
{
848+
`SHOW COLLATION`,
849+
[]sql.Row{{"utf8_bin", "utf8mb4", int64(1), "Yes", "Yes", int64(1)}},
850+
},
851+
{
852+
`SHOW COLLATION LIKE 'foo'`,
853+
[]sql.Row{},
854+
},
855+
{
856+
`SHOW COLLATION LIKE 'utf8%'`,
857+
[]sql.Row{{"utf8_bin", "utf8mb4", int64(1), "Yes", "Yes", int64(1)}},
858+
},
859+
{
860+
`SHOW COLLATION WHERE charset = 'foo'`,
861+
[]sql.Row{},
862+
},
863+
{
864+
"SHOW COLLATION WHERE `Default` = 'Yes'",
865+
[]sql.Row{{"utf8_bin", "utf8mb4", int64(1), "Yes", "Yes", int64(1)}},
866+
},
847867
}
848868

849869
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):
@@ -1218,6 +1221,63 @@ func parseShowTableStatus(query string) (sql.Node, error) {
12181221
}
12191222
}
12201223

1224+
func parseShowCollation(query string) (sql.Node, error) {
1225+
buf := bufio.NewReader(strings.NewReader(query))
1226+
err := parseFuncs{
1227+
expect("show"),
1228+
skipSpaces,
1229+
expect("collation"),
1230+
skipSpaces,
1231+
}.exec(buf)
1232+
1233+
if err != nil {
1234+
return nil, err
1235+
}
1236+
1237+
if _, err = buf.Peek(1); err == io.EOF {
1238+
return plan.NewShowCollation(), nil
1239+
}
1240+
1241+
var clause string
1242+
if err := readIdent(&clause)(buf); err != nil {
1243+
return nil, err
1244+
}
1245+
1246+
if err := skipSpaces(buf); err != nil {
1247+
return nil, err
1248+
}
1249+
1250+
switch strings.ToUpper(clause) {
1251+
case "WHERE", "LIKE":
1252+
bs, err := ioutil.ReadAll(buf)
1253+
if err != nil {
1254+
return nil, err
1255+
}
1256+
1257+
expr, err := parseExpr(string(bs))
1258+
if err != nil {
1259+
return nil, err
1260+
}
1261+
1262+
var filter sql.Expression
1263+
if strings.ToUpper(clause) == "LIKE" {
1264+
filter = expression.NewLike(
1265+
expression.NewUnresolvedColumn("collation"),
1266+
expr,
1267+
)
1268+
} else {
1269+
filter = expr
1270+
}
1271+
1272+
return plan.NewFilter(
1273+
filter,
1274+
plan.NewShowCollation(),
1275+
), nil
1276+
default:
1277+
return nil, errUnexpectedSyntax.New("one of: LIKE or WHERE", clause)
1278+
}
1279+
}
1280+
12211281
var fixSessionRegex = regexp.MustCompile(`(,\s*|(set|SET)\s+)(SESSION|session)\s+([a-zA-Z0-9_]+)\s*=`)
12221282
var fixGlobalRegex = regexp.MustCompile(`(,\s*|(set|SET)\s+)(GLOBAL|global)\s+([a-zA-Z0-9_]+)\s*=`)
12231283

sql/parse/parse_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,21 @@ var fixtures = map[string]sql.Node{
992992
)},
993993
plan.NewUnresolvedTable("dual", ""),
994994
),
995+
"SHOW COLLATION": plan.NewShowCollation(),
996+
"SHOW COLLATION LIKE 'foo'": plan.NewFilter(
997+
expression.NewLike(
998+
expression.NewUnresolvedColumn("collation"),
999+
expression.NewLiteral("foo", sql.Text),
1000+
),
1001+
plan.NewShowCollation(),
1002+
),
1003+
"SHOW COLLATION WHERE Charset = 'foo'": plan.NewFilter(
1004+
expression.NewEquals(
1005+
expression.NewUnresolvedColumn("charset"),
1006+
expression.NewLiteral("foo", sql.Text),
1007+
),
1008+
plan.NewShowCollation(),
1009+
),
9951010
}
9961011

9971012
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)