Skip to content

Commit

Permalink
TEXT types should be string, not ubyte[] (Fixes #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Sabalausky authored and Nick Sabalausky committed Oct 5, 2014
1 parent 84ad287 commit 2007dce
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion source/mysql/protocol/extra_types.d
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public:
col.collation = isNull? "<NULL>": t;
break;
case 14:
col.colType = cast(string) r[j].get!(ubyte[]);
col.colType = r[j].get!string();
break;
case 15:
col.key = t;
Expand Down
10 changes: 8 additions & 2 deletions source/mysql/protocol/packet_helpers.d
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ SQLValue consumeIfComplete(T, int N=T.sizeof)(ref ubyte[] packet, bool binary, b
: packet.consumeNonBinaryValueIfComplete!T(unsigned);
}

SQLValue consumeIfComplete()(ref ubyte[] packet, SQLType sqlType, bool binary, bool unsigned)
SQLValue consumeIfComplete()(ref ubyte[] packet, SQLType sqlType, bool binary, bool unsigned, ushort charSet)
{
switch(sqlType)
{
Expand Down Expand Up @@ -714,7 +714,13 @@ SQLValue consumeIfComplete()(ref ubyte[] packet, SQLType sqlType, bool binary, b
packet.popFront(); // LCB length
}
else
result.value = packet.consume(cast(size_t)lcb.value);
{
auto data = packet.consume(cast(size_t)lcb.value);
if(charSet == 0x3F) // CharacterSet == binary
result.value = data; // BLOB-ish
else
result.value = cast(string)data; // TEXT-ish
}

// Type BIT is treated as a length coded binary (like a BLOB or VARCHAR),
// not like an integral type. So convert the binary data to a bool.
Expand Down
2 changes: 1 addition & 1 deletion source/mysql/result.d
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public:
do
{
FieldDescription fd = rh[i];
sqlValue = packet.consumeIfComplete(fd.type, binary, fd.unsigned);
sqlValue = packet.consumeIfComplete(fd.type, binary, fd.unsigned, fd.charSet);
// TODO: Support chunk delegate
if(sqlValue.isIncomplete)
packet ~= con.getPacket();
Expand Down
8 changes: 4 additions & 4 deletions source/mysql/test/integration.d
Original file line number Diff line number Diff line change
Expand Up @@ -858,10 +858,10 @@ unittest
assertBasicTests!string("VARCHAR(10)", "", "aoeu");
assertBasicTests!string("CHAR(10)", "", "aoeu");

assertBasicTests!(ubyte[])("TINYTEXT", "", "aoeu");
assertBasicTests!(ubyte[])("MEDIUMTEXT", "", "aoeu");
assertBasicTests!(ubyte[])("TEXT", "", "aoeu");
assertBasicTests!(ubyte[])("LONGTEXT", "", "aoeu");
assertBasicTests!string("TINYTEXT", "", "aoeu");
assertBasicTests!string("MEDIUMTEXT", "", "aoeu");
assertBasicTests!string("TEXT", "", "aoeu");
assertBasicTests!string("LONGTEXT", "", "aoeu");

assertBasicTests!(ubyte[])("TINYBLOB", "", "aoeu");
assertBasicTests!(ubyte[])("MEDIUMBLOB", "", "aoeu");
Expand Down
32 changes: 32 additions & 0 deletions source/mysql/test/regression.d
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,38 @@ unittest
assert(results[1][1] == Date(1950, 4, 24));
}

// Issue #33: TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT types treated as ubyte[]
debug(MYSQL_INTEGRATION_TESTS)
unittest
{
mixin(scopedCn);
auto cmd = Command(cn);
ulong rowsAffected;
cmd.sql =
"DROP TABLE IF EXISTS `issue33`";
cmd.execSQL(rowsAffected);
cmd.sql =
"CREATE TABLE `issue33` (
`text` TEXT,
`blob` BLOB
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
cmd.execSQL(rowsAffected);

cmd.sql = "INSERT INTO `issue33` (`text`, `blob`) VALUES ('hello', 'world')";
cmd.execSQL(rowsAffected);

cmd = Command(cn, "SELECT `text`, `blob` FROM `issue33`");
cmd.prepare();
auto results = cmd.execPreparedResult();
assert(results.length == 1);
auto pText = results[0][0].peek!string();
auto pBlob = results[0][1].peek!(ubyte[])();
assert(pText);
assert(pBlob);
assert(*pText == "hello");
assert(*pBlob == cast(ubyte[])"world".dup);
}

// Issue #39: Unsupported SQL type NEWDECIMAL
debug(MYSQL_INTEGRATION_TESTS)
unittest
Expand Down

0 comments on commit 2007dce

Please sign in to comment.