1 // ByteFX.Data data access components for .Net
2 // Copyright (C) 2002-2003 ByteFX, Inc.
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.
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.
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
20 using System.Collections.Specialized;
22 using System.ComponentModel;
23 using System.Globalization;
24 using ByteFX.Data.Common;
26 namespace ByteFX.Data.MySqlClient
29 /// Represents an open connection to a MySQL Server database. This class cannot be inherited.
31 /// <include file='docs/MySqlConnection.xml' path='MyDocs/MyMembers[@name="Class"]/*'/>
32 [System.Drawing.ToolboxBitmap( typeof(MySqlConnection), "Designers.connection.bmp")]
33 [System.ComponentModel.DesignerCategory("Code")]
35 public sealed class MySqlConnection : Component, IDbConnection, ICloneable
37 internal ConnectionState state;
38 private MySqlInternalConnection internalConnection;
39 private MySqlDataReader dataReader;
40 private NumberFormatInfo numberFormat;
41 private MySqlConnectionString settings;
44 /// Occurs when the state of the connection changes.
46 public event StateChangeEventHandler StateChange;
50 /// Creates a new connection
52 public MySqlConnection()
54 settings = new MySqlConnectionString();
58 /// Creates a new connection
60 /// <param name="container"></param>
61 public MySqlConnection(System.ComponentModel.IContainer container)
63 settings = new MySqlConnectionString();
67 // Have a constructor that takes a connection string.
69 /// Creates a new connection using the specified connection string.
71 /// <param name="connectString"></param>
72 public MySqlConnection(string connectString)
74 settings = new MySqlConnectionString(connectString);
78 /// Gets the name of the MySQL server to which to connect.
82 public string DataSource
84 get { return settings.Host; }
88 /// Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.
90 /// <include file='docs/MySqlConnection.xml' path='MyDocs/MyMembers[@name="ConnectionTimeout"]/*'/>
92 public int ConnectionTimeout
94 get { return settings.ConnectTimeout; }
98 /// Gets the name of the current database or the database to be used after a connection is opened.
101 public string Database
103 get { return settings.Database; }
107 /// Indicates if this connection should use compression when communicating with the server.
110 public bool UseCompression
112 get { return settings.UseCompression; }
116 /// Gets the current state of the connection.
119 public ConnectionState State
121 get { return state; }
124 internal MySqlDataReader Reader
126 get { return dataReader; }
127 set { dataReader = value; }
130 internal MySqlInternalConnection InternalConnection
132 get { return internalConnection; }
135 internal NumberFormatInfo NumberFormat
139 if (numberFormat == null)
141 numberFormat = new NumberFormatInfo();
142 numberFormat = (NumberFormatInfo)NumberFormatInfo.InvariantInfo.Clone();
143 numberFormat.NumberDecimalSeparator = ".";
150 /// Gets a string containing the version of the MySQL server to which the client is connected.
153 public string ServerVersion
155 get { return internalConnection.Driver.Version; }
158 internal Encoding Encoding
162 if (internalConnection == null)
163 return System.Text.Encoding.Default;
165 return internalConnection.Driver.Encoding;
171 /// Gets or sets the string used to connect to a MySQL Server database.
173 /// <include file='docs/MySqlConnection.xml' path='MyDocs/MyMembers[@name="ConnectionString"]/*'/>
175 [Editor(typeof(Designers.ConnectionStringEditor), typeof(System.Drawing.Design.UITypeEditor))]
179 public string ConnectionString
183 // Always return exactly what the user set.
184 // Security-sensitive information may be removed.
185 return settings.ConnectString;
189 settings.ConnectString = value;
190 if (internalConnection != null)
191 internalConnection.Settings = settings;
199 /// Begins a database transaction.
201 /// <returns></returns>
202 public MySqlTransaction BeginTransaction()
204 if (state != ConnectionState.Open)
205 throw new MySqlException("Invalid operation: The connection is closed");
207 MySqlTransaction t = new MySqlTransaction();
209 InternalConnection.Driver.SendCommand( DBCmd.QUERY, "BEGIN");
216 IDbTransaction IDbConnection.BeginTransaction()
218 return BeginTransaction();
224 /// <param name="level"></param>
225 /// <returns></returns>
226 public MySqlTransaction BeginTransaction(IsolationLevel level)
228 if (state != ConnectionState.Open)
229 throw new MySqlException("Invalid operation: The connection is closed");
231 MySqlTransaction t = new MySqlTransaction();
233 t.IsolationLevel = level;
234 string cmd = "SET SESSION TRANSACTION ISOLATION LEVEL ";
237 case IsolationLevel.ReadCommitted:
238 cmd += "READ COMMITTED"; break;
239 case IsolationLevel.ReadUncommitted:
240 cmd += "READ UNCOMMITTED"; break;
241 case IsolationLevel.RepeatableRead:
242 cmd += "REPEATABLE READ"; break;
243 case IsolationLevel.Serializable:
244 cmd += "SERIALIZABLE"; break;
245 case IsolationLevel.Chaos:
246 throw new NotSupportedException("Chaos isolation level is not supported");
248 InternalConnection.Driver.SendCommand( DBCmd.QUERY, cmd );
249 InternalConnection.Driver.SendCommand( DBCmd.QUERY, "BEGIN");
256 /// <param name="level"></param>
257 /// <returns></returns>
258 IDbTransaction IDbConnection.BeginTransaction(IsolationLevel level)
260 return BeginTransaction(level);
265 /// Changes the current database for an open MySqlConnection.
267 /// <param name="dbName"></param>
268 public void ChangeDatabase(string dbName)
270 if (state != ConnectionState.Open)
271 throw new MySqlException("Invalid operation: The connection is closed");
273 //TODOinternalConnection.ChangeDatabase( dbName );
274 InternalConnection.Driver.SendCommand( DBCmd.INIT_DB, dbName );
277 internal void SetState( ConnectionState newState )
279 ConnectionState oldState = state;
281 if (this.StateChange != null)
282 StateChange(this, new StateChangeEventArgs( oldState, newState ));
286 /// Opens a database connection with the property settings specified by the ConnectionString.
290 if (state == ConnectionState.Open)
291 throw new MySqlException("error connecting: The connection is already Open (state=Open).");
293 SetState( ConnectionState.Connecting );
295 if (settings.Pooling)
297 internalConnection = MySqlPoolManager.GetConnection( settings );
301 internalConnection = new MySqlInternalConnection( settings );
302 internalConnection.Open();
305 SetState( ConnectionState.Open );
306 internalConnection.SetServerVariables(this);
307 ChangeDatabase( settings.Database );
312 /// Closes the connection to the database. This is the preferred method of closing any open connection.
316 if (state == ConnectionState.Closed) return;
318 if (dataReader != null)
321 if (settings.Pooling)
322 MySqlPoolManager.ReleaseConnection( internalConnection );
324 internalConnection.Close();
326 SetState( ConnectionState.Closed );
329 IDbCommand IDbConnection.CreateCommand()
331 return CreateCommand();
335 /// Creates and returns a MySqlCommand object associated with the MySqlConnection.
337 /// <returns></returns>
338 public MySqlCommand CreateCommand()
340 // Return a new instance of a command object.
341 MySqlCommand c = new MySqlCommand();
348 /// Creates a new MySqlConnection object with the exact same ConnectionString value
350 /// <returns>A cloned MySqlConnection object</returns>
351 object ICloneable.Clone()
353 MySqlConnection clone = new MySqlConnection();
354 clone.ConnectionString = this.ConnectionString;
355 //TODO: how deep should this go?
362 /// Releases the resources used by the MySqlConnection.
364 public new void Dispose()
366 if (State == ConnectionState.Open)