MySql - fixed problem where socket was not getting closed properly (thanks Steve!)
[mono.git] / mcs / class / ByteFX.Data / mysqlclient / dataadapter.cs
1 // ByteFX.Data data access components for .Net
2 // Copyright (C) 2002-2003  ByteFX, Inc.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 // 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 // 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18 using System.Data;
19 using System.Data.Common;
20 using System.ComponentModel;
21
22 namespace ByteFX.Data.MySqlClient
23 {
24         [System.Drawing.ToolboxBitmap( typeof(MySqlDataAdapter), "Designers.dataadapter.bmp")]
25         [System.ComponentModel.DesignerCategory("Code")]
26         public sealed class MySqlDataAdapter : DbDataAdapter, IDbDataAdapter
27         {
28                 private MySqlCommand m_selectCommand;
29                 private MySqlCommand m_insertCommand;
30                 private MySqlCommand m_updateCommand;
31                 private MySqlCommand m_deleteCommand;
32
33                 /*
34                         * Inherit from Component through DbDataAdapter. The event
35                         * mechanism is designed to work with the Component.Events
36                         * property. These variables are the keys used to find the
37                         * events in the components list of events.
38                         */
39                 static private readonly object EventRowUpdated = new object(); 
40                 static private readonly object EventRowUpdating = new object(); 
41
42
43                 public MySqlDataAdapter()
44                 {
45                 }
46
47                 public MySqlDataAdapter( MySqlCommand selectCommand ) 
48                 {
49                         SelectCommand = selectCommand;
50                 }
51
52                 public MySqlDataAdapter( string selectCommandText, string selectConnString) 
53                 {
54                         SelectCommand = new MySqlCommand( selectCommandText, 
55                                 new MySqlConnection(selectConnString) );
56                 }
57
58                 public MySqlDataAdapter( string selectCommandText, MySqlConnection conn) 
59                 {
60                         SelectCommand = new MySqlCommand( selectCommandText, conn );
61                 }
62
63                 #region Properties
64                 [DataSysDescription("Used during Fill/FillSchema")]
65                 [Category("Fill")]
66                 public MySqlCommand SelectCommand 
67                 {
68                         get { return m_selectCommand; }
69                         set { m_selectCommand = value; }
70                 }
71
72                 IDbCommand IDbDataAdapter.SelectCommand 
73                 {
74                         get { return m_selectCommand; }
75                         set { m_selectCommand = (MySqlCommand)value; }
76                 }
77
78                 [DataSysDescription("Used during Update for new rows in Dataset.")]
79                 public MySqlCommand InsertCommand 
80                 {
81                         get { return m_insertCommand; }
82                         set { m_insertCommand = value; }
83                 }
84
85                 IDbCommand IDbDataAdapter.InsertCommand 
86                 {
87                         get { return m_insertCommand; }
88                         set { m_insertCommand = (MySqlCommand)value; }
89                 }
90
91                 [DataSysDescription("Used during Update for modified rows in Dataset.")]
92                 public MySqlCommand UpdateCommand 
93                 {
94                         get { return m_updateCommand; }
95                         set { m_updateCommand = value; }
96                 }
97
98                 IDbCommand IDbDataAdapter.UpdateCommand 
99                 {
100                         get { return m_updateCommand; }
101                         set { m_updateCommand = (MySqlCommand)value; }
102                 }
103
104                 [DataSysDescription("Used during Update for deleted rows in Dataset.")]
105                 public MySqlCommand DeleteCommand 
106                 {
107                         get { return m_deleteCommand; }
108                         set { m_deleteCommand = value; }
109                 }
110
111                 IDbCommand IDbDataAdapter.DeleteCommand 
112                 {
113                         get { return m_deleteCommand; }
114                         set { m_deleteCommand = (MySqlCommand)value; }
115                 }
116                 #endregion
117
118                 /*
119                         * Implement abstract methods inherited from DbDataAdapter.
120                         */
121                 override protected RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
122                 {
123                         return new MySqlRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
124                 }
125
126                 override protected RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
127                 {
128                         return new MySqlRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
129                 }
130
131                 override protected void OnRowUpdating(RowUpdatingEventArgs value)
132                 {
133                         MySqlRowUpdatingEventHandler handler = (MySqlRowUpdatingEventHandler) Events[EventRowUpdating];
134                         if ((null != handler) && (value is MySqlRowUpdatingEventArgs)) 
135                         {
136                                 handler(this, (MySqlRowUpdatingEventArgs) value);
137                         }
138                 }
139
140                 override protected void OnRowUpdated(RowUpdatedEventArgs value)
141                 {
142                         MySqlRowUpdatedEventHandler handler = (MySqlRowUpdatedEventHandler) Events[EventRowUpdated];
143                         if ((null != handler) && (value is MySqlRowUpdatedEventArgs)) 
144                         {
145                                 handler(this, (MySqlRowUpdatedEventArgs) value);
146                         }
147                 }
148
149                 public event MySqlRowUpdatingEventHandler RowUpdating
150                 {
151                         add { Events.AddHandler(EventRowUpdating, value); }
152                         remove { Events.RemoveHandler(EventRowUpdating, value); }
153                 }
154
155                 public event MySqlRowUpdatedEventHandler RowUpdated
156                 {
157                         add { Events.AddHandler(EventRowUpdated, value); }
158                         remove { Events.RemoveHandler(EventRowUpdated, value); }
159                 }
160         }
161
162         public delegate void MySqlRowUpdatingEventHandler(object sender, MySqlRowUpdatingEventArgs e);
163         public delegate void MySqlRowUpdatedEventHandler(object sender, MySqlRowUpdatedEventArgs e);
164
165         public class MySqlRowUpdatingEventArgs : RowUpdatingEventArgs
166         {
167                 public MySqlRowUpdatingEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) 
168                         : base(row, command, statementType, tableMapping) 
169                 {
170                 }
171
172                 // Hide the inherited implementation of the command property.
173                 new public MySqlCommand Command
174                 {
175                         get  { return (MySqlCommand)base.Command; }
176                         set  { base.Command = value; }
177                 }
178         }
179
180         public class MySqlRowUpdatedEventArgs : RowUpdatedEventArgs
181         {
182                 public MySqlRowUpdatedEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
183                         : base(row, command, statementType, tableMapping) 
184                 {
185                 }
186
187                 // Hide the inherited implementation of the command property.
188                 new public MySqlCommand Command
189                 {
190                         get  { return (MySqlCommand)base.Command; }
191                 }
192         }
193 }