-
Notifications
You must be signed in to change notification settings - Fork 47
Refactored the OracleValueConverter to support Get<int> and Get<long>. #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.