Skip to content

Commit 8af47e3

Browse files
committed
Don't map TINYINT(1) UNSIGNED as bool. Fixes #530
1 parent 0b59086 commit 8af47e3

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public object GetValue(int ordinal)
354354
{
355355
case ColumnType.Tiny:
356356
var value = ParseInt32(data);
357-
if (Connection.TreatTinyAsBoolean && columnDefinition.ColumnLength == 1)
357+
if (Connection.TreatTinyAsBoolean && columnDefinition.ColumnLength == 1 && !isUnsigned)
358358
return value != 0;
359359
return isUnsigned ? (object) (byte) value : (sbyte) value;
360360

src/MySqlConnector/Core/TypeMapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static MySqlDbType ConvertToMySqlDbType(ColumnDefinitionPayload columnDef
179179
switch (columnDefinition.ColumnType)
180180
{
181181
case ColumnType.Tiny:
182-
return treatTinyAsBoolean && columnDefinition.ColumnLength == 1 ? MySqlDbType.Bool :
182+
return treatTinyAsBoolean && columnDefinition.ColumnLength == 1 && !isUnsigned ? MySqlDbType.Bool :
183183
isUnsigned ? MySqlDbType.UByte : MySqlDbType.Byte;
184184

185185
case ColumnType.Int24:

tests/SideBySide/DataTypes.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ public void QueryTinyIntSbyte(string column, string dataTypeName, object[] expec
226226
}
227227
}
228228

229+
[Theory()]
230+
[InlineData("TinyInt1U", "TINYINT", new object[] { null, (byte) 0, (byte) 1, (byte) 0, (byte) 1, (byte) 255, (byte) 123 })]
231+
public void QueryTinyInt1Unsigned(string column, string dataTypeName, object[] expected)
232+
{
233+
DoQuery("bools", column, dataTypeName, expected, reader => reader.GetByte(0), baselineCoercedNullValue: default(byte));
234+
}
235+
229236
[Theory]
230237
[InlineData("SByte", "TINYINT", new object[] { null, default(sbyte), sbyte.MinValue, sbyte.MaxValue, (sbyte) 123 })]
231238
public void QuerySByte(string column, string dataTypeName, object[] expected)
@@ -983,6 +990,7 @@ public void Geometry()
983990
[InlineData("Boolean", "datatypes_bools", MySqlDbType.Bool, 1, typeof(bool), "N", 0, 0)]
984991
[InlineData("TinyInt1", "datatypes_bools", MySqlDbType.Bool, 1, typeof(bool), "N", 0, 0)]
985992
#endif
993+
[InlineData("TinyInt1U", "datatypes_bools", MySqlDbType.UByte, 1, typeof(byte), "N", 0, 0)]
986994
[InlineData("size", "datatypes_enums", MySqlDbType.Enum, 7, typeof(string), "N", 0, 0)]
987995
[InlineData("color", "datatypes_enums", MySqlDbType.Enum, 6, typeof(string), "", 0, 0)]
988996
[InlineData("char38", "datatypes_guids", MySqlDbType.String, 38, typeof(string), "N", 0, 0)]
@@ -1107,6 +1115,7 @@ private void DoGetSchemaTable(string column, string table, MySqlDbType mySqlDbTy
11071115
[InlineData("rowid", "datatypes_bools", MySqlDbType.Int32, "INT", 11, typeof(int), "AK", -1, 0)]
11081116
[InlineData("Boolean", "datatypes_bools", MySqlDbType.Bool, "BOOL", 1, typeof(bool), "N", -1, 0)]
11091117
[InlineData("TinyInt1", "datatypes_bools", MySqlDbType.Bool, "BOOL", 1, typeof(bool), "N", -1, 0)]
1118+
[InlineData("TinyInt1U", "datatypes_bools", MySqlDbType.UByte, "TINYINT", 1, typeof(byte), "N", -1, 0)]
11101119
[InlineData("size", "datatypes_enums", MySqlDbType.Enum, "ENUM", 7, typeof(string), "N", -1, 0)]
11111120
[InlineData("color", "datatypes_enums", MySqlDbType.Enum, "ENUM", 6, typeof(string), "", -1, 0)]
11121121
[InlineData("char38", "datatypes_guids", MySqlDbType.String, "CHAR(38)", 38, typeof(string), "N", -1, 0)]

tests/SideBySide/DataTypesFixture.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ public DataTypesFixture()
1212
create table datatypes_bools(
1313
rowid integer not null primary key auto_increment,
1414
Boolean bool null,
15-
TinyInt1 tinyint(1) null
15+
TinyInt1 tinyint(1) null,
16+
TinyInt1U tinyint(1) unsigned null
1617
);
1718
18-
insert into datatypes_bools(Boolean, TinyInt1)
19+
insert into datatypes_bools(Boolean, TinyInt1, TinyInt1U)
1920
values
20-
(null, null),
21-
(0, 0),
22-
(1, 1),
23-
(false, false),
24-
(true, true),
25-
(-1, -1),
26-
(123, 123);
21+
(null, null, null),
22+
(0, 0, 0),
23+
(1, 1, 1),
24+
(false, false, false),
25+
(true, true, true),
26+
(-1, -1, 255),
27+
(123, 123, 123);
2728
2829
drop table if exists datatypes_bits;
2930
create table datatypes_bits(

0 commit comments

Comments
 (0)