2007-01-08 Nagappan A <anagappan@novell.com>
[mono.git] / mcs / class / System.Data / System.Data.Common / DbDataRecord.cs
1 //
2 // System.Data.Common.DbDataRecord.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002-2003
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Data;
36
37 namespace System.Data.Common {
38         public class DbDataRecord : IDataRecord, ICustomTypeDescriptor
39         {
40                 #region Fields
41
42                 SchemaInfo[] schema;
43                 object[] values;
44                 int fieldCount;
45                 FieldNameLookup lookup;
46
47                 #endregion
48                 
49                 #region Constructors
50
51                 internal DbDataRecord (SchemaInfo[] schema, object[] values, FieldNameLookup lookup)
52                 {
53                         this.schema = schema;
54                         this.lookup = lookup;
55                         this.values = values;
56                         this.fieldCount = values.Length;
57                 }
58
59                 #endregion
60
61                 #region Properties
62
63                 public int FieldCount {
64                         get { return fieldCount; }
65                 }
66
67                 public object this [string name] {
68                         get { return this [GetOrdinal (name)]; }
69                 }
70
71                 [System.Runtime.CompilerServices.IndexerName("Item")]
72                 public object this [int index] {
73                         get { return GetValue (index); }
74                 }       
75
76                 #endregion
77
78                 #region Methods
79
80                 public bool GetBoolean (int i)
81                 {
82                         return (bool) GetValue (i);
83                 }
84
85                 public byte GetByte (int i)
86                 {
87                         return (byte) GetValue (i);
88                 }
89
90                 public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
91                 {
92                          object value = GetValue (i);
93                          if (!(value is byte []))
94                                 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
95                                                                                                    
96                         if ( buffer == null ) {
97                                 // Return length of data
98                                 return ((byte []) value).Length;
99                         }
100                         else {
101                                 // Copy data into buffer
102                                 Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
103                                 return ((byte []) value).Length - dataIndex;
104                         }
105
106                 }
107
108                 public char GetChar (int i)
109                 {
110                         return (char) GetValue (i);
111                 }
112
113                 public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
114                 {
115                         object value = GetValue (i);
116                         char [] valueBuffer;
117                                                                                                     
118                         if (value is char[])
119                                 valueBuffer = (char[])value;
120                         else if (value is string)
121                                 valueBuffer = ((string)value).ToCharArray();
122                         else
123                                 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
124                                                                                                                              if ( buffer == null ) {
125                                 // Return length of data
126                                 return valueBuffer.Length;
127                         }
128                         else {
129                                 // Copy data into buffer
130                                 Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
131                                 return valueBuffer.Length - dataIndex;
132                         }
133
134                 }
135
136                 public IDataReader GetData (int i)
137                 {
138                         return (IDataReader) GetValue (i);
139                 }
140
141                 public string GetDataTypeName (int i)
142                 {
143                         return schema[i].DataTypeName;
144                 }
145
146                 public DateTime GetDateTime (int i)
147                 {
148                         return (DateTime) GetValue (i); 
149                 }
150
151                 public decimal GetDecimal (int i)
152                 {
153                         return (decimal) GetValue (i);
154                 }
155
156                 public double GetDouble (int i)
157                 {
158                         return (double) GetValue (i);
159                 }
160
161                 public Type GetFieldType (int i)
162                 {
163                         return schema[i].FieldType;
164                 }
165
166                 public float GetFloat (int i)
167                 {
168                         return (float) GetValue (i);
169                 }
170                 
171                 public Guid GetGuid (int i)
172                 {
173                         return (Guid) GetValue (i);
174                 }
175                 
176                 public short GetInt16 (int i)
177                 {
178                         return (short) GetValue (i); 
179                 }
180         
181                 public int GetInt32 (int i)
182                 {
183                         return (int) GetValue (i); 
184                 }
185
186                 public long GetInt64 (int i)
187                 {
188                         return (long) GetValue (i); 
189                 }
190
191                 public string GetName (int i)
192                 {
193                         return (string) lookup [i];
194                 }
195
196                 public int GetOrdinal (string name)
197                 {
198                         return lookup.IndexOf (name);
199                 }
200
201                 public string GetString (int i)
202                 {
203                         return (string) GetValue (i);
204                 }
205
206                 public object GetValue (int i)
207                 {
208                        if ((i < 0) || (i > fieldCount))
209                                 throw new IndexOutOfRangeException();
210
211                         object value = values [i];
212                         if (value == null)
213                                 value = DBNull.Value;
214                         return value;
215                 }
216
217                 public int GetValues (object[] values)
218                 {
219                         if(values == null)
220                                 throw new ArgumentNullException("values");
221                         
222                         int count = values.Length > this.values.Length ? this.values.Length : values.Length;
223                         for(int i = 0; i < count; i++)
224                                 values[i] = this.values[i];
225
226                         return count;
227                 }
228
229                 [MonoTODO]
230                 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
231                 {
232                         return new AttributeCollection (null);
233                 }
234
235                 [MonoTODO]
236                 string ICustomTypeDescriptor.GetClassName ()
237                 {
238                         return "";
239                 }
240
241                 [MonoTODO]
242                 string ICustomTypeDescriptor.GetComponentName ()
243                 {
244                         return null;
245                 }
246
247                 [MonoTODO]
248                 TypeConverter ICustomTypeDescriptor.GetConverter ()
249                 {
250                         return null;
251                 }       
252
253                 [MonoTODO]
254                 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
255                 {
256                         return null;
257                 }       
258
259                 [MonoTODO]
260                 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
261                 {
262                         return null;
263                 }       
264
265                 [MonoTODO]
266                 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
267                 {
268                         return null;
269                 }       
270
271                 [MonoTODO]
272                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
273                 {
274                         return new EventDescriptorCollection(null);
275                 }       
276
277                 [MonoTODO]
278                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
279                 {
280                         return new EventDescriptorCollection(null);
281                 }       
282
283                 [MonoTODO]
284                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
285                 {
286                         DataColumnPropertyDescriptor[] descriptors = 
287                                 new DataColumnPropertyDescriptor[FieldCount];
288
289                         DataColumnPropertyDescriptor descriptor;
290                         for(int col = 0; col < FieldCount; col++) {
291                                 descriptor = new DataColumnPropertyDescriptor(
292                                         GetName(col), col, null);
293                                 descriptor.SetComponentType(typeof(DbDataRecord));
294                                 descriptor.SetPropertyType(GetFieldType(col));
295                                 
296                                 descriptors[col] = descriptor;
297                         }
298
299                         return new PropertyDescriptorCollection (descriptors);
300                 }       
301
302                 [MonoTODO]
303                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute[] attributes)
304                 {
305                         PropertyDescriptorCollection descriptors;
306                         descriptors = ((ICustomTypeDescriptor) this).GetProperties ();
307                         // TODO: filter out descriptors which do not contain
308                         //       any of those attributes
309                         //       except, those descriptors 
310                         //       that contain DefaultMemeberAttribute
311                         return descriptors;
312                 }       
313
314                 [MonoTODO]
315                 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
316                 {
317                         return this;
318                 }       
319
320                 public bool IsDBNull (int i)
321                 {
322                         return GetValue (i) == DBNull.Value;
323                 }
324
325                 #endregion // Methods
326         }
327 }