Skip to content

Use utf8mb4 by default #220

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

Merged
merged 2 commits into from
May 28, 2021
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
40 changes: 34 additions & 6 deletions source/mysql/protocol/comms.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Next tasks for this sub-package's cleanup:
module mysql.protocol.comms;

import std.algorithm;
import std.array;
import std.conv;
import std.digest.sha;
import std.exception;
Expand All @@ -45,7 +46,7 @@ package struct ProtocolPrepared
import std.datetime;
import std.variant;
import mysql.types;

static ubyte[] makeBitmap(in Variant[] inParams)
{
size_t bml = (inParams.length+7)/8;
Expand Down Expand Up @@ -451,7 +452,7 @@ package struct ProtocolPrepared
Variant[] inParams, ParameterSpecialization[] psa)
{
conn.autoPurge();

ubyte[] packet;
conn.resetPacket();

Expand Down Expand Up @@ -771,7 +772,7 @@ body
}

conn.autoPurge();

conn.resetPacket();

ubyte[] header;
Expand Down Expand Up @@ -810,7 +811,7 @@ body
// Request a conventional maximum packet length.
1.packInto(packet[8..12]);

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

// There's a statutory block of zero bytes here - fill them in.
foreach(i; 0 .. 23)
Expand Down Expand Up @@ -967,7 +968,7 @@ package(mysql) SvrCapFlags setClientFlags(SvrCapFlags serverCaps, SvrCapFlags ca
// didn't supply it
cCaps |= SvrCapFlags.PROTOCOL41;
cCaps |= SvrCapFlags.SECURE_CONNECTION;

return cCaps;
}

Expand Down Expand Up @@ -998,7 +999,7 @@ package(mysql) PreparedServerInfo performRegister(Connection conn, const(char[])
scope(failure) conn.kill();

PreparedServerInfo info;

conn.sendCmd(CommandType.STMT_PREPARE, sql);
conn._fieldCount = 0;

Expand Down Expand Up @@ -1112,3 +1113,30 @@ package(mysql) void enableMultiStatements(Connection conn, bool on)
auto packet = conn.getPacket();
enforce!MYXProtocol(packet[0] == 254 && packet.length == 5, "Unexpected response to SET_OPTION command");
}

private ubyte getDefaultCollation(string serverVersion)
{
// MySQL >= 5.5.3 supports utf8mb4
const v = serverVersion
.splitter('.')
.map!(a => a.parse!ushort)
.array;

if (v[0] < 5)
return 33; // Set utf8_general_ci as default
if (v[1] < 5)
return 33; // Set utf8_general_ci as default
if (v[2] < 3)
return 33; // Set utf8_general_ci as default

return 45; // Set utf8mb4_general_ci as default
}

unittest
{
assert(getDefaultCollation("5.5.3") == 45);
assert(getDefaultCollation("5.5.2") == 33);

// MariaDB: https://mariadb.com/kb/en/connection/#initial-handshake-packet
assert(getDefaultCollation("5.5.5-10.0.7-MariaDB") == 45);
}