Skip to content

Commit 8cb0c43

Browse files
committed
Implement GetChar. Fixes #456
1 parent 27ba26d commit 8cb0c43

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ public long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffs
112112
return lengthToCopy;
113113
}
114114

115-
public char GetChar(int ordinal) => (char) GetValue(ordinal);
115+
public char GetChar(int ordinal)
116+
{
117+
var stringValue = (string) GetValue(ordinal);
118+
return stringValue.Length > 0 ? stringValue[0] : throw new InvalidCastException();
119+
}
116120

117121
public long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length)
118122
{

tests/SideBySide/DataTypes.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,34 @@ public void QueryGuid(string column, string dataTypeName, object[] expected)
347347
DoQuery<GetGuidWhenNullException>("strings", column, dataTypeName, expected, reader => reader.GetGuid(0));
348348
}
349349

350+
[Theory]
351+
[InlineData("utf8", new[] {null, "", "ASCII", "Ũńıċōđĕ", c_251ByteString})]
352+
[InlineData("cp1251", new[] { null, "", "ASCII", "АБВГабвг", c_251ByteString })]
353+
public void QueryChar(string column, string[] expected)
354+
{
355+
using (var cmd = m_database.Connection.CreateCommand())
356+
{
357+
cmd.CommandText = $@"select `{column}` from datatypes_strings order by rowid;";
358+
using (var reader = cmd.ExecuteReader())
359+
{
360+
for (var i = 0; i < expected.Length; i++)
361+
{
362+
Assert.True(reader.Read());
363+
if (expected[i] == null)
364+
Assert.True(reader.IsDBNull(0));
365+
else if (expected[i].Length == 0)
366+
#if BASELINE
367+
Assert.Throws<IndexOutOfRangeException>(() => reader.GetChar(0));
368+
#else
369+
Assert.Throws<InvalidCastException>(() => reader.GetChar(0));
370+
#endif
371+
else
372+
Assert.Equal(expected[i][0], reader.GetChar(0));
373+
}
374+
}
375+
}
376+
}
377+
350378
[Theory]
351379
[InlineData(false)]
352380
[InlineData(true)]

0 commit comments

Comments
 (0)