From 0563713a40c499774d24166a208f77905ba5b9a2 Mon Sep 17 00:00:00 2001 From: David Ellingsworth Date: Fri, 14 Jun 2024 11:34:56 -0400 Subject: [PATCH] GH-3530: Add an extension method to specify the return types when calling DbDataReader.GetValues() --- .../AdoNet/DbDataReaderExtensions.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/NHibernate/AdoNet/DbDataReaderExtensions.cs b/src/NHibernate/AdoNet/DbDataReaderExtensions.cs index 7b1095a28f..1634bc7cfc 100644 --- a/src/NHibernate/AdoNet/DbDataReaderExtensions.cs +++ b/src/NHibernate/AdoNet/DbDataReaderExtensions.cs @@ -239,5 +239,41 @@ public static bool TryGetTimeSpan(this DbDataReader rs, int ordinal, out TimeSpa value = default; return false; } + + public static object[] GetValues(this DbDataReader rs, System.Type[] types) + { + if (types.Length != rs.FieldCount) + { + throw new InvalidOperationException("Exptected number of types does not match the number of fields."); + } + + var values = new object[rs.FieldCount]; + + for (var i = 0; i < rs.FieldCount; i++) + { + var typeCode = System.Type.GetTypeCode(types[i]); + + values[i] = typeCode switch + { + TypeCode.Boolean => rs.GetBoolean(i), + TypeCode.Char => rs.GetChar(i), + TypeCode.Byte => rs.GetByte(i), + TypeCode.Int16 => rs.GetInt16(i), + TypeCode.Int32 => rs.GetInt32(i), + TypeCode.Int64 => rs.GetInt64(i), + TypeCode.Single => rs.GetFloat(i), + TypeCode.Double => rs.GetDouble(i), + TypeCode.Decimal => rs.GetDecimal(i), + TypeCode.DateTime => rs.GetDateTime(i), + TypeCode.String => rs.GetString(i), + TypeCode.UInt16 => (ushort) rs[i], + TypeCode.UInt32 => (uint) rs[i], + TypeCode.UInt64 => (ulong) rs[i], + _ => rs[i] + }; + } + + return values; + } } }