Skip to content

Commit

Permalink
nhibernateGH-3530: Many drivers lack support for the DbDataReader.Get…
Browse files Browse the repository at this point in the history
…Char method. Add a NoCharDbDataReader to use with these drivers.
  • Loading branch information
David Ellingsworth authored and David Ellingsworth committed Jun 11, 2024
1 parent 4b010ce commit de669f2
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/NHibernate/AdoNet/NoCharDbDataReader.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
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
};
}
}
}

0 comments on commit de669f2

Please sign in to comment.