5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
29 namespace DbLinq.Util
\r
31 #if MONO_STRICT || !DEBUG
\r
36 static class IDataRecordExtensions
\r
38 // please note that sometimes (depending on driver), GetValue() returns DBNull instead of null
\r
39 // so at this level, we handle both
\r
41 public static string GetAsString(this IDataRecord dataRecord, int index)
\r
43 if (dataRecord.IsDBNull(index))
\r
45 object o = dataRecord.GetValue(index);
\r
46 if (o == null) // this is not supposed to happen
\r
48 return o.ToString();
\r
51 public static bool GetAsBool(this IDataRecord dataRecord, int index)
\r
53 object b = dataRecord.GetValue(index);
\r
54 return TypeConvert.ToBoolean(b);
\r
57 public static bool? GetAsNullableBool(this IDataRecord dataRecord, int index)
\r
59 if (dataRecord.IsDBNull(index))
\r
61 return GetAsBool(dataRecord, index);
\r
64 public static char GetAsChar(this IDataRecord dataRecord, int index)
\r
66 object c = dataRecord.GetValue(index);
\r
67 return TypeConvert.ToChar(c);
\r
70 public static char? GetAsNullableChar(this IDataRecord dataRecord, int index)
\r
72 if (dataRecord.IsDBNull(index))
\r
74 return GetAsChar(dataRecord, index);
\r
77 public static U GetAsNumeric<U>(this IDataRecord dataRecord, int index)
\r
79 if (dataRecord.IsDBNull(index))
\r
81 return GetAsNumeric<U>(dataRecord.GetValue(index));
\r
84 public static U? GetAsNullableNumeric<U>(this IDataRecord dataRecord, int index)
\r
87 if (dataRecord.IsDBNull(index))
\r
89 return GetAsNumeric<U>(dataRecord.GetValue(index));
\r
92 private static U GetAsNumeric<U>(object o)
\r
94 if (o == null || o is DBNull)
\r
96 return TypeConvert.ToNumber<U>(o);
\r
99 public static int GetAsEnum(this IDataRecord dataRecord, Type enumType, int index)
\r
101 int enumAsInt = dataRecord.GetAsNumeric<int>(index);
\r
105 public static byte[] GetAsBytes(this IDataRecord dataRecord, int index)
\r
107 if (dataRecord.IsDBNull(index))
\r
109 object obj = dataRecord.GetValue(index);
\r
111 return null; //nullable blob?
\r
112 byte[] bytes = obj as byte[];
\r
114 return bytes; //works for BLOB field
\r
115 Console.WriteLine("GetBytes: received unexpected type:" + obj);
\r
116 //return _rdr.GetInt32(index);
\r
117 return new byte[0];
\r
120 public static object GetAsObject(this IDataRecord dataRecord, int index)
\r
122 if (dataRecord.IsDBNull(index))
\r
124 object obj = dataRecord.GetValue(index);
\r
128 public static DateTime GetAsDateTime(this IDataRecord dataRecord, int index)
\r
130 return dataRecord.GetDateTime(index);
\r
133 public static DateTime? GetAsNullableDateTime(this IDataRecord dataRecord, int index)
\r
135 if (dataRecord.IsDBNull(index))
\r
137 return GetAsDateTime(dataRecord, index);
\r
140 public static Guid GetAsGuid(this IDataRecord dataRecord, int index)
\r
142 return dataRecord.GetGuid(index);
\r
144 public static Guid? GetAsNullableGuid(this IDataRecord dataRecord, int index)
\r
146 if (dataRecord.IsDBNull(index))
\r
148 return GetAsGuid(dataRecord, index);
\r