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


#pragma hdrstop

#include "CDSDatabaseF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TItsDatabase::TItsDatabase()
{
	FCompleted = false;
}
//---------------------------------------------------------------------------
void TItsDatabase::Clear()
{
}
//---------------------------------------------------------------------------
TItsDatabase::~TItsDatabase()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsDatabaseManager *ItsDatabaseManager = NULL;
//---------------------------------------------------------------------------

/*
* Database Manager
*/
TItsDatabaseManager::TItsDatabaseManager()
{
}
//---------------------------------------------------------------------------
TItsDatabaseManager::~TItsDatabaseManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TItsDatabaseManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT TABLESPACE_NAME, TOTAL_MB, USED_MB, FREE_MB, USED_PERCENT  \r\n"
           "  FROM VW_TABLESPACE_USAGE                                        \r\n"
           " ORDER BY TABLESPACE_NAME                                         \r\n";

    FLists.Lock();
    try
    {
        //FLists.RemoveAll();
        FOR_STL(TItsDatabase*, pTmp, FLists)
        {
            pTmp->Completed = false;
        }

        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String TABLESPACE_NAME = pADO->FieldByName("TABLESPACE_NAME")->AsString;

				TItsDatabase *pObj = FLists.Find(TABLESPACE_NAME);
                if (!pObj)
                {
                    pObj = new TItsDatabase();
                    pObj->TABLESPACE_NAME = TABLESPACE_NAME;

    				FLists.Push(pObj->TABLESPACE_NAME, pObj);
                }

                pObj->TOTAL_MB     = pADO->FieldByName("TOTAL_MB")->AsInteger;
                pObj->USED_MB      = pADO->FieldByName("USED_MB")->AsInteger;
                pObj->FREE_MB      = pADO->FieldByName("FREE_MB")->AsInteger;
                pObj->USED_PERCENT = pADO->FieldByName("USED_PERCENT")->AsInteger;

                pObj->Completed = true;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsDatabaseManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsDatabaseManager::LoadFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

