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

#include <vcl.h>
#pragma hdrstop

#include "DMAdoDbF.h"
#include "VMSCommLibF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

//TDMAdoDb *DMAdoDb = NULL;
//---------------------------------------------------------------------------
__fastcall TDMAdoDb::TDMAdoDb(TComponent* Owner)
    : TDataModule(Owner)
{
    FConnString = "";
}
//---------------------------------------------------------------------------
void __fastcall TDMAdoDb::DataModuleCreate(TObject *Sender)
{
//
}
//---------------------------------------------------------------------------
void __fastcall TDMAdoDb::DataModuleDestroy(TObject *Sender)
{
//
}
//---------------------------------------------------------------------------

void __fastcall TDMAdoDb::SetErrorString(AnsiString AErrorString)
{
    FErrorString = AErrorString;
    MERROR(AnsiString("DCOM " + FErrorString).c_str());
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TDMAdoDb::SetConnectString(String AProvider, String AServerName, String AUserName, String APassword, String ACatalog/*=""*/)
{
    FConnString  = "";
    FConnString += "Provider="         + AProvider;
    FConnString += ";Password="        + APassword;
    FConnString += ";Persist Security Info=True";
    FConnString += ";User ID="         + AUserName;
    if (ACatalog != "")
    {
        FConnString += ";Initial Catalog=" + ACatalog;
    }
    FConnString += ";Data Source="     + AServerName;
}
//---------------------------------------------------------------------------

void __fastcall TDMAdoDb::SetConnectString(String AConnString)
{
    FConnString = AConnString;
}
//---------------------------------------------------------------------------

bool __fastcall TDMAdoDb::Connect(String AConnStr/*=""*/)
{
    bool bResult = false;

    FConnString = (AConnStr == "") ? FConnString : AConnStr;
	try
    {
        Close();

        FDbConn = new TADOConnection(NULL);
        if (!FDbConn) return false;

        FDbConn->ConnectionTimeout = 10;
        FDbConn->ConnectionString  = FConnString;
        FDbConn->KeepConnection	   = true;
        FDbConn->LoginPrompt       = false;
        FDbConn->Open();
        bResult = true;
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
    }
    return bResult;
}
//---------------------------------------------------------------------------

bool __fastcall TDMAdoDb::Close()
{
    bool bResult = false;

	try
    {
        if (!FDbConn) return false;

        if (FDbConn->Connected)
        {
            FDbConn->Close();
            FDbConn->Connected = false;
        }
        delete FDbConn;
        FDbConn = NULL;
        bResult = true;
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
    }
    return bResult;
}
//---------------------------------------------------------------------------

bool __fastcall TDMAdoDb::SQLClose(TADOQuery *adoQry)
{
    try
    {
        if (adoQry->Active)
        {
            adoQry->Recordset->Close();
            adoQry->Close();
            adoQry->Active = false;
        }
        adoQry->SQL->Clear();

        return true;
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
    }
    return false;
}
//---------------------------------------------------------------------------

int __fastcall TDMAdoDb::SQLText(TADOQuery *adoQry, String sQry)
{
    try
    {
        adoQry->Close();
        adoQry->SQL->BeginUpdate();
        adoQry->SQL->Text = sQry;
        adoQry->SQL->EndUpdate();
        return 0;
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
    }
    return 0;
}
//---------------------------------------------------------------------------

bool __fastcall TDMAdoDb::SQLBind(TADOQuery *adoQry, String param, String var)
{
    try
    {
        adoQry->Parameters->ParamByName(param)->Value = var;
        return true;
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
    }
    return false;
}
//---------------------------------------------------------------------------

bool __fastcall TDMAdoDb::SQLBindStream(TADOQuery *adoQry, String param, TStream * pStream)
{
    try
    {
        adoQry->Parameters->ParamByName(param)->LoadFromStream(pStream, ftBlob);
        return true;
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
    }
    return false;
}
//---------------------------------------------------------------------------

int __fastcall TDMAdoDb::SQLOpen(TADOQuery *adoQry)
{
    try{
#if 0
        if (0)
        {
            String sLogSql = adoQry->SQL->Text;

            sLogSql += "\r\n";
            for (int ii = 0; ii < adoQry->Parameters->Count; ii++)
            {
                sLogSql += ":" + adoQry->Parameters->Items[ii]->Name + "  :  " + adoQry->Parameters->Items[ii]->Value;
                sLogSql += "\r\n";
            }

            //LogSql(sLogSql);
        }
#endif

        adoQry->Open();
        adoQry->First();
        return adoQry->RecordCount;
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
    }
    return -1;
}
//---------------------------------------------------------------------------

int  __fastcall TDMAdoDb::SQLExec(TADOQuery *adoQry)
{
    try
    {
#if 0
        if (0)
        {
            String sLogSql = adoQry->SQL->Text;

            sLogSql += "\r\n";
            for (int ii = 0; ii < adoQry->Parameters->Count; ii++)
            {
                sLogSql += ":" + adoQry->Parameters->Items[ii]->Name + "  :  " + adoQry->Parameters->Items[ii]->Value;
                sLogSql += "\r\n";
            }

            //LogSql(sLogSql);
        }
#endif
        adoQry->ExecSQL();
        return adoQry->RowsAffected;
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
    }
    return -1;
}
//---------------------------------------------------------------------------

bool __fastcall TDMAdoDb::SQLClear(TADOQuery *adoQry)
{
    try
    {
        adoQry->Close();
        adoQry->SQL->Clear();

        return true;
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
    }
    return false;
}
//---------------------------------------------------------------------------

bool __fastcall TDMAdoDb::LocateItem(TADOQuery *adoQry, String sFldNm, String sFldVal)
{
    try
    {
        TLocateOptions Opts;
        Opts.Clear();
        Opts << loPartialKey;

        return adoQry->Locate(sFldNm, sFldVal, Opts);
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
    }
    return false;
}
//---------------------------------------------------------------------------

bool __fastcall TDMAdoDb::IsDbAlive(AnsiString &ASysTime)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') AS CURTIME \r\n"
           "  FROM DUAL                                            \r\n";

    ASysTime = "";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Connection = GetConnection();

            SQLText(pADO, sQry);
            SQLOpen(pADO);

            ASysTime = pADO->FieldByName("CURTIME")->AsString;
        }
        catch(EDatabaseError &E)
        {
            DBERROR(String(E.ClassName()), E.Message, sQry);
            return false;
        }
        catch(Exception &e)
        {
            DBERROR(String(e.ClassName()), e.Message, sQry);
            return false;
        }
    }
    __finally
    {
        if (pADO)
        {
            SQLClose(pADO);
            delete pADO; pADO = NULL;
        }
    }

    MINFO("DCOM <<< Check Database Alive: Ok(%s)", ASysTime.c_str());
    return true;
}
//---------------------------------------------------------------------------
void __fastcall TDMAdoDb::SetDbState(bool AFlag)
{
    if (AFlag)
    {
        SendMessage((HWND)g_AppCfg.lMainWinHandle, WM_DB_THREAD, dbm_status, 1);
    }
    else
    {
        SendMessage((HWND)g_AppCfg.lMainWinHandle, WM_DB_THREAD, dbm_status, -1);
    }
}
//---------------------------------------------------------------------------

