From de669f27ee1e633f28f84fccbb8999cede2a543a Mon Sep 17 00:00:00 2001 From: David Ellingsworth Date: Tue, 11 Jun 2024 09:13:02 -0400 Subject: [PATCH] GH-3530: Many drivers lack support for the DbDataReader.GetChar method. Add a NoCharDbDataReader to use with these drivers. --- src/NHibernate/AdoNet/NoCharDbDataReader.cs | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/NHibernate/AdoNet/NoCharDbDataReader.cs diff --git a/src/NHibernate/AdoNet/NoCharDbDataReader.cs b/src/NHibernate/AdoNet/NoCharDbDataReader.cs new file mode 100644 index 0000000000..d7d4860b92 --- /dev/null +++ b/src/NHibernate/AdoNet/NoCharDbDataReader.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Data.Common; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NHibernate.AdoNet +{ + /// + /// Many database drivers lack support for DbDataReader.GetChar and throw a + /// NotSupportedException. This reader provides an implementation on top of + /// the indexer method for defficient drivers. + /// + public class NoCharDbDataReader : DbDataReaderWrapper + { + public NoCharDbDataReader(DbDataReader reader) : base(reader) { } + + public override char GetChar(int ordinal) + { + // The underlying DataReader does not support the GetChar method. + // Use the indexer to obtain the value and convert it to a char if necessary. + var value = DataReader[ordinal]; + + return value switch + { + string { Length: > 0 } s => s[0], + _ => (char) value + }; + } + } +}