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

using System.Data.SQLite;



namespace DatabaseClient
{
    class SqliteClient : DBClient
    {
        SQLiteConnection conn;

        public SqliteClient() { }

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

        public override int ExecNonQuery(string sql)
        {
            int res = 0;
            try
            {
                if (conn!=null)
                {
                    conn.Open();

                    var cmd = new SQLiteCommand(sql, conn);
                    res = cmd.ExecuteNonQuery();

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

        public override DataTable ExecQuery(string sql)
        {
            DataTable dt = null;
            try
            {
                if (conn!=null)
                {
                    conn.Open();

                    var cmd = new SQLiteCommand(sql, conn);
                    SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                    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;
        }
    }
}
