namespace Ion.Storage { using Holo; using MySql.Data.MySqlClient; using System; using System.Data; public class DatabaseClient : IDisposable { private MySqlCommand mCommand; private MySqlConnection mConnection; private uint mHandle; private DateTime mLastActivity; private DatabaseManager mManager; public DatabaseClient(uint Handle, DatabaseManager pManager) { if (pManager == null) { throw new ArgumentNullException("pManager"); } this.mHandle = Handle; this.mManager = pManager; this.mConnection = new MySqlConnection(this.mManager.CreateConnectionString()); this.mCommand = this.mConnection.CreateCommand(); this.UpdateLastActivity(); } public void AddParamWithValue(string sParam, object val) { this.mCommand.get_Parameters().AddWithValue(sParam, val); } public void Connect() { if (this.mConnection == null) { throw new DatabaseException("Connection instance of database client " + this.mHandle + " holds no value."); } if (this.mConnection.State != ConnectionState.Closed) { throw new DatabaseException("Connection instance of database client " + this.mHandle + " requires to be closed before it can open again."); } try { this.mConnection.Open(); } catch (MySqlException exception) { throw new DatabaseException(string.Concat(new object[] { "Failed to open connection for database client ", this.mHandle, ", exception message: ", exception.Message })); } } public void Destroy() { this.Disconnect(); this.mConnection.Dispose(); this.mConnection = null; this.mCommand.Dispose(); this.mCommand = null; this.mManager = null; } public void Disconnect() { try { this.mConnection.Close(); } catch { } } public void Dispose() { if (!this.isAnonymous) { this.mCommand.CommandText = null; this.mCommand.get_Parameters().Clear(); this.mManager.ReleaseClient(this.mHandle); } else { this.Destroy(); } } public bool findsResult(string sQuery) { bool hasRows = false; try { this.mCommand.CommandText = sQuery; MySqlDataReader reader = this.mCommand.ExecuteReader(); hasRows = reader.HasRows; reader.Close(); } catch (Exception exception) { Out.WriteError(exception.Message + "\n(^^" + sQuery + "^^)"); } return hasRows; } public DataColumn getColumn(string sQuery) { DataColumn column = new DataTable().Columns.Add(); try { DataSet dataSet = new DataSet(); this.mCommand.CommandText = sQuery; using (MySqlDataAdapter adapter = new MySqlDataAdapter(this.mCommand)) { adapter.Fill(dataSet); } column = dataSet.Tables[0].Columns[0]; } catch (Exception exception) { Out.WriteError(exception.Message + "\n(^^" + sQuery + "^^)"); } return column; } public DataSet getDataSet(string sQuery) { DataSet dataSet = new DataSet(); try { this.mCommand.CommandText = sQuery; using (MySqlDataAdapter adapter = new MySqlDataAdapter(this.mCommand)) { adapter.Fill(dataSet); } this.mCommand.CommandText = null; } catch (Exception exception) { Out.WriteError(exception.Message + "\n(^^" + sQuery + "^^)"); } return dataSet; } public int getInt(string sQuery) { int result = 0; try { this.mCommand.CommandText = sQuery; try { bool flag = int.TryParse(this.mCommand.ExecuteScalar().ToString(), out result); } catch { } } catch (Exception exception) { Out.WriteError(exception.Message + "\n(^^" + sQuery + "^^)"); } return result; } public DatabaseManager GetManager() { return this.mManager; } public DataRow getRow(string sQuery) { DataRow row = new DataTable().NewRow(); try { this.mCommand.CommandText = sQuery; DataSet dataSet = new DataSet(); using (MySqlDataAdapter adapter = new MySqlDataAdapter(this.mCommand)) { adapter.Fill(dataSet); } row = dataSet.Tables[0].Rows[0]; } catch { } return row; } public string getString(string sQuery) { string str = ""; try { this.mCommand.CommandText = sQuery; str = this.mCommand.ExecuteScalar().ToString(); this.mCommand.CommandText = null; } catch (Exception exception) { Out.WriteError(exception.Message + "\n(^^" + sQuery + "^^)"); } return str; } public DataTable getTable(string sQuery) { DataTable dataTable = new DataTable(); try { this.mCommand.CommandText = sQuery; using (MySqlDataAdapter adapter = new MySqlDataAdapter(this.mCommand)) { adapter.Fill(dataTable); } this.mCommand.CommandText = null; } catch (Exception exception) { Out.WriteError(exception.Message + "\n(^^" + sQuery + "^^)"); } return dataTable; } public void runQuery(string sQuery) { try { this.mCommand.CommandText = sQuery; this.mCommand.ExecuteScalar(); this.mCommand.CommandText = null; } catch (Exception exception) { Out.WriteError(exception.Message + "\n(^^" + sQuery + "^^)"); } } public void UpdateLastActivity() { this.mLastActivity = DateTime.Now; } public uint Handle { get { return this.mHandle; } } public int Inactivity { get { TimeSpan span = (TimeSpan) (DateTime.Now - this.mLastActivity); return (int) span.TotalSeconds; } } public bool isAnonymous { get { return (this.mHandle == 0); } } public DateTime lastActivity { get { return this.mLastActivity; } } public ConnectionState State { get { return ((this.mConnection != null) ? this.mConnection.State : ConnectionState.Broken); } } } }