2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / ByteFX.Data / mysqlclient / dataadapter.cs
1 // ByteFX.Data data access components for .Net\r
2 // Copyright (C) 2002-2003  ByteFX, Inc.\r
3 //\r
4 // This library is free software; you can redistribute it and/or\r
5 // modify it under the terms of the GNU Lesser General Public\r
6 // License as published by the Free Software Foundation; either\r
7 // version 2.1 of the License, or (at your option) any later version.\r
8 // \r
9 // This library is distributed in the hope that it will be useful,\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
12 // Lesser General Public License for more details.\r
13 // \r
14 // You should have received a copy of the GNU Lesser General Public\r
15 // License along with this library; if not, write to the Free Software\r
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
17 \r
18 using System.Data;\r
19 using System.Data.Common;\r
20 using System.ComponentModel;\r
21 \r
22 namespace ByteFX.Data.MySqlClient\r
23 {\r
24         /// <summary>\r
25         /// Represents a set of data commands and a database connection that are used to fill a dataset and update a MySQL database. This class cannot be inherited.\r
26         /// </summary>\r
27         /// <include file='docs/MySqlDataAdapter.xml' path='MyDocs/MyMembers[@name="Class"]/*'/>\r
28         [System.Drawing.ToolboxBitmap( typeof(MySqlDataAdapter), "MySqlClient.resources.dataadapter.bmp")]\r
29         [System.ComponentModel.DesignerCategory("Code")]\r
30         [Designer("ByteFX.Data.MySqlClient.Design.MySqlDataAdapterDesigner,MySqlClient.Design")]\r
31         public sealed class MySqlDataAdapter : DbDataAdapter, IDbDataAdapter\r
32         {\r
33                 private MySqlCommand m_selectCommand;\r
34                 private MySqlCommand m_insertCommand;\r
35                 private MySqlCommand m_updateCommand;\r
36                 private MySqlCommand m_deleteCommand;\r
37 \r
38                 /*\r
39                         * Inherit from Component through DbDataAdapter. The event\r
40                         * mechanism is designed to work with the Component.Events\r
41                         * property. These variables are the keys used to find the\r
42                         * events in the components list of events.\r
43                         */\r
44                 static private readonly object EventRowUpdated = new object(); \r
45                 static private readonly object EventRowUpdating = new object(); \r
46 \r
47 \r
48                 /// <summary>\r
49                 /// Initializes a new instance of the MySqlDataAdapter class.\r
50                 /// </summary>\r
51                 public MySqlDataAdapter()\r
52                 {\r
53                 }\r
54 \r
55                 /// <summary>\r
56                 /// Initializes a new instance of the MySqlDataAdapter class with the specified MySqlCommand as the SelectCommand property.\r
57                 /// </summary>\r
58                 /// <param name="selectCommand"></param>\r
59                 public MySqlDataAdapter( MySqlCommand selectCommand ) \r
60                 {\r
61                         SelectCommand = selectCommand;\r
62                 }\r
63 \r
64                 /// <summary>\r
65                 /// Initializes a new instance of the MySqlDataAdapter class with a SelectCommand and a MySqlConnection object.\r
66                 /// </summary>\r
67                 /// <param name="selectCommandText"></param>\r
68                 /// <param name="conn"></param>\r
69                 public MySqlDataAdapter( string selectCommandText, MySqlConnection conn) \r
70                 {\r
71                         SelectCommand = new MySqlCommand( selectCommandText, conn );\r
72                 }\r
73 \r
74                 /// <summary>\r
75                 /// Initializes a new instance of the MySqlDataAdapter class with a SelectCommand and a connection string.\r
76                 /// </summary>\r
77                 /// <param name="selectCommandText"></param>\r
78                 /// <param name="selectConnString"></param>\r
79                 public MySqlDataAdapter( string selectCommandText, string selectConnString) \r
80                 {\r
81                         SelectCommand = new MySqlCommand( selectCommandText, \r
82                                 new MySqlConnection(selectConnString) );\r
83                 }\r
84 \r
85                 #region Properties\r
86                 /// <summary>\r
87                 /// Gets or sets a SQL statement to delete records from the data set.\r
88                 /// </summary>\r
89                 [Description("Used during Update for deleted rows in Dataset.")]\r
90                 public MySqlCommand DeleteCommand \r
91                 {\r
92                         get { return m_deleteCommand; }\r
93                         set { m_deleteCommand = value; }\r
94                 }\r
95 \r
96                 IDbCommand IDbDataAdapter.DeleteCommand \r
97                 {\r
98                         get { return m_deleteCommand; }\r
99                         set { m_deleteCommand = (MySqlCommand)value; }\r
100                 }\r
101 \r
102                 /// <summary>\r
103                 /// Gets or sets a SQL statement to insert new records into the data source.\r
104                 /// </summary>\r
105                 [Description("Used during Update for new rows in Dataset.")]\r
106                 public MySqlCommand InsertCommand \r
107                 {\r
108                         get { return m_insertCommand; }\r
109                         set { m_insertCommand = value; }\r
110                 }\r
111 \r
112                 IDbCommand IDbDataAdapter.InsertCommand \r
113                 {\r
114                         get { return m_insertCommand; }\r
115                         set { m_insertCommand = (MySqlCommand)value; }\r
116                 }\r
117 \r
118                 /// <summary>\r
119                 /// Gets or sets a SQL statement used to select records in the data source.\r
120                 /// </summary>\r
121                 [Description("Used during Fill/FillSchema")]\r
122                 [Category("Fill")]\r
123                 public MySqlCommand SelectCommand \r
124                 {\r
125                         get { return m_selectCommand; }\r
126                         set { m_selectCommand = value; }\r
127                 }\r
128 \r
129                 IDbCommand IDbDataAdapter.SelectCommand \r
130                 {\r
131                         get { return m_selectCommand; }\r
132                         set { m_selectCommand = (MySqlCommand)value; }\r
133                 }\r
134 \r
135                 /// <summary>\r
136                 /// Gets or sets a SQL statement used to update records in the data source.\r
137                 /// </summary>\r
138                 [Description("Used during Update for modified rows in Dataset.")]\r
139                 public MySqlCommand UpdateCommand \r
140                 {\r
141                         get { return m_updateCommand; }\r
142                         set { m_updateCommand = value; }\r
143                 }\r
144 \r
145                 IDbCommand IDbDataAdapter.UpdateCommand \r
146                 {\r
147                         get { return m_updateCommand; }\r
148                         set { m_updateCommand = (MySqlCommand)value; }\r
149                 }\r
150 \r
151                 #endregion\r
152 \r
153                 /*\r
154                         * Implement abstract methods inherited from DbDataAdapter.\r
155                         */\r
156                 /// <summary>\r
157                 /// Overridden. See <see cref="DbDataAdapter.CreateRowUpdatedEvent"/>.\r
158                 /// </summary>\r
159                 /// <param name="dataRow"></param>\r
160                 /// <param name="command"></param>\r
161                 /// <param name="statementType"></param>\r
162                 /// <param name="tableMapping"></param>\r
163                 /// <returns></returns>\r
164                 override protected RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)\r
165                 {\r
166                         return new MySqlRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);\r
167                 }\r
168 \r
169                 /// <summary>\r
170                 /// Overridden. See <see cref="DbDataAdapter.CreateRowUpdatingEvent"/>.\r
171                 /// </summary>\r
172                 /// <param name="dataRow"></param>\r
173                 /// <param name="command"></param>\r
174                 /// <param name="statementType"></param>\r
175                 /// <param name="tableMapping"></param>\r
176                 /// <returns></returns>\r
177                 override protected RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)\r
178                 {\r
179                         return new MySqlRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);\r
180                 }\r
181 \r
182                 /// <summary>\r
183                 /// Overridden. Raises the RowUpdating event.\r
184                 /// </summary>\r
185                 /// <param name="value">A MySqlRowUpdatingEventArgs that contains the event data.</param>\r
186                 override protected void OnRowUpdating(RowUpdatingEventArgs value)\r
187                 {\r
188                         MySqlRowUpdatingEventHandler handler = (MySqlRowUpdatingEventHandler) Events[EventRowUpdating];\r
189                         if ((null != handler) && (value is MySqlRowUpdatingEventArgs)) \r
190                         {\r
191                                 handler(this, (MySqlRowUpdatingEventArgs) value);\r
192                         }\r
193                 }\r
194 \r
195                 /// <summary>\r
196                 /// Overridden. Raises the RowUpdated event.\r
197                 /// </summary>\r
198                 /// <param name="value">A MySqlRowUpdatedEventArgs that contains the event data. </param>\r
199                 override protected void OnRowUpdated(RowUpdatedEventArgs value)\r
200                 {\r
201                         MySqlRowUpdatedEventHandler handler = (MySqlRowUpdatedEventHandler) Events[EventRowUpdated];\r
202                         if ((null != handler) && (value is MySqlRowUpdatedEventArgs)) \r
203                         {\r
204                                 handler(this, (MySqlRowUpdatedEventArgs) value);\r
205                         }\r
206                 }\r
207 \r
208                 /// <summary>\r
209                 /// Occurs during Update before a command is executed against the data source. The attempt to update is made, so the event fires.\r
210                 /// </summary>\r
211                 public event MySqlRowUpdatingEventHandler RowUpdating\r
212                 {\r
213                         add { Events.AddHandler(EventRowUpdating, value); }\r
214                         remove { Events.RemoveHandler(EventRowUpdating, value); }\r
215                 }\r
216 \r
217                 /// <summary>\r
218                 /// Occurs during Update after a command is executed against the data source. The attempt to update is made, so the event fires.\r
219                 /// </summary>\r
220                 public event MySqlRowUpdatedEventHandler RowUpdated\r
221                 {\r
222                         add { Events.AddHandler(EventRowUpdated, value); }\r
223                         remove { Events.RemoveHandler(EventRowUpdated, value); }\r
224                 }\r
225         }\r
226 \r
227         /// <summary>\r
228         /// Represents the method that will handle the <see cref="MySqlDataAdapter.RowUpdating"/> event of a <see cref="MySqlDataAdapter"/>.\r
229         /// </summary>\r
230         public delegate void MySqlRowUpdatingEventHandler(object sender, MySqlRowUpdatingEventArgs e);\r
231 \r
232         /// <summary>\r
233         /// Represents the method that will handle the <see cref="MySqlDataAdapter.RowUpdated"/> event of a <see cref="MySqlDataAdapter"/>.\r
234         /// </summary>\r
235         public delegate void MySqlRowUpdatedEventHandler(object sender, MySqlRowUpdatedEventArgs e);\r
236 \r
237         /// <summary>\r
238         /// Provides data for the RowUpdating event. This class cannot be inherited.\r
239         /// </summary>\r
240         public sealed class MySqlRowUpdatingEventArgs : RowUpdatingEventArgs\r
241         {\r
242                 /// <summary>\r
243                 /// Initializes a new instance of the MySqlRowUpdatingEventArgs class.\r
244                 /// </summary>\r
245                 /// <param name="row">The <see cref="DataRow"/> to <see cref="DbDataAdapter.Update"/>.</param>\r
246                 /// <param name="command">The <see cref="IDbCommand"/> to execute during <see cref="DbDataAdapter.Update"/>.</param>\r
247                 /// <param name="statementType">One of the <see cref="StatementType"/> values that specifies the type of query executed.</param>\r
248                 /// <param name="tableMapping">The <see cref="DataTableMapping"/> sent through an <see cref="DbDataAdapter.Update"/>.</param>\r
249                 public MySqlRowUpdatingEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) \r
250                         : base(row, command, statementType, tableMapping) \r
251                 {\r
252                 }\r
253 \r
254                 /// <summary>\r
255                 /// Gets or sets the MySqlCommand to execute when performing the Update.\r
256                 /// </summary>\r
257                 new public MySqlCommand Command\r
258                 {\r
259                         get  { return (MySqlCommand)base.Command; }\r
260                         set  { base.Command = value; }\r
261                 }\r
262         }\r
263 \r
264         /// <summary>\r
265         /// Provides data for the RowUpdated event. This class cannot be inherited.\r
266         /// </summary>\r
267         public sealed class MySqlRowUpdatedEventArgs : RowUpdatedEventArgs\r
268         {\r
269                 /// <summary>\r
270                 /// Initializes a new instance of the MySqlRowUpdatedEventArgs class.\r
271                 /// </summary>\r
272                 /// <param name="row">The <see cref="DataRow"/> sent through an <see cref="DbDataAdapter.Update"/>.</param>\r
273                 /// <param name="command">The <see cref="IDbCommand"/> executed when <see cref="DbDataAdapter.Update"/> is called.</param>\r
274                 /// <param name="statementType">One of the <see cref="StatementType"/> values that specifies the type of query executed.</param>\r
275                 /// <param name="tableMapping">The <see cref="DataTableMapping"/> sent through an <see cref="DbDataAdapter.Update"/>.</param>\r
276                 public MySqlRowUpdatedEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)\r
277                         : base(row, command, statementType, tableMapping) \r
278                 {\r
279                 }\r
280 \r
281                 /// <summary>\r
282                 /// Gets or sets the MySqlCommand executed when Update is called.\r
283                 /// </summary>\r
284                 new public MySqlCommand Command\r
285                 {\r
286                         get  { return (MySqlCommand)base.Command; }\r
287                 }\r
288         }\r
289 }\r