GetCharsetAndDefaultCollation gets charset and default collation map.
(ctx context.Context, db *sql.Conn)
| 1612 | |
| 1613 | // GetCharsetAndDefaultCollation gets charset and default collation map. |
| 1614 | func GetCharsetAndDefaultCollation(ctx context.Context, db *sql.Conn) (map[string]string, error) { |
| 1615 | charsetAndDefaultCollation := make(map[string]string) |
| 1616 | query := "SHOW CHARACTER SET" |
| 1617 | |
| 1618 | // Show an example. |
| 1619 | /* |
| 1620 | mysql> SHOW CHARACTER SET; |
| 1621 | +----------+---------------------------------+---------------------+--------+ |
| 1622 | | Charset | Description | Default collation | Maxlen | |
| 1623 | +----------+---------------------------------+---------------------+--------+ |
| 1624 | | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 | |
| 1625 | | ascii | US ASCII | ascii_general_ci | 1 | |
| 1626 | | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | |
| 1627 | | binary | Binary pseudo charset | binary | 1 | |
| 1628 | | cp1250 | Windows Central European | cp1250_general_ci | 1 | |
| 1629 | | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 | |
| 1630 | +----------+---------------------------------+---------------------+--------+ |
| 1631 | */ |
| 1632 | |
| 1633 | rows, err := db.QueryContext(ctx, query) |
| 1634 | if err != nil { |
| 1635 | return nil, errors.Annotatef(err, "sql: %s", query) |
| 1636 | } |
| 1637 | |
| 1638 | defer rows.Close() |
| 1639 | for rows.Next() { |
| 1640 | var charset, description, collation string |
| 1641 | var maxlen int |
| 1642 | if scanErr := rows.Scan(&charset, &description, &collation, &maxlen); scanErr != nil { |
| 1643 | return nil, errors.Annotatef(err, "sql: %s", query) |
| 1644 | } |
| 1645 | charsetAndDefaultCollation[strings.ToLower(charset)] = collation |
| 1646 | } |
| 1647 | if err = rows.Close(); err != nil { |
| 1648 | return nil, errors.Annotatef(err, "sql: %s", query) |
| 1649 | } |
| 1650 | if err = rows.Err(); err != nil { |
| 1651 | return nil, errors.Annotatef(err, "sql: %s", query) |
| 1652 | } |
| 1653 | return charsetAndDefaultCollation, err |
| 1654 | } |