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 mex) { throw new DatabaseException(string.Concat(new object[] { "Failed to open connection for database client ", this.mHandle, ", exception message: ", mex.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 Found = false; try { this.mCommand.CommandText = sQuery; MySqlDataReader dReader = this.mCommand.ExecuteReader(); Found = dReader.HasRows; dReader.Close(); } catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); } return Found; } public DataColumn getColumn(string sQuery) { DataColumn dReturn = new DataTable().Columns.Add(); try { DataSet tmpSet = new DataSet(); this.mCommand.CommandText = sQuery; using (MySqlDataAdapter pAdapter = new MySqlDataAdapter(this.mCommand)) { pAdapter.Fill(tmpSet); } dReturn = tmpSet.Tables[0].Columns[0]; } catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); } return dReturn; } public DataSet getDataSet(string sQuery) { DataSet pDataSet = new DataSet(); try { this.mCommand.CommandText = sQuery; using (MySqlDataAdapter pAdapter = new MySqlDataAdapter(this.mCommand)) { pAdapter.Fill(pDataSet); } this.mCommand.CommandText = null; } catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); } return pDataSet; } public int getInt(string sQuery) { int i = 0; try { this.mCommand.CommandText = sQuery; try { bool succes = int.TryParse(this.mCommand.ExecuteScalar().ToString(), out i); } catch { } } catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); } return i; } public DatabaseManager GetManager() { return this.mManager; } public DataRow getRow(string sQuery) { DataRow dReturn = new DataTable().NewRow(); try { this.mCommand.CommandText = sQuery; DataSet tmpSet = new DataSet(); using (MySqlDataAdapter pAdapter = new MySqlDataAdapter(this.mCommand)) { pAdapter.Fill(tmpSet); } dReturn = tmpSet.Tables[0].Rows[0]; } catch { } return dReturn; } public string getString(string sQuery) { string pString = ""; try { this.mCommand.CommandText = sQuery; pString = this.mCommand.ExecuteScalar().ToString(); this.mCommand.CommandText = null; } catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); } return pString; } public DataTable getTable(string sQuery) { DataTable pDataTable = new DataTable(); try { this.mCommand.CommandText = sQuery; using (MySqlDataAdapter pAdapter = new MySqlDataAdapter(this.mCommand)) { pAdapter.Fill(pDataTable); } this.mCommand.CommandText = null; } catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); } return pDataTable; } public long insertQuery(string sQuery) { try { this.mCommand.CommandText = sQuery; this.mCommand.ExecuteScalar(); long lastID = this.mCommand.get_LastInsertedId(); this.mCommand.CommandText = null; return lastID; } catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); return 0L; } } public void runQuery(string sQuery) { try { this.mCommand.CommandText = sQuery; this.mCommand.ExecuteScalar(); this.mCommand.CommandText = null; } catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); } } internal string[] runReadRow(string p) { throw new NotImplementedException(); } 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); } } } }