This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 611
Expand file tree
/
Copy pathOracleValue.cs
More file actions
157 lines (127 loc) · 4.21 KB
/
Copy pathOracleValue.cs
File metadata and controls
157 lines (127 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
namespace ServiceStack.OrmLite.Oracle
{
public class OracleValue : IConvertible
{
private readonly object _oracleValue;
private readonly Type _oracleValueType;
public OracleValue(object oracleValue)
{
_oracleValue = oracleValue;
_oracleValueType = oracleValue.GetType();
}
public TypeCode GetTypeCode()
{
return typeof(OracleValue).GetTypeCode();
}
public bool ToBoolean(IFormatProvider provider)
{
return Convert.ToBoolean(Value);
}
public char ToChar(IFormatProvider provider)
{
return Convert.ToChar(Value);
}
public sbyte ToSByte(IFormatProvider provider)
{
return Convert.ToSByte(Value);
}
public byte ToByte(IFormatProvider provider)
{
return GetMethodValue<byte>("ToByte");
}
public short ToInt16(IFormatProvider provider)
{
return GetMethodValue<short>("ToInt16");
}
public ushort ToUInt16(IFormatProvider provider)
{
return Convert.ToUInt16(Value);
}
public int ToInt32(IFormatProvider provider)
{
return GetMethodValue<int>("ToInt32");
}
public uint ToUInt32(IFormatProvider provider)
{
return Convert.ToUInt32(Value);
}
public long ToInt64(IFormatProvider provider)
{
return GetMethodValue<long>("ToInt64");
}
public ulong ToUInt64(IFormatProvider provider)
{
return Convert.ToUInt64(Value);
}
public float ToSingle(IFormatProvider provider)
{
return GetMethodValue<float>("ToSingle");
}
public double ToDouble(IFormatProvider provider)
{
return GetMethodValue<double>("ToDouble");
}
public decimal ToDecimal(IFormatProvider provider)
{
return (decimal) Value;
}
private object Value
{
get
{
return GetMethodValue<object>("get_Value");
}
}
public bool IsNull()
{
if (_oracleValue is DBNull || _oracleValue == null)
return true;
return GetMethodValue<bool>("get_IsNull");
}
public DateTime ToDateTime(IFormatProvider provider)
{
return Convert.ToDateTime(Value);
}
public string ToString(IFormatProvider provider)
{
return _oracleValue == null ? string.Empty : _oracleValue.ToString();
}
public override string ToString()
{
return ToString(CultureInfo.CurrentCulture);
}
public object ToType(Type conversionType, IFormatProvider provider)
{
if (conversionType == typeof (DateTimeOffset?) && IsNull())
return null;
if (conversionType == typeof (DateTimeOffset) || conversionType == typeof (DateTimeOffset?))
return ToDateTimeOffset();
return Convert.ChangeType(Value, conversionType, provider);
}
private DateTimeOffset ToDateTimeOffset()
{
if (_oracleValue is DateTime)
return new DateTimeOffset((DateTime)_oracleValue);
var dateTime = (DateTime) Value;
var offset = GetMethodValue<TimeSpan>("GetTimeZoneOffset");
return new DateTimeOffset(dateTime, offset);
}
private readonly IDictionary<string, MethodInfo> _methodCache = new Dictionary<string, MethodInfo>();
private T GetMethodValue<T>(string methodName)
{
MethodInfo method;
if (!_methodCache.TryGetValue(methodName, out method))
{
method = _oracleValueType.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
_methodCache.Add(methodName, method);
}
return method != null
? (T)method.Invoke(_oracleValue, null)
: (T)_oracleValue;
}
}
}