Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DbDataAdapter.cs « System.Data.Common « System.Data « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a6af3ff84abda6cd38ffacb87cf835c597dd9749 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//
// System.Data.Common.DbDataAdapter.cs
//
// Author:
//   Rodrigo Moya (rodrigo@ximian.com)
//   Tim Coleman (tim@timcoleman.com)
//
// (C) Ximian, Inc
// Copyright (C) 2002 Tim Coleman
//

using System.Collections;
using System.Data;

namespace System.Data.Common
{
	/// <summary>
	/// Aids implementation of the IDbDataAdapter interface. Inheritors of DbDataAdapter  implement a set of functions to provide strong typing, but inherit most of the functionality needed to fully implement a DataAdapter.
	/// </summary>
	public abstract class DbDataAdapter : DataAdapter, ICloneable
	{
		#region Fields

		public const string DefaultSourceTableName = "Table";

		#endregion
		
		#region Constructors

		protected DbDataAdapter() 
		{
		}

		#endregion

		#region Properties

		IDbCommand DeleteCommand {
			get { return ((IDbDataAdapter)this).DeleteCommand; }
		}

		IDbCommand InsertCommand {
			get { return ((IDbDataAdapter)this).InsertCommand; }
		}

		IDbCommand SelectCommand {
			get { return ((IDbDataAdapter)this).SelectCommand; }
		}


		IDbCommand UpdateCommand {
			get { return ((IDbDataAdapter)this).UpdateCommand; }
		}

		#endregion

		#region Methods

		protected abstract RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping);
		protected abstract RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping);

		[MonoTODO]
		protected override void Dispose (bool disposing)
		{
			throw new NotImplementedException ();
		}

                public override int Fill (DataSet dataSet)
                {
			return Fill (dataSet, DefaultSourceTableName);
                }

		public int Fill (DataTable dataTable) 
		{
			return Fill (dataTable.DataSet, dataTable.TableName);
		}

		public int Fill (DataSet dataSet, string srcTable) 
		{
			return Fill (dataSet, 0, 0, srcTable);
		}

		protected virtual int Fill (DataTable dataTable, IDataReader dataReader) 
		{
			return Fill (dataTable.DataSet, dataTable.TableName, dataReader, 0, 0);
		}

		protected virtual int Fill (DataTable dataTable, IDbCommand command, CommandBehavior behavior) 
		{
			return Fill (dataTable.DataSet, 0, 0, dataTable.TableName, command, behavior);
		}

		public int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable) 
		{
			return this.Fill (dataSet, startRecord, maxRecords, srcTable, SelectCommand, CommandBehavior.Default);
		}

		protected virtual int Fill (DataSet dataSet, string srcTable, IDataReader dataReader, int startRecord, int maxRecords) 
		{
			if (startRecord < 0)
				throw new ArgumentException ("The startRecord parameter was less than 0.");
			if (maxRecords < 0)
				throw new ArgumentException ("The maxRecords parameter was less than 0.");

                        DataTable table;
                        int readCount = 0;
                        int resultCount = 0;

			string tableName = srcTable;
			string baseColumnName;
			string columnName;
			ArrayList primaryKey;	
			bool resultsFound;
			object[] itemArray;
			DataTableMapping tableMapping;

			DataRow row; // FIXME needed for incorrect operation below.

                        do
                        {
				if (dataSet.Tables.Contains (tableName))
					table = dataSet.Tables[tableName];
				else
					table = new DataTable (tableName);

				primaryKey = new ArrayList ();	

				foreach (DataRow schemaRow in dataReader.GetSchemaTable ().Rows)
				{
					// generate a unique column name in the dataset table.
					baseColumnName = (string)(schemaRow["BaseColumnName"]);
					if (baseColumnName == "")
						baseColumnName = "Column";

					columnName = baseColumnName;

					for (int i = 1; table.Columns.Contains (columnName); i += 1) 
						columnName = String.Format ("{0}{1}", baseColumnName, i);


					tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, tableName, (string)(schemaRow["BaseTableName"]), MissingMappingAction);

					// check to see if the column mapping exists
					if (tableMapping.ColumnMappings.IndexOfDataSetColumn (baseColumnName) < 0)
					{
						if (MissingSchemaAction == MissingSchemaAction.Error)
							throw new SystemException ();

						table.Columns.Add (columnName, Type.GetType ((string)(schemaRow["DataType"])));
						tableMapping.ColumnMappings.Add (columnName, baseColumnName);

					}
				
					if (!TableMappings.Contains (tableMapping))
						TableMappings.Add (tableMapping);

					if ((schemaRow["IsKey"]).Equals(DBNull.Value) == false)
						if ((bool)(schemaRow["IsKey"]))
							primaryKey.Add (table.Columns[columnName]);	
				}

				if (MissingSchemaAction == MissingSchemaAction.AddWithKey && primaryKey.Count > 0)
					table.PrimaryKey = (DataColumn[])(primaryKey.ToArray());


				for (int k = 0; k < startRecord; k += 1)
					dataReader.Read ();

				resultsFound = false;

                                itemArray = new object[dataReader.FieldCount];

                                while (dataReader.Read () && !(maxRecords > 0 && readCount >= maxRecords))
                                {
                                        dataReader.GetValues (itemArray);
					row = table.Rows.Add (itemArray);
					if (AcceptChangesDuringFill)
						row.AcceptChanges ();

					/* FIXME

					this is the way it should be done, but LoadDataRow has not been implemented yet.

					table.BeginLoadData ();
					table.LoadDataRow (itemArray, AcceptChangesDuringFill);
					table.EndLoadData ();
					*/

                                        readCount += 1;
					resultsFound = true;
                                }

				if (resultsFound)
				{
					dataSet.Tables.Add (table);
                                	tableName = String.Format ("{0}{1}", srcTable, ++resultCount);
				}


				startRecord = 0;
				maxRecords = 0;
                        } while (dataReader.NextResult ());

                        dataReader.Close ();
                        return readCount;
		}

		protected virtual int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior) 
		{
			if (command.Connection.State == ConnectionState.Closed)
			{
				command.Connection.Open ();
				behavior |= CommandBehavior.CloseConnection;
			}
		
			return this.Fill (dataSet, srcTable, command.ExecuteReader (behavior), startRecord, maxRecords);
		}

		[MonoTODO]
		public override DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType) 
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		public DataTable FillSchema (DataTable dataTable, SchemaType schemaType) 
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		public DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, string srcTable) 
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		protected virtual DataTable FillSchema (DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior) 
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		protected virtual DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior) 
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		public override IDataParameter[] GetFillParameters () 
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		object ICloneable.Clone ()
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		public int Update (DataRow[] dataRows) 
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		public override int Update (DataSet ds) 
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		public int Update (DataTable dt) 
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		protected virtual int Update (DataRow[] row, DataTableMapping dtm) 
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		public int Update (DataSet ds, string s) 
		{
			throw new NotImplementedException ();
		}



		[MonoTODO]
		protected virtual void OnFillError (FillErrorEventArgs value) 
		{
			throw new NotImplementedException ();
		}

		protected abstract void OnRowUpdated (RowUpdatedEventArgs value);
		protected abstract void OnRowUpdating (RowUpdatingEventArgs value);
		
		#endregion
		
		#region Events

		public event FillErrorEventHandler FillError;

		#endregion
	}
}