Skip to content

Commit 81e5adc

Browse files
committed
Use utf8mb4 by default
This commit changes default collation as "utf8mb4_generic_ci" which is available from MySQL 5.5.3.
1 parent 27ea51f commit 81e5adc

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

source/mysql/protocol/comms.d

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import std.conv;
2525
import std.digest.sha;
2626
import std.exception;
2727
import std.range;
28+
import std.regex;
29+
import std.typecons;
2830
import std.variant;
2931

3032
import mysql.connection;
@@ -810,7 +812,7 @@ body
810812
// Request a conventional maximum packet length.
811813
1.packInto(packet[8..12]);
812814

813-
packet ~= 33; // Set UTF-8 as default charSet
815+
packet ~= getDefaultCollation(conn._serverVersion);
814816

815817
// There's a statutory block of zero bytes here - fill them in.
816818
foreach(i; 0 .. 23)
@@ -1112,3 +1114,17 @@ package(mysql) void enableMultiStatements(Connection conn, bool on)
11121114
auto packet = conn.getPacket();
11131115
enforce!MYXProtocol(packet[0] == 254 && packet.length == 5, "Unexpected response to SET_OPTION command");
11141116
}
1117+
1118+
private ubyte getDefaultCollation(string serverVersion)
1119+
{
1120+
static re = ctRegex!`^(\d{1,2})\.(\d{1,2})\.(\d{1,3})(.*)`;
1121+
auto captured = serverVersion.matchFirst(re);
1122+
auto major = captured[1].to!ushort;
1123+
auto minor = captured[2].to!ushort;
1124+
auto patch = captured[3].to!ushort;
1125+
auto mysqlServerVersion = tuple(major, minor, patch);
1126+
1127+
if (mysqlServerVersion >= tuple(5,5,3)) // MySQL >= 5.5.3 supports utf8mb4
1128+
return 45; // Set utf8mb4_general_ci as default
1129+
return 33; // Set utf8_general_ci as default
1130+
}

0 commit comments

Comments
 (0)