DbDataReader.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //------------------------------------------------------------------------------
  2. // <copyright file="DbDataReader.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. // <owner current="true" primary="false">Microsoft</owner>
  7. //------------------------------------------------------------------------------
  8. namespace System.Data.Common {
  9. using System;
  10. using System.Collections;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.IO;
  14. using System.Threading.Tasks;
  15. using System.Threading;
  16. public abstract class DbDataReader : MarshalByRefObject, IDataReader, IEnumerable { // V1.2.3300
  17. protected DbDataReader() : base() {
  18. }
  19. abstract public int Depth {
  20. get;
  21. }
  22. abstract public int FieldCount {
  23. get;
  24. }
  25. abstract public bool HasRows {
  26. get;
  27. }
  28. abstract public bool IsClosed {
  29. get;
  30. }
  31. abstract public int RecordsAffected {
  32. get;
  33. }
  34. virtual public int VisibleFieldCount {
  35. // NOTE: This is virtual because not all providers may choose to support
  36. // this property, since it was added in Whidbey
  37. get {
  38. return FieldCount;
  39. }
  40. }
  41. abstract public object this [ int ordinal ] {
  42. get;
  43. }
  44. abstract public object this [ string name ] {
  45. get;
  46. }
  47. virtual public void Close()
  48. {
  49. }
  50. [
  51. EditorBrowsableAttribute(EditorBrowsableState.Never)
  52. ]
  53. public void Dispose() {
  54. Dispose(true);
  55. }
  56. protected virtual void Dispose(bool disposing) {
  57. if (disposing) {
  58. Close();
  59. }
  60. }
  61. abstract public string GetDataTypeName(int ordinal);
  62. [
  63. EditorBrowsableAttribute(EditorBrowsableState.Never)
  64. ]
  65. abstract public IEnumerator GetEnumerator();
  66. abstract public Type GetFieldType(int ordinal);
  67. abstract public string GetName(int ordinal);
  68. abstract public int GetOrdinal(string name);
  69. virtual public DataTable GetSchemaTable()
  70. {
  71. throw new NotSupportedException();
  72. }
  73. abstract public bool GetBoolean(int ordinal);
  74. abstract public byte GetByte(int ordinal);
  75. abstract public long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length);
  76. abstract public char GetChar(int ordinal);
  77. abstract public long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length);
  78. [
  79. EditorBrowsableAttribute(EditorBrowsableState.Never)
  80. ]
  81. public DbDataReader GetData(int ordinal) {
  82. return GetDbDataReader(ordinal);
  83. }
  84. IDataReader IDataRecord.GetData(int ordinal) {
  85. return GetDbDataReader(ordinal);
  86. }
  87. virtual protected DbDataReader GetDbDataReader(int ordinal) {
  88. // NOTE: This method is virtual because we're required to implement
  89. // it however most providers won't support it. Only the OLE DB
  90. // provider supports it right now, and they can override it.
  91. throw ADP.NotSupported();
  92. }
  93. abstract public DateTime GetDateTime(int ordinal);
  94. abstract public Decimal GetDecimal(int ordinal);
  95. abstract public double GetDouble(int ordinal);
  96. abstract public float GetFloat(int ordinal);
  97. abstract public Guid GetGuid(int ordinal);
  98. abstract public Int16 GetInt16(int ordinal);
  99. abstract public Int32 GetInt32(int ordinal);
  100. abstract public Int64 GetInt64(int ordinal);
  101. [
  102. EditorBrowsableAttribute(EditorBrowsableState.Never)
  103. ]
  104. virtual public Type GetProviderSpecificFieldType(int ordinal) {
  105. // NOTE: This is virtual because not all providers may choose to support
  106. // this method, since it was added in Whidbey.
  107. return GetFieldType(ordinal);
  108. }
  109. [
  110. EditorBrowsableAttribute(EditorBrowsableState.Never)
  111. ]
  112. virtual public Object GetProviderSpecificValue(int ordinal) {
  113. // NOTE: This is virtual because not all providers may choose to support
  114. // this method, since it was added in Whidbey
  115. return GetValue(ordinal);
  116. }
  117. [
  118. EditorBrowsableAttribute(EditorBrowsableState.Never)
  119. ]
  120. virtual public int GetProviderSpecificValues(object[] values) {
  121. // NOTE: This is virtual because not all providers may choose to support
  122. // this method, since it was added in Whidbey
  123. return GetValues(values);
  124. }
  125. abstract public String GetString(int ordinal);
  126. virtual public Stream GetStream(int ordinal) {
  127. using (MemoryStream bufferStream = new MemoryStream())
  128. {
  129. long bytesRead = 0;
  130. long bytesReadTotal = 0;
  131. byte[] buffer = new byte[4096];
  132. do {
  133. bytesRead = GetBytes(ordinal, bytesReadTotal, buffer, 0, buffer.Length);
  134. bufferStream.Write(buffer, 0, (int)bytesRead);
  135. bytesReadTotal += bytesRead;
  136. } while (bytesRead > 0);
  137. return new MemoryStream(bufferStream.ToArray(), false);
  138. }
  139. }
  140. virtual public TextReader GetTextReader(int ordinal) {
  141. if (IsDBNull(ordinal)) {
  142. return new StringReader(String.Empty);
  143. }
  144. else {
  145. return new StringReader(GetString(ordinal));
  146. }
  147. }
  148. abstract public Object GetValue(int ordinal);
  149. virtual public T GetFieldValue<T>(int ordinal) {
  150. return (T)GetValue(ordinal);
  151. }
  152. public Task<T> GetFieldValueAsync<T>(int ordinal) {
  153. return GetFieldValueAsync<T>(ordinal, CancellationToken.None);
  154. }
  155. virtual public Task<T> GetFieldValueAsync<T>(int ordinal, CancellationToken cancellationToken) {
  156. if (cancellationToken.IsCancellationRequested) {
  157. return ADP.CreatedTaskWithCancellation<T>();
  158. }
  159. else {
  160. try {
  161. return Task.FromResult<T>(GetFieldValue<T>(ordinal));
  162. }
  163. catch (Exception e) {
  164. return ADP.CreatedTaskWithException<T>(e);
  165. }
  166. }
  167. }
  168. abstract public int GetValues(object[] values);
  169. abstract public bool IsDBNull(int ordinal);
  170. public Task<bool> IsDBNullAsync(int ordinal) {
  171. return IsDBNullAsync(ordinal, CancellationToken.None);
  172. }
  173. virtual public Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken) {
  174. if (cancellationToken.IsCancellationRequested) {
  175. return ADP.CreatedTaskWithCancellation<bool>();
  176. }
  177. else {
  178. try {
  179. return IsDBNull(ordinal) ? ADP.TrueTask : ADP.FalseTask;
  180. }
  181. catch (Exception e) {
  182. return ADP.CreatedTaskWithException<bool>(e);
  183. }
  184. }
  185. }
  186. abstract public bool NextResult();
  187. abstract public bool Read();
  188. public Task<bool> ReadAsync() {
  189. return ReadAsync(CancellationToken.None);
  190. }
  191. virtual public Task<bool> ReadAsync(CancellationToken cancellationToken) {
  192. if (cancellationToken.IsCancellationRequested) {
  193. return ADP.CreatedTaskWithCancellation<bool>();
  194. }
  195. else {
  196. try {
  197. return Read() ? ADP.TrueTask : ADP.FalseTask;
  198. }
  199. catch (Exception e) {
  200. return ADP.CreatedTaskWithException<bool>(e);
  201. }
  202. }
  203. }
  204. public Task<bool> NextResultAsync() {
  205. return NextResultAsync(CancellationToken.None);
  206. }
  207. virtual public Task<bool> NextResultAsync(CancellationToken cancellationToken) {
  208. if (cancellationToken.IsCancellationRequested) {
  209. return ADP.CreatedTaskWithCancellation<bool>();
  210. }
  211. else {
  212. try {
  213. return NextResult() ? ADP.TrueTask : ADP.FalseTask;
  214. }
  215. catch (Exception e) {
  216. return ADP.CreatedTaskWithException<bool>(e);
  217. }
  218. }
  219. }
  220. }
  221. }