using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;

using Cosmos.Common;

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : MSSQL DB 관련 처리
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Database
{
    public class DatabaseMssql : IDatabaseSQL
    {
        private SqlDB sqlDb = null;

        public bool Begin()
        {
            return sqlDb.DBBeginTransaction();
        }

        public bool Commit()
        {
            return sqlDb.DBTransactionCommit();
        }

        public bool Rollback()
        {
            return sqlDb.DBTransactionRollback();
        }
        
        public void Close()
        {
            sqlDb.DBClose();
        }

        /// <summary>
        /// SQL DataTable를 이용한 Select
        /// <para>return -> 1:OK, -1:Connection 정보 에러, -2:SQL 에러, -3:기타 에러, 0:Select 결과 없음</para>
        /// </summary>
        /// <param name="sQuery"></param>
        /// <param name="outDtReturn"></param>
        /// <returns></returns>
        public int DBSelect(string sQuery, out DataTable outDtReturn)
        {
            int iReturn = -9;
            DataTable dtReturn = null;

            string strCommand = sQuery;
            CommandType sqlCommandType = CommandType.Text;

            //System.Diagnostics.Debug.Print("SQL=" + sQuery);
            iReturn = sqlDb.DBDataTableSelect(strCommand, sqlCommandType, (SqlParameter[])null, out dtReturn);

            outDtReturn = dtReturn;
            return iReturn;
        }

        /// <summary>
        /// return -> 1:OK, -1:Connection 에러, -2:SQL 에러, -3:기타 에러, -4:적용된 Row 없음
        /// </summary>
        /// <param name="sQuery"></param>
        /// <returns></returns>
        public int DBExecuteNonQuery(string sQuery)
        {
            int iReturn = 0;

            CommandType sqlCommandType = CommandType.Text;

            iReturn = sqlDb.DBExecuteNonQuery(sQuery, sqlCommandType, (SqlParameter[])null);

            return iReturn;
        }

        /// <summary>
        /// return -> 1:OK, -1:Connection 에러, -2:SQL 에러, -3:기타 에러, -4:적용된 Row 없음
        /// </summary>
        /// <param name="sQuery"></param>
        /// <returns></returns>
        public int DBExecuteNonQuery(string[] sQuery)
        {
            int iReturn = 0;

            CommandType sqlCommandType = CommandType.Text;

            iReturn = sqlDb.DBExecuteNonQuery(sQuery, sqlCommandType, (SqlParameter[])null);

            return iReturn;
        }

        /// <summary>
        /// return -> 1:OK, -1:Connection 에러, -2:SQL 에러, -3:기타 에러, -4:적용된 Row 없음
        /// </summary>
        /// <param name="sQuery"></param>
        /// <returns></returns>
        public int DBExecuteNonQuery(string[] sQuery, bool bStandAlone)
        {
            int iReturn = 0;

            CommandType sqlCommandType = CommandType.Text;

            iReturn = sqlDb.DBExecuteNonQuery(sQuery, sqlCommandType, (SqlParameter[])null, bStandAlone);

            return iReturn;
        }

        /// <summary>
        /// MSSQL Database Conntction String
        /// </summary>
        /// <param name="pDataSource"></param>
        /// <param name="pInitialCatalog"></param>
        /// <param name="pUID"></param>
        /// <param name="pPassword"></param>
        public bool SetDBConnectionString(string pDataSource, string pInitialCatalog, string pUID, string pPassword)
        {
            bool bReturn = false;

            if(sqlDb != null)
            {
                if (sqlDb.IsDBOpen()) sqlDb.DBClose();
            }

            sqlDb = new SqlDB(pDataSource, pInitialCatalog, pUID, pPassword);

            bReturn = sqlDb.IsDBOpen();

            return bReturn;
        }

    }
}
