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
;
53 public DbDataRecord (object[] values
, PropertyDescriptorCollection descriptors
, FieldNameLookup fieldNameLookup
)
58 public DbDataRecord (SchemaInfo
[] schemaInfo
, object[] values
, PropertyDescriptorCollection descriptors
, FieldNameLookup fieldNameLookup
)
63 internal DbDataRecord (SchemaInfo
[] schema
, object[] values
, FieldNameLookup lookup
)
68 this.fieldCount
= values
.Length
;
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); }
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
;
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
);
131 valueBuffer
= (char[])value;
132 else if (value is string)
133 valueBuffer
= ((string)value).ToCharArray();
135 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
136 if ( buffer
== null ) {
137 // Return length of data
138 return valueBuffer
.Length
;
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
];
210 public virtual object GetObjectRef (int i
)
212 throw new NotImplementedException ();
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
];
233 value = DBNull
.Value
;
237 public int GetValues (object[] values
)
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
];
250 AttributeCollection ICustomTypeDescriptor
.GetAttributes ()
252 return new AttributeCollection(null);
256 string ICustomTypeDescriptor
.GetClassName ()
262 string ICustomTypeDescriptor
.GetComponentName ()
268 TypeConverter ICustomTypeDescriptor
.GetConverter ()
274 EventDescriptor ICustomTypeDescriptor
.GetDefaultEvent ()
280 PropertyDescriptor ICustomTypeDescriptor
.GetDefaultProperty ()
286 object ICustomTypeDescriptor
.GetEditor (Type editorBaseType
)
292 EventDescriptorCollection ICustomTypeDescriptor
.GetEvents ()
294 return new EventDescriptorCollection(null);
298 EventDescriptorCollection ICustomTypeDescriptor
.GetEvents (Attribute
[] attributes
)
300 return new EventDescriptorCollection(null);
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
);
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
336 object ICustomTypeDescriptor
.GetPropertyOwner (PropertyDescriptor pd
)
341 public bool IsDBNull (int i
)
343 return GetValue (i
) == DBNull
.Value
;
346 public virtual bool IsSetAsDefault (int i
)
348 throw new NotImplementedException ();
351 public void SetSchemaInfo (SchemaInfo
[] schemaInfo
)
353 throw new NotImplementedException ();
357 #endregion // Methods