Skip to content

Commit

Permalink
fix show create database to actually show charset/collation (#2418)
Browse files Browse the repository at this point in the history
Co-authored-by: James Cor <james@dolthub.com>
  • Loading branch information
jycor and James Cor committed Mar 28, 2024
1 parent e66f664 commit 6aa6537
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
35 changes: 35 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -6267,6 +6267,41 @@ where
},
},
},
{
Name: "test show create database",
SetUpScript: []string{
"create database def_db;",
"create database latin1_db character set latin1;",
"create database bin_db charset binary;",
"create database mb3_db collate utf8mb3_general_ci;",
},
Assertions: []ScriptTestAssertion{
{
Query: "show create database def_db",
Expected: []sql.Row{
{"def_db", "CREATE DATABASE `def_db` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin */"},
},
},
{
Query: "show create database latin1_db",
Expected: []sql.Row{
{"latin1_db", "CREATE DATABASE `latin1_db` /*!40100 DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci */"},
},
},
{
Query: "show create database bin_db",
Expected: []sql.Row{
{"bin_db", "CREATE DATABASE `bin_db` /*!40100 DEFAULT CHARACTER SET binary COLLATE binary */"},
},
},
{
Query: "show create database mb3_db",
Expected: []sql.Row{
{"mb3_db", "CREATE DATABASE `mb3_db` /*!40100 DEFAULT CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci */"},
},
},
},
},
}

var SpatialScriptTests = []ScriptTest{
Expand Down
5 changes: 3 additions & 2 deletions sql/planbuilder/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1489,9 +1489,10 @@ func (b *Builder) buildDBDDL(inScope *scope, c *ast.DBDDL) (outScope *scope) {
}
for _, cc := range c.CharsetCollate {
ccType := strings.ToLower(cc.Type)
if ccType == "character set" {
switch ccType {
case "character set", "charset":
charsetStr = cc.Value
} else if ccType == "collate" {
case "collate":
collationStr = cc.Value
}
}
Expand Down
10 changes: 8 additions & 2 deletions sql/rowexec/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,19 @@ func (b *BaseBuilder) buildShowCreateDatabase(ctx *sql.Context, n *plan.ShowCrea
buf.WriteString("/*!32312 IF NOT EXISTS*/ ")
}

// TODO: grab collation from server?
collId := sql.Collation_Default
if collDb, ok := n.Database().(sql.CollatedDatabase); ok {
collId = collDb.GetCollation(ctx)
}

buf.WriteRune('`')
buf.WriteString(name)
buf.WriteRune('`')
buf.WriteString(fmt.Sprintf(
" /*!40100 DEFAULT CHARACTER SET %s COLLATE %s */",
sql.Collation_Default.CharacterSet().String(),
sql.Collation_Default.String(),
collId.CharacterSet().String(),
collId.String(),
))

return sql.RowsToRowIter(
Expand Down

0 comments on commit 6aa6537

Please sign in to comment.