using System;
using System.Data;
using MySql.Data.MySqlClient;
using Holo;
namespace Ion.Storage
{
public static class dataHandling
{
///
/// Converts a DataColumn to an array .
///
/// The DataColumn input.
public static string[] dColToArray(DataColumn dCol)
{
string[] dString = new string[dCol.Table.Rows.Count];
for (int l = 0; l < dString.Length; l++)
dString[l] = Convert.ToString(dCol.Table.Rows[l][0]);
return dString;
}
///
/// Converts a DataColumn to an array .
///
/// The DataColumn input.
/// The output type of the array will become int.
public static int[] dColToArray(DataColumn dCol, object Tick)
{
int[] dInt = new int[dCol.Table.Rows.Count];
for (int l = 0; l < dInt.Length; l++)
dInt[l] = Convert.ToInt32(dCol.Table.Rows[l][0]);
return dInt;
}
}
///
/// 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 isAnonymous
{
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 runQuery(string sQuery)
{
try
{
mCommand.CommandText = sQuery;
mCommand.ExecuteScalar();
mCommand.CommandText = null;
}
catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); }
}
///
/// retrieves a dataset
///
public DataSet getDataSet(string sQuery)
{
DataSet pDataSet = new DataSet();
try
{
mCommand.CommandText = sQuery;
using (MySqlDataAdapter pAdapter = new MySqlDataAdapter(mCommand))
{
pAdapter.Fill(pDataSet);
}
mCommand.CommandText = null;
}
catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); }
return pDataSet;
}
public DataTable getTable(string sQuery)
{
DataTable pDataTable = new DataTable();
try
{
mCommand.CommandText = sQuery;
using (MySqlDataAdapter pAdapter = new MySqlDataAdapter(mCommand))
{
pAdapter.Fill(pDataTable);
}
mCommand.CommandText = null;
}
catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); }
return pDataTable;
}
public DataRow getRow(string sQuery)
{
DataRow dReturn = new DataTable().NewRow();
try
{
mCommand.CommandText = sQuery;
DataSet tmpSet = new DataSet();
using (MySqlDataAdapter pAdapter = new MySqlDataAdapter(mCommand))
{
pAdapter.Fill(tmpSet);
}
dReturn = tmpSet.Tables[0].Rows[0];
}
catch { }
return dReturn;
}
public DataColumn getColumn(string sQuery)
{
DataColumn dReturn = new DataTable().Columns.Add();
try
{
DataSet tmpSet = new DataSet();
mCommand.CommandText = sQuery;
using (MySqlDataAdapter pAdapter = new MySqlDataAdapter(mCommand))
{
pAdapter.Fill(tmpSet);
}
dReturn = tmpSet.Tables[0].Columns[0];
}
catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); }
return dReturn;
}
///
/// method to get a string
///
/// The query
/// A String gotten from the database
public String getString(string sQuery)
{
string pString = "";
try
{
mCommand.CommandText = sQuery;
pString = mCommand.ExecuteScalar().ToString();
mCommand.CommandText = null;
}
catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); }
return pString;
}
///
/// returns an integer
///
/// The query
public Int32 getInt(string sQuery)
{
int i = 0;
try
{
mCommand.CommandText = sQuery;
try
{
bool succes = int.TryParse(mCommand.ExecuteScalar().ToString(), out i);
}
catch { }
}
catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); }
return i;
}
public bool findsResult(string sQuery)
{
bool Found = false;
try
{
mCommand.CommandText = sQuery;
MySqlDataReader dReader = mCommand.ExecuteReader();
Found = dReader.HasRows;
dReader.Close();
}
catch (Exception ex) { Out.WriteError(ex.Message + "\n(^^" + sQuery + "^^)"); }
return Found;
}
#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.isAnonymous == 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
}
}