2 // System.Data.Common.DbDataRecord.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2002-2003
10 using System.Collections;
11 using System.ComponentModel;
14 namespace System.Data.Common {
15 public class DbDataRecord : IDataRecord, ICustomTypeDescriptor
22 FieldNameLookup lookup;
30 public DbDataRecord (object[] values, PropertyDescriptorCollection descriptors, FieldNameLookup fieldNameLookup)
35 public DbDataRecord (SchemaInfo[] schemaInfo, object[] values, PropertyDescriptorCollection descriptors, FieldNameLookup fieldNameLookup)
40 internal DbDataRecord (SchemaInfo[] schema, object[] values, FieldNameLookup lookup)
45 this.fieldCount = values.Length;
52 public int FieldCount {
53 get { return fieldCount; }
56 public object this [string name] {
57 get { return this [GetOrdinal (name)]; }
60 [System.Runtime.CompilerServices.IndexerName("Item")]
61 public object this [int index] {
62 get { return GetValue (index); }
69 public bool GetBoolean (int i)
71 return (bool) GetValue (i);
74 public byte GetByte (int i)
76 return (byte) GetValue (i);
79 public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
81 object value = GetValue (i);
82 if (!(value is byte []))
83 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
85 if ( buffer == null ) {
86 // Return length of data
87 return ((byte []) value).Length;
90 // Copy data into buffer
91 Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
92 return ((byte []) value).Length - dataIndex;
97 public char GetChar (int i)
99 return (char) GetValue (i);
102 public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
104 object value = GetValue (i);
108 valueBuffer = (char[])value;
109 else if (value is string)
110 valueBuffer = ((string)value).ToCharArray();
112 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
113 if ( buffer == null ) {
114 // Return length of data
115 return valueBuffer.Length;
118 // Copy data into buffer
119 Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
120 return valueBuffer.Length - dataIndex;
125 public IDataReader GetData (int i)
127 return (IDataReader) GetValue (i);
130 public string GetDataTypeName (int i)
132 return schema[i].DataTypeName;
135 public DateTime GetDateTime (int i)
137 return (DateTime) GetValue (i);
140 public decimal GetDecimal (int i)
142 return (decimal) GetValue (i);
145 public double GetDouble (int i)
147 return (double) GetValue (i);
150 public Type GetFieldType (int i)
152 return schema[i].FieldType;
155 public float GetFloat (int i)
157 return (float) GetValue (i);
160 public Guid GetGuid (int i)
162 return (Guid) GetValue (i);
165 public short GetInt16 (int i)
167 return (short) GetValue (i);
170 public int GetInt32 (int i)
172 return (int) GetValue (i);
175 public long GetInt64 (int i)
177 return (long) GetValue (i);
180 public string GetName (int i)
182 return (string) lookup [i];
187 public virtual object GetObjectRef (int i)
189 throw new NotImplementedException ();
193 public int GetOrdinal (string name)
195 return lookup.IndexOf (name);
198 public string GetString (int i)
200 return (string) GetValue (i);
203 public object GetValue (int i)
205 if ((i < 0) || (i > fieldCount))
206 throw new IndexOutOfRangeException();
208 object value = values [i];
210 value = DBNull.Value;
214 public int GetValues (object[] values)
217 throw new ArgumentNullException("values");
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];
227 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
229 return new AttributeCollection(null);
233 string ICustomTypeDescriptor.GetClassName ()
239 string ICustomTypeDescriptor.GetComponentName ()
245 TypeConverter ICustomTypeDescriptor.GetConverter ()
251 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
257 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
263 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
269 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
271 return new EventDescriptorCollection(null);
275 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
277 return new EventDescriptorCollection(null);
281 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
283 DataColumnPropertyDescriptor[] descriptors =
284 new DataColumnPropertyDescriptor[FieldCount];
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));
294 descriptors[col] = descriptor;
297 return new PropertyDescriptorCollection (descriptors);
301 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute[] attributes)
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
313 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
318 public bool IsDBNull (int i)
320 return GetValue (i) == null;
323 public virtual bool IsSetAsDefault (int i)
325 throw new NotImplementedException ();
328 public void SetSchemaInfo (SchemaInfo[] schemaInfo)
330 throw new NotImplementedException ();
334 #endregion // Methods