Skip to content

Commit 693b8e0

Browse files
committed
Complete BOOL to integral conversions. Fixes #782
1 parent bec152d commit 693b8e0

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public sbyte GetSByte(int ordinal)
8989
return checked((sbyte) ulongValue);
9090
if (value is decimal decimalValue)
9191
return (sbyte) decimalValue;
92+
if (value is bool boolValue)
93+
return boolValue ? (sbyte) 1 : (sbyte) 0;
9294
return (sbyte) value;
9395
}
9496

@@ -114,6 +116,8 @@ public byte GetByte(int ordinal)
114116
return checked((byte) ulongValue);
115117
if (value is decimal decimalValue)
116118
return (byte) decimalValue;
119+
if (value is bool boolValue)
120+
return boolValue ? (byte) 1 : (byte) 0;
117121
return (byte) value;
118122
}
119123

@@ -226,7 +230,18 @@ public int GetInt32(int ordinal)
226230

227231
if (columnDefinition.ColumnType == ColumnType.Bit)
228232
return checked((int) ReadBit(data, columnDefinition));
229-
return GetInt32Core(data, columnDefinition);
233+
234+
var result = GetInt32Core(data, columnDefinition);
235+
if (columnDefinition.ColumnType == ColumnType.Tiny &&
236+
Connection.TreatTinyAsBoolean &&
237+
columnDefinition.ColumnLength == 1 &&
238+
(columnDefinition.ColumnFlags & ColumnFlags.Unsigned) == 0 &&
239+
result != 0)
240+
{
241+
// coerce all non-zero TINYINT(1) results to 1, since it represents a BOOL value
242+
result = 1;
243+
}
244+
return result;
230245
}
231246

232247
protected abstract int GetInt32Core(ReadOnlySpan<byte> data, ColumnDefinitionPayload columnDefinition);
@@ -254,7 +269,7 @@ public long GetInt64(int ordinal)
254269
if (value is decimal)
255270
return (long) (decimal) value;
256271
if (value is bool)
257-
return (bool)value ? 1 : 0;
272+
return (bool) value ? 1 : 0;
258273
return (long) value;
259274
}
260275

tests/SideBySide/QueryTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,51 @@ END AS value
12141214
Assert.False(await reader.NextResultAsync());
12151215
}
12161216

1217+
[Theory]
1218+
[InlineData(false, false)]
1219+
[InlineData(false, true)]
1220+
[InlineData(true, false)]
1221+
[InlineData(true, true)]
1222+
public void GetIntForTinyInt1(bool treatTinyAsBoolean, bool prepare)
1223+
{
1224+
var csb = AppConfig.CreateConnectionStringBuilder();
1225+
csb.TreatTinyAsBoolean = treatTinyAsBoolean;
1226+
csb.IgnorePrepare = false;
1227+
using var connection = new MySqlConnection(csb.ConnectionString);
1228+
connection.Open();
1229+
connection.Execute(@"drop table if exists datatypes_tinyint1;
1230+
create table datatypes_tinyint1(value tinyint(1));
1231+
insert into datatypes_tinyint1(value) values(0), (1), (2), (-1), (-128), (127);");
1232+
1233+
using var command = new MySqlCommand("select value from datatypes_tinyint1;", connection);
1234+
if (prepare)
1235+
command.Prepare();
1236+
using var reader = command.ExecuteReader();
1237+
1238+
int[] expected = { 0, 1, 2, -1, -128, 127 };
1239+
if (treatTinyAsBoolean)
1240+
expected = expected.Select(x => x == 0 ? 0 : 1).ToArray();
1241+
1242+
for (int i = 0; i < expected.Length; i++)
1243+
{
1244+
Assert.True(reader.Read());
1245+
#if !BASELINE
1246+
Assert.Equal((sbyte) expected[i], reader.GetSByte(0));
1247+
if (treatTinyAsBoolean)
1248+
Assert.Equal((byte) expected[i], reader.GetByte(0));
1249+
#endif
1250+
Assert.Equal((short) expected[i], reader.GetInt16(0));
1251+
Assert.Equal(expected[i], reader.GetInt32(0));
1252+
Assert.Equal((long) expected[i], reader.GetInt64(0));
1253+
#if !BASELINE
1254+
Assert.Equal(expected[i], reader.GetFieldValue<int>(0));
1255+
#endif
1256+
}
1257+
1258+
Assert.False(reader.Read());
1259+
}
1260+
1261+
12171262
class BoolTest
12181263
{
12191264
public int Id { get; set; }

0 commit comments

Comments
 (0)