namespace Holo.OldDatabase { using Holo; using MySql.Data.MySqlClient; using System; using System.Data; using System.IO; using System.Text; public class Database { private bool _Ready = false; public bool closeAfterNextQuery; private MySqlCommand Command = new MySqlCommand(); private MySqlConnection Connection; public static string connectionString = string.Concat(new object[] { "Server=", Eucalypt.dbHost, ";Uid=", Eucalypt.dbUsername, ";Pwd=", Eucalypt.dbPassword, ";Database=", Eucalypt.dbName, ";Port=", Eucalypt.dbPort, ";Pooling=false" }); private MySqlDataAdapter dataAdapter = new MySqlDataAdapter(); public Database(bool openNow, bool closeAfterFirstQuery, int ID) { if (openNow) { this.Open(); } this.closeAfterNextQuery = closeAfterFirstQuery; } public void addParameterWithValue(string Parameter, object Value) { this.Command.get_Parameters().AddWithValue("@" + Parameter, Value); } public void addRawParameter(MySqlParameter Parameter) { this.Command.get_Parameters().Add(Parameter); } public void clearParameters() { this.Command.get_Parameters().Clear(); } public void Close() { if (this._Ready) { this.Connection.Close(); this.Connection = null; this.dataAdapter = null; this.Command = null; this.closeAfterNextQuery = false; this._Ready = false; } } public bool findsResult(string Query) { bool hasRows = false; if (this._Ready) { try { this.Command.CommandText = Query; MySqlDataReader reader = this.Command.ExecuteReader(); hasRows = reader.HasRows; reader.Close(); } catch (Exception exception) { WriteError(exception.Message + "\n(^^" + Query + "^^)"); } if (this.closeAfterNextQuery) { this.Close(); } return hasRows; } Out.WriteError("Database connection not active"); return hasRows; } public DataColumn getColumn(string Query) { DataColumn column = new DataTable().Columns.Add(); if (this._Ready) { try { DataSet dataSet = new DataSet(); this.Command.CommandText = Query; this.dataAdapter.Fill(dataSet); column = dataSet.Tables[0].Columns[0]; } catch { } if (this.closeAfterNextQuery) { this.Close(); } return column; } Out.WriteError("Database connection not active"); return column; } public DataSet getDataSet(string Query) { DataSet dataSet = new DataSet(); if (this._Ready) { try { this.Command.CommandText = Query; this.dataAdapter.Fill(dataSet); } catch (Exception exception) { WriteError(exception.Message + "\n(^^" + Query + "^^)"); } if (this.closeAfterNextQuery) { this.Close(); } return dataSet; } Out.WriteError("Database connection not active"); return dataSet; } public int getInteger(string Query) { int num = 0; if (this._Ready) { try { this.Command.CommandText = Query; num = int.Parse(this.Command.ExecuteScalar().ToString()); } catch { } if (this.closeAfterNextQuery) { this.Close(); } return num; } return num; } public DataRow getRow(string Query) { DataRow row = new DataTable().NewRow(); if (this._Ready) { try { DataSet dataSet = new DataSet(); this.Command.CommandText = Query; this.dataAdapter.Fill(dataSet); row = dataSet.Tables[0].Rows[0]; } catch { } if (this.closeAfterNextQuery) { this.Close(); } return row; } Out.WriteError("Database connection not active"); return row; } public string getString(string Query) { string str = ""; if (this._Ready) { try { this.Command.CommandText = Query; str = this.Command.ExecuteScalar().ToString(); } catch { } if (this.closeAfterNextQuery) { this.Close(); } return str; } Out.WriteError("Database connection not active"); return ""; } public DataTable getTable(string Query) { DataTable dataTable = new DataTable(); if (this._Ready) { try { this.Command.CommandText = Query; this.dataAdapter.Fill(dataTable); } catch (Exception exception) { WriteError(exception.Message + "\n(^^" + Query + "^^)"); } if (this.closeAfterNextQuery) { this.Close(); } return dataTable; } Out.WriteError("Database connection not active"); return dataTable; } public DataTable getTable(string Query, object Quiet) { DataTable dataTable = new DataTable(); if (this._Ready) { try { this.Command.CommandText = Query; this.dataAdapter.Fill(dataTable); } catch { } if (this.closeAfterNextQuery) { this.Close(); } return dataTable; } Out.WriteError("Database connection not active"); return dataTable; } public void Open() { try { this.Connection = new MySqlConnection(connectionString); this.Connection.Open(); this.Command.set_Connection(this.Connection); this.dataAdapter.set_SelectCommand(this.Command); this._Ready = true; } catch (MySqlException exception) { WriteError(exception.Message); } } public void runQuery(string Query) { if (this._Ready) { try { this.Command.CommandText = Query; this.Command.ExecuteNonQuery(); } catch (Exception exception) { WriteError(exception.Message + "\n(^^" + Query + "^^)"); } if (this.closeAfterNextQuery) { this.Close(); } } else { Out.WriteError("Database connection not active"); } } public string Stripslash(string Query) { try { return Query.Replace(@"\", @"\").Replace("'", @"\'"); } catch { return ""; } } public static void WriteError(string Message) { try { FileStream stream = new FileStream("DB.err", FileMode.Append, FileAccess.Write); byte[] bytes = Encoding.ASCII.GetBytes(Message + "\r\n"); stream.Write(bytes, 0, bytes.Length); } catch { Message = "FAILED TO SAVE ERROR TO FILE: " + Message; } Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.Write("[DATABASE ERROR] => "); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine(Message); Console.ForegroundColor = ConsoleColor.Gray; } public bool Ready { get { return this._Ready; } } } }