DbDataRecord.cs 8.1 KB

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