DbDataRecord.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // System.Data.Common.DbDataRecord.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002-2003
  8. //
  9. using System.Collections;
  10. using System.ComponentModel;
  11. using System.Data;
  12. namespace System.Data.Common {
  13. public class DbDataRecord : IDataRecord, ICustomTypeDescriptor
  14. {
  15. #region Fields
  16. SchemaInfo[] schema;
  17. object[] values;
  18. int fieldCount;
  19. FieldNameLookup lookup;
  20. #endregion
  21. #region Constructors
  22. #if NET_1_2
  23. [MonoTODO]
  24. public DbDataRecord (object[] values, PropertyDescriptorCollection descriptors, FieldNameLookup fieldNameLookup)
  25. {
  26. }
  27. [MonoTODO]
  28. public DbDataRecord (SchemaInfo[] schemaInfo, object[] values, PropertyDescriptorCollection descriptors, FieldNameLookup fieldNameLookup)
  29. {
  30. }
  31. #endif
  32. internal DbDataRecord (SchemaInfo[] schema, object[] values, FieldNameLookup lookup)
  33. {
  34. this.schema = schema;
  35. this.lookup = lookup;
  36. this.values = values;
  37. this.fieldCount = values.Length;
  38. }
  39. #endregion
  40. #region Properties
  41. public int FieldCount {
  42. get { return fieldCount; }
  43. }
  44. public object this [string name] {
  45. get { return this [GetOrdinal (name)]; }
  46. }
  47. [System.Runtime.CompilerServices.IndexerName("Item")]
  48. public object this [int index] {
  49. get { return GetValue (index); }
  50. }
  51. #endregion
  52. #region Methods
  53. public bool GetBoolean (int i)
  54. {
  55. return (bool) GetValue (i);
  56. }
  57. public byte GetByte (int i)
  58. {
  59. return (byte) GetValue (i);
  60. }
  61. public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
  62. {
  63. object value = GetValue (i);
  64. if (!(value is byte []))
  65. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  66. if ( buffer == null ) {
  67. // Return length of data
  68. return ((byte []) value).Length;
  69. }
  70. else {
  71. // Copy data into buffer
  72. Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
  73. return ((byte []) value).Length - dataIndex;
  74. }
  75. }
  76. public char GetChar (int i)
  77. {
  78. return (char) GetValue (i);
  79. }
  80. public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
  81. {
  82. object value = GetValue (i);
  83. char [] valueBuffer;
  84. if (value is char[])
  85. valueBuffer = (char[])value;
  86. else if (value is string)
  87. valueBuffer = ((string)value).ToCharArray();
  88. else
  89. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  90. if ( buffer == null ) {
  91. // Return length of data
  92. return valueBuffer.Length;
  93. }
  94. else {
  95. // Copy data into buffer
  96. Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
  97. return valueBuffer.Length - dataIndex;
  98. }
  99. }
  100. public IDataReader GetData (int i)
  101. {
  102. return (IDataReader) GetValue (i);
  103. }
  104. public string GetDataTypeName (int i)
  105. {
  106. return schema[i].DataTypeName;
  107. }
  108. public DateTime GetDateTime (int i)
  109. {
  110. return (DateTime) GetValue (i);
  111. }
  112. public decimal GetDecimal (int i)
  113. {
  114. return (decimal) GetValue (i);
  115. }
  116. public double GetDouble (int i)
  117. {
  118. return (double) GetValue (i);
  119. }
  120. public Type GetFieldType (int i)
  121. {
  122. return schema[i].FieldType;
  123. }
  124. public float GetFloat (int i)
  125. {
  126. return (float) GetValue (i);
  127. }
  128. public Guid GetGuid (int i)
  129. {
  130. return (Guid) GetValue (i);
  131. }
  132. public short GetInt16 (int i)
  133. {
  134. return (short) GetValue (i);
  135. }
  136. public int GetInt32 (int i)
  137. {
  138. return (int) GetValue (i);
  139. }
  140. public long GetInt64 (int i)
  141. {
  142. return (long) GetValue (i);
  143. }
  144. public string GetName (int i)
  145. {
  146. return (string) lookup [i];
  147. }
  148. #if NET_1_2
  149. [MonoTODO]
  150. public virtual object GetObjectRef (int i)
  151. {
  152. throw new NotImplementedException ();
  153. }
  154. #endif
  155. public int GetOrdinal (string name)
  156. {
  157. return lookup.IndexOf (name);
  158. }
  159. public string GetString (int i)
  160. {
  161. return (string) GetValue (i);
  162. }
  163. public object GetValue (int i)
  164. {
  165. if ((i < 0) || (i > fieldCount))
  166. throw new IndexOutOfRangeException();
  167. object value = values [i];
  168. if (value == null)
  169. value = DBNull.Value;
  170. return value;
  171. }
  172. public int GetValues (object[] values)
  173. {
  174. if(values == null)
  175. throw new ArgumentNullException("values");
  176. int count = values.Length > this.values.Length ? this.values.Length : values.Length;
  177. for(int i = 0; i < count; i++)
  178. values[i] = this.values[i];
  179. return count;
  180. }
  181. [MonoTODO]
  182. AttributeCollection ICustomTypeDescriptor.GetAttributes ()
  183. {
  184. return new AttributeCollection(null);
  185. }
  186. [MonoTODO]
  187. string ICustomTypeDescriptor.GetClassName ()
  188. {
  189. return "";
  190. }
  191. [MonoTODO]
  192. string ICustomTypeDescriptor.GetComponentName ()
  193. {
  194. return null;
  195. }
  196. [MonoTODO]
  197. TypeConverter ICustomTypeDescriptor.GetConverter ()
  198. {
  199. return null;
  200. }
  201. [MonoTODO]
  202. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
  203. {
  204. return null;
  205. }
  206. [MonoTODO]
  207. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
  208. {
  209. return null;
  210. }
  211. [MonoTODO]
  212. object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
  213. {
  214. return null;
  215. }
  216. [MonoTODO]
  217. EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
  218. {
  219. return new EventDescriptorCollection(null);
  220. }
  221. [MonoTODO]
  222. EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
  223. {
  224. return new EventDescriptorCollection(null);
  225. }
  226. [MonoTODO]
  227. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
  228. {
  229. DataColumnPropertyDescriptor[] descriptors =
  230. new DataColumnPropertyDescriptor[FieldCount];
  231. DataColumnPropertyDescriptor descriptor;
  232. DataColumn dataColumn;
  233. for(int col = 0; col < FieldCount; col++) {
  234. descriptor = new DataColumnPropertyDescriptor(
  235. GetName(col), col, null);
  236. descriptor.SetComponentType(typeof(DbDataRecord));
  237. descriptor.SetPropertyType(GetFieldType(col));
  238. descriptors[col] = descriptor;
  239. }
  240. return new PropertyDescriptorCollection (descriptors);
  241. }
  242. [MonoTODO]
  243. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute[] attributes)
  244. {
  245. PropertyDescriptorCollection descriptors;
  246. descriptors = ((ICustomTypeDescriptor) this).GetProperties ();
  247. // TODO: filter out descriptors which do not contain
  248. // any of those attributes
  249. // except, those descriptors
  250. // that contain DefaultMemeberAttribute
  251. return descriptors;
  252. }
  253. [MonoTODO]
  254. object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
  255. {
  256. return this;
  257. }
  258. public bool IsDBNull (int i)
  259. {
  260. return GetValue (i) == null;
  261. }
  262. #if NET_1_2
  263. public virtual bool IsSetAsDefault (int i)
  264. {
  265. throw new NotImplementedException ();
  266. }
  267. public void SetSchemaInfo (SchemaInfo[] schemaInfo)
  268. {
  269. throw new NotImplementedException ();
  270. }
  271. #endif
  272. #endregion // Methods
  273. }
  274. }