using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using LoginServer;
public class MySQL
{
    public MySqlConnection mySqlConnection { get; set; }
    public MySqlCommand mySqlCommand { get; set; }
    public MySQL conn;
    public MySQL()
    {
        mySqlConnection = new MySqlConnection();
        mySqlConnection.ConnectionString = string.Format("server={0};port={1} ;user id={2}; password={3}; database={4}; pooling=false", "127.0.0.1", "3306", "root", "", "warock");
    }

    public void ConnectSQL()
    {
        conn = new MySQL();
        bool sqlconn = conn.TestConnect();
        if (!sqlconn)
        {
            new cLog(C.MSG_ERROR, "Error Connecting to the Database");
            Console.Read();
            return;
        }
        else
        {
            new cLog(C.MSG_SUCCESS, "Connected to the Database");
 
        }

    }
    public bool TestConnect()
    {
        try
        {
            OpenConnection();
            CloseConnection();
            return true;
        }
        catch
        {
            return false;
        }
    }
    public void OpenConnection() { mySqlConnection.Open(); }
    public void CloseConnection() { mySqlConnection.Close(); }

    public MySqlCommand CreateCommand(string query)
    {
        MySqlCommand newCommand = new MySqlCommand(query, mySqlConnection);
        return newCommand;
    }

    public MySqlDataReader CreateReader(string query)
    {
        if (mySqlConnection.State != System.Data.ConnectionState.Open)
            OpenConnection();

        MySqlCommand mysqlCmd = new MySqlCommand(query, mySqlConnection);
        MySqlDataReader mysqlReader = mysqlCmd.ExecuteReader();
        return mysqlReader;
    }

    public void ExecuteQuery(string query)
    {
        MySqlCommand set = new MySqlCommand(query, mySqlConnection);

        try
        {
            if (mySqlConnection.State != System.Data.ConnectionState.Open)
                OpenConnection();

            set.ExecuteNonQuery();
        }
        catch (Exception excp)
        {
            Console.WriteLine(excp.Message);
        }
        CloseConnection();
    }
    public bool runSQL(string Syntax)
    {
        try
        {
            MySqlCommand SqlCmd = new MySqlCommand(Syntax, conn.mySqlConnection);
            SqlCmd.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public MySqlCommand SelectCommand(string p)
    {
        throw new NotImplementedException();
    }
}