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