namespace OubHackMS.MySQL
{
    using MySql.Data.MySqlClient;
    using OubHackMS.Core;
    using OubHackMS.Logging;
    using System;
    using System.Data;

    internal class MySQL : LinkedClass
    {
        private string connectionString = "Server=127.0.0.1;Database=oubliette;Uid=oubliette;Pwd=Mh62bX5Jnt76yMKc;Pooling=false";
        private IDbConnection mConnection;

        public void close()
        {
            try
            {
                this.mConnection.Close();
            }
            catch (MySqlException exception)
            {
                base.parent.Log(Level.LOG_CONSOLE_ERROR, new object[] { "MySQL disconnection error! - " + exception.Message });
                base.parent.Log(Level.LOG_FILE_ERROR, new object[] { "MySQL disconnection error! -  " + exception.ToString() });
            }
        }

        private void connect()
        {
            try
            {
                this.mConnection = new MySqlConnection(this.connectionString);
                this.mConnection.Open();
            }
            catch (MySqlException exception)
            {
                switch (exception.Number)
                {
                    case 0:
                        base.parent.Log(Level.LOG_CONSOLE_ERROR, new object[] { "MySQL connection error! - " + exception.Message });
                        base.parent.Log(Level.LOG_FILE_ERROR, new object[] { "MySQL connection error! -  " + exception.ToString() });
                        return;

                    case 0x415:
                        break;

                    default:
                        throw new Exception("MySQL connection failure");
                }
                base.parent.Log(Level.LOG_CONSOLE_ERROR, new object[] { "MySQL auth error! - " + exception.Message });
                base.parent.Log(Level.LOG_FILE_ERROR, new object[] { "MySQL auth error! -  " + exception.ToString() });
            }
            catch (Exception exception2)
            {
                base.parent.Log(Level.LOG_CONSOLE_ERROR, new object[] { "MySQL connection error! - " + exception2.Message });
                base.parent.Log(Level.LOG_FILE_ERROR, new object[] { "MySQL connection error! -  " + exception2.ToString() });
                throw new Exception("MySQL connection could not be established.");
            }
        }

        public void execute(string sQuery)
        {
            this.connect();
            if (this.mConnection != null)
            {
                IDbCommand command = this.mConnection.CreateCommand();
                command.CommandText = sQuery;
                command.ExecuteNonQuery();
            }
            this.close();
        }

        public override void onLink()
        {
            this.connect();
        }

        public IDataReader query(string sQuery)
        {
            this.connect();
            if (this.mConnection != null)
            {
                IDbCommand command = this.mConnection.CreateCommand();
                command.CommandText = sQuery;
                return command.ExecuteReader();
            }
            return null;
        }
    }
}

