Skip to content

Commit c2b2f26

Browse files
committed
Stop using regex and add test
1 parent b280b87 commit c2b2f26

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

source/mysql/protocol/comms.d

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ Next tasks for this sub-package's cleanup:
2121
module mysql.protocol.comms;
2222

2323
import std.algorithm;
24+
import std.array;
2425
import std.conv;
2526
import std.digest.sha;
2627
import std.exception;
2728
import std.range;
28-
import std.regex;
29-
import std.typecons;
3029
import std.variant;
3130

3231
import mysql.connection;
@@ -47,7 +46,7 @@ package struct ProtocolPrepared
4746
import std.datetime;
4847
import std.variant;
4948
import mysql.types;
50-
49+
5150
static ubyte[] makeBitmap(in Variant[] inParams)
5251
{
5352
size_t bml = (inParams.length+7)/8;
@@ -453,7 +452,7 @@ package struct ProtocolPrepared
453452
Variant[] inParams, ParameterSpecialization[] psa)
454453
{
455454
conn.autoPurge();
456-
455+
457456
ubyte[] packet;
458457
conn.resetPacket();
459458

@@ -773,7 +772,7 @@ body
773772
}
774773

775774
conn.autoPurge();
776-
775+
777776
conn.resetPacket();
778777

779778
ubyte[] header;
@@ -969,7 +968,7 @@ package(mysql) SvrCapFlags setClientFlags(SvrCapFlags serverCaps, SvrCapFlags ca
969968
// didn't supply it
970969
cCaps |= SvrCapFlags.PROTOCOL41;
971970
cCaps |= SvrCapFlags.SECURE_CONNECTION;
972-
971+
973972
return cCaps;
974973
}
975974

@@ -1000,7 +999,7 @@ package(mysql) PreparedServerInfo performRegister(Connection conn, const(char[])
1000999
scope(failure) conn.kill();
10011000

10021001
PreparedServerInfo info;
1003-
1002+
10041003
conn.sendCmd(CommandType.STMT_PREPARE, sql);
10051004
conn._fieldCount = 0;
10061005

@@ -1117,14 +1116,27 @@ package(mysql) void enableMultiStatements(Connection conn, bool on)
11171116

11181117
private ubyte getDefaultCollation(string serverVersion)
11191118
{
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
1119+
// MySQL >= 5.5.3 supports utf8mb4
1120+
const v = serverVersion
1121+
.splitter('.')
1122+
.map!(a => a.parse!ushort)
1123+
.array;
1124+
1125+
if (v[0] < 5)
1126+
return 33; // Set utf8_general_ci as default
1127+
if (v[1] < 5)
1128+
return 33; // Set utf8_general_ci as default
1129+
if (v[2] < 3)
1130+
return 33; // Set utf8_general_ci as default
1131+
1132+
return 45; // Set utf8mb4_general_ci as default
1133+
}
1134+
1135+
unittest
1136+
{
1137+
assert(getDefaultCollation("5.5.3") == 45);
1138+
assert(getDefaultCollation("5.5.2") == 33);
1139+
1140+
// MariaDB: https://mariadb.com/kb/en/connection/#initial-handshake-packet
1141+
assert(getDefaultCollation("5.5.5-10.0.7-MariaDB") == 45);
11301142
}

0 commit comments

Comments
 (0)