**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Data / System.Data.Common / DbDataRecord.cs
blob69a2013f64d69937f29fd4e327334ff983c2f3ef
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 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Data;
37 namespace System.Data.Common {
38 public class DbDataRecord : IDataRecord, ICustomTypeDescriptor
40 #region Fields
42 SchemaInfo[] schema;
43 object[] values;
44 int fieldCount;
45 FieldNameLookup lookup;
47 #endregion
49 #region Constructors
51 #if NET_2_0
52 [MonoTODO]
53 public DbDataRecord (object[] values, PropertyDescriptorCollection descriptors, FieldNameLookup fieldNameLookup)
57 [MonoTODO]
58 public DbDataRecord (SchemaInfo[] schemaInfo, object[] values, PropertyDescriptorCollection descriptors, FieldNameLookup fieldNameLookup)
61 #endif
63 internal DbDataRecord (SchemaInfo[] schema, object[] values, FieldNameLookup lookup)
65 this.schema = schema;
66 this.lookup = lookup;
67 this.values = values;
68 this.fieldCount = values.Length;
71 #endregion
73 #region Properties
75 public int FieldCount {
76 get { return fieldCount; }
79 public object this [string name] {
80 get { return this [GetOrdinal (name)]; }
83 [System.Runtime.CompilerServices.IndexerName("Item")]
84 public object this [int index] {
85 get { return GetValue (index); }
88 #endregion
90 #region Methods
92 public bool GetBoolean (int i)
94 return (bool) GetValue (i);
97 public byte GetByte (int i)
99 return (byte) GetValue (i);
102 public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
104 object value = GetValue (i);
105 if (!(value is byte []))
106 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
108 if ( buffer == null ) {
109 // Return length of data
110 return ((byte []) value).Length;
112 else {
113 // Copy data into buffer
114 Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
115 return ((byte []) value).Length - dataIndex;
120 public char GetChar (int i)
122 return (char) GetValue (i);
125 public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
127 object value = GetValue (i);
128 char [] valueBuffer;
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;
140 else {
141 // Copy data into buffer
142 Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
143 return valueBuffer.Length - dataIndex;
148 public IDataReader GetData (int i)
150 return (IDataReader) GetValue (i);
153 public string GetDataTypeName (int i)
155 return schema[i].DataTypeName;
158 public DateTime GetDateTime (int i)
160 return (DateTime) GetValue (i);
163 public decimal GetDecimal (int i)
165 return (decimal) GetValue (i);
168 public double GetDouble (int i)
170 return (double) GetValue (i);
173 public Type GetFieldType (int i)
175 return schema[i].FieldType;
178 public float GetFloat (int i)
180 return (float) GetValue (i);
183 public Guid GetGuid (int i)
185 return (Guid) GetValue (i);
188 public short GetInt16 (int i)
190 return (short) GetValue (i);
193 public int GetInt32 (int i)
195 return (int) GetValue (i);
198 public long GetInt64 (int i)
200 return (long) GetValue (i);
203 public string GetName (int i)
205 return (string) lookup [i];
208 #if NET_2_0
209 [MonoTODO]
210 public virtual object GetObjectRef (int i)
212 throw new NotImplementedException ();
214 #endif
216 public int GetOrdinal (string name)
218 return lookup.IndexOf (name);
221 public string GetString (int i)
223 return (string) GetValue (i);
226 public object GetValue (int i)
228 if ((i < 0) || (i > fieldCount))
229 throw new IndexOutOfRangeException();
231 object value = values [i];
232 if (value == null)
233 value = DBNull.Value;
234 return value;
237 public int GetValues (object[] values)
239 if(values == null)
240 throw new ArgumentNullException("values");
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];
246 return count;
249 [MonoTODO]
250 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
252 return new AttributeCollection(null);
255 [MonoTODO]
256 string ICustomTypeDescriptor.GetClassName ()
258 return "";
261 [MonoTODO]
262 string ICustomTypeDescriptor.GetComponentName ()
264 return null;
267 [MonoTODO]
268 TypeConverter ICustomTypeDescriptor.GetConverter ()
270 return null;
273 [MonoTODO]
274 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
276 return null;
279 [MonoTODO]
280 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
282 return null;
285 [MonoTODO]
286 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
288 return null;
291 [MonoTODO]
292 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
294 return new EventDescriptorCollection(null);
297 [MonoTODO]
298 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
300 return new EventDescriptorCollection(null);
303 [MonoTODO]
304 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
306 DataColumnPropertyDescriptor[] descriptors =
307 new DataColumnPropertyDescriptor[FieldCount];
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));
317 descriptors[col] = descriptor;
320 return new PropertyDescriptorCollection (descriptors);
323 [MonoTODO]
324 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute[] attributes)
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;
335 [MonoTODO]
336 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
338 return this;
341 public bool IsDBNull (int i)
343 return GetValue (i) == DBNull.Value;
345 #if NET_2_0
346 public virtual bool IsSetAsDefault (int i)
348 throw new NotImplementedException ();
351 public void SetSchemaInfo (SchemaInfo[] schemaInfo)
353 throw new NotImplementedException ();
355 #endif
357 #endregion // Methods