-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from Havagan/feature/convert-int-long
Refactored the OracleValueConverter to support Get<int> and Get<long>.
- Loading branch information
Showing
8 changed files
with
220 additions
and
64 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
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
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
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
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 |
---|---|---|
@@ -1,73 +1,88 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Data.Common; | ||
using System.Linq; | ||
using System.Reflection.Emit; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Dapper.Oracle | ||
{ | ||
internal static class OracleValueConverter | ||
{ | ||
private static string[] OracleStringTypes { get; } = { "Oracle.DataAccess.Types.OracleString", "Oracle.ManagedDataAccess.Types.OracleString" }; | ||
|
||
public static T Convert<T>(object val) | ||
/// <summary> | ||
/// Convert the value to the provided generic type. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="value"></param> | ||
/// <returns></returns> | ||
public static T Convert<T>(object value) | ||
{ | ||
if (val == null) | ||
value = GetValue(value); | ||
|
||
if (value == null || value == DBNull.Value) | ||
{ | ||
return default(T); | ||
} | ||
|
||
if (val == DBNull.Value) | ||
// Convert the Oracle native data type to .NET data type. | ||
// See: https://docs.oracle.com/en/database/oracle/oracle-database/19/clrnt/datatype-conversion.html#GUID-70A2F34D-AB7F-4E0C-89C9-452A45FF1CAC | ||
if (value is IConvertible) | ||
{ | ||
if (default(T) != null) | ||
{ | ||
throw new ApplicationException("Attempting to cast a DBNull to a non nullable type!"); | ||
} | ||
|
||
return default(T); | ||
var nullableType = Nullable.GetUnderlyingType(typeof(T)); | ||
return (T)System.Convert.ChangeType(value, nullableType ?? typeof(T)); | ||
} | ||
|
||
Type valueType = val.GetType(); | ||
if (typeof(T) == typeof(string) && OracleStringTypes.Contains(valueType.FullName)) | ||
{ | ||
// Need this, because... you know,Oracle... | ||
val = val.ToString(); | ||
return default(T); | ||
} | ||
|
||
if ((string)val == "null") // Seriously. W.T.F ???? | ||
{ | ||
return default(T); | ||
} | ||
/// <summary> | ||
/// Gets the underlying primitive value from a nested type instance. | ||
/// </summary> | ||
/// <param name="value"></param> | ||
/// <returns></returns> | ||
private static object GetValue(object value) | ||
{ | ||
if (value == null || value == DBNull.Value) | ||
{ | ||
return null; | ||
} | ||
|
||
return (T)val; | ||
// Recursively handle nested database parameters. | ||
// An OracleParameter can have a value that is an instance of an Oracle data structure (OracleBlob, OracleDecimal, etc.). | ||
// This assumes we want the underlying primitive value of the parameter. | ||
while (value is DbParameter parameter) | ||
{ | ||
value = parameter.Value; | ||
} | ||
|
||
// We need this, because, you know, Oracle. OracleDecimal,OracleFloat,OracleYaddiAddy,OracleYourUncle etc value types. | ||
if (Regex.IsMatch(valueType.FullName, @"Oracle\.\w+\.Types\.Oracle\w+")) | ||
Type valueType = value.GetType(); | ||
|
||
if (IsOracleDataStructure(valueType)) | ||
{ | ||
// Fix Oracle 11g (to not throw the exception "Invalid operation on null data" if a function returns NULL) | ||
var isNullProperty = valueType.GetProperty("IsNull"); | ||
if (isNullProperty != null && isNullProperty.CanRead) | ||
{ | ||
var isNull = (bool)isNullProperty.GetValue(val); | ||
if (isNull) | ||
{ | ||
if (default(T) != null) | ||
{ | ||
throw new ApplicationException("Attempting to cast a DBNull to a non nullable type!"); | ||
} | ||
return default(T); | ||
} | ||
// If not isNull, continue and get the Value | ||
} | ||
var isNull = (bool)valueType.GetProperty("IsNull", typeof(bool))?.GetValue(value); | ||
|
||
var valueProperty = valueType.GetProperty("Value"); | ||
if (valueProperty != null && valueProperty.CanRead) | ||
if (isNull) | ||
{ | ||
object reflected = valueProperty.GetValue(val); | ||
return (T)reflected; | ||
return null; | ||
} | ||
|
||
value = valueType.GetProperty("Value")?.GetValue(value); | ||
} | ||
|
||
return (T)System.Convert.ChangeType(val, typeof(T)); | ||
return value; | ||
} | ||
|
||
/// <summary> | ||
/// Determine if the type is an Oracle data structure. | ||
/// </summary> | ||
/// <param name="valueType"></param> | ||
/// <returns></returns> | ||
private static bool IsOracleDataStructure(Type valueType) | ||
{ | ||
// We need this, because, you know, Oracle. OracleDecimal,OracleFloat,OracleYaddiAddy,OracleYourUncle etc value types. | ||
// See: https://docs.oracle.com/en/database/oracle/oracle-database/19/odpnt/intro003.html#GUID-425C9EBA-CFFC-47FE-B490-604251714ACA | ||
return Regex.IsMatch(valueType.FullName, @"Oracle\.\w+\.Types\.Oracle\w+"); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.