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