RowEnumerableDataReader.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // RowEnumerableDataReader.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc. http://www.novell.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. namespace System.Data
  34. {
  35. internal class RowEnumerableDataReader : IDataReader
  36. {
  37. EnumerableRowCollection source;
  38. IEnumerator e;
  39. int depth;
  40. public RowEnumerableDataReader (IEnumerable source, int depth)
  41. {
  42. this.source = source as EnumerableRowCollection;
  43. if (this.source == null)
  44. this.source = new EnumerableRowCollection<DataRow> ((IEnumerable<DataRow>) source);
  45. this.depth = depth;
  46. }
  47. public DataRow Current {
  48. get { return e != null ? (DataRow) e.Current : null; }
  49. }
  50. public int Depth {
  51. get { return depth; }
  52. }
  53. public bool IsClosed {
  54. get { return e == null; }
  55. }
  56. public int RecordsAffected {
  57. get { return -1; }
  58. }
  59. public void Close ()
  60. {
  61. e = null;
  62. }
  63. public DataTable GetSchemaTable ()
  64. {
  65. return new DataTableReader (source.Table).GetSchemaTable ();
  66. }
  67. public bool NextResult ()
  68. {
  69. return e.MoveNext ();
  70. }
  71. public bool Read ()
  72. {
  73. if (e == null)
  74. e = ((IEnumerable) source).GetEnumerator ();
  75. return NextResult ();
  76. }
  77. // IDisposable
  78. public void Dispose ()
  79. {
  80. Close ();
  81. }
  82. // IDataRecord
  83. DataTable GetTable ()
  84. {
  85. DataRow r = Current;
  86. if (r == null)
  87. foreach (DataRow rr in source) {
  88. r = rr;
  89. break;
  90. }
  91. return r.Table;
  92. }
  93. public int FieldCount {
  94. get { return GetTable ().Columns.Count; }
  95. }
  96. public object this [int i] {
  97. get { return Current [i]; }
  98. }
  99. public object this [string name] {
  100. get { return Current [name]; }
  101. }
  102. public string GetDataTypeName (int i)
  103. {
  104. return GetFieldType (i).Name;
  105. }
  106. public Type GetFieldType (int i)
  107. {
  108. return GetTable ().Columns [i].DataType;
  109. }
  110. public string GetName (int i)
  111. {
  112. return GetTable ().Columns [i].ColumnName;
  113. }
  114. public int GetOrdinal (string name)
  115. {
  116. return GetTable ().Columns [name].Ordinal;
  117. }
  118. public long GetBytes (int i, long fieldOffset, byte [] buffer, int bufferoffset, int length)
  119. {
  120. // FIXME: do we need it?
  121. throw new NotSupportedException ();
  122. }
  123. public long GetChars (int i, long fieldOffset, char [] buffer, int bufferoffset, int length)
  124. {
  125. // FIXME: do we need it?
  126. throw new NotSupportedException ();
  127. }
  128. public IDataReader GetData (int i)
  129. {
  130. // FIXME: do we need it?
  131. throw new NotSupportedException ();
  132. }
  133. public int GetValues (object [] values)
  134. {
  135. int fieldCount = FieldCount;
  136. int i;
  137. //target object is byval so we can not just assign new object[] to values , calling side will not change
  138. //hence copy each item into values
  139. for (i = 0; i < values.Length && i < fieldCount; ++i)
  140. values[i] = Current[i];
  141. return i - 1;
  142. }
  143. public bool IsDBNull (int i)
  144. {
  145. return Current.IsNull (i);
  146. }
  147. public bool GetBoolean (int i)
  148. {
  149. return (bool) Current [i];
  150. }
  151. public byte GetByte (int i)
  152. {
  153. return (byte) Current [i];
  154. }
  155. public char GetChar (int i)
  156. {
  157. return (char) Current [i];
  158. }
  159. public DateTime GetDateTime (int i)
  160. {
  161. return (DateTime) Current [i];
  162. }
  163. public decimal GetDecimal (int i)
  164. {
  165. return (decimal) Current [i];
  166. }
  167. public double GetDouble (int i)
  168. {
  169. return (double) Current [i];
  170. }
  171. public float GetFloat (int i)
  172. {
  173. return (float) Current [i];
  174. }
  175. public Guid GetGuid (int i)
  176. {
  177. return (Guid) Current [i];
  178. }
  179. public short GetInt16 (int i)
  180. {
  181. return (short) Current [i];
  182. }
  183. public int GetInt32 (int i)
  184. {
  185. return (int) Current [i];
  186. }
  187. public long GetInt64 (int i)
  188. {
  189. return (long) Current [i];
  190. }
  191. public string GetString (int i)
  192. {
  193. return (string) Current [i];
  194. }
  195. public object GetValue (int i)
  196. {
  197. return Current [i];
  198. }
  199. }
  200. }