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;
43 public event StateChangeEventHandler StateChange;
47 /// Creates a new connection
49 public MySqlConnection()
51 settings = new MySqlConnectionString();
55 /// Creates a new connection
57 /// <param name="container"></param>
58 public MySqlConnection(System.ComponentModel.IContainer container)
60 settings = new MySqlConnectionString();
64 // Have a constructor that takes a connection string.
66 /// Creates a new connection using the specified connection string.
68 /// <param name="connectString"></param>
69 public MySqlConnection(string connectString)
71 settings = new MySqlConnectionString(connectString);
75 /// Gets the name of the MySQL server to which to connect.
79 public string DataSource
81 get { return settings.Host; }
85 /// Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.
87 /// <include file='docs/MySqlConnection.xml' path='MyDocs/MyMembers[@name="ConnectionTimeout"]/*'/>
89 public int ConnectionTimeout
91 get { return settings.ConnectTimeout; }
95 /// Gets the name of the current database or the database to be used after a connection is opened.
98 public string Database
100 get { return settings.Database; }
104 /// Indicates if this connection should use compression when communicating with the server.
107 public bool UseCompression
109 get { return settings.UseCompression; }
113 /// Gets the current state of the connection.
116 public ConnectionState State
118 get { return state; }
121 internal MySqlDataReader Reader
123 get { return dataReader; }
124 set { dataReader = value; }
127 internal MySqlInternalConnection InternalConnection
129 get { return internalConnection; }
132 internal NumberFormatInfo NumberFormat
136 if (numberFormat == null)
138 numberFormat = new NumberFormatInfo();
139 numberFormat = (NumberFormatInfo)NumberFormatInfo.InvariantInfo.Clone();
140 numberFormat.NumberDecimalSeparator = ".";
147 /// Gets a string containing the version of the MySQL server to which the client is connected.
150 public string ServerVersion
152 get { return ""; } //internalConnection.GetServerVersion(); }
155 internal Encoding Encoding
159 //TODO if (encoding == null)
160 return System.Text.Encoding.Default;
168 /// Gets or sets the string used to connect to a MySQL Server database.
171 [Editor(typeof(Designers.ConnectionStringEditor), typeof(System.Drawing.Design.UITypeEditor))]
175 public string ConnectionString
179 // Always return exactly what the user set.
180 // Security-sensitive information may be removed.
181 return settings.ConnectString;
185 settings.ConnectString = value;
186 if (internalConnection != null)
187 internalConnection.Settings = settings;
195 /// Begins a database transaction.
197 /// <returns></returns>
198 public MySqlTransaction BeginTransaction()
200 if (state != ConnectionState.Open)
201 throw new MySqlException("Invalid operation: The connection is closed");
203 MySqlTransaction t = new MySqlTransaction();
205 InternalConnection.Driver.SendCommand( DBCmd.QUERY, "BEGIN");
212 IDbTransaction IDbConnection.BeginTransaction()
214 return BeginTransaction();
220 /// <param name="level"></param>
221 /// <returns></returns>
222 public MySqlTransaction BeginTransaction(IsolationLevel level)
224 if (state != ConnectionState.Open)
225 throw new MySqlException("Invalid operation: The connection is closed");
227 MySqlTransaction t = new MySqlTransaction();
229 t.IsolationLevel = level;
230 string cmd = "SET SESSION TRANSACTION ISOLATION LEVEL ";
233 case IsolationLevel.ReadCommitted:
234 cmd += "READ COMMITTED"; break;
235 case IsolationLevel.ReadUncommitted:
236 cmd += "READ UNCOMMITTED"; break;
237 case IsolationLevel.RepeatableRead:
238 cmd += "REPEATABLE READ"; break;
239 case IsolationLevel.Serializable:
240 cmd += "SERIALIZABLE"; break;
241 case IsolationLevel.Chaos:
242 throw new NotSupportedException("Chaos isolation level is not supported");
244 InternalConnection.Driver.SendCommand( DBCmd.QUERY, cmd );
245 InternalConnection.Driver.SendCommand( DBCmd.QUERY, "BEGIN");
252 /// <param name="level"></param>
253 /// <returns></returns>
254 IDbTransaction IDbConnection.BeginTransaction(IsolationLevel level)
256 return BeginTransaction(level);
261 /// Changes the current database for an open MySqlConnection.
263 /// <param name="dbName"></param>
264 public void ChangeDatabase(string dbName)
266 if (state != ConnectionState.Open)
267 throw new MySqlException("Invalid operation: The connection is closed");
269 //TODOinternalConnection.ChangeDatabase( dbName );
270 InternalConnection.Driver.SendCommand( DBCmd.INIT_DB, dbName );
273 internal void SetState( ConnectionState newState )
275 ConnectionState oldState = state;
277 if (this.StateChange != null)
278 StateChange(this, new StateChangeEventArgs( oldState, newState ));
282 /// Opens a database connection with the property settings specified by the ConnectionString.
286 if (state == ConnectionState.Open)
287 throw new MySqlException("error connecting: The connection is already Open (state=Open).");
289 SetState( ConnectionState.Connecting );
291 if (settings.Pooling)
293 internalConnection = MySqlPoolManager.GetConnection( settings );
297 internalConnection = new MySqlInternalConnection( settings );
298 internalConnection.Open();
301 SetState( ConnectionState.Open );
302 internalConnection.SetServerVariables(this);
303 ChangeDatabase( settings.Database );
308 /// Closes the connection to the database. This is the preferred method of closing any open connection.
312 if (state == ConnectionState.Closed) return;
314 if (dataReader != null)
317 if (settings.Pooling)
318 MySqlPoolManager.ReleaseConnection( internalConnection );
320 internalConnection.Close();
322 SetState( ConnectionState.Closed );
325 IDbCommand IDbConnection.CreateCommand()
327 return CreateCommand();
331 /// Creates and returns a MySqlCommand object associated with the MySqlConnection.
333 /// <returns></returns>
334 public MySqlCommand CreateCommand()
336 // Return a new instance of a command object.
337 MySqlCommand c = new MySqlCommand();
343 public object Clone()
345 MySqlConnection clone = new MySqlConnection();
346 clone.ConnectionString = this.ConnectionString;
347 //TODO: how deep should this go?
354 /// Releases the resources used by the MySqlConnection.
356 public new void Dispose()
358 if (State == ConnectionState.Open)