forked from raquelsa/AspNet.Identity.MySQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataRecordExtensions.cs
326 lines (267 loc) · 11.6 KB
/
DataRecordExtensions.cs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using System;
using System.Data;
namespace AspNet.Identity.MySQL
{
public static class DataRecordExtensions
{
public static bool IsDBNull(this IDataRecord record, String column)
{
return record.IsDBNull(record.GetOrdinal(column));
}
public static Object GetValue(this IDataRecord record, String column)
{
return record.GetValue(record.GetOrdinal(column));
}
public static String GetString(this IDataRecord record, String column)
{
return record.GetString(record.GetOrdinal(column));
}
public static Int16 GetInt16(this IDataRecord record, String column)
{
return record.GetInt16(record.GetOrdinal(column));
}
public static Int32 GetInt32(this IDataRecord record, String column)
{
return record.GetInt32(record.GetOrdinal(column));
}
public static Int64 GetInt64(this IDataRecord record, String column)
{
return record.GetInt64(record.GetOrdinal(column));
}
public static Decimal GetDecimal(this IDataRecord record, String column)
{
return record.GetDecimal(record.GetOrdinal(column));
}
// overload to match IDataRecord methods
public static Single GetFloat(this IDataRecord record, String column)
{
return GetSingle(record, column);
}
// the proper name for Float in .NET
public static Single GetSingle(this IDataRecord record, String column)
{
return record.GetFloat(record.GetOrdinal(column));
}
public static Double GetDouble(this IDataRecord record, String column)
{
return record.GetDouble(record.GetOrdinal(column));
}
public static Boolean GetBoolean(this IDataRecord record, String column)
{
return record.GetBoolean(record.GetOrdinal(column));
}
public static Char GetChar(this IDataRecord record, String column)
{
return record.GetChar(record.GetOrdinal(column));
}
public static Byte GetByte(this IDataRecord record, String column)
{
return record.GetByte(record.GetOrdinal(column));
}
public static long GetChars(this IDataRecord record, String column, long fieldOffset, char[] buffer, int bufferOffset, int length)
{
return record.GetChars(record.GetOrdinal(column), fieldOffset, buffer, bufferOffset, length);
}
public static long GetBytes(this IDataRecord record, String name, long fieldOffset, byte[] buffer, int bufferOffset, int length)
{
return record.GetBytes(record.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
}
public static DateTime GetDateTime(this IDataRecord record, String column)
{
return record.GetDateTime(record.GetOrdinal(column));
}
public static Guid GetGuid(this IDataRecord record, String column)
{
return record.GetGuid(record.GetOrdinal(column));
}
public static IDataReader GetData(this IDataRecord record, String column)
{
return record.GetData(record.GetOrdinal(column));
}
public static String GetDataTypeName(this IDataRecord record, String column)
{
return record.GetDataTypeName(record.GetOrdinal(column));
}
public static Type GetFieldType(this IDataRecord record, String column)
{
return record.GetFieldType(record.GetOrdinal(column));
}
#region special signed/unsigned cases not available in IDataRecord/IDataRecord
public static SByte GetSByte(this IDataRecord record, String column)
{
return record.GetSByte(record.GetOrdinal(column));
}
public static SByte GetSByte(this IDataRecord record, int index)
{
var value = record.GetValue(index);
if (value is SByte)
{
return (SByte) value;
}
return Convert.ToSByte(value);
}
public static UInt16 GetUInt16(this IDataRecord record, String column)
{
return record.GetUInt16(record.GetOrdinal(column));
}
public static UInt16 GetUInt16(this IDataRecord record, int index)
{
var value = record.GetValue(index);
if (value is UInt16)
{
return (UInt16) value;
}
return Convert.ToUInt16(value);
}
public static UInt32 GetUInt32(this IDataRecord record, String column)
{
return record.GetUInt32(record.GetOrdinal(column));
}
public static UInt32 GetUInt32(this IDataRecord record, int index)
{
var value = record.GetValue(index);
if (value is UInt32)
{
return (UInt32) value;
}
return Convert.ToUInt32(value);
}
public static UInt64 GetUInt64(this IDataRecord record, String column)
{
return record.GetUInt64(record.GetOrdinal(column));
}
public static UInt64 GetUInt64(this IDataRecord record, int index)
{
var value = record.GetValue(index);
if (value is UInt64)
{
return (UInt64) value;
}
return Convert.ToUInt64(value);
}
#endregion
#region altogether new methods not part of IDataRecord
// simpler to just catch exception then check the entire field count
public static bool HasColumn(this IDataRecord record, String columnName)
{
try
{
record.GetOrdinal(columnName);
return true;
}
catch (IndexOutOfRangeException)
// we don't want to catch ALL exceptions, just the one that indicates failure
{
return false;
}
}
// useful for enums
public static T GetField<T>(this IDataRecord record, String column)
{
return (T) record.GetValue(record.GetOrdinal(column));
}
#endregion
#region get as nullable
// not truly as a Nullable, simply casted to Object (which can be null)
public static Object GetValueAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Object) null : record.GetValue(ordinal);
}
// again doesn't return a Nullable because Strings themselves can be null
public static String GetStringAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (String) null : record.GetString(ordinal);
}
public static Nullable<Int16> GetInt16AsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<Int16>) null : record.GetInt16(ordinal);
}
public static Nullable<Int32> GetInt32AsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<Int32>) null : record.GetInt32(ordinal);
}
public static Nullable<Int64> GetInt64AsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<Int64>) null : record.GetInt64(ordinal);
}
public static Nullable<Decimal> GetDecimalAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<Decimal>) null : record.GetDecimal(ordinal);
}
public static Nullable<Single> GetFloatAsNullable(this IDataRecord record, String column)
{
return GetSingleAsNullable(record, column);
}
public static Nullable<Single> GetSingleAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<Single>) null : record.GetFloat(ordinal);
}
public static Nullable<Double> GetDoubleAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<Double>) null : record.GetDouble(ordinal);
}
public static Nullable<Boolean> GetBooleanAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<Boolean>) null : record.GetBoolean(ordinal);
}
public static Nullable<Char> GetCharAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<Char>) null : record.GetChar(ordinal);
}
public static Nullable<Byte> GetByteAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<Byte>) null : record.GetByte(ordinal);
}
public static Nullable<DateTime> GetDateTimeAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<DateTime>) null : record.GetDateTime(ordinal);
}
public static Nullable<Guid> GetGuidAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<Guid>) null : record.GetGuid(ordinal);
}
public static Nullable<SByte> GetSByteAsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<SByte>) null : record.GetSByte(ordinal);
}
public static Nullable<UInt16> GetUInt16AsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<UInt16>) null : record.GetUInt16(ordinal);
}
public static Nullable<UInt32> GetUInt32AsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<UInt32>) null : record.GetUInt32(ordinal);
}
public static Nullable<UInt64> GetUInt64AsNullable(this IDataRecord record, String column)
{
var ordinal = record.GetOrdinal(column);
return record.IsDBNull(ordinal) ? (Nullable<UInt64>) null : record.GetUInt64(ordinal);
}
// similar to GetValueAsNullable except object is required to be a struct and we'll cast to the Nullable<T> of it
// GetFieldValue is a new method in DbDataReader but not in DataRecord
// we created this similar functionality here with different method name
public static Nullable<T> GetFieldAsNullable<T>(this IDataRecord record, String column)
where T : struct
{
var ordinal = record.GetOrdinal(column);
return (T) (record.IsDBNull(ordinal) ? null : record.GetValue(ordinal));
}
#endregion
}
}