DbDataReader.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // System.Data.Common.DbDataReader.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 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.IO;
  35. #if NET_4_5
  36. using System.Threading;
  37. using System.Threading.Tasks;
  38. #endif
  39. namespace System.Data.Common {
  40. public abstract class DbDataReader : MarshalByRefObject, IDataReader, IDataRecord, IDisposable, IEnumerable
  41. {
  42. #region Constructors
  43. protected DbDataReader ()
  44. {
  45. }
  46. #endregion // Constructors
  47. #region Properties
  48. public abstract int Depth { get; }
  49. public abstract int FieldCount { get; }
  50. public abstract bool HasRows { get; }
  51. public abstract bool IsClosed { get; }
  52. public abstract object this [int ordinal] { get; }
  53. public abstract object this [string name] { get; }
  54. public abstract int RecordsAffected { get; }
  55. #if NET_2_0
  56. public virtual int VisibleFieldCount {
  57. get { return FieldCount; }
  58. }
  59. #endif
  60. #endregion // Properties
  61. #region Methods
  62. public abstract void Close ();
  63. public abstract bool GetBoolean (int ordinal);
  64. public abstract byte GetByte (int ordinal);
  65. public abstract long GetBytes (int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length);
  66. public abstract char GetChar (int ordinal);
  67. public abstract long GetChars (int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length);
  68. [EditorBrowsable (EditorBrowsableState.Never)]
  69. public void Dispose ()
  70. {
  71. Dispose (true);
  72. }
  73. protected virtual void Dispose (bool disposing)
  74. {
  75. if (disposing)
  76. Close ();
  77. }
  78. #if NET_2_0
  79. [EditorBrowsable (EditorBrowsableState.Never)]
  80. public DbDataReader GetData (int ordinal)
  81. {
  82. return ((DbDataReader) this [ordinal]);
  83. }
  84. #endif
  85. public abstract string GetDataTypeName (int ordinal);
  86. public abstract DateTime GetDateTime (int ordinal);
  87. public abstract decimal GetDecimal (int ordinal);
  88. public abstract double GetDouble (int ordinal);
  89. [EditorBrowsable (EditorBrowsableState.Never)]
  90. public abstract IEnumerator GetEnumerator ();
  91. public abstract Type GetFieldType (int ordinal);
  92. public abstract float GetFloat (int ordinal);
  93. public abstract Guid GetGuid (int ordinal);
  94. public abstract short GetInt16 (int ordinal);
  95. public abstract int GetInt32 (int ordinal);
  96. public abstract long GetInt64 (int ordinal);
  97. public abstract string GetName (int ordinal);
  98. public abstract int GetOrdinal (string name);
  99. #if NET_2_0
  100. [EditorBrowsable (EditorBrowsableState.Never)]
  101. public virtual Type GetProviderSpecificFieldType (int ordinal)
  102. {
  103. return GetFieldType (ordinal);
  104. }
  105. [EditorBrowsable (EditorBrowsableState.Never)]
  106. public virtual object GetProviderSpecificValue (int ordinal)
  107. {
  108. return GetValue (ordinal);
  109. }
  110. [EditorBrowsable (EditorBrowsableState.Never)]
  111. public virtual int GetProviderSpecificValues (object[] values)
  112. {
  113. return GetValues (values);
  114. }
  115. protected virtual DbDataReader GetDbDataReader (int ordinal)
  116. {
  117. return ((DbDataReader) this [ordinal]);
  118. }
  119. #endif
  120. public abstract DataTable GetSchemaTable ();
  121. public abstract string GetString (int ordinal);
  122. public abstract object GetValue (int ordinal);
  123. public abstract int GetValues (object[] values);
  124. IDataReader IDataRecord.GetData (int ordinal)
  125. {
  126. return ((IDataReader) this).GetData (ordinal);
  127. }
  128. public abstract bool IsDBNull (int ordinal);
  129. public abstract bool NextResult ();
  130. public abstract bool Read ();
  131. internal static DataTable GetSchemaTableTemplate ()
  132. {
  133. Type booleanType = typeof (bool);
  134. Type stringType = typeof (string);
  135. Type intType = typeof (int);
  136. Type typeType = typeof (Type);
  137. Type shortType = typeof (short);
  138. DataTable schemaTable = new DataTable ("SchemaTable");
  139. schemaTable.Columns.Add ("ColumnName", stringType);
  140. schemaTable.Columns.Add ("ColumnOrdinal", intType);
  141. schemaTable.Columns.Add ("ColumnSize", intType);
  142. schemaTable.Columns.Add ("NumericPrecision", shortType);
  143. schemaTable.Columns.Add ("NumericScale", shortType);
  144. schemaTable.Columns.Add ("IsUnique", booleanType);
  145. schemaTable.Columns.Add ("IsKey", booleanType);
  146. schemaTable.Columns.Add ("BaseServerName", stringType);
  147. schemaTable.Columns.Add ("BaseCatalogName", stringType);
  148. schemaTable.Columns.Add ("BaseColumnName", stringType);
  149. schemaTable.Columns.Add ("BaseSchemaName", stringType);
  150. schemaTable.Columns.Add ("BaseTableName", stringType);
  151. schemaTable.Columns.Add ("DataType", typeType);
  152. schemaTable.Columns.Add ("AllowDBNull", booleanType);
  153. schemaTable.Columns.Add ("ProviderType", intType);
  154. schemaTable.Columns.Add ("IsAliased", booleanType);
  155. schemaTable.Columns.Add ("IsExpression", booleanType);
  156. schemaTable.Columns.Add ("IsIdentity", booleanType);
  157. schemaTable.Columns.Add ("IsAutoIncrement", booleanType);
  158. schemaTable.Columns.Add ("IsRowVersion", booleanType);
  159. schemaTable.Columns.Add ("IsHidden", booleanType);
  160. schemaTable.Columns.Add ("IsLong", booleanType);
  161. schemaTable.Columns.Add ("IsReadOnly", booleanType);
  162. return schemaTable;
  163. }
  164. #if NET_4_5
  165. [MonoTODO]
  166. public virtual T GetFieldValue<T> (int i)
  167. {
  168. throw new NotImplementedException ();
  169. }
  170. public Task<T> GetFieldValueAsync<T> (int ordinal)
  171. {
  172. return GetFieldValueAsync<T> (ordinal, CancellationToken.None);
  173. }
  174. public virtual Task<T> GetFieldValueAsync<T> (int ordinal, CancellationToken cancellationToken)
  175. {
  176. if (cancellationToken.IsCancellationRequested) {
  177. return TaskHelper.CreateCanceledTask<T> ();
  178. }
  179. try {
  180. return Task.FromResult<T> (GetFieldValue<T> (ordinal));
  181. } catch (Exception e) {
  182. return TaskHelper.CreateExceptionTask<T> (e);
  183. }
  184. }
  185. public Task<bool> NextResultAsync ()
  186. {
  187. return NextResultAsync (CancellationToken.None);
  188. }
  189. public Task<bool> IsDBNullAsync (int ordinal)
  190. {
  191. return IsDBNullAsync (ordinal, CancellationToken.None);
  192. }
  193. [MonoTODO]
  194. public virtual Stream GetStream (int i)
  195. {
  196. throw new NotImplementedException ();
  197. }
  198. [MonoTODO]
  199. public virtual TextReader GetTextReader (int i)
  200. {
  201. throw new NotImplementedException ();
  202. }
  203. public virtual Task<bool> IsDBNullAsync (int ordinal, CancellationToken cancellationToken)
  204. {
  205. if (cancellationToken.IsCancellationRequested) {
  206. return TaskHelper.CreateCanceledTask<bool> ();
  207. }
  208. try {
  209. return Task.FromResult (IsDBNull (ordinal));
  210. } catch (Exception e) {
  211. return TaskHelper.CreateExceptionTask<bool> (e);
  212. }
  213. }
  214. public virtual Task<bool> NextResultAsync (CancellationToken cancellationToken)
  215. {
  216. if (cancellationToken.IsCancellationRequested) {
  217. return TaskHelper.CreateCanceledTask<bool> ();
  218. }
  219. try {
  220. return Task.FromResult (NextResult ());
  221. } catch (Exception e) {
  222. return TaskHelper.CreateExceptionTask<bool> (e);
  223. }
  224. }
  225. public Task<bool> ReadAsync ()
  226. {
  227. return ReadAsync (CancellationToken.None);
  228. }
  229. public virtual Task<bool> ReadAsync (CancellationToken cancellationToken)
  230. {
  231. if (cancellationToken.IsCancellationRequested) {
  232. return TaskHelper.CreateCanceledTask<bool> ();
  233. }
  234. try {
  235. return Task.FromResult (Read ());
  236. } catch (Exception e) {
  237. return TaskHelper.CreateExceptionTask<bool> (e);
  238. }
  239. }
  240. #endif
  241. #endregion // Methods
  242. }
  243. }