DbDataRecord.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Collections;
  32. using System.ComponentModel;
  33. using System.Data;
  34. namespace System.Data.Common {
  35. public class DbDataRecord : IDataRecord, ICustomTypeDescriptor
  36. {
  37. #region Fields
  38. SchemaInfo[] schema;
  39. object[] values;
  40. int fieldCount;
  41. FieldNameLookup lookup;
  42. #endregion
  43. #region Constructors
  44. #if NET_2_0
  45. [MonoTODO]
  46. public DbDataRecord (object[] values, PropertyDescriptorCollection descriptors, FieldNameLookup fieldNameLookup)
  47. {
  48. }
  49. [MonoTODO]
  50. public DbDataRecord (SchemaInfo[] schemaInfo, object[] values, PropertyDescriptorCollection descriptors, FieldNameLookup fieldNameLookup)
  51. {
  52. }
  53. #endif
  54. internal DbDataRecord (SchemaInfo[] schema, object[] values, FieldNameLookup lookup)
  55. {
  56. this.schema = schema;
  57. this.lookup = lookup;
  58. this.values = values;
  59. this.fieldCount = values.Length;
  60. }
  61. #endregion
  62. #region Properties
  63. public int FieldCount {
  64. get { return fieldCount; }
  65. }
  66. public object this [string name] {
  67. get { return this [GetOrdinal (name)]; }
  68. }
  69. [System.Runtime.CompilerServices.IndexerName("Item")]
  70. public object this [int index] {
  71. get { return GetValue (index); }
  72. }
  73. #endregion
  74. #region Methods
  75. public bool GetBoolean (int i)
  76. {
  77. return (bool) GetValue (i);
  78. }
  79. public byte GetByte (int i)
  80. {
  81. return (byte) GetValue (i);
  82. }
  83. public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
  84. {
  85. object value = GetValue (i);
  86. if (!(value is byte []))
  87. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  88. if ( buffer == null ) {
  89. // Return length of data
  90. return ((byte []) value).Length;
  91. }
  92. else {
  93. // Copy data into buffer
  94. Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
  95. return ((byte []) value).Length - dataIndex;
  96. }
  97. }
  98. public char GetChar (int i)
  99. {
  100. return (char) GetValue (i);
  101. }
  102. public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
  103. {
  104. object value = GetValue (i);
  105. char [] valueBuffer;
  106. if (value is char[])
  107. valueBuffer = (char[])value;
  108. else if (value is string)
  109. valueBuffer = ((string)value).ToCharArray();
  110. else
  111. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  112. if ( buffer == null ) {
  113. // Return length of data
  114. return valueBuffer.Length;
  115. }
  116. else {
  117. // Copy data into buffer
  118. Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
  119. return valueBuffer.Length - dataIndex;
  120. }
  121. }
  122. public IDataReader GetData (int i)
  123. {
  124. return (IDataReader) GetValue (i);
  125. }
  126. public string GetDataTypeName (int i)
  127. {
  128. return schema[i].DataTypeName;
  129. }
  130. public DateTime GetDateTime (int i)
  131. {
  132. return (DateTime) GetValue (i);
  133. }
  134. public decimal GetDecimal (int i)
  135. {
  136. return (decimal) GetValue (i);
  137. }
  138. public double GetDouble (int i)
  139. {
  140. return (double) GetValue (i);
  141. }
  142. public Type GetFieldType (int i)
  143. {
  144. return schema[i].FieldType;
  145. }
  146. public float GetFloat (int i)
  147. {
  148. return (float) GetValue (i);
  149. }
  150. public Guid GetGuid (int i)
  151. {
  152. return (Guid) GetValue (i);
  153. }
  154. public short GetInt16 (int i)
  155. {
  156. return (short) GetValue (i);
  157. }
  158. public int GetInt32 (int i)
  159. {
  160. return (int) GetValue (i);
  161. }
  162. public long GetInt64 (int i)
  163. {
  164. return (long) GetValue (i);
  165. }
  166. public string GetName (int i)
  167. {
  168. return (string) lookup [i];
  169. }
  170. #if NET_2_0
  171. [MonoTODO]
  172. public virtual object GetObjectRef (int i)
  173. {
  174. throw new NotImplementedException ();
  175. }
  176. #endif
  177. public int GetOrdinal (string name)
  178. {
  179. return lookup.IndexOf (name);
  180. }
  181. public string GetString (int i)
  182. {
  183. return (string) GetValue (i);
  184. }
  185. public object GetValue (int i)
  186. {
  187. if ((i < 0) || (i > fieldCount))
  188. throw new IndexOutOfRangeException();
  189. object value = values [i];
  190. if (value == null)
  191. value = DBNull.Value;
  192. return value;
  193. }
  194. public int GetValues (object[] values)
  195. {
  196. if(values == null)
  197. throw new ArgumentNullException("values");
  198. int count = values.Length > this.values.Length ? this.values.Length : values.Length;
  199. for(int i = 0; i < count; i++)
  200. values[i] = this.values[i];
  201. return count;
  202. }
  203. [MonoTODO]
  204. AttributeCollection ICustomTypeDescriptor.GetAttributes ()
  205. {
  206. return new AttributeCollection(null);
  207. }
  208. [MonoTODO]
  209. string ICustomTypeDescriptor.GetClassName ()
  210. {
  211. return "";
  212. }
  213. [MonoTODO]
  214. string ICustomTypeDescriptor.GetComponentName ()
  215. {
  216. return null;
  217. }
  218. [MonoTODO]
  219. TypeConverter ICustomTypeDescriptor.GetConverter ()
  220. {
  221. return null;
  222. }
  223. [MonoTODO]
  224. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
  225. {
  226. return null;
  227. }
  228. [MonoTODO]
  229. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
  230. {
  231. return null;
  232. }
  233. [MonoTODO]
  234. object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
  235. {
  236. return null;
  237. }
  238. [MonoTODO]
  239. EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
  240. {
  241. return new EventDescriptorCollection(null);
  242. }
  243. [MonoTODO]
  244. EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
  245. {
  246. return new EventDescriptorCollection(null);
  247. }
  248. [MonoTODO]
  249. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
  250. {
  251. DataColumnPropertyDescriptor[] descriptors =
  252. new DataColumnPropertyDescriptor[FieldCount];
  253. DataColumnPropertyDescriptor descriptor;
  254. DataColumn dataColumn;
  255. for(int col = 0; col < FieldCount; col++) {
  256. descriptor = new DataColumnPropertyDescriptor(
  257. GetName(col), col, null);
  258. descriptor.SetComponentType(typeof(DbDataRecord));
  259. descriptor.SetPropertyType(GetFieldType(col));
  260. descriptors[col] = descriptor;
  261. }
  262. return new PropertyDescriptorCollection (descriptors);
  263. }
  264. [MonoTODO]
  265. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute[] attributes)
  266. {
  267. PropertyDescriptorCollection descriptors;
  268. descriptors = ((ICustomTypeDescriptor) this).GetProperties ();
  269. // TODO: filter out descriptors which do not contain
  270. // any of those attributes
  271. // except, those descriptors
  272. // that contain DefaultMemeberAttribute
  273. return descriptors;
  274. }
  275. [MonoTODO]
  276. object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
  277. {
  278. return this;
  279. }
  280. public bool IsDBNull (int i)
  281. {
  282. return GetValue (i) == DBNull.Value;
  283. }
  284. #if NET_2_0
  285. public virtual bool IsSetAsDefault (int i)
  286. {
  287. throw new NotImplementedException ();
  288. }
  289. public void SetSchemaInfo (SchemaInfo[] schemaInfo)
  290. {
  291. throw new NotImplementedException ();
  292. }
  293. #endif
  294. #endregion // Methods
  295. }
  296. }