2004-03-09 Umadevi S (sumadevi@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 using System.Collections;
11 using System.ComponentModel;
12 using System.Data;
13
14 namespace System.Data.Common {
15         public class DbDataRecord : IDataRecord, ICustomTypeDescriptor
16         {
17                 #region Fields
18
19                 SchemaInfo[] schema;
20                 object[] values;
21                 int fieldCount;
22                 FieldNameLookup lookup;
23
24                 #endregion
25                 
26                 #region Constructors
27
28 #if NET_1_2
29                 [MonoTODO]
30                 public DbDataRecord (object[] values, PropertyDescriptorCollection descriptors, FieldNameLookup fieldNameLookup)
31                 {
32                 }
33
34                 [MonoTODO]
35                 public DbDataRecord (SchemaInfo[] schemaInfo, object[] values, PropertyDescriptorCollection descriptors, FieldNameLookup fieldNameLookup)
36                 {
37                 }
38 #endif
39
40                 internal DbDataRecord (SchemaInfo[] schema, object[] values, FieldNameLookup lookup)
41                 {
42                         this.schema = schema;
43                         this.lookup = lookup;
44                         this.values = values;
45                         this.fieldCount = values.Length;
46                 }
47
48                 #endregion
49
50                 #region Properties
51
52                 public int FieldCount {
53                         get { return fieldCount; }
54                 }
55
56                 public object this [string name] {
57                         get { return this [GetOrdinal (name)]; }
58                 }
59
60                 [System.Runtime.CompilerServices.IndexerName("Item")]
61                 public object this [int index] {
62                         get { return GetValue (index); }
63                 }       
64
65                 #endregion
66
67                 #region Methods
68
69                 public bool GetBoolean (int i)
70                 {
71                         return (bool) GetValue (i);
72                 }
73
74                 public byte GetByte (int i)
75                 {
76                         return (byte) GetValue (i);
77                 }
78
79                 public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
80                 {
81                          object value = GetValue (i);
82                          if (!(value is byte []))
83                                 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
84                                                                                                    
85                         if ( buffer == null ) {
86                                 // Return length of data
87                                 return ((byte []) value).Length;
88                         }
89                         else {
90                                 // Copy data into buffer
91                                 Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
92                                 return ((byte []) value).Length - dataIndex;
93                         }
94
95                 }
96
97                 public char GetChar (int i)
98                 {
99                         return (char) GetValue (i);
100                 }
101
102                 public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
103                 {
104                         object value = GetValue (i);
105                         char [] valueBuffer;
106                                                                                                     
107                         if (value is char[])
108                                 valueBuffer = (char[])value;
109                         else if (value is string)
110                                 valueBuffer = ((string)value).ToCharArray();
111                         else
112                                 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
113                                                                                                                              if ( buffer == null ) {
114                                 // Return length of data
115                                 return valueBuffer.Length;
116                         }
117                         else {
118                                 // Copy data into buffer
119                                 Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
120                                 return valueBuffer.Length - dataIndex;
121                         }
122
123                 }
124
125                 public IDataReader GetData (int i)
126                 {
127                         return (IDataReader) GetValue (i);
128                 }
129
130                 public string GetDataTypeName (int i)
131                 {
132                         return schema[i].DataTypeName;
133                 }
134
135                 public DateTime GetDateTime (int i)
136                 {
137                         return (DateTime) GetValue (i); 
138                 }
139
140                 public decimal GetDecimal (int i)
141                 {
142                         return (decimal) GetValue (i);
143                 }
144
145                 public double GetDouble (int i)
146                 {
147                         return (double) GetValue (i);
148                 }
149
150                 public Type GetFieldType (int i)
151                 {
152                         return schema[i].FieldType;
153                 }
154
155                 public float GetFloat (int i)
156                 {
157                         return (float) GetValue (i);
158                 }
159                 
160                 public Guid GetGuid (int i)
161                 {
162                         return (Guid) GetValue (i);
163                 }
164                 
165                 public short GetInt16 (int i)
166                 {
167                         return (short) GetValue (i); 
168                 }
169         
170                 public int GetInt32 (int i)
171                 {
172                         return (int) GetValue (i); 
173                 }
174
175                 public long GetInt64 (int i)
176                 {
177                         return (long) GetValue (i); 
178                 }
179
180                 public string GetName (int i)
181                 {
182                         return (string) lookup [i];
183                 }
184
185 #if NET_1_2
186                 [MonoTODO]
187                 public virtual object GetObjectRef (int i)
188                 {
189                         throw new NotImplementedException ();
190                 }
191 #endif
192
193                 public int GetOrdinal (string name)
194                 {
195                         return lookup.IndexOf (name);
196                 }
197
198                 public string GetString (int i)
199                 {
200                         return (string) GetValue (i);
201                 }
202
203                 public object GetValue (int i)
204                 {
205                        if ((i < 0) || (i > fieldCount))
206                                 throw new IndexOutOfRangeException();
207
208                         object value = values [i];
209                         if (value == null)
210                                 value = DBNull.Value;
211                         return value;
212                 }
213
214                 public int GetValues (object[] values)
215                 {
216                         if(values == null)
217                                 throw new ArgumentNullException("values");
218                         
219                         int count = values.Length > this.values.Length ? this.values.Length : values.Length;
220                         for(int i = 0; i < count; i++)
221                                 values[i] = this.values[i];
222
223                         return count;
224                 }
225
226                 [MonoTODO]
227                 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
228                 {
229                         return new AttributeCollection(null);
230                 }
231
232                 [MonoTODO]
233                 string ICustomTypeDescriptor.GetClassName ()
234                 {
235                         return "";
236                 }
237
238                 [MonoTODO]
239                 string ICustomTypeDescriptor.GetComponentName ()
240                 {
241                         return null;
242                 }
243
244                 [MonoTODO]
245                 TypeConverter ICustomTypeDescriptor.GetConverter ()
246                 {
247                         return null;
248                 }       
249
250                 [MonoTODO]
251                 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
252                 {
253                         return null;
254                 }       
255
256                 [MonoTODO]
257                 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
258                 {
259                         return null;
260                 }       
261
262                 [MonoTODO]
263                 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
264                 {
265                         return null;
266                 }       
267
268                 [MonoTODO]
269                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
270                 {
271                         return new EventDescriptorCollection(null);
272                 }       
273
274                 [MonoTODO]
275                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
276                 {
277                         return new EventDescriptorCollection(null);
278                 }       
279
280                 [MonoTODO]
281                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
282                 {
283                         DataColumnPropertyDescriptor[] descriptors = 
284                                 new DataColumnPropertyDescriptor[FieldCount];
285
286                         DataColumnPropertyDescriptor descriptor;
287                         DataColumn dataColumn;
288                         for(int col = 0; col < FieldCount; col++) {
289                                 descriptor = new DataColumnPropertyDescriptor(
290                                         GetName(col), col, null);
291                                 descriptor.SetComponentType(typeof(DbDataRecord));
292                                 descriptor.SetPropertyType(GetFieldType(col));
293                                 
294                                 descriptors[col] = descriptor;
295                         }
296
297                         return new PropertyDescriptorCollection (descriptors);
298                 }       
299
300                 [MonoTODO]
301                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute[] attributes)
302                 {
303                         PropertyDescriptorCollection descriptors;
304                         descriptors = ((ICustomTypeDescriptor) this).GetProperties ();
305                         // TODO: filter out descriptors which do not contain
306                         //       any of those attributes
307                         //       except, those descriptors 
308                         //       that contain DefaultMemeberAttribute
309                         return descriptors;
310                 }       
311
312                 [MonoTODO]
313                 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
314                 {
315                         return this;
316                 }       
317
318                 public bool IsDBNull (int i)
319                 {
320                         return GetValue (i) == null;
321                 }
322 #if NET_1_2
323                 public virtual bool IsSetAsDefault (int i)
324                 {
325                         throw new NotImplementedException ();
326                 }
327
328                 public void SetSchemaInfo (SchemaInfo[] schemaInfo)
329                 {
330                         throw new NotImplementedException ();
331                 }
332 #endif
333
334                 #endregion // Methods
335         }
336 }