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
42 readonly SchemaInfo [] schema;
43 readonly object [] values;
44 readonly int fieldCount;
50 internal DbDataRecord (SchemaInfo[] schema, object[] values)
54 this.fieldCount = values.Length;
61 public int FieldCount {
62 get { return fieldCount; }
65 public object this [string name] {
66 get { return this [GetOrdinal (name)]; }
69 [System.Runtime.CompilerServices.IndexerName("Item")]
70 public object this [int index] {
71 get { return GetValue (index); }
78 public bool GetBoolean (int i)
80 return (bool) GetValue (i);
83 public byte GetByte (int i)
85 return (byte) GetValue (i);
88 public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
90 object value = GetValue (i);
91 if (!(value is byte []))
92 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
94 if ( buffer == null ) {
95 // Return length of data
96 return ((byte []) value).Length;
99 // Copy data into buffer
100 Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
101 return ((byte []) value).Length - dataIndex;
106 public char GetChar (int i)
108 return (char) GetValue (i);
111 public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
113 object value = GetValue (i);
117 valueBuffer = (char[])value;
118 else if (value is string)
119 valueBuffer = ((string)value).ToCharArray();
121 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
122 if ( buffer == null ) {
123 // Return length of data
124 return valueBuffer.Length;
127 // Copy data into buffer
128 Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
129 return valueBuffer.Length - dataIndex;
134 public IDataReader GetData (int i)
136 return (IDataReader) GetValue (i);
139 public string GetDataTypeName (int i)
141 return schema[i].DataTypeName;
144 public DateTime GetDateTime (int i)
146 return (DateTime) GetValue (i);
149 public decimal GetDecimal (int i)
151 return (decimal) GetValue (i);
154 public double GetDouble (int i)
156 return (double) GetValue (i);
159 public Type GetFieldType (int i)
161 return schema[i].FieldType;
164 public float GetFloat (int i)
166 return (float) GetValue (i);
169 public Guid GetGuid (int i)
171 return (Guid) GetValue (i);
174 public short GetInt16 (int i)
176 return (short) GetValue (i);
179 public int GetInt32 (int i)
181 return (int) GetValue (i);
184 public long GetInt64 (int i)
186 return (long) GetValue (i);
189 public string GetName (int i)
191 return schema [i].ColumnName;
194 public int GetOrdinal (string name)
196 for (int i = 0; i < FieldCount; i++)
197 if (schema [i].ColumnName == name)
202 public string GetString (int i)
204 return (string) GetValue (i);
207 public object GetValue (int i)
209 if ((i < 0) || (i > fieldCount))
210 throw new IndexOutOfRangeException();
215 public int GetValues (object[] values)
218 throw new ArgumentNullException("values");
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];
228 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
230 return new AttributeCollection (null);
234 string ICustomTypeDescriptor.GetClassName ()
240 string ICustomTypeDescriptor.GetComponentName ()
246 TypeConverter ICustomTypeDescriptor.GetConverter ()
252 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
258 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
264 object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
270 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
272 return new EventDescriptorCollection(null);
276 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
278 return new EventDescriptorCollection(null);
282 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
284 DataColumnPropertyDescriptor[] descriptors =
285 new DataColumnPropertyDescriptor[FieldCount];
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));
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) == DBNull.Value;
323 #endregion // Methods