using System;
using System.Data.Odbc;

namespace Hitsuji.Dao.DaoEssences
{
    /// <summary>
    /// ODBCドライバを利用したDB処理のコアクラス
    /// </summary>
    public class OdbcDaoEssence : IDaoEssence
    {
        private string sql_;

        private OdbcConnection connection_ ;
        private OdbcTransaction transaction_;
        private OdbcCommand command_ ;
        private OdbcDataAdapter adapter_ ;


        public OdbcDaoEssence()
        {
            connection_ = new OdbcConnection();
            command_ = connection_.CreateCommand();
            command_.CommandType = System.Data.CommandType.Text;
        }

        public void setConnectionText(string text)
        {
            connection_.ConnectionString = text;
        }


        public bool getConnection()
        {
            try
            {
                command_ = connection_.CreateCommand();
                connection_.Open();
                
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool openConnection()
        {
            try
            {
                //接続が閉じている場合は開く
                if (connection_.State != System.Data.ConnectionState.Open)
                {
                    connection_.Open();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool closeConnection()
        {
            try
            {
                //接続が閉じている場合は開く
                if (connection_.State == System.Data.ConnectionState.Open)
                {
                    connection_.Close();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool beginTransaction()
        {
            try
            {
                command_.CommandText = "begin transaction tran1";
                command_.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool commit()
        {
            try
            {
                command_.CommandText = "commit transaction tran1";
                command_.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        public bool rollback()
        {
            try
            {
                command_.CommandText = "rollback transaction tran1";
                command_.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        public void setSql(string sql)
        {
            command_.CommandText = sql;
        }

        public void addParam(string key, string value, OdbcType paramType)
        {
            try
            {
                OdbcParameter param = command_.CreateParameter();
                param.ParameterName = key;
                param.Value = value;
                param.OdbcType = paramType;
                command_.Parameters.Add(param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void addParam(string key, string value)
        {
            try
            {
                OdbcParameter param = command_.CreateParameter();
                param.ParameterName = key;
                param.Value = value;
                command_.Parameters.Add(param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void clearAllParams()
        {
            command_.Parameters.Clear();
        }

        public int postUpdate(object param, bool debugSql = false)
        {
            try
            {
                if (debugSql) System.Diagnostics.Debug.WriteLine(this.sql_);

                return command_.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// SQLを実行し、結果をデータセットで取得する
        /// </summary>
        /// <param name="param"></param>
        /// <param name="debugSql"></param>
        /// <returns></returns>
        public object postSelect(object param, bool debugSql = false)
        {
            try
            {
                if (debugSql) System.Diagnostics.Debug.WriteLine(this.sql_);

                System.Data.DataSet dataSet = new System.Data.DataSet();
                adapter_ = new OdbcDataAdapter(command_);
                adapter_.Fill(dataSet);

                return dataSet;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
