DbDataRecord.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. using System.Runtime.CompilerServices;
  35. namespace System.Data.Common
  36. {
  37. public abstract class DbDataRecord : IDataRecord, ICustomTypeDescriptor
  38. {
  39. protected DbDataRecord ()
  40. {
  41. }
  42. public abstract int FieldCount { get; }
  43. public abstract object this [string name] { get; }
  44. public abstract object this [int i] { get; }
  45. public abstract bool GetBoolean (int i);
  46. public abstract byte GetByte (int i);
  47. public abstract long GetBytes (int i, long dataIndex, byte [] buffer, int bufferIndex,int length);
  48. public abstract char GetChar (int i);
  49. public abstract long GetChars (int i, long dataIndex, char [] buffer, int bufferIndex, int length);
  50. public abstract string GetDataTypeName (int i);
  51. public abstract DateTime GetDateTime (int i);
  52. public abstract decimal GetDecimal (int i);
  53. public abstract double GetDouble (int i);
  54. public abstract Type GetFieldType (int i);
  55. public abstract float GetFloat (int i);
  56. public abstract Guid GetGuid (int i);
  57. public abstract short GetInt16 (int i);
  58. public abstract int GetInt32 (int i);
  59. public abstract long GetInt64 (int i);
  60. public abstract string GetName (int i);
  61. public abstract int GetOrdinal (string name);
  62. public abstract string GetString (int i);
  63. public abstract object GetValue (int i);
  64. public abstract int GetValues (object [] values);
  65. public abstract bool IsDBNull (int i);
  66. public IDataReader GetData (int i)
  67. {
  68. return (IDataReader) GetValue (i);
  69. }
  70. protected virtual DbDataReader GetDbDataReader (int i)
  71. {
  72. throw new NotSupportedException ();
  73. }
  74. [MonoTODO]
  75. AttributeCollection ICustomTypeDescriptor.GetAttributes ()
  76. {
  77. return new AttributeCollection (null);
  78. }
  79. [MonoTODO]
  80. string ICustomTypeDescriptor.GetClassName ()
  81. {
  82. return string.Empty;
  83. }
  84. [MonoTODO]
  85. string ICustomTypeDescriptor.GetComponentName ()
  86. {
  87. return null;
  88. }
  89. [MonoTODO]
  90. TypeConverter ICustomTypeDescriptor.GetConverter ()
  91. {
  92. return null;
  93. }
  94. [MonoTODO]
  95. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
  96. {
  97. return null;
  98. }
  99. [MonoTODO]
  100. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
  101. {
  102. return null;
  103. }
  104. [MonoTODO]
  105. object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
  106. {
  107. return null;
  108. }
  109. [MonoTODO]
  110. EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
  111. {
  112. return new EventDescriptorCollection (null);
  113. }
  114. [MonoTODO]
  115. EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
  116. {
  117. return new EventDescriptorCollection (null);
  118. }
  119. [MonoTODO]
  120. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
  121. {
  122. DataColumnPropertyDescriptor[] descriptors =
  123. new DataColumnPropertyDescriptor [FieldCount];
  124. DataColumnPropertyDescriptor descriptor;
  125. for (int col = 0; col < FieldCount; col++) {
  126. descriptor = new DataColumnPropertyDescriptor(
  127. GetName (col), col, null);
  128. descriptor.SetComponentType (typeof (DbDataRecord));
  129. descriptor.SetPropertyType (GetFieldType (col));
  130. descriptors [col] = descriptor;
  131. }
  132. return new PropertyDescriptorCollection (descriptors);
  133. }
  134. [MonoTODO]
  135. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
  136. {
  137. PropertyDescriptorCollection descriptors;
  138. descriptors = ((ICustomTypeDescriptor) this).GetProperties ();
  139. // TODO: filter out descriptors which do not contain
  140. // any of those attributes
  141. // except, those descriptors
  142. // that contain DefaultMemeberAttribute
  143. return descriptors;
  144. }
  145. [MonoTODO]
  146. object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
  147. {
  148. return this;
  149. }
  150. }
  151. class DbDataRecordImpl : DbDataRecord
  152. {
  153. #region Fields
  154. readonly SchemaInfo [] schema;
  155. readonly object [] values;
  156. readonly int fieldCount;
  157. #endregion
  158. #region Constructors
  159. // FIXME: this class should actually be reimplemented to be one
  160. // of the derived classes of DbDataRecord, which should become
  161. // almost abstract.
  162. internal DbDataRecordImpl (SchemaInfo[] schema, object[] values)
  163. {
  164. this.schema = schema;
  165. this.values = values;
  166. this.fieldCount = values.Length;
  167. }
  168. #endregion
  169. #region Properties
  170. public override int FieldCount {
  171. get { return fieldCount; }
  172. }
  173. public override object this [string name] {
  174. get { return this [GetOrdinal (name)]; }
  175. }
  176. public override object this [int i] {
  177. get { return GetValue (i); }
  178. }
  179. #endregion
  180. #region Methods
  181. public override bool GetBoolean (int i)
  182. {
  183. return (bool) GetValue (i);
  184. }
  185. public override byte GetByte (int i)
  186. {
  187. return (byte) GetValue (i);
  188. }
  189. public override long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
  190. {
  191. object value = GetValue (i);
  192. if (!(value is byte []))
  193. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  194. if ( buffer == null ) {
  195. // Return length of data
  196. return ((byte []) value).Length;
  197. } else {
  198. // Copy data into buffer
  199. Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
  200. return ((byte []) value).Length - dataIndex;
  201. }
  202. }
  203. public override char GetChar (int i)
  204. {
  205. return (char) GetValue (i);
  206. }
  207. public override long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
  208. {
  209. object value = GetValue (i);
  210. char [] valueBuffer;
  211. if (value is char[])
  212. valueBuffer = (char []) value;
  213. else if (value is string)
  214. valueBuffer = ((string) value).ToCharArray ();
  215. else
  216. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  217. if (buffer == null) {
  218. // Return length of data
  219. return valueBuffer.Length;
  220. } else {
  221. // Copy data into buffer
  222. Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
  223. return valueBuffer.Length - dataIndex;
  224. }
  225. }
  226. public override string GetDataTypeName (int i)
  227. {
  228. return schema[i].DataTypeName;
  229. }
  230. public override DateTime GetDateTime (int i)
  231. {
  232. return (DateTime) GetValue (i);
  233. }
  234. #if NET_2_0
  235. [MonoTODO]
  236. protected override DbDataReader GetDbDataReader (int ordinal)
  237. {
  238. throw new NotImplementedException ();
  239. }
  240. #endif
  241. public override decimal GetDecimal (int i)
  242. {
  243. return (decimal) GetValue (i);
  244. }
  245. public override double GetDouble (int i)
  246. {
  247. return (double) GetValue (i);
  248. }
  249. public override Type GetFieldType (int i)
  250. {
  251. return schema[i].FieldType;
  252. }
  253. public override float GetFloat (int i)
  254. {
  255. return (float) GetValue (i);
  256. }
  257. public override Guid GetGuid (int i)
  258. {
  259. return (Guid) GetValue (i);
  260. }
  261. public override short GetInt16 (int i)
  262. {
  263. return (short) GetValue (i);
  264. }
  265. public override int GetInt32 (int i)
  266. {
  267. return (int) GetValue (i);
  268. }
  269. public override long GetInt64 (int i)
  270. {
  271. return (long) GetValue (i);
  272. }
  273. public override string GetName (int i)
  274. {
  275. return schema [i].ColumnName;
  276. }
  277. public override int GetOrdinal (string name)
  278. {
  279. for (int i = 0; i < FieldCount; i++)
  280. if (schema [i].ColumnName == name)
  281. return i;
  282. return -1;
  283. }
  284. public override string GetString (int i)
  285. {
  286. return (string) GetValue (i);
  287. }
  288. public override object GetValue (int i)
  289. {
  290. if (i < 0 || i > fieldCount)
  291. throw new IndexOutOfRangeException ();
  292. return values [i];
  293. }
  294. public override int GetValues (object[] values)
  295. {
  296. if (values == null)
  297. throw new ArgumentNullException("values");
  298. int count = values.Length > this.values.Length ? this.values.Length : values.Length;
  299. for(int i = 0; i < count; i++)
  300. values [i] = this.values [i];
  301. return count;
  302. }
  303. public override bool IsDBNull (int i)
  304. {
  305. return GetValue (i) == DBNull.Value;
  306. }
  307. #endregion // Methods
  308. }
  309. }