2 // System.Data.Common.DbDataRecord.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2002-2003
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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;
37 namespace System.Data.Common {
38 public class DbDataRecord : IDataRecord, ICustomTypeDescriptor
45 FieldNameLookup lookup;
51 internal DbDataRecord (SchemaInfo[] schema, object[] values, FieldNameLookup lookup)
56 this.fieldCount = values.Length;
63 public int FieldCount {
64 get { return fieldCount; }
67 public object this [string name] {
68 get { return this [GetOrdinal (name)]; }
71 [System.Runtime.CompilerServices.IndexerName("Item")]
72 public object this [int index] {
73 get { return GetValue (index); }
80 public bool GetBoolean (int i)
82 return (bool) GetValue (i);
85 public byte GetByte (int i)
87 return (byte) GetValue (i);
90 public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
92 object value = GetValue (i);
93 if (!(value is byte []))
94 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
96 if ( buffer == null ) {
97 // Return length of data
98 return ((byte []) value).Length;
101 // Copy data into buffer
102 Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
103 return ((byte []) value).Length - dataIndex;
108 public char GetChar (int i)
110 return (char) GetValue (i);
113 public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
115 object value = GetValue (i);
119 valueBuffer = (char[])value;
120 else if (value is string)
121 valueBuffer = ((string)value).ToCharArray();
123 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
124 if ( buffer == null ) {
125 // Return length of data
126 return valueBuffer.Length;
129 // Copy data into buffer
130 Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
131 return valueBuffer.Length - dataIndex;
136 public IDataReader GetData (int i)
138 return (IDataReader) GetValue (i);
141 public string GetDataTypeName (int i)
143 return schema[i].DataTypeName;
146 public DateTime GetDateTime (int i)
148 return (DateTime) GetValue (i);
151 public decimal GetDecimal (int i)
153 return (decimal) GetValue (i);
156 public double GetDouble (int i)
158 return (double) GetValue (i);
161 public Type GetFieldType (int i)
163 return schema[i].FieldType;
166 public float GetFloat (int i)
168 return (float) GetValue (i);
171 public Guid GetGuid (int i)
173 return (Guid) GetValue (i);
176 public short GetInt16 (int i)
178 return (short) GetValue (i);
181 public int GetInt32 (int i)
183 return (int) GetValue (i);
186 public long GetInt64 (int i)
188 return (long) GetValue (i);
191 public string GetName (int i)
193 return (string) lookup [i];
196 public int GetOrdinal (string name)
198 return lookup.IndexOf (name);
201 public string GetString (int i)
203 return (string) GetValue (i);
206 public object GetValue (int i)
208 if ((i < 0) || (i > fieldCount))
209 throw new IndexOutOfRangeException();
211 object value = values [i];
213 value = DBNull.Value;
217 public int GetValues (object[] values)
220 throw new ArgumentNullException("values");
222 int count = values.Length > this.values.Length ? this.values.Length : values.Length;
223 for(int i = 0; i < count; i++)
224 values[i] = this.values[i];
230 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
232 return new AttributeCollection (null);
236 string ICustomTypeDescriptor.GetClassName ()
242 string ICustomTypeDescriptor.GetComponentName ()
248 TypeConverter ICustomTypeDescriptor.GetConverter ()
254 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
260 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
266 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
272 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
274 return new EventDescriptorCollection(null);
278 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
280 return new EventDescriptorCollection(null);
284 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
286 DataColumnPropertyDescriptor[] descriptors =
287 new DataColumnPropertyDescriptor[FieldCount];
289 DataColumnPropertyDescriptor descriptor;
290 for(int col = 0; col < FieldCount; col++) {
291 descriptor = new DataColumnPropertyDescriptor(
292 GetName(col), col, null);
293 descriptor.SetComponentType(typeof(DbDataRecord));
294 descriptor.SetPropertyType(GetFieldType(col));
296 descriptors[col] = descriptor;
299 return new PropertyDescriptorCollection (descriptors);
303 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute[] attributes)
305 PropertyDescriptorCollection descriptors;
306 descriptors = ((ICustomTypeDescriptor) this).GetProperties ();
307 // TODO: filter out descriptors which do not contain
308 // any of those attributes
309 // except, those descriptors
310 // that contain DefaultMemeberAttribute
315 object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
320 public bool IsDBNull (int i)
322 return GetValue (i) == DBNull.Value;
325 #endregion // Methods