Warrior_EA/Database/DatabaseVersionManager.mqh
super.admin 0a527b0cf9 convert
2025-05-30 16:35:54 +02:00

59 lines
2.2 KiB
MQL5

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CDatabaseVersionManager
{
public:
string ReadStoredDbVersion(string versionFilePath)
{
if(!FileIsExist(versionFilePath, FILE_COMMON))
return "NA";
int fileHandle = INVALID_HANDLE; // Initialize to a safe value
int attempts = 0;
while(attempts < 5)
{
fileHandle = FileOpen(versionFilePath, FILE_COMMON | FILE_READ);
if(fileHandle != INVALID_HANDLE)
{
break; // Success, break out of the loop
}
Print("Retry " + IntegerToString(attempts + 1) + " failed to open file for reading: " + versionFilePath);
Sleep(1000); // Wait for a second before retrying
attempts++;
}
if(fileHandle == INVALID_HANDLE)
{
Print("Failed to open file for reading after retries: " + versionFilePath);
return "ERROR";
}
string dbVersion = FileReadString(fileHandle);
FileClose(fileHandle);
return dbVersion;
}
bool UpdateStoredDbVersion(string versionFilePath, string dbVersion)
{
int fileHandle = INVALID_HANDLE; // Initialize to a safe value
int attempts = 0;
while(attempts < 5)
{
fileHandle = FileOpen(versionFilePath, FILE_COMMON | FILE_WRITE);
if(fileHandle != INVALID_HANDLE)
{
break; // Success, break out of the loop
}
Print("Retry " + IntegerToString(attempts + 1) + " failed to open file for writing: " + versionFilePath);
Sleep(1000); // Wait for a second before retrying
attempts++;
}
if(fileHandle == INVALID_HANDLE)
{
Print("Failed to open file for writing after retries: " + versionFilePath);
return false;
}
FileWriteString(fileHandle, dbVersion);
FileClose(fileHandle);
return true;
}
};
//+------------------------------------------------------------------+