//---------------------------------------------------------------------------

#pragma hdrstop

#include "CDSFcltF.h"
#include "ITS_OPLibF.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)


/*
* CDSFclt Class
*/
TCDSFclt::TCDSFclt()
{
}
//---------------------------------------------------------------------------
TCDSFclt::~TCDSFclt()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TCDSFcltManager *CDSFcltManager = NULL;
//---------------------------------------------------------------------------

/*
* CDSFclt Manager
*/
TCDSFcltManager::TCDSFcltManager()
{
}
//---------------------------------------------------------------------------
TCDSFcltManager::~TCDSFcltManager()
{
}
//---------------------------------------------------------------------------

bool TCDSFcltManager::Merge(String AType, String AId, String AName, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_FCLT_INFR L                                         \r\n"
           "USING (SELECT :p01 AS FCLT_ID,                                    \r\n"
           "              :p02 AS FCLT_TYPE,                                  \r\n"
           "              :p03 AS FCLT_LCTN                                   \r\n"
           "         FROM DUAL) M                                             \r\n"
           "        ON (L.FCLT_ID = M.FCLT_ID AND L.FCLT_TYPE = M.FCLT_TYPE)  \r\n"
           "WHEN MATCHED                                                      \r\n"
           "THEN                                                              \r\n"
           "   UPDATE SET L.FCLT_LCTN = M.FCLT_LCTN,                          \r\n"
           "              L.DEL_YN    = 'N'                                   \r\n"
           "WHEN NOT MATCHED                                                  \r\n"
           "THEN                                                              \r\n"
           "   INSERT (  FCLT_ID,   FCLT_TYPE,   FCLT_LCTN)                   \r\n"
           "   VALUES (M.FCLT_ID, M.FCLT_TYPE, M.FCLT_LCTN)                   \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
            pADO->SQL->Clear();
            pADO->SQL->Text = sQry;
            pADO->Parameters->ParamByName("p01")->Value = AId;
            pADO->Parameters->ParamByName("p02")->Value = AType;
            pADO->Parameters->ParamByName("p03")->Value = AName;
            pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("CDSFcltManager::Merge", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("CDSFcltManager::Merge", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

bool TCDSFcltManager::Delete(String AId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "DELETE TB_FCLT_INFR      \r\n"
           " WHERE FCLT_ID = :p01    \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
            pADO->SQL->Clear();
            pADO->SQL->Text = sQry;
            pADO->Parameters->ParamByName("p01")->Value = AId;
            pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("CDSFcltManager::Delete", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("CDSFcltManager::Delete", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    return true;
}
//---------------------------------------------------------------------------

