101 lines
3 KiB
MQL5
101 lines
3 KiB
MQL5
|
//+------------------------------------------------------------------+
|
||
|
//| Warrior_EA |
|
||
|
//| AnimateDread |
|
||
|
//| |
|
||
|
//+------------------------------------------------------------------+
|
||
|
#include "..\\System\\PrintVerbose.mqh"
|
||
|
class CDatabaseConnectionManager
|
||
|
{
|
||
|
private:
|
||
|
bool m_transactionActive;
|
||
|
int m_databaseHandle;
|
||
|
string m_dbPath;
|
||
|
bool m_verboseMode;
|
||
|
public:
|
||
|
|
||
|
CDatabaseConnectionManager(bool verbose = false)
|
||
|
{
|
||
|
m_transactionActive = false;
|
||
|
m_verboseMode = verbose;
|
||
|
m_databaseHandle = INVALID_HANDLE;
|
||
|
}
|
||
|
bool Init(string path)
|
||
|
{
|
||
|
m_dbPath = path;
|
||
|
return (true);
|
||
|
}
|
||
|
int GetDatabaseHandle()
|
||
|
{
|
||
|
return m_databaseHandle;
|
||
|
}
|
||
|
bool OpenDatabase()
|
||
|
{
|
||
|
if(m_databaseHandle != INVALID_HANDLE)
|
||
|
{
|
||
|
if(m_verboseMode)
|
||
|
PrintVerbose("Database is already open.");
|
||
|
return true;
|
||
|
}
|
||
|
int attempts = 0;
|
||
|
while(attempts < 5)
|
||
|
{
|
||
|
m_databaseHandle = DatabaseOpen(m_dbPath, DATABASE_OPEN_COMMON | DATABASE_OPEN_CREATE | DATABASE_OPEN_READWRITE);
|
||
|
if(m_databaseHandle != INVALID_HANDLE)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
Print("Attempt " + IntegerToString(attempts + 1) + " failed to open database.");
|
||
|
Sleep(1000); // Wait for a second before retrying
|
||
|
attempts++;
|
||
|
}
|
||
|
Print("Database open failed after retries.");
|
||
|
return false;
|
||
|
}
|
||
|
bool CloseDatabase()
|
||
|
{
|
||
|
if(m_databaseHandle != INVALID_HANDLE)
|
||
|
{
|
||
|
DatabaseClose(m_databaseHandle);
|
||
|
m_databaseHandle = INVALID_HANDLE;
|
||
|
}
|
||
|
return(true);
|
||
|
}
|
||
|
bool TransactionBegin()
|
||
|
{
|
||
|
if(!DatabaseTransactionBegin(m_databaseHandle))
|
||
|
{
|
||
|
Print("DatabaseTransactionBegin failed.");
|
||
|
return(false);
|
||
|
}
|
||
|
else
|
||
|
m_transactionActive = true;
|
||
|
return(true);
|
||
|
}
|
||
|
bool TransactionCommit()
|
||
|
{
|
||
|
if(!m_transactionActive)
|
||
|
return(true);
|
||
|
if(!DatabaseTransactionCommit(m_databaseHandle))
|
||
|
{
|
||
|
Print("DatabaseTransactionCommit failed. Reverting changes...");
|
||
|
TransactionRollback();
|
||
|
m_transactionActive = false;
|
||
|
return(false);
|
||
|
}
|
||
|
m_transactionActive = false;
|
||
|
return(true);
|
||
|
}
|
||
|
bool TransactionRollback()
|
||
|
{
|
||
|
if(!m_transactionActive)
|
||
|
return(true);
|
||
|
if(!DatabaseTransactionRollback(m_databaseHandle))
|
||
|
{
|
||
|
Print("DatabaseTransactionRollback failed.");
|
||
|
return(false);
|
||
|
}
|
||
|
return(true);
|
||
|
}
|
||
|
};
|
||
|
//+------------------------------------------------------------------+
|