forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nhibernateGH-3530: Many drivers lack support for the DbDataReader.Get…
…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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} | ||
} |