-// ByteFX.Data data access components for .Net
-// Copyright (C) 2002-2003 ByteFX, Inc.
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License, or (at your option) any later version.
-//
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
-using System;
-using System.Data;
-using System.Collections.Specialized;
-using System.Text;
-using System.ComponentModel;
-using System.Globalization;
-using ByteFX.Data.Common;
-
-namespace ByteFX.Data.MySqlClient
-{
- /// <summary>
- /// Represents an open connection to a MySQL Server database. This class cannot be inherited.
- /// </summary>
- /// <include file='docs/MySqlConnection.xml' path='MyDocs/MyMembers[@name="Class"]/*'/>
- [System.Drawing.ToolboxBitmap( typeof(MySqlConnection), "Designers.connection.bmp")]
- [System.ComponentModel.DesignerCategory("Code")]
- [ToolboxItem(true)]
- public sealed class MySqlConnection : Component, IDbConnection, ICloneable
- {
- internal ConnectionState state;
- private MySqlInternalConnection internalConnection;
- private MySqlDataReader dataReader;
- private NumberFormatInfo numberFormat;
- private MySqlConnectionString settings;
-
- public event StateChangeEventHandler StateChange;
-
-
- /// <summary>
- /// Creates a new connection
- /// </summary>
- public MySqlConnection()
- {
- settings = new MySqlConnectionString();
- }
-
- /// <summary>
- /// Creates a new connection
- /// </summary>
- /// <param name="container"></param>
- public MySqlConnection(System.ComponentModel.IContainer container)
- {
- settings = new MySqlConnectionString();
- }
-
-
- // Have a constructor that takes a connection string.
- /// <summary>
- /// Creates a new connection using the specified connection string.
- /// </summary>
- /// <param name="connectString"></param>
- public MySqlConnection(string connectString)
- {
- settings = new MySqlConnectionString(connectString);
- }
-
- /// <summary>
- /// Gets the name of the MySQL server to which to connect.
- /// </summary>
- #region Properties
- [Browsable(true)]
- public string DataSource
- {
- get { return settings.Host; }
- }
-
- /// <summary>
- /// Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.
- /// </summary>
- /// <include file='docs/MySqlConnection.xml' path='MyDocs/MyMembers[@name="ConnectionTimeout"]/*'/>
- [Browsable(true)]
- public int ConnectionTimeout
- {
- get { return settings.ConnectTimeout; }
- }
-
- /// <summary>
- /// Gets the name of the current database or the database to be used after a connection is opened.
- /// </summary>
- [Browsable(true)]
- public string Database
- {
- get { return settings.Database; }
- }
-
- /// <summary>
- /// Indicates if this connection should use compression when communicating with the server.
- /// </summary>
- [Browsable(false)]
- public bool UseCompression
- {
- get { return settings.UseCompression; }
- }
-
- /// <summary>
- /// Gets the current state of the connection.
- /// </summary>
- [Browsable(false)]
- public ConnectionState State
- {
- get { return state; }
- }
-
- internal MySqlDataReader Reader
- {
- get { return dataReader; }
- set { dataReader = value; }
- }
-
- internal MySqlInternalConnection InternalConnection
- {
- get { return internalConnection; }
- }
-
- internal NumberFormatInfo NumberFormat
- {
- get
- {
- if (numberFormat == null)
- {
- numberFormat = new NumberFormatInfo();
- numberFormat = (NumberFormatInfo)NumberFormatInfo.InvariantInfo.Clone();
- numberFormat.NumberDecimalSeparator = ".";
- }
- return numberFormat;
- }
- }
-
- /// <summary>
- /// Gets a string containing the version of the MySQL server to which the client is connected.
- /// </summary>
- [Browsable(false)]
- public string ServerVersion
- {
- get { return ""; } //internalConnection.GetServerVersion(); }
- }
-
- internal Encoding Encoding
- {
- get
- {
-//TODO if (encoding == null)
- return System.Text.Encoding.Default;
-// else
-// return encoding;
- }
- }
-
-
- /// <summary>
- /// Gets or sets the string used to connect to a MySQL Server database.
- /// </summary>
-#if WINDOWS
- [Editor(typeof(Designers.ConnectionStringEditor), typeof(System.Drawing.Design.UITypeEditor))]
-#endif
- [Browsable(true)]
- [Category("Data")]
- public string ConnectionString
- {
- get
- {
- // Always return exactly what the user set.
- // Security-sensitive information may be removed.
- return settings.ConnectString;
- }
- set
- {
- settings.ConnectString = value;
- if (internalConnection != null)
- internalConnection.Settings = settings;
- }
- }
-
- #endregion
-
- #region Transactions
- /// <summary>
- /// Begins a database transaction.
- /// </summary>
- /// <returns></returns>
- public MySqlTransaction BeginTransaction()
- {
- if (state != ConnectionState.Open)
- throw new MySqlException("Invalid operation: The connection is closed");
-
- MySqlTransaction t = new MySqlTransaction();
- t.Connection = this;
- InternalConnection.Driver.SendCommand( DBCmd.QUERY, "BEGIN");
- return t;
- }
-
- /// <summary>
- ///
- /// </summary>
- IDbTransaction IDbConnection.BeginTransaction()
- {
- return BeginTransaction();
- }
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="level"></param>
- /// <returns></returns>
- public MySqlTransaction BeginTransaction(IsolationLevel level)
- {
- if (state != ConnectionState.Open)
- throw new MySqlException("Invalid operation: The connection is closed");
-
- MySqlTransaction t = new MySqlTransaction();
- t.Connection = this;
- t.IsolationLevel = level;
- string cmd = "SET SESSION TRANSACTION ISOLATION LEVEL ";
- switch (level)
- {
- case IsolationLevel.ReadCommitted:
- cmd += "READ COMMITTED"; break;
- case IsolationLevel.ReadUncommitted:
- cmd += "READ UNCOMMITTED"; break;
- case IsolationLevel.RepeatableRead:
- cmd += "REPEATABLE READ"; break;
- case IsolationLevel.Serializable:
- cmd += "SERIALIZABLE"; break;
- case IsolationLevel.Chaos:
- throw new NotSupportedException("Chaos isolation level is not supported");
- }
- InternalConnection.Driver.SendCommand( DBCmd.QUERY, cmd );
- InternalConnection.Driver.SendCommand( DBCmd.QUERY, "BEGIN");
- return t;
- }
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="level"></param>
- /// <returns></returns>
- IDbTransaction IDbConnection.BeginTransaction(IsolationLevel level)
- {
- return BeginTransaction(level);
- }
- #endregion
-
- /// <summary>
- /// Changes the current database for an open MySqlConnection.
- /// </summary>
- /// <param name="dbName"></param>
- public void ChangeDatabase(string dbName)
- {
- if (state != ConnectionState.Open)
- throw new MySqlException("Invalid operation: The connection is closed");
-
- //TODOinternalConnection.ChangeDatabase( dbName );
- InternalConnection.Driver.SendCommand( DBCmd.INIT_DB, dbName );
- }
-
- internal void SetState( ConnectionState newState )
- {
- ConnectionState oldState = state;
- state = newState;
- if (this.StateChange != null)
- StateChange(this, new StateChangeEventArgs( oldState, newState ));
- }
-
- /// <summary>
- /// Opens a database connection with the property settings specified by the ConnectionString.
- /// </summary>
- public void Open()
- {
- if (state == ConnectionState.Open)
- throw new MySqlException("error connecting: The connection is already Open (state=Open).");
-
- SetState( ConnectionState.Connecting );
-
- if (settings.Pooling)
- {
- internalConnection = MySqlPoolManager.GetConnection( settings );
- }
- else
- {
- internalConnection = new MySqlInternalConnection( settings );
- internalConnection.Open();
- }
-
- SetState( ConnectionState.Open );
- internalConnection.SetServerVariables(this);
- ChangeDatabase( settings.Database );
- }
-
-
- /// <summary>
- /// Closes the connection to the database. This is the preferred method of closing any open connection.
- /// </summary>
- public void Close()
- {
- if (state == ConnectionState.Closed) return;
-
- if (dataReader != null)
- dataReader.Close();
-
- if (settings.Pooling)
- MySqlPoolManager.ReleaseConnection( internalConnection );
- else
- internalConnection.Close();
-
- SetState( ConnectionState.Closed );
- }
-
- IDbCommand IDbConnection.CreateCommand()
- {
- return CreateCommand();
- }
-
- /// <summary>
- /// Creates and returns a MySqlCommand object associated with the MySqlConnection.
- /// </summary>
- /// <returns></returns>
- public MySqlCommand CreateCommand()
- {
- // Return a new instance of a command object.
- MySqlCommand c = new MySqlCommand();
- c.Connection = this;
- return c;
- }
-
- #region ICloneable
- public object Clone()
- {
- MySqlConnection clone = new MySqlConnection();
- clone.ConnectionString = this.ConnectionString;
- //TODO: how deep should this go?
- return clone;
- }
- #endregion
-
- #region IDisposeable
- /// <summary>
- /// Releases the resources used by the MySqlConnection.
- /// </summary>
- public new void Dispose()
- {
- if (State == ConnectionState.Open)
- Close();
- base.Dispose();
- }
- #endregion
- }
-}
+// ByteFX.Data data access components for .Net\r
+// Copyright (C) 2002-2003 ByteFX, Inc.\r
+//\r
+// This library is free software; you can redistribute it and/or\r
+// modify it under the terms of the GNU Lesser General Public\r
+// License as published by the Free Software Foundation; either\r
+// version 2.1 of the License, or (at your option) any later version.\r
+// \r
+// This library is distributed in the hope that it will be useful,\r
+// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+// Lesser General Public License for more details.\r
+// \r
+// You should have received a copy of the GNU Lesser General Public\r
+// License along with this library; if not, write to the Free Software\r
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
+\r
+using System;\r
+using System.Data;\r
+using System.Collections.Specialized;\r
+using System.Text;\r
+using System.ComponentModel;\r
+using System.Globalization;\r
+using ByteFX.Data.Common;\r
+\r
+namespace ByteFX.Data.MySqlClient\r
+{\r
+ /// <summary>\r
+ /// Represents an open connection to a MySQL Server database. This class cannot be inherited.\r
+ /// </summary>\r
+ /// <include file='docs/MySqlConnection.xml' path='MyDocs/MyMembers[@name="Class"]/*'/>\r
+ [System.Drawing.ToolboxBitmap( typeof(MySqlConnection), "MySqlClient.resources.connection.bmp")]\r
+ [System.ComponentModel.DesignerCategory("Code")]\r
+ [ToolboxItem(true)]\r
+ public sealed class MySqlConnection : Component, IDbConnection, ICloneable\r
+ {\r
+ internal ConnectionState state;\r
+ private MySqlInternalConnection internalConnection;\r
+ private MySqlDataReader dataReader;\r
+ private NumberFormatInfo numberFormat;\r
+ private MySqlConnectionString settings;\r
+\r
+ /// <summary>\r
+ /// Occurs when the state of the connection changes.\r
+ /// </summary>\r
+ public event StateChangeEventHandler StateChange;\r
+\r
+\r
+ /// <summary>\r
+ /// Creates a new connection\r
+ /// </summary>\r
+ public MySqlConnection()\r
+ {\r
+ settings = new MySqlConnectionString("server=localhost");\r
+ }\r
+\r
+ /// <summary>\r
+ /// Creates a new connection\r
+ /// </summary>\r
+ /// <param name="container"></param>\r
+ public MySqlConnection(System.ComponentModel.IContainer container)\r
+ {\r
+ settings = new MySqlConnectionString();\r
+ }\r
+ \r
+\r
+ // Have a constructor that takes a connection string.\r
+ /// <summary>\r
+ /// Creates a new connection using the specified connection string.\r
+ /// </summary>\r
+ /// <param name="connectString"></param>\r
+ public MySqlConnection(string connectString)\r
+ {\r
+ settings = new MySqlConnectionString(connectString);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Gets the name of the MySQL server to which to connect.\r
+ /// </summary>\r
+ #region Properties\r
+ [Browsable(true)]\r
+ public string DataSource\r
+ {\r
+ get { return settings.Server; }\r
+ }\r
+\r
+ /// <summary>\r
+ /// Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.\r
+ /// </summary>\r
+ /// <include file='docs/MySqlConnection.xml' path='MyDocs/MyMembers[@name="ConnectionTimeout"]/*'/>\r
+ [Browsable(true)]\r
+ public int ConnectionTimeout\r
+ {\r
+ get { return settings.ConnectionTimeout; }\r
+ }\r
+ \r
+ /// <summary>\r
+ /// Gets the name of the current database or the database to be used after a connection is opened.\r
+ /// </summary>\r
+ [Browsable(true)]\r
+ public string Database\r
+ {\r
+ get { return settings.Database; }\r
+ }\r
+\r
+ /// <summary>\r
+ /// Indicates if this connection should use compression when communicating with the server.\r
+ /// </summary>\r
+ [Browsable(false)]\r
+ public bool UseCompression\r
+ {\r
+ get { return settings.UseCompression; }\r
+ }\r
+ \r
+ /// <summary>\r
+ /// Gets the current state of the connection.\r
+ /// </summary>\r
+ [Browsable(false)]\r
+ public ConnectionState State\r
+ {\r
+ get { return state; }\r
+ }\r
+\r
+ internal MySqlDataReader Reader\r
+ {\r
+ get { return dataReader; }\r
+ set { dataReader = value; }\r
+ }\r
+\r
+ internal MySqlInternalConnection InternalConnection\r
+ {\r
+ get { return internalConnection; }\r
+ }\r
+\r
+ internal NumberFormatInfo NumberFormat\r
+ {\r
+ get \r
+ {\r
+ if (numberFormat == null)\r
+ {\r
+ numberFormat = new NumberFormatInfo();\r
+ numberFormat = (NumberFormatInfo)NumberFormatInfo.InvariantInfo.Clone();\r
+ numberFormat.NumberDecimalSeparator = ".";\r
+ }\r
+ return numberFormat;\r
+ }\r
+ }\r
+\r
+ /// <summary>\r
+ /// Gets a string containing the version of the MySQL server to which the client is connected.\r
+ /// </summary>\r
+ [Browsable(false)]\r
+ public string ServerVersion \r
+ {\r
+ get { return internalConnection.Driver.VersionString; }\r
+ }\r
+\r
+ internal Encoding Encoding \r
+ {\r
+ get \r
+ {\r
+ if (internalConnection == null)\r
+ return System.Text.Encoding.Default;\r
+ else \r
+ return internalConnection.Driver.Encoding;\r
+ }\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Gets or sets the string used to connect to a MySQL Server database.\r
+ /// </summary>\r
+ /// <include file='docs/MySqlConnection.xml' path='MyDocs/MyMembers[@name="ConnectionString"]/*'/>\r
+#if WINDOWS\r
+ [Editor("ByteFX.Data.MySqlClient.Design.ConnectionStringTypeEditor,MySqlClient.Design", typeof(System.Drawing.Design.UITypeEditor))]\r
+#endif\r
+ [Browsable(true)]\r
+ [Category("Data")]\r
+ [Description("Information used to connect to a DataSource, such as 'Server=xxx;UserId=yyy;Password=zzz;Database=dbdb'.")]\r
+ public string ConnectionString\r
+ {\r
+ get\r
+ {\r
+ // Always return exactly what the user set.\r
+ // Security-sensitive information may be removed.\r
+ return settings.GetConnectionString();\r
+ }\r
+ set\r
+ {\r
+ if (this.State != ConnectionState.Closed)\r
+ throw new MySqlException("Not allowed to change the 'ConnectionString' property while the connection (state=" + State + ").");\r
+\r
+ settings.SetConnectionString(value);\r
+ if (internalConnection != null)\r
+ internalConnection.Settings = settings;\r
+ }\r
+ }\r
+\r
+ #endregion\r
+\r
+ #region Transactions\r
+ /// <summary>\r
+ /// Begins a database transaction.\r
+ /// </summary>\r
+ /// <returns></returns>\r
+ public MySqlTransaction BeginTransaction()\r
+ {\r
+ if (state != ConnectionState.Open)\r
+ throw new MySqlException("Invalid operation: The connection is closed");\r
+\r
+ MySqlTransaction t = new MySqlTransaction();\r
+ t.Connection = this;\r
+ InternalConnection.Driver.Send( DBCmd.QUERY, "BEGIN");\r
+ return t;\r
+ }\r
+\r
+ /// <summary>\r
+ /// \r
+ /// </summary>\r
+ IDbTransaction IDbConnection.BeginTransaction()\r
+ {\r
+ return BeginTransaction();\r
+ }\r
+\r
+ /// <summary>\r
+ /// \r
+ /// </summary>\r
+ /// <param name="level"></param>\r
+ /// <returns></returns>\r
+ public MySqlTransaction BeginTransaction(IsolationLevel level)\r
+ {\r
+ if (state != ConnectionState.Open)\r
+ throw new MySqlException("Invalid operation: The connection is closed");\r
+\r
+ MySqlTransaction t = new MySqlTransaction();\r
+ t.Connection = this;\r
+ t.IsolationLevel = level;\r
+ string cmd = "SET SESSION TRANSACTION ISOLATION LEVEL ";\r
+ switch (level) \r
+ {\r
+ case IsolationLevel.ReadCommitted:\r
+ cmd += "READ COMMITTED"; break;\r
+ case IsolationLevel.ReadUncommitted:\r
+ cmd += "READ UNCOMMITTED"; break;\r
+ case IsolationLevel.RepeatableRead:\r
+ cmd += "REPEATABLE READ"; break;\r
+ case IsolationLevel.Serializable:\r
+ cmd += "SERIALIZABLE"; break;\r
+ case IsolationLevel.Chaos:\r
+ throw new NotSupportedException("Chaos isolation level is not supported");\r
+ }\r
+ InternalConnection.Driver.Send( DBCmd.QUERY, cmd );\r
+ InternalConnection.Driver.Send( DBCmd.QUERY, "BEGIN");\r
+ return t;\r
+ }\r
+\r
+ /// <summary>\r
+ /// \r
+ /// </summary>\r
+ /// <param name="level"></param>\r
+ /// <returns></returns>\r
+ IDbTransaction IDbConnection.BeginTransaction(IsolationLevel level)\r
+ {\r
+ return BeginTransaction(level);\r
+ }\r
+ #endregion\r
+\r
+ /// <summary>\r
+ /// Changes the current database for an open MySqlConnection.\r
+ /// </summary>\r
+ /// <param name="dbName"></param>\r
+ public void ChangeDatabase(string dbName)\r
+ {\r
+ if (state != ConnectionState.Open)\r
+ throw new MySqlException("Invalid operation: The connection is closed");\r
+\r
+ //TODOinternalConnection.ChangeDatabase( dbName );\r
+ InternalConnection.Driver.Send( DBCmd.INIT_DB, dbName );\r
+ }\r
+\r
+ internal void SetState( ConnectionState newState ) \r
+ {\r
+ ConnectionState oldState = state;\r
+ state = newState;\r
+ if (this.StateChange != null)\r
+ StateChange(this, new StateChangeEventArgs( oldState, newState ));\r
+ }\r
+\r
+ /// <summary>\r
+ /// Opens a database connection with the property settings specified by the ConnectionString.\r
+ /// </summary>\r
+ public void Open()\r
+ {\r
+ if (state == ConnectionState.Open)\r
+ throw new MySqlException("error connecting: The connection is already Open (state=Open).");\r
+\r
+ SetState( ConnectionState.Connecting );\r
+\r
+ try \r
+ {\r
+ if (settings.Pooling) \r
+ {\r
+ internalConnection = MySqlPoolManager.GetConnection( settings );\r
+ }\r
+ else\r
+ {\r
+ internalConnection = new MySqlInternalConnection( settings );\r
+ internalConnection.Open();\r
+ }\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ SetState( ConnectionState.Closed );\r
+ throw ex;\r
+ }\r
+\r
+\r
+ SetState( ConnectionState.Open );\r
+ internalConnection.SetServerVariables(this);\r
+ if (settings.Database != null && settings.Database != String.Empty)\r
+ ChangeDatabase( settings.Database );\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Closes the connection to the database. This is the preferred method of closing any open connection.\r
+ /// </summary>\r
+ public void Close()\r
+ {\r
+ if (state == ConnectionState.Closed) return;\r
+\r
+ if (dataReader != null)\r
+ dataReader.Close();\r
+\r
+ if (settings.Pooling)\r
+ MySqlPoolManager.ReleaseConnection( internalConnection );\r
+ else\r
+ internalConnection.Close();\r
+\r
+ SetState( ConnectionState.Closed );\r
+ }\r
+\r
+ IDbCommand IDbConnection.CreateCommand()\r
+ {\r
+ return CreateCommand();\r
+ }\r
+\r
+ /// <summary>\r
+ /// Creates and returns a MySqlCommand object associated with the MySqlConnection.\r
+ /// </summary>\r
+ /// <returns></returns>\r
+ public MySqlCommand CreateCommand()\r
+ {\r
+ // Return a new instance of a command object.\r
+ MySqlCommand c = new MySqlCommand();\r
+ c.Connection = this;\r
+ return c;\r
+ }\r
+\r
+ #region ICloneable\r
+ /// <summary>\r
+ /// Creates a new MySqlConnection object with the exact same ConnectionString value\r
+ /// </summary>\r
+ /// <returns>A cloned MySqlConnection object</returns>\r
+ object ICloneable.Clone()\r
+ {\r
+ MySqlConnection clone = new MySqlConnection();\r
+ clone.ConnectionString = this.ConnectionString;\r
+ //TODO: how deep should this go?\r
+ return clone;\r
+ }\r
+ #endregion\r
+\r
+ #region IDisposeable\r
+ /// <summary>\r
+ /// Releases the resources used by the MySqlConnection.\r
+ /// </summary>\r
+ public new void Dispose() \r
+ {\r
+ if (State == ConnectionState.Open)\r
+ Close();\r
+ base.Dispose();\r
+ }\r
+ #endregion\r
+ }\r
+}\r