2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Util / IDataRecordExtensions.cs
1 #region MIT license\r
2 // \r
3 // MIT license\r
4 //\r
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne\r
6 // \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
13 // \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
16 // \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
23 // THE SOFTWARE.\r
24 // \r
25 #endregion\r
26 using System;\r
27 using System.Data;\r
28 \r
29 namespace DbLinq.Util\r
30 {\r
31 #if MONO_STRICT || !DEBUG\r
32     internal\r
33 #else\r
34     public\r
35 #endif\r
36     static class IDataRecordExtensions\r
37     {\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
40 \r
41         public static string GetAsString(this IDataRecord dataRecord, int index)\r
42         {\r
43             if (dataRecord.IsDBNull(index))\r
44                 return null;\r
45             object o = dataRecord.GetValue(index);\r
46             if (o == null) // this is not supposed to happen\r
47                 return null;\r
48             return o.ToString();\r
49         }\r
50 \r
51         public static bool GetAsBool(this IDataRecord dataRecord, int index)\r
52         {\r
53             object b = dataRecord.GetValue(index);\r
54             return TypeConvert.ToBoolean(b);\r
55         }\r
56 \r
57         public static bool? GetAsNullableBool(this IDataRecord dataRecord, int index)\r
58         {\r
59             if (dataRecord.IsDBNull(index))\r
60                 return null;\r
61             return GetAsBool(dataRecord, index);\r
62         }\r
63 \r
64         public static char GetAsChar(this IDataRecord dataRecord, int index)\r
65         {\r
66             object c = dataRecord.GetValue(index);\r
67             return TypeConvert.ToChar(c);\r
68         }\r
69 \r
70         public static char? GetAsNullableChar(this IDataRecord dataRecord, int index)\r
71         {\r
72             if (dataRecord.IsDBNull(index))\r
73                 return null;\r
74             return GetAsChar(dataRecord, index);\r
75         }\r
76 \r
77         public static U GetAsNumeric<U>(this IDataRecord dataRecord, int index)\r
78         {\r
79             if (dataRecord.IsDBNull(index))\r
80                 return default(U);\r
81             return GetAsNumeric<U>(dataRecord.GetValue(index));\r
82         }\r
83 \r
84         public static U? GetAsNullableNumeric<U>(this IDataRecord dataRecord, int index)\r
85             where U : struct\r
86         {\r
87             if (dataRecord.IsDBNull(index))\r
88                 return null;\r
89             return GetAsNumeric<U>(dataRecord.GetValue(index));\r
90         }\r
91 \r
92         private static U GetAsNumeric<U>(object o)\r
93         {\r
94             if (o == null || o is DBNull)\r
95                 return default(U);\r
96             return TypeConvert.ToNumber<U>(o);\r
97         }\r
98 \r
99         public static int GetAsEnum(this IDataRecord dataRecord, Type enumType, int index)\r
100         {\r
101             int enumAsInt = dataRecord.GetAsNumeric<int>(index);\r
102             return enumAsInt;\r
103         }\r
104 \r
105         public static byte[] GetAsBytes(this IDataRecord dataRecord, int index)\r
106         {\r
107             if (dataRecord.IsDBNull(index))\r
108                 return null;\r
109             object obj = dataRecord.GetValue(index);\r
110             if (obj == null)\r
111                 return null; //nullable blob?\r
112             byte[] bytes = obj as byte[];\r
113             if (bytes != null)\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
118         }\r
119 \r
120         public static object GetAsObject(this IDataRecord dataRecord, int index)\r
121         {\r
122             if (dataRecord.IsDBNull(index))\r
123                 return null;\r
124             object obj = dataRecord.GetValue(index);\r
125             return obj;\r
126         }\r
127 \r
128         public static DateTime GetAsDateTime(this IDataRecord dataRecord, int index)\r
129         {\r
130             return dataRecord.GetDateTime(index);\r
131         }\r
132 \r
133         public static DateTime? GetAsNullableDateTime(this IDataRecord dataRecord, int index)\r
134         {\r
135             if (dataRecord.IsDBNull(index))\r
136                 return null;\r
137             return GetAsDateTime(dataRecord, index);\r
138         }\r
139 \r
140         public static Guid GetAsGuid(this IDataRecord dataRecord, int index)\r
141         {\r
142             return dataRecord.GetGuid(index);\r
143         }\r
144         public static Guid? GetAsNullableGuid(this IDataRecord dataRecord, int index)\r
145         {\r
146             if (dataRecord.IsDBNull(index))\r
147                 return null;\r
148             return GetAsGuid(dataRecord, index);\r
149         }\r
150     }\r
151 }\r