* SqlMetaData.cs: Implemented an overloaded constructor, missing
[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                 readonly SchemaInfo [] schema;
43                 readonly object [] values;
44                 readonly int fieldCount;
45
46                 #endregion
47                 
48                 #region Constructors
49
50                 internal DbDataRecord (SchemaInfo[] schema, object[] values)
51                 {
52                         this.schema = schema;
53                         this.values = values;
54                         this.fieldCount = values.Length;
55                 }
56
57                 #endregion
58
59                 #region Properties
60
61                 public int FieldCount {
62                         get { return fieldCount; }
63                 }
64
65                 public object this [string name] {
66                         get { return this [GetOrdinal (name)]; }
67                 }
68
69                 [System.Runtime.CompilerServices.IndexerName("Item")]
70                 public object this [int index] {
71                         get { return GetValue (index); }
72                 }       
73
74                 #endregion
75
76                 #region Methods
77
78                 public bool GetBoolean (int i)
79                 {
80                         return (bool) GetValue (i);
81                 }
82
83                 public byte GetByte (int i)
84                 {
85                         return (byte) GetValue (i);
86                 }
87
88                 public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
89                 {
90                          object value = GetValue (i);
91                          if (!(value is byte []))
92                                 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
93                                                                                                    
94                         if ( buffer == null ) {
95                                 // Return length of data
96                                 return ((byte []) value).Length;
97                         }
98                         else {
99                                 // Copy data into buffer
100                                 Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
101                                 return ((byte []) value).Length - dataIndex;
102                         }
103
104                 }
105
106                 public char GetChar (int i)
107                 {
108                         return (char) GetValue (i);
109                 }
110
111                 public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
112                 {
113                         object value = GetValue (i);
114                         char [] valueBuffer;
115                                                                                                     
116                         if (value is char[])
117                                 valueBuffer = (char[])value;
118                         else if (value is string)
119                                 valueBuffer = ((string)value).ToCharArray();
120                         else
121                                 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
122                                                                                                                              if ( buffer == null ) {
123                                 // Return length of data
124                                 return valueBuffer.Length;
125                         }
126                         else {
127                                 // Copy data into buffer
128                                 Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
129                                 return valueBuffer.Length - dataIndex;
130                         }
131
132                 }
133
134                 public IDataReader GetData (int i)
135                 {
136                         return (IDataReader) GetValue (i);
137                 }
138
139                 public string GetDataTypeName (int i)
140                 {
141                         return schema[i].DataTypeName;
142                 }
143
144                 public DateTime GetDateTime (int i)
145                 {
146                         return (DateTime) GetValue (i); 
147                 }
148
149                 public decimal GetDecimal (int i)
150                 {
151                         return (decimal) GetValue (i);
152                 }
153
154                 public double GetDouble (int i)
155                 {
156                         return (double) GetValue (i);
157                 }
158
159                 public Type GetFieldType (int i)
160                 {
161                         return schema[i].FieldType;
162                 }
163
164                 public float GetFloat (int i)
165                 {
166                         return (float) GetValue (i);
167                 }
168                 
169                 public Guid GetGuid (int i)
170                 {
171                         return (Guid) GetValue (i);
172                 }
173                 
174                 public short GetInt16 (int i)
175                 {
176                         return (short) GetValue (i); 
177                 }
178         
179                 public int GetInt32 (int i)
180                 {
181                         return (int) GetValue (i); 
182                 }
183
184                 public long GetInt64 (int i)
185                 {
186                         return (long) GetValue (i); 
187                 }
188
189                 public string GetName (int i)
190                 {
191                         return schema [i].ColumnName;
192                 }
193
194                 public int GetOrdinal (string name)
195                 {
196                         for (int i = 0; i < FieldCount; i++)
197                                 if (schema [i].ColumnName == name)
198                                         return i;
199                         return -1;
200                 }
201
202                 public string GetString (int i)
203                 {
204                         return (string) GetValue (i);
205                 }
206
207                 public object GetValue (int i)
208                 {
209                        if ((i < 0) || (i > fieldCount))
210                                 throw new IndexOutOfRangeException();
211
212                         return values [i];
213                 }
214
215                 public int GetValues (object[] values)
216                 {
217                         if(values == null)
218                                 throw new ArgumentNullException("values");
219                         
220                         int count = values.Length > this.values.Length ? this.values.Length : values.Length;
221                         for(int i = 0; i < count; i++)
222                                 values[i] = this.values[i];
223
224                         return count;
225                 }
226
227                 [MonoTODO]
228                 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
229                 {
230                         return new AttributeCollection (null);
231                 }
232
233                 [MonoTODO]
234                 string ICustomTypeDescriptor.GetClassName ()
235                 {
236                         return "";
237                 }
238
239                 [MonoTODO]
240                 string ICustomTypeDescriptor.GetComponentName ()
241                 {
242                         return null;
243                 }
244
245                 [MonoTODO]
246                 TypeConverter ICustomTypeDescriptor.GetConverter ()
247                 {
248                         return null;
249                 }       
250
251                 [MonoTODO]
252                 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
253                 {
254                         return null;
255                 }       
256
257                 [MonoTODO]
258                 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
259                 {
260                         return null;
261                 }       
262
263                 [MonoTODO]
264                 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
265                 {
266                         return null;
267                 }       
268
269                 [MonoTODO]
270                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
271                 {
272                         return new EventDescriptorCollection(null);
273                 }       
274
275                 [MonoTODO]
276                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
277                 {
278                         return new EventDescriptorCollection(null);
279                 }       
280
281                 [MonoTODO]
282                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
283                 {
284                         DataColumnPropertyDescriptor[] descriptors = 
285                                 new DataColumnPropertyDescriptor[FieldCount];
286
287                         DataColumnPropertyDescriptor descriptor;
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) == DBNull.Value;
321                 }
322
323                 #endregion // Methods
324         }
325 }