2002-05-11 Tim Coleman
[mono.git] / mcs / class / Mono.Data.PostgreSqlClient / Mono.Data.PostgreSqlClient / PgSqlDataAdapter.cs
1 //
2 // System.Data.SqlClient.SqlDataAdapter.cs
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Daniel Morgan (danmorg@sc.rr.com)
7 //
8 // (C) Ximian, Inc 2002
9 //
10
11 using System;
12 using System.ComponentModel;
13 using System.Data;
14 using System.Data.Common;
15
16 namespace System.Data.SqlClient
17 {
18         /// <summary>
19         /// Represents a set of command-related properties that are used 
20         /// to fill the DataSet and update a data source, all this 
21         /// from a SQL database.
22         /// </summary>
23         public sealed class SqlDataAdapter : DbDataAdapter 
24         {
25                 #region Fields
26
27                 SqlCommand deleteCommand;
28                 SqlCommand insertCommand;
29                 SqlCommand selectCommand;
30                 SqlCommand updateCommand;
31
32                 bool isDirty;  // indicates if query has changed since last SELECT
33
34                 #endregion
35
36                 #region Constructors
37                 
38                 public SqlDataAdapter ()        
39                         : this (new SqlCommand ())
40                 {
41                 }
42
43                 public SqlDataAdapter (SqlCommand selectCommand)
44                 {
45                         this.deleteCommand = new SqlCommand ();
46                         this.insertCommand = new SqlCommand ();
47                         this.selectCommand = selectCommand;
48                         this.updateCommand = new SqlCommand ();
49                         this.isDirty = true;
50                 }
51
52                 public SqlDataAdapter (string selectCommandText, SqlConnection selectConnection) 
53                         : this (new SqlCommand (selectCommandText, selectConnection))
54                 { 
55                 }
56
57                 public SqlDataAdapter (string selectCommandText, string selectConnectionString)
58                         : this (selectCommandText, new SqlConnection (selectConnectionString))
59                 {
60                 }
61
62                 #endregion
63
64                 #region Properties
65
66                 public new SqlCommand DeleteCommand {
67                         get { return deleteCommand; }
68                         set { deleteCommand = value; }
69                 }
70
71                 public new SqlCommand InsertCommand {
72                         get { return insertCommand; }
73                         set { insertCommand = value; }
74                 }
75
76                 public new SqlCommand SelectCommand {
77                         get { return selectCommand; }
78                         set { 
79                                 this.isDirty = true;
80                                 selectCommand = value; 
81                         }
82                 }
83
84                 public new SqlCommand UpdateCommand {
85                         get { return updateCommand; }
86                         set { updateCommand = value; }
87                 }
88
89                 #endregion // Properties
90
91                 #region Methods
92
93                 public override int Fill (DataSet dataSet) 
94                 {
95                         // Adds or refreshes rows in the DataSet to match those in 
96                         // the data source using the DataSet name, and creates
97                         // a DataTable named "Table"
98
99                         // If the SELECT query has changed, then clear the results
100                         // that we previously retrieved.
101
102                         int changeCount = 0;
103
104                         if (this.isDirty) 
105                         {
106                                 dataSet.Tables.Clear ();
107                         }
108
109                         // Run the SELECT query and get the results in a datareader
110                         SqlDataReader dataReader = selectCommand.ExecuteReader ();
111
112
113                         // The results table in dataSet is called "Table"
114                         string tableName = "Table";
115                         DataTable table;
116
117                         int i = 0;
118                         do 
119                         {
120                                 if (!this.isDirty)  // table already exists
121                                 {
122                                         table = dataSet.Tables[tableName];
123                                 }
124                                 else // create a new table
125                                 {
126                                         table = new DataTable (tableName);
127                                         for (int j = 0; j < dataReader.FieldCount; j += 1) 
128                                         {
129                                                 string baseColumnName = dataReader.GetName (j);
130                                                 string columnName = "";
131
132                                                 if (baseColumnName == "")
133                                                         baseColumnName = "Column";
134                                                 else
135                                                         columnName = baseColumnName;
136
137                                                 for (int k = 1; table.Columns.Contains (columnName) || columnName == ""; k += 1)
138                                                         columnName = String.Format ("{0}{1}", baseColumnName, k);
139                                                         
140                                                 table.Columns.Add (new DataColumn (columnName, dataReader.GetFieldType (j)));
141                                         }
142                                         dataSet.Tables.Add (table);
143                                 } 
144
145                                 DataRow thisRow;
146                                 object[] itemArray = new object[dataReader.FieldCount];
147
148                                 while (dataReader.Read ())
149                                 {
150                                         // need to check for existing rows to reconcile if we have key
151                                         // information.  skip this step for now
152
153                                         // append rows to the end of the current table.
154                                         dataReader.GetValues (itemArray);
155                                         thisRow = table.NewRow ();
156                                         thisRow.ItemArray = itemArray;
157                                         table.ImportRow (thisRow);
158                                         changeCount += 1;
159                                 }
160
161                                 i += 1;
162                                 tableName = String.Format ("Table{0}", i);
163                         } while (dataReader.NextResult ());
164
165                         dataReader.Close ();
166                         this.isDirty = false;
167                         return changeCount;
168                 }
169
170                 [MonoTODO]
171                 public override DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType) 
172                 {
173                         throw new NotImplementedException ();
174                 }
175
176                 [MonoTODO]
177                 public override IDataParameter[] GetFillParameters () 
178                 {
179                         throw new NotImplementedException ();
180                 }
181
182                 [MonoTODO]
183                 public override int Update (DataSet dataSet) 
184                 {
185                         throw new NotImplementedException ();
186                 }
187
188                 [MonoTODO]
189                 protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) 
190                 {
191                         throw new NotImplementedException ();
192                 }
193
194
195                 [MonoTODO]
196                 protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) 
197                 {
198                         throw new NotImplementedException ();
199                 }
200
201                 protected override void OnRowUpdated (RowUpdatedEventArgs value) 
202                 {
203                         throw new NotImplementedException ();
204                 }
205
206                 protected override void OnRowUpdating (RowUpdatingEventArgs value) 
207                 {
208                         throw new NotImplementedException ();
209                 }
210
211                 #endregion // Methods
212
213                 #region Events and Delegates
214
215                 public event SqlRowUpdatedEventHandler RowUpdated;
216                 public event SqlRowUpdatingEventHandler RowUpdating;
217
218                 #endregion // Events and Delegates
219
220         }
221 }