using System; using System.Data; using MySql.Data.MySqlClient; namespace Uber.Storage { /// /// Represents a client of a database, /// public class DatabaseClient : IDisposable { #region Fields private uint mHandle; private DateTime mLastActivity; private DatabaseManager mManager; private MySqlConnection mConnection; private MySqlCommand mCommand; #endregion #region Properties /// /// Gets the handle of this database client. /// public uint Handle { get { return mHandle; } } /// /// Gets whether this database client is anonymous and does not recycle in the database manager. /// public bool Anonymous { get { return (mHandle == 0); } } /// /// Gets the DateTime object representing the date and time this client has been used for the last time. /// public DateTime LastActivity { get { return mLastActivity; } } /// /// Gets the amount of seconds that this client has been inactive. /// public int Inactivity { get { return (int)(DateTime.Now - mLastActivity).TotalSeconds; } } /// /// Gets the state of the connection instance. /// public ConnectionState State { get { return (mConnection != null) ? mConnection.State : ConnectionState.Broken; } } #endregion #region Constructor /// /// Constructs a new database client with a given handle to a given database proxy. /// /// The identifier of this database client as an unsigned 32 bit integer. /// The instance of the DatabaseManager that manages the database proxy of this database client. public DatabaseClient(uint Handle, DatabaseManager pManager) { if (pManager == null) throw new ArgumentNullException("pManager"); mHandle = Handle; mManager = pManager; mConnection = new MySqlConnection(mManager.CreateConnectionString()); mCommand = mConnection.CreateCommand(); UpdateLastActivity(); } #endregion #region Methods /// /// Attempts to open the database connection. /// public void Connect() { if (mConnection == null) throw new DatabaseException("Connection instance of database client " + mHandle + " holds no value."); if (mConnection.State != ConnectionState.Closed) throw new DatabaseException("Connection instance of database client " + mHandle + " requires to be closed before it can open again."); try { mConnection.Open(); } catch (MySqlException mex) { throw new DatabaseException("Failed to open connection for database client " + mHandle + ", exception message: " + mex.Message); } } /// /// Attempts to close the database connection. /// public void Disconnect() { try { mConnection.Close(); } catch { } } /// /// Closes the database connection (if open) and disposes all resources. /// public void Destroy() { Disconnect(); mConnection.Dispose(); mConnection = null; mCommand.Dispose(); mCommand = null; mManager = null; } /// /// Updates the last activity timestamp to the current date and time. /// public void UpdateLastActivity() { mLastActivity = DateTime.Now; } /// /// Returns the DatabaseManager of this database client. /// public DatabaseManager GetManager() { return mManager; } public void AddParamWithValue(string sParam, object val) { mCommand.Parameters.AddWithValue(sParam, val); } public void ExecuteQuery(string sQuery) { mCommand.CommandText = sQuery; mCommand.ExecuteScalar(); mCommand.CommandText = null; } public DataSet ReadDataSet(string sQuery) { DataSet pDataSet = new DataSet(); mCommand.CommandText = sQuery; using (MySqlDataAdapter pAdapter = new MySqlDataAdapter(mCommand)) { pAdapter.Fill(pDataSet); } mCommand.CommandText = null; return pDataSet; } public DataTable ReadDataTable(string sQuery) { DataTable pDataTable = new DataTable(); mCommand.CommandText = sQuery; using (MySqlDataAdapter pAdapter = new MySqlDataAdapter(mCommand)) { pAdapter.Fill(pDataTable); } mCommand.CommandText = null; return pDataTable; } public DataRow ReadDataRow(string sQuery) { DataTable pDataTable = ReadDataTable(sQuery); if (pDataTable != null && pDataTable.Rows.Count > 0) return pDataTable.Rows[0]; return null; } public String ReadString(string sQuery) { mCommand.CommandText = sQuery; String result = mCommand.ExecuteScalar().ToString(); mCommand.CommandText = null; return result; } public Int32 ReadInt32(string Query) { mCommand.CommandText = Query; Int32 result = Int32.Parse(mCommand.ExecuteScalar().ToString()); mCommand.CommandText = null; return result; } public UInt32 ReadUInt32(string sQuery) { mCommand.CommandText = sQuery; UInt32 result = (UInt32)mCommand.ExecuteScalar(); mCommand.CommandText = null; return result; } #region IDisposable members /// /// Returns the DatabaseClient to the DatabaseManager, where the connection will stay alive for 30 seconds of inactivity. /// public void Dispose() { if (this.Anonymous == false) // No disposing for this client yet! Return to the manager! { // Reset this! mCommand.CommandText = null; mCommand.Parameters.Clear(); mManager.ReleaseClient(mHandle); } else // Anonymous client, dispose this right away! { Destroy(); } } #endregion #endregion } }