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

improve: GetAllCharsets and GetCharsetInfoByID #247

Merged
merged 1 commit into from
Mar 19, 2019
Merged
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
32 changes: 13 additions & 19 deletions charset/charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Collation struct {
}

var charsets = make(map[string]*Charset)
var collationsMap = make(map[int]*Collation)
var descs = make([]*Desc, 0, len(charsetInfos))

// All the supported charsets should be in the following table.
var charsetInfos = []*Charset{
Expand All @@ -60,21 +62,6 @@ type Desc struct {

// GetAllCharsets gets all charset descriptions in the local charsets.
func GetAllCharsets() []*Desc {
descs := make([]*Desc, 0, len(charsets))
// The charsetInfos is an array, so the iterate order will be stable.
for _, ci := range charsetInfos {
c, ok := charsets[ci.Name]
if !ok {
continue
}
desc := &Desc{
Name: c.Name,
DefaultCollation: c.DefaultCollation,
Desc: c.Desc,
Maxlen: c.Maxlen,
}
descs = append(descs, desc)
}
return descs
}

Expand Down Expand Up @@ -150,10 +137,8 @@ func GetCharsetInfoByID(coID int) (string, string, error) {
if coID == mysql.DefaultCollationID {
return mysql.DefaultCharset, mysql.DefaultCollationName, nil
}
for _, collation := range collations {
if coID == collation.ID {
return collation.CharsetName, collation.Name, nil
}
if collation, ok := collationsMap[coID]; ok {
return collation.CharsetName, collation.Name, nil
}
return "", "", errors.Errorf("Unknown charset id %d", coID)
}
Expand Down Expand Up @@ -412,8 +397,17 @@ var collations = []*Collation{
func init() {
for _, c := range charsetInfos {
charsets[c.Name] = c
desc := &Desc{
Name: c.Name,
DefaultCollation: c.DefaultCollation,
Desc: c.Desc,
Maxlen: c.Maxlen,
}
descs = append(descs, desc)
}

for _, c := range collations {
collationsMap[c.ID] = c
charset, ok := charsets[c.CharsetName]
if !ok {
continue
Expand Down