RowEnumerableDataReader.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 (source == null)
  44. 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. // FIXME: do we need it?
  136. throw new NotSupportedException ();
  137. }
  138. public bool IsDBNull (int i)
  139. {
  140. return Current.IsNull (i);
  141. }
  142. public bool GetBoolean (int i)
  143. {
  144. return (bool) Current [i];
  145. }
  146. public byte GetByte (int i)
  147. {
  148. return (byte) Current [i];
  149. }
  150. public char GetChar (int i)
  151. {
  152. return (char) Current [i];
  153. }
  154. public DateTime GetDateTime (int i)
  155. {
  156. return (DateTime) Current [i];
  157. }
  158. public decimal GetDecimal (int i)
  159. {
  160. return (decimal) Current [i];
  161. }
  162. public double GetDouble (int i)
  163. {
  164. return (double) Current [i];
  165. }
  166. public float GetFloat (int i)
  167. {
  168. return (float) Current [i];
  169. }
  170. public Guid GetGuid (int i)
  171. {
  172. return (Guid) Current [i];
  173. }
  174. public short GetInt16 (int i)
  175. {
  176. return (short) Current [i];
  177. }
  178. public int GetInt32 (int i)
  179. {
  180. return (int) Current [i];
  181. }
  182. public long GetInt64 (int i)
  183. {
  184. return (long) Current [i];
  185. }
  186. public string GetString (int i)
  187. {
  188. return (string) Current [i];
  189. }
  190. public object GetValue (int i)
  191. {
  192. return Current [i];
  193. }
  194. }
  195. }