Skip to content

Commit

Permalink
nhibernateGH-3530: Use type specific methods of DbDataReader where po…
Browse files Browse the repository at this point in the history
…ssible and

use Convert with the user provided locale if necessary.
  • Loading branch information
David Ellingsworth authored and David Ellingsworth committed Jun 11, 2024
1 parent ac708f6 commit bb38501
Show file tree
Hide file tree
Showing 39 changed files with 494 additions and 54 deletions.
243 changes: 243 additions & 0 deletions src/NHibernate/AdoNet/DbDataReaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
using System;
using System.Data.Common;

namespace NHibernate.AdoNet
{
internal static class DbDataReaderExtensions
{
public static bool TryGetBoolean(this DbDataReader rs, int ordinal, out bool value)
{
try
{
value = rs.GetBoolean(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}

public static bool TryGetByte(this DbDataReader rs, int ordinal, out byte value)
{
try
{
value = rs.GetByte(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}

public static bool TryGetChar(this DbDataReader rs, int ordinal, out char value)
{
try
{
value = rs.GetChar(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}

public static bool TryGetDecimal(this DbDataReader rs, int ordinal, out decimal value)
{
try
{
value = rs.GetDecimal(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}

public static bool TryGetDouble(this DbDataReader rs, int ordinal, out double value)
{
try
{
value = rs.GetDouble(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}

public static bool TryGetDateTime(this DbDataReader rs, int ordinal, out DateTime value)
{
try
{
value = rs.GetDateTime(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}
public static bool TryGetFloat(this DbDataReader rs, int ordinal, out float value)
{
try
{
value = rs.GetFloat(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}
public static bool TryGetGuid(this DbDataReader rs, int ordinal, out Guid value)
{
try
{
value = rs.GetGuid(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}

public static bool TryGetUInt16(this DbDataReader rs, int ordinal, out ushort value)
{
var dbValue = rs[ordinal];

if (dbValue is ushort)
{
value = (ushort) dbValue;
return true;
}

value = default;
return false;
}

public static bool TryGetInt16(this DbDataReader rs, int ordinal, out short value)
{
try
{
value = rs.GetInt16(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}
public static bool TryGetInt32(this DbDataReader rs, int ordinal, out int value)
{
try
{
value = rs.GetInt32(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}

public static bool TryGetUInt32(this DbDataReader rs, int ordinal, out uint value)
{
var dbValue = rs[ordinal];

if (dbValue is uint)
{
value = (uint) dbValue;
return true;
}

value = default;
return false;
}

public static bool TryGetInt64(this DbDataReader rs, int ordinal, out long value)
{
try
{
value = rs.GetInt64(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}

public static bool TryGetUInt64(this DbDataReader rs, int ordinal, out ulong value)
{
var dbValue = rs[ordinal];

if (dbValue is ulong)
{
value = (ulong) dbValue;
return true;
}

value = default;
return false;
}

public static bool TryGetSByte(this DbDataReader rs, int ordinal, out sbyte value)
{
var dbValue = rs[ordinal];

if (dbValue is sbyte)
{
value = (sbyte) rs[ordinal];
return true;
}

value = default;
return false;
}

public static bool TryGetString(this DbDataReader rs, int ordinal, out string value)
{
try
{
value = rs.GetString(ordinal);
return true;
}
catch (InvalidCastException)
{
value = default;
return false;
}
}

public static bool TryGetTimeSpan(this DbDataReader rs, int ordinal, out TimeSpan value)
{
var dbValue = rs[ordinal];

if (dbValue is TimeSpan)
{
value = (TimeSpan) dbValue;
return true;
}

value = default;
return false;
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/AbstractDateTimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.Generic;
using System.Data.Common;
using System.Globalization;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/ByteType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Data;
using System.Data.Common;
using System.Numerics;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/Int16Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Collections.Generic;
using System.Data;
using System.Numerics;
using NHibernate.AdoNet;

namespace NHibernate.Type
{
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/Int32Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Collections.Generic;
using System.Data;
using System.Numerics;
using NHibernate.AdoNet;

namespace NHibernate.Type
{
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/Int64Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Data;
using System.Data.Common;
using System.Numerics;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/TicksType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System;
using System.Data;
using System.Data.Common;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/TimeAsTimeSpanType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using NHibernate.SqlTypes;
using System.Collections.Generic;
using System.Data;
using NHibernate.AdoNet;

namespace NHibernate.Type
{
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/TimeSpanType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using NHibernate.SqlTypes;
using System.Collections.Generic;
using System.Data;
using NHibernate.AdoNet;

namespace NHibernate.Type
{
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/UInt16Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Data;
using System.Data.Common;
using System.Numerics;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/UInt32Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Data;
using System.Data.Common;
using System.Numerics;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/UInt64Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Data;
using System.Data.Common;
using System.Numerics;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/UriType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

using System;
using System.Data.Common;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/XDocType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System;
using System.Data.Common;
using System.Xml.Linq;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/Type/XmlDocType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System;
using System.Data.Common;
using System.Xml;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand Down
19 changes: 16 additions & 3 deletions src/NHibernate/Type/AbstractCharType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Data.Common;
using NHibernate.AdoNet;
using NHibernate.Engine;
using NHibernate.SqlTypes;

Expand All @@ -20,12 +21,24 @@ public AbstractCharType(SqlType sqlType) : base(sqlType)

public override object Get(DbDataReader rs, int index, ISessionImplementor session)
{
string dbValue = Convert.ToString(rs[index]);
if (rs.TryGetChar(index, out var dbValue))
{
return dbValue;
}

if (!rs.TryGetString(index, out var strValue))
{
var locale = session.Factory.Settings.Locale;

strValue = Convert.ToString(rs[index], locale);
}

// The check of the Length is a workaround see NH-2340
if (dbValue.Length > 0)
if (strValue.Length > 0)
{
return dbValue[0];
return strValue[0];
}

return '\0'; // This line should never be executed
}

Expand Down
Loading

0 comments on commit bb38501

Please sign in to comment.