Skip to content

Commit b476d5d

Browse files
David EllingsworthDavid Ellingsworth
David Ellingsworth
authored and
David Ellingsworth
committed
GH-3530: Many drivers lack support for the DbDataReader.GetChar method. Add a NoCharDbDataReader to use with these drivers.
1 parent 1c18cfe commit b476d5d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.Common;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace NHibernate.AdoNet
9+
{
10+
/// <summary>
11+
/// Many database drivers lack support for DbDataReader.GetChar and throw a
12+
/// NotSupportedException. This reader provides an implementation on top of
13+
/// the indexer method for defficient drivers.
14+
/// </summary>
15+
public class NoCharDbDataReader : DbDataReaderWrapper
16+
{
17+
public NoCharDbDataReader(DbDataReader reader) : base(reader) { }
18+
19+
public override char GetChar(int ordinal)
20+
{
21+
// The underlying DataReader does not support the GetChar method.
22+
// Use the indexer to obtain the value and convert it to a char if necessary.
23+
var value = DataReader[ordinal];
24+
25+
return value switch
26+
{
27+
string { Length: > 0 } s => s[0],
28+
_ => (char) value
29+
};
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)