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