Skip to content

Commit 0fdd454

Browse files
committed
Implement GetFieldValue<Stream>. Fixes #631
1 parent e7485e3 commit 0fdd454

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlDataReader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ public override T GetFieldValue<T>(int ordinal)
268268
return (T) Convert.ChangeType(GetDateTimeOffset(ordinal), typeof(T));
269269
if (typeof(T) == typeof(TextReader) || typeof(T) == typeof(StringReader))
270270
return (T) (object) GetTextReader(ordinal);
271+
if (typeof(T) == typeof(Stream))
272+
return (T) (object) GetStream(ordinal);
271273

272274
return base.GetFieldValue<T>(ordinal);
273275
}

tests/SideBySide/DataTypes.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
#if BASELINE
1818
using GetValueWhenNullException = System.Data.SqlTypes.SqlNullValueException;
1919
using GetGuidWhenNullException = MySql.Data.MySqlClient.MySqlException;
20+
using GetBytesWhenNullException = System.NullReferenceException;
2021
#else
2122
using GetValueWhenNullException = System.InvalidCastException;
2223
using GetGuidWhenNullException = System.InvalidCastException;
24+
using GetBytesWhenNullException = System.InvalidCastException;
2325
#endif
2426

2527
namespace SideBySide
@@ -824,13 +826,20 @@ public void QueryBlob(string column, int padLength)
824826
if (data.Length < padLength)
825827
Array.Resize(ref data, padLength);
826828

827-
#if BASELINE
828-
DoQuery<NullReferenceException>("blobs", "`" + column + "`", "BLOB", new object[] { null, data }, GetBytes);
829-
// https://bugs.mysql.com/bug.php?id=93374
830-
// DoQuery<NullReferenceException>("blobs", "`" + column + "`", "BLOB", new object[] { null, data }, GetStreamBytes);
831-
#else
832-
DoQuery<InvalidCastException>("blobs", "`" + column + "`", "BLOB", new object[] { null, data }, GetBytes);
833-
DoQuery<InvalidCastException>("blobs", "`" + column + "`", "BLOB", new object[] { null, data }, GetStreamBytes);
829+
DoQuery<GetBytesWhenNullException>("blobs", "`" + column + "`", "BLOB", new object[] { null, data }, GetBytes);
830+
#if !BASELINE // https://bugs.mysql.com/bug.php?id=93374
831+
DoQuery("blobs", "`" + column + "`", "BLOB", new object[] { null, data }, GetStreamBytes);
832+
DoQuery("blobs", "`" + column + "`", "BLOB", new object[] { null, data }, reader => reader.GetStream(0), matchesDefaultType: false, assertEqual: (e, a) =>
833+
{
834+
using (var stream = (Stream) a)
835+
{
836+
Assert.True(stream.CanRead);
837+
Assert.False(stream.CanWrite);
838+
var bytes = new byte[stream.Length];
839+
Assert.Equal(bytes.Length, stream.Read(bytes, 0, bytes.Length));
840+
Assert.Equal(e, bytes);
841+
}
842+
}, getFieldValueType: typeof(Stream));
834843
#endif
835844
}
836845

0 commit comments

Comments
 (0)