Skip to content

Commit 4fb39ab

Browse files
committed
Throw InvalidCastException from GetGuid if value is null.
1 parent 6c3273e commit 4fb39ab

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffs
9191
var column = ResultSet.ColumnDefinitions[ordinal];
9292
var columnType = column.ColumnType;
9393
if ((column.ColumnFlags & ColumnFlags.Binary) == 0 ||
94-
(columnType != ColumnType.String && columnType != ColumnType.VarString && columnType != ColumnType.TinyBlob &&
95-
columnType != ColumnType.Blob && columnType != ColumnType.MediumBlob && columnType != ColumnType.LongBlob))
94+
(columnType != ColumnType.String && columnType != ColumnType.VarString && columnType != ColumnType.TinyBlob &&
95+
columnType != ColumnType.Blob && columnType != ColumnType.MediumBlob && columnType != ColumnType.LongBlob))
9696
{
9797
throw new InvalidCastException("Can't convert {0} to bytes.".FormatInvariant(columnType));
9898
}
@@ -128,12 +128,14 @@ public Guid GetGuid(int ordinal)
128128
if (value is byte[] bytes && bytes.Length == 16)
129129
return new Guid(bytes);
130130

131+
if (m_dataOffsets[ordinal] == -1)
132+
throw new InvalidCastException("Column is NULL.");
131133
throw new MySqlException("The value could not be converted to a GUID: {0}".FormatInvariant(value));
132134
}
133135

134136
public short GetInt16(int ordinal)
135137
{
136-
object value = GetValue(ordinal);
138+
var value = GetValue(ordinal);
137139
if (value is short)
138140
return (short) value;
139141

@@ -158,7 +160,7 @@ public short GetInt16(int ordinal)
158160

159161
public int GetInt32(int ordinal)
160162
{
161-
object value = GetValue(ordinal);
163+
var value = GetValue(ordinal);
162164
if (value is int)
163165
return (int) value;
164166

@@ -183,7 +185,7 @@ public int GetInt32(int ordinal)
183185

184186
public long GetInt64(int ordinal)
185187
{
186-
object value = GetValue(ordinal);
188+
var value = GetValue(ordinal);
187189
if (value is long)
188190
return (long) value;
189191

@@ -216,7 +218,7 @@ public long GetInt64(int ordinal)
216218

217219
public double GetDouble(int ordinal)
218220
{
219-
object value = GetValue(ordinal);
221+
var value = GetValue(ordinal);
220222
return value is float floatValue ? floatValue : (double) value;
221223
}
222224

tests/SideBySide/DataTypes.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
// Additionally, that is what DbDataReader.GetFieldValue<T> throws. For consistency, we prefer InvalidCastException.
1515
#if BASELINE
1616
using GetValueWhenNullException = System.Data.SqlTypes.SqlNullValueException;
17+
using GetGuidWhenNullException = MySql.Data.MySqlClient.MySqlException;
1718
#else
1819
using GetValueWhenNullException = System.InvalidCastException;
20+
using GetGuidWhenNullException = System.InvalidCastException;
1921
#endif
2022

2123
namespace SideBySide
@@ -286,7 +288,7 @@ public void QueryGuid(string column, string dataTypeName, object[] expected)
286288
for (int i = 0; i < expected.Length; i++)
287289
if (expected[i] != null)
288290
expected[i] = Guid.Parse((string) expected[i]);
289-
DoQuery<MySqlException>("strings", column, dataTypeName, expected, reader => reader.GetGuid(0));
291+
DoQuery<GetGuidWhenNullException>("strings", column, dataTypeName, expected, reader => reader.GetGuid(0));
290292
}
291293

292294
[Theory]
@@ -379,7 +381,7 @@ public async Task GetGuid(string column, Type fieldType)
379381

380382
Assert.True(await reader.ReadAsync().ConfigureAwait(false));
381383
Assert.True(reader.IsDBNull(0));
382-
Assert.Throws<MySqlException>(() => reader.GetGuid(0));
384+
Assert.Throws<GetGuidWhenNullException>(() => reader.GetGuid(0));
383385

384386
Assert.True(await reader.ReadAsync().ConfigureAwait(false));
385387
Assert.False(reader.IsDBNull(0));

0 commit comments

Comments
 (0)