0
|
1 using System;
|
|
2 using System.Data.SqlTypes;
|
|
3
|
|
4 using BLToolkit.Reflection;
|
|
5
|
|
6 namespace BLToolkit.Mapping
|
|
7 {
|
|
8 [CLSCompliant(false)]
|
|
9 public static class MapGetData<T>
|
|
10 {
|
|
11 public abstract class MB<V>
|
|
12 {
|
|
13 public abstract V From(IMapDataSource s, object o, int i);
|
|
14 }
|
|
15
|
|
16 public static T From(IMapDataSource s, object o, int i)
|
|
17 {
|
|
18 return I.From(s, o, i);
|
|
19 }
|
|
20
|
|
21 public static MB<T> I = GetGetter();
|
|
22 private static MB<T> GetGetter()
|
|
23 {
|
|
24 Type t = typeof(T);
|
|
25
|
|
26 // Scalar Types.
|
|
27 //
|
|
28 if (t == typeof(SByte)) return (MB<T>)(object)(new I8());
|
|
29 if (t == typeof(Int16)) return (MB<T>)(object)(new I16());
|
|
30 if (t == typeof(Int32)) return (MB<T>)(object)(new I32());
|
|
31 if (t == typeof(Int64)) return (MB<T>)(object)(new I64());
|
|
32
|
|
33 if (t == typeof(Byte)) return (MB<T>)(object)(new U8());
|
|
34 if (t == typeof(UInt16)) return (MB<T>)(object)(new U16());
|
|
35 if (t == typeof(UInt32)) return (MB<T>)(object)(new U32());
|
|
36 if (t == typeof(UInt64)) return (MB<T>)(object)(new U64());
|
|
37
|
|
38 if (t == typeof(Single)) return (MB<T>)(object)(new R4());
|
|
39 if (t == typeof(Double)) return (MB<T>)(object)(new R8());
|
|
40
|
|
41 if (t == typeof(Boolean)) return (MB<T>)(object)(new B());
|
|
42 if (t == typeof(Decimal)) return (MB<T>)(object)(new D());
|
|
43
|
|
44 if (t == typeof(Char)) return (MB<T>)(object)(new C());
|
|
45 if (t == typeof(Guid)) return (MB<T>)(object)(new G());
|
|
46 if (t == typeof(DateTime)) return (MB<T>)(object)(new DT());
|
|
47 if (t == typeof(DateTimeOffset)) return (MB<T>)(object)(new DTO());
|
|
48
|
|
49 // Enums.
|
|
50 //
|
|
51 if (t.IsEnum)
|
|
52 {
|
|
53 t = Enum.GetUnderlyingType(t);
|
|
54
|
|
55 if (t == typeof(SByte)) return new EI8<T>();
|
|
56 if (t == typeof(Int16)) return new EI16<T>();
|
|
57 if (t == typeof(Int32)) return new EI32<T>();
|
|
58 if (t == typeof(Int64)) return new EI64<T>();
|
|
59
|
|
60 if (t == typeof(Byte)) return new EU8<T>();
|
|
61 if (t == typeof(UInt16)) return new EU16<T>();
|
|
62 if (t == typeof(UInt32)) return new EU32<T>();
|
|
63 if (t == typeof(UInt64)) return new EU64<T>();
|
|
64 }
|
|
65
|
|
66 // Nullable Types.
|
|
67 //
|
|
68 if (t == typeof(SByte?)) return (MB<T>)(object)(new NI8());
|
|
69 if (t == typeof(Int16?)) return (MB<T>)(object)(new NI16());
|
|
70 if (t == typeof(Int32?)) return (MB<T>)(object)(new NI32());
|
|
71 if (t == typeof(Int64?)) return (MB<T>)(object)(new NI64());
|
|
72
|
|
73 if (t == typeof(Byte?)) return (MB<T>)(object)(new NU8());
|
|
74 if (t == typeof(UInt16?)) return (MB<T>)(object)(new NU16());
|
|
75 if (t == typeof(UInt32?)) return (MB<T>)(object)(new NU32());
|
|
76 if (t == typeof(UInt64?)) return (MB<T>)(object)(new NU64());
|
|
77
|
|
78 if (t == typeof(Single?)) return (MB<T>)(object)(new NR4());
|
|
79 if (t == typeof(Double?)) return (MB<T>)(object)(new NR8());
|
|
80
|
|
81 if (t == typeof(Boolean?)) return (MB<T>)(object)(new NB());
|
|
82 if (t == typeof(Decimal?)) return (MB<T>)(object)(new ND());
|
|
83
|
|
84 if (t == typeof(Char?)) return (MB<T>)(object)(new NC());
|
|
85 if (t == typeof(Guid?)) return (MB<T>)(object)(new NG());
|
|
86 if (t == typeof(DateTime?)) return (MB<T>)(object)(new NDT());
|
|
87 if (t == typeof(DateTimeOffset?)) return (MB<T>)(object)(new NDTO());
|
|
88
|
|
89 // Nullable Enums.
|
|
90 //
|
|
91 if (TypeHelper.IsNullable(t) && Nullable.GetUnderlyingType(t).IsEnum)
|
|
92 {
|
|
93 Type enumType = Nullable.GetUnderlyingType(t);
|
|
94 t = Enum.GetUnderlyingType(enumType);
|
|
95
|
|
96 if (t == typeof(SByte)) return (MB<T>)Activator.CreateInstance(typeof(NEI8<>).MakeGenericType(typeof(T), enumType));
|
|
97 if (t == typeof(Int16)) return (MB<T>)Activator.CreateInstance(typeof(NEI16<>).MakeGenericType(typeof(T), enumType));
|
|
98 if (t == typeof(Int32)) return (MB<T>)Activator.CreateInstance(typeof(NEI32<>).MakeGenericType(typeof(T), enumType));
|
|
99 if (t == typeof(Int64)) return (MB<T>)Activator.CreateInstance(typeof(NEI64<>).MakeGenericType(typeof(T), enumType));
|
|
100
|
|
101 if (t == typeof(Byte)) return (MB<T>)Activator.CreateInstance(typeof(NEU8<>).MakeGenericType(typeof(T), enumType));
|
|
102 if (t == typeof(UInt16)) return (MB<T>)Activator.CreateInstance(typeof(NEU16<>).MakeGenericType(typeof(T), enumType));
|
|
103 if (t == typeof(UInt32)) return (MB<T>)Activator.CreateInstance(typeof(NEU32<>).MakeGenericType(typeof(T), enumType));
|
|
104 if (t == typeof(UInt64)) return (MB<T>)Activator.CreateInstance(typeof(NEU64<>).MakeGenericType(typeof(T), enumType));
|
|
105 }
|
|
106
|
|
107 #if !SILVERLIGHT
|
|
108
|
|
109 // SqlTypes.
|
|
110 //
|
|
111 if (t == typeof(SqlString)) return (MB<T>)(object)(new dbS());
|
|
112
|
|
113 if (t == typeof(SqlByte)) return (MB<T>)(object)(new dbU8());
|
|
114 if (t == typeof(SqlInt16)) return (MB<T>)(object)(new dbI16());
|
|
115 if (t == typeof(SqlInt32)) return (MB<T>)(object)(new dbI32());
|
|
116 if (t == typeof(SqlInt64)) return (MB<T>)(object)(new dbI64());
|
|
117
|
|
118 if (t == typeof(SqlSingle)) return (MB<T>)(object)(new dbR4());
|
|
119 if (t == typeof(SqlDouble)) return (MB<T>)(object)(new dbR8());
|
|
120 if (t == typeof(SqlDecimal)) return (MB<T>)(object)(new dbD());
|
|
121 if (t == typeof(SqlMoney)) return (MB<T>)(object)(new dbM());
|
|
122
|
|
123 if (t == typeof(SqlBoolean)) return (MB<T>)(object)(new dbB());
|
|
124 if (t == typeof(SqlGuid)) return (MB<T>)(object)(new dbG());
|
|
125 if (t == typeof(SqlDateTime)) return (MB<T>)(object)(new dbDT());
|
|
126
|
|
127 #endif
|
|
128
|
|
129 return new Default<T>();
|
|
130 }
|
|
131
|
|
132 // Default setter.
|
|
133 //
|
|
134 public sealed class Default<V> : MB<V> { public override V From(IMapDataSource s, object o, int i) { return (V)s.GetValue (o, i); } }
|
|
135
|
|
136 // Scalar Types.
|
|
137 //
|
|
138 sealed class I8 : MB<SByte> { public override SByte From(IMapDataSource s, object o, int i) { return s.GetSByte (o, i); } }
|
|
139 sealed class I16 : MB<Int16> { public override Int16 From(IMapDataSource s, object o, int i) { return s.GetInt16 (o, i); } }
|
|
140 sealed class I32 : MB<Int32> { public override Int32 From(IMapDataSource s, object o, int i) { return s.GetInt32 (o, i); } }
|
|
141 sealed class I64 : MB<Int64> { public override Int64 From(IMapDataSource s, object o, int i) { return s.GetInt64 (o, i); } }
|
|
142
|
|
143 sealed class U8 : MB<Byte> { public override Byte From(IMapDataSource s, object o, int i) { return s.GetByte (o, i); } }
|
|
144 sealed class U16 : MB<UInt16> { public override UInt16 From(IMapDataSource s, object o, int i) { return s.GetUInt16 (o, i); } }
|
|
145 sealed class U32 : MB<UInt32> { public override UInt32 From(IMapDataSource s, object o, int i) { return s.GetUInt32 (o, i); } }
|
|
146 sealed class U64 : MB<UInt64> { public override UInt64 From(IMapDataSource s, object o, int i) { return s.GetUInt64 (o, i); } }
|
|
147
|
|
148 sealed class R4 : MB<Single> { public override Single From(IMapDataSource s, object o, int i) { return s.GetSingle (o, i); } }
|
|
149 sealed class R8 : MB<Double> { public override Double From(IMapDataSource s, object o, int i) { return s.GetDouble (o, i); } }
|
|
150
|
|
151 sealed class B : MB<Boolean> { public override Boolean From(IMapDataSource s, object o, int i) { return s.GetBoolean (o, i); } }
|
|
152 sealed class D : MB<Decimal> { public override Decimal From(IMapDataSource s, object o, int i) { return s.GetDecimal (o, i); } }
|
|
153
|
|
154 sealed class C : MB<Char> { public override Char From(IMapDataSource s, object o, int i) { return s.GetChar (o, i); } }
|
|
155 sealed class G : MB<Guid> { public override Guid From(IMapDataSource s, object o, int i) { return s.GetGuid (o, i); } }
|
|
156 sealed class DT : MB<DateTime> { public override DateTime From(IMapDataSource s, object o, int i) { return s.GetDateTime (o, i); } }
|
|
157 sealed class DTO : MB<DateTimeOffset> { public override DateTimeOffset From(IMapDataSource s, object o, int i) { return s.GetDateTimeOffset (o, i); } }
|
|
158 // Enums.
|
|
159 //
|
|
160 sealed class EI8<E> : MB<E> { public override E From(IMapDataSource s, object o, int i) { return (E)(object)s.GetSByte (o, i); } }
|
|
161 sealed class EI16<E> : MB<E> { public override E From(IMapDataSource s, object o, int i) { return (E)(object)s.GetInt16 (o, i); } }
|
|
162 sealed class EI32<E> : MB<E> { public override E From(IMapDataSource s, object o, int i) { return (E)(object)s.GetInt32 (o, i); } }
|
|
163 sealed class EI64<E> : MB<E> { public override E From(IMapDataSource s, object o, int i) { return (E)(object)s.GetInt64 (o, i); } }
|
|
164
|
|
165 sealed class EU8<E> : MB<E> { public override E From(IMapDataSource s, object o, int i) { return (E)(object)s.GetByte (o, i); } }
|
|
166 sealed class EU16<E> : MB<E> { public override E From(IMapDataSource s, object o, int i) { return (E)(object)s.GetUInt16 (o, i); } }
|
|
167 sealed class EU32<E> : MB<E> { public override E From(IMapDataSource s, object o, int i) { return (E)(object)s.GetUInt32 (o, i); } }
|
|
168 sealed class EU64<E> : MB<E> { public override E From(IMapDataSource s, object o, int i) { return (E)(object)s.GetUInt64 (o, i); } }
|
|
169
|
|
170 // Nullable Types.
|
|
171 //
|
|
172 sealed class NI8 : MB<SByte?> { public override SByte? From(IMapDataSource s, object o, int i) { return s.GetNullableSByte (o, i); } }
|
|
173 sealed class NI16 : MB<Int16?> { public override Int16? From(IMapDataSource s, object o, int i) { return s.GetNullableInt16 (o, i); } }
|
|
174 sealed class NI32 : MB<Int32?> { public override Int32? From(IMapDataSource s, object o, int i) { return s.GetNullableInt32 (o, i); } }
|
|
175 sealed class NI64 : MB<Int64?> { public override Int64? From(IMapDataSource s, object o, int i) { return s.GetNullableInt64 (o, i); } }
|
|
176
|
|
177 sealed class NU8 : MB<Byte?> { public override Byte? From(IMapDataSource s, object o, int i) { return s.GetNullableByte (o, i); } }
|
|
178 sealed class NU16 : MB<UInt16?> { public override UInt16? From(IMapDataSource s, object o, int i) { return s.GetNullableUInt16 (o, i); } }
|
|
179 sealed class NU32 : MB<UInt32?> { public override UInt32? From(IMapDataSource s, object o, int i) { return s.GetNullableUInt32 (o, i); } }
|
|
180 sealed class NU64 : MB<UInt64?> { public override UInt64? From(IMapDataSource s, object o, int i) { return s.GetNullableUInt64 (o, i); } }
|
|
181
|
|
182 sealed class NR4 : MB<Single?> { public override Single? From(IMapDataSource s, object o, int i) { return s.GetNullableSingle (o, i); } }
|
|
183 sealed class NR8 : MB<Double?> { public override Double? From(IMapDataSource s, object o, int i) { return s.GetNullableDouble (o, i); } }
|
|
184
|
|
185 sealed class NB : MB<Boolean?> { public override Boolean? From(IMapDataSource s, object o, int i) { return s.GetNullableBoolean (o, i); } }
|
|
186 sealed class ND : MB<Decimal?> { public override Decimal? From(IMapDataSource s, object o, int i) { return s.GetNullableDecimal (o, i); } }
|
|
187
|
|
188 sealed class NC : MB<Char?> { public override Char? From(IMapDataSource s, object o, int i) { return s.GetNullableChar (o, i); } }
|
|
189 sealed class NG : MB<Guid?> { public override Guid? From(IMapDataSource s, object o, int i) { return s.GetNullableGuid (o, i); } }
|
|
190 sealed class NDT : MB<DateTime?> { public override DateTime? From(IMapDataSource s, object o, int i) { return s.GetNullableDateTime (o, i); } }
|
|
191 sealed class NDTO : MB<DateTimeOffset?> { public override DateTimeOffset? From(IMapDataSource s, object o, int i) { return s.GetNullableDateTimeOffset (o, i); } }
|
|
192
|
|
193 // Nullable Enums.
|
|
194 //
|
|
195 sealed class NEI8<E> : MB<E?> where E : struct { public override E? From(IMapDataSource s, object o, int i) { return /*s.IsNull(o, i) ? (E?)null :*/ (E)(object)s.GetSByte (o, i); } }
|
|
196 sealed class NEI16<E> : MB<E?> where E : struct { public override E? From(IMapDataSource s, object o, int i) { return /*s.IsNull(o, i) ? (E?)null :*/ (E)(object)s.GetInt16 (o, i); } }
|
|
197 sealed class NEI32<E> : MB<E?> where E : struct { public override E? From(IMapDataSource s, object o, int i) { return /*s.IsNull(o, i) ? (E?)null :*/ (E)(object)s.GetInt32 (o, i); } }
|
|
198 sealed class NEI64<E> : MB<E?> where E : struct { public override E? From(IMapDataSource s, object o, int i) { return /*s.IsNull(o, i) ? (E?)null :*/ (E)(object)s.GetInt64 (o, i); } }
|
|
199
|
|
200 sealed class NEU8<E> : MB<E?> where E : struct { public override E? From(IMapDataSource s, object o, int i) { return /*s.IsNull(o, i) ? (E?)null :*/ (E)(object)s.GetByte (o, i); } }
|
|
201 sealed class NEU16<E> : MB<E?> where E : struct { public override E? From(IMapDataSource s, object o, int i) { return /*s.IsNull(o, i) ? (E?)null :*/ (E)(object)s.GetUInt16(o, i); } }
|
|
202 sealed class NEU32<E> : MB<E?> where E : struct { public override E? From(IMapDataSource s, object o, int i) { return /*s.IsNull(o, i) ? (E?)null :*/ (E)(object)s.GetUInt32(o, i); } }
|
|
203 sealed class NEU64<E> : MB<E?> where E : struct { public override E? From(IMapDataSource s, object o, int i) { return /*s.IsNull(o, i) ? (E?)null :*/ (E)(object)s.GetUInt64(o, i); } }
|
|
204
|
|
205 #if !SILVERLIGHT
|
|
206
|
|
207 // SqlTypes.
|
|
208 //
|
|
209 sealed class dbS : MB<SqlString> { public override SqlString From(IMapDataSource s, object o, int i) { return s.GetSqlString (o, i); } }
|
|
210
|
|
211 sealed class dbU8 : MB<SqlByte> { public override SqlByte From(IMapDataSource s, object o, int i) { return s.GetSqlByte (o, i); } }
|
|
212 sealed class dbI16 : MB<SqlInt16> { public override SqlInt16 From(IMapDataSource s, object o, int i) { return s.GetSqlInt16 (o, i); } }
|
|
213 sealed class dbI32 : MB<SqlInt32> { public override SqlInt32 From(IMapDataSource s, object o, int i) { return s.GetSqlInt32 (o, i); } }
|
|
214 sealed class dbI64 : MB<SqlInt64> { public override SqlInt64 From(IMapDataSource s, object o, int i) { return s.GetSqlInt64 (o, i); } }
|
|
215
|
|
216 sealed class dbR4 : MB<SqlSingle> { public override SqlSingle From(IMapDataSource s, object o, int i) { return s.GetSqlSingle (o, i); } }
|
|
217 sealed class dbR8 : MB<SqlDouble> { public override SqlDouble From(IMapDataSource s, object o, int i) { return s.GetSqlDouble (o, i); } }
|
|
218 sealed class dbD : MB<SqlDecimal> { public override SqlDecimal From(IMapDataSource s, object o, int i) { return s.GetSqlDecimal (o, i); } }
|
|
219 sealed class dbM : MB<SqlMoney> { public override SqlMoney From(IMapDataSource s, object o, int i) { return s.GetSqlMoney (o, i); } }
|
|
220
|
|
221 sealed class dbB : MB<SqlBoolean> { public override SqlBoolean From(IMapDataSource s, object o, int i) { return s.GetSqlBoolean (o, i); } }
|
|
222 sealed class dbG : MB<SqlGuid> { public override SqlGuid From(IMapDataSource s, object o, int i) { return s.GetSqlGuid (o, i); } }
|
|
223 sealed class dbDT : MB<SqlDateTime> { public override SqlDateTime From(IMapDataSource s, object o, int i) { return s.GetSqlDateTime (o, i); } }
|
|
224
|
|
225 #endif
|
|
226 }
|
|
227 }
|