Skip to content

Allow reading bool as integer, like MySql.Data allows (Fixes #782) #783

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 1 commit into from
Mar 26, 2020
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
15 changes: 12 additions & 3 deletions src/MySqlConnector/Core/Row.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public short GetInt16(int ordinal)
return checked((short) (ulong) value);
if (value is decimal)
return (short) (decimal) value;
if (value is bool)
return (bool) value ? (short) 1 : (short) 0;
return (short) value;
}

Expand All @@ -207,16 +209,15 @@ public int GetInt32(int ordinal)
throw new InvalidCastException();

var columnDefinition = ResultSet.ColumnDefinitions[ordinal];
if ((columnDefinition.ColumnType != ColumnType.Tiny &&
if (columnDefinition.ColumnType != ColumnType.Tiny &&
columnDefinition.ColumnType != ColumnType.Short &&
columnDefinition.ColumnType != ColumnType.Int24 &&
columnDefinition.ColumnType != ColumnType.Long &&
columnDefinition.ColumnType != ColumnType.Longlong &&
columnDefinition.ColumnType != ColumnType.Bit &&
columnDefinition.ColumnType != ColumnType.Year &&
columnDefinition.ColumnType != ColumnType.Decimal &&
columnDefinition.ColumnType != ColumnType.NewDecimal) ||
(columnDefinition.ColumnType == ColumnType.Tiny && Connection.TreatTinyAsBoolean && columnDefinition.ColumnLength == 1 && (columnDefinition.ColumnFlags & ColumnFlags.Unsigned) == 0))
columnDefinition.ColumnType != ColumnType.NewDecimal)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this fails to coerce "bool" values to 0 or 1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: 693b8e0

{
throw new InvalidCastException("Can't convert {0} to Int32".FormatInvariant(ResultSet.ColumnTypes![ordinal]));
}
Expand Down Expand Up @@ -252,6 +253,8 @@ public long GetInt64(int ordinal)
return checked((long) (ulong) value);
if (value is decimal)
return (long) (decimal) value;
if (value is bool)
return (bool)value ? 1 : 0;
return (long) value;
}

Expand All @@ -277,6 +280,8 @@ public ushort GetUInt16(int ordinal)
return checked((ushort) (ulong) value);
if (value is decimal)
return (ushort) (decimal) value;
if (value is bool)
return (bool) value ? (ushort) 1 : (ushort) 0;
return (ushort) value;
}

Expand All @@ -302,6 +307,8 @@ public uint GetUInt32(int ordinal)
return checked((uint) (ulong) value);
if (value is decimal)
return (uint) (decimal) value;
if (value is bool)
return (bool) value ? (uint) 1 : (uint) 0;
return (uint) value;
}

Expand All @@ -327,6 +334,8 @@ public ulong GetUInt64(int ordinal)
return checked((ulong) (long) value);
if (value is decimal)
return (ulong) (decimal) value;
if (value is bool)
return (bool) value ? (ulong) 1 : (ulong) 0;
return (ulong) value;
}

Expand Down