2002-11-11 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / System.Data / System.Data.Common / DbEnumerator.cs
1 //
2 // System.Data.SqlClient.DbEnumerator.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002
8 //
9
10 using System;
11 using System.Collections;
12 using System.ComponentModel;
13 using System.Data;
14
15 namespace System.Data.Common {
16         public class DbEnumerator : IEnumerator
17         {
18                 #region Fields
19
20                 IDataReader reader;
21                 bool closeReader;
22                 SchemaInfo[] schema;
23                 FieldNameLookup lookup;
24                 int fieldCount;
25         
26                 #endregion // Fields
27
28                 #region Constructors
29
30                 public DbEnumerator (IDataReader reader) 
31                         : this (reader, false)
32                 {
33                 }
34
35                 public DbEnumerator (IDataReader reader, bool closeReader)
36                 {
37                         this.reader = reader;
38                         this.closeReader = closeReader;
39                         this.lookup = new FieldNameLookup ();
40                         this.fieldCount = reader.FieldCount;
41                         LoadSchema (reader.GetSchemaTable ());
42                 }
43
44                 #endregion // Constructors
45
46                 #region Properties
47
48                 public object Current {
49                         get { 
50                                 object[] values = new object[fieldCount];
51                                 reader.GetValues (values);
52                                 return new DbDataRecord (schema, values, lookup); 
53                         }
54                 }
55
56                 #endregion // Properties
57
58                 #region Methods
59
60                 private void LoadSchema (DataTable schemaTable)
61                 {
62                         schema = new SchemaInfo [fieldCount];
63                         int index = 0;
64                         foreach (DataRow row in schemaTable.Rows) {
65                                 SchemaInfo columnSchema = new SchemaInfo ();
66
67                                 lookup.Add ((string) row["ColumnName"]);
68
69                                 columnSchema.AllowDBNull = (bool) row ["AllowDBNull"];
70                                 columnSchema.ColumnName = row ["ColumnName"].ToString ();
71                                 columnSchema.ColumnOrdinal = (int) row ["ColumnOrdinal"];
72                                 columnSchema.ColumnSize = (int) row ["ColumnSize"];
73                                 columnSchema.DataTypeName = reader.GetDataTypeName (index);
74                                 columnSchema.FieldType = reader.GetFieldType (index);
75                                 columnSchema.IsReadOnly = (bool) row ["IsReadOnly"];
76                                 columnSchema.TableName = row ["BaseTableName"].ToString ();
77
78                                 if (row ["NumericPrecision"] != DBNull.Value)
79                                         columnSchema.NumericPrecision = (byte) row ["NumericPrecision"];
80                                 else
81                                         columnSchema.NumericPrecision = (byte) 0;
82
83                                 if (row ["NumericScale"] != DBNull.Value)
84                                         columnSchema.NumericScale = (byte) row ["NumericScale"];
85                                 else
86                                         columnSchema.NumericScale = (byte) 0;
87
88                                 schema [index] = columnSchema;
89                                 index += 1;
90                         }
91                 }
92
93                 public bool MoveNext ()
94                 {
95                         if (reader.Read ()) 
96                                 return true;
97                         if (closeReader)
98                                 reader.Close ();
99                         return false;
100                 }
101
102                 [EditorBrowsable (EditorBrowsableState.Never)]
103                 public void Reset ()
104                 {
105                         throw new NotSupportedException ();
106                 }
107                 
108                 #endregion // Methods
109         }
110 }