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

#include <vcl.h>
#pragma hdrstop

#include "DMDbF.h"
#include "ITSLogF.h"
#include "AppGlobalF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDMDb *DMDb;
//---------------------------------------------------------------------------
__fastcall TDMDb::TDMDb(TComponent* Owner)
    : TDataModule(Owner)
{
    FConnString = "";
    FException  = true;
}
//---------------------------------------------------------------------------
void __fastcall TDMDb::DataModuleCreate(TObject *Sender)
{
//
}
//---------------------------------------------------------------------------
void __fastcall TDMDb::DataModuleDestroy(TObject *Sender)
{
//
}
//---------------------------------------------------------------------------

void __fastcall TDMDb::SetErrorString(String AErrorString)
{
    FErrorString = AErrorString;
}
//---------------------------------------------------------------------------

void __fastcall TDMDb::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 TDMDb::SetConnectString(String AConnStr)
{
    FConnString  = AConnStr;
}
//---------------------------------------------------------------------------

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

	try
    {
        Close();

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

        FDbConn->ConnectionTimeout = 10;
        FDbConn->ConnectionString  = AConnStr == "" ? FConnString : AConnStr;
        FDbConn->KeepConnection	   = true;
        FDbConn->LoginPrompt       = false;
        FDbConn->Open();
        bResult = true;
    }
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
        if (FException)
        {
            DBERRORMSG("Connect", String(E.ClassName()), E.Message, "Connect");
            throw Exception(ErrorString);
        }
        else
        {
            DBERRORLOG("Connect", String(E.ClassName()), E.Message, "Connect");
        }
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
        if (FException)
        {
            DBERRORMSG("Connect", String(e.ClassName()), e.Message, "Connect");
            throw Exception(ErrorString);
        }
        else
        {
            DBERRORLOG("Connect", String(e.ClassName()), e.Message, "Connect");
        }
    }
    return bResult;
}
//---------------------------------------------------------------------------

bool __fastcall TDMDb::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;
        if (FException)
        {
            DBERRORMSG("Close", String(E.ClassName()), E.Message, "Close");
            throw Exception(ErrorString);
        }
        else
        {
            DBERRORLOG("Close", String(E.ClassName()), E.Message, "Close");
        }
    }
    catch (Exception &e)
    {
        ErrorString = String(e.ClassName()) + e.Message;
        if (FException)
        {
            DBERRORMSG("Close", String(e.ClassName()), e.Message, "Close");
            throw Exception(ErrorString);
        }
        else
        {
            DBERRORLOG("Close", String(e.ClassName()), e.Message, "Close");
        }
    }
    return bResult;
}
//---------------------------------------------------------------------------

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

        return true;
    }
#if 0
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
        if (FException) throw Exception(ErrorString);
    }
    catch (Exception &exception)
    {
        ErrorString = String(exception.ClassName()) + exception.Message;
        if (FException) throw Exception(ErrorString);
    }
    return false;
#endif
}
//---------------------------------------------------------------------------

int __fastcall TDMDb::SQLText(TADOQuery *adoQry, String sQry)
{
#if 0
    try
#endif
    {
        adoQry->Close();
        adoQry->SQL->Text = sQry;
        return 0;
    }
#if 0
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
        if (FException) throw Exception(ErrorString);
    }
    catch (Exception &exception)
    {
        ErrorString = String(exception.ClassName()) + exception.Message;
        if (FException) throw Exception(ErrorString);
    }
    return 0;
#endif
}
//---------------------------------------------------------------------------

bool __fastcall TDMDb::SQLBind(TADOQuery *adoQry, String param, String var)
{
#if 0
    try
#endif
    {
        adoQry->Parameters->ParamByName(param)->Value = var;
        return true;
    }
#if 0
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
        if (FException) throw Exception(ErrorString);
    }
    catch (Exception &exception)
    {
        ErrorString = String(exception.ClassName()) + exception.Message;
        if (FException) throw Exception(ErrorString);
    }
    return false;
#endif
}
//---------------------------------------------------------------------------

bool __fastcall TDMDb::SQLBindStream(TADOQuery *adoQry, String param, TStream * pStream)
{
#if 0
    try
#endif
    {
        adoQry->Parameters->ParamByName(param)->LoadFromStream(pStream, ftBlob);
        return true;
    }
#if 0
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
        if (FException) throw Exception(ErrorString);
    }
    catch (Exception &exception)
    {
        ErrorString = String(exception.ClassName()) + exception.Message;
        if (FException) throw Exception(ErrorString);
    }
    return false;
#endif
}
//---------------------------------------------------------------------------

int __fastcall TDMDb::SQLOpen(TADOQuery *adoQry)
{
#if 0
    try
#endif
    {
#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;
    }
#if 0
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
        if (FException) throw Exception(ErrorString);
    }
    catch (Exception &exception)
    {
        ErrorString = String(exception.ClassName()) + exception.Message;
        if (FException) throw Exception(ErrorString);
    }
    return -1;
#endif
}
//---------------------------------------------------------------------------

int  __fastcall TDMDb::SQLExec(TADOQuery *adoQry)
{
#if 0
    try
#endif
    {
#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;
    }
#if 0
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
        if (FException) throw Exception(ErrorString);
    }
    catch (Exception &exception)
    {
        ErrorString = String(exception.ClassName()) + exception.Message;
        if (FException) throw Exception(ErrorString);
    }
    return -1;
#endif
}
//---------------------------------------------------------------------------

bool __fastcall TDMDb::SQLClear(TADOQuery *adoQry)
{
#if 0
    try
#endif
    {
        adoQry->Close();
        adoQry->SQL->Clear();

        return true;
    }
#if 0
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
        if (FException) throw Exception(ErrorString);
    }
    catch (Exception &exception)
    {
        ErrorString = String(exception.ClassName()) + exception.Message;
        if (FException) throw Exception(ErrorString);
    }
    return false;
#endif
}
//---------------------------------------------------------------------------

bool __fastcall TDMDb::LocateItem(TADOQuery *adoQry, String sFldNm, String sFldVal)
{
#if 0
    try
#endif
    {
        TLocateOptions Opts;
        Opts.Clear();
        Opts << loPartialKey;

        return adoQry->Locate(sFldNm, sFldVal, Opts);
    }
#if 0
    catch(EDatabaseError &E)
    {
        ErrorString = String(E.ClassName()) + E.Message;
        if (FException) throw Exception(ErrorString);
    }
    catch (Exception &exception)
    {
        ErrorString = String(exception.ClassName()) + exception.Message;
        if (FException) throw Exception(ErrorString);
    }
    return false;
#endif
}
//---------------------------------------------------------------------------

