using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseClient
{
   public class MysqlClient : DBClient
    {
        MySqlConnection conn;

        public MysqlClient() { }

        public override void Connect(string connString)
        {
            try
            {
                conn = new MySqlConnection(connString);
            }
            catch (Exception err)
            {
                GlobalVar.log.Error(err.Message);
            }
        }

        public override DataTable ExecQuery(string sql)
        {
            DataTable dt = null;
            try
            {
                if (conn != null)
                {
                    conn.Open();
                    
                    MySqlCommand command = new MySqlCommand(sql, conn);
                    MySqlDataAdapter da = new MySqlDataAdapter(command);
                    DataSet ds = new DataSet();
                    da.Fill(ds);

                    conn.Close();

                    if (ds.Tables.Count > 0)
                    {
                        dt = ds.Tables[0];
                    }

                }

            }
            catch (Exception err)
            {
                GlobalVar.log.Error(err.Message);
            }
            return dt;
        }

        public override int ExecNonQuery(string sql)
        {
            int res = 0;

            try
            {
                if (conn != null)
                {
                    conn.Open();

                    MySqlCommand command = new MySqlCommand(sql, conn);
                    res = command.ExecuteNonQuery();

                    conn.Close();
                }
            }
            catch (Exception err)
            {
                GlobalVar.log.Error(err.Message);
            }

            return res;
        }

        public static bool ExecQueryDataTable(string sql,string ConnectStr,out DataTable dt, out string err)
        {
            bool success = false;
            err = "";
            dt = null;
            using (MySqlConnection conn = new MySqlConnection(ConnectStr))
            {
                try
                {
                    conn.Open();

                    MySqlCommand command = new MySqlCommand(sql, conn);
                    MySqlDataAdapter da = new MySqlDataAdapter(command);
                    DataSet ds = new DataSet();
                    da.Fill(ds);

                    if (ds.Tables.Count > 0)
                    {
                        dt = ds.Tables[0];
                    }
                    success = true;
                }
                catch (Exception er)
                {
                    err = er.Message;
                }
                finally
                {
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }

            }
           
            return success;
        }



    }
}
