Last active
March 22, 2021 14:39
-
-
Save Vladimir-Novick/a109d6987bf834f9cb1dd4bd7340a81c to your computer and use it in GitHub Desktop.
PostgreSQL - Dump and restore schema from c++ application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int PgSQL_Dump::RestoreFromDump(ConnInfo* connInfo, string targetFileName) { | |
int ret = 0; | |
stringstream ss; | |
string connectionString; | |
for (auto& elemItem : connInfo->configInfo) { | |
if (elemItem.key.compare("CONFIG_STRING") == 0) { | |
connectionString = elemItem.value; | |
break; | |
} | |
} | |
string dumpFileName = targetFileName + "\\db.dump"; | |
ss << " --no-owner --clean -d \"" << connectionString << "\" -1 " | |
<< dumpFileName ; | |
string s = ss.str(); | |
char buff[100]; | |
strcpy(buff, "pg_restore.exe"); | |
char bufDirectory[255]; | |
strcpy(bufDirectory, connInfo->baseFolder.c_str()); | |
SHELLEXECUTEINFO ShExecInfo = { 0 }; | |
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); | |
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; | |
ShExecInfo.hwnd = NULL; | |
ShExecInfo.lpVerb = NULL; | |
ShExecInfo.lpFile = buff; | |
ShExecInfo.lpParameters = s.c_str(); | |
ShExecInfo.lpDirectory = bufDirectory; | |
ShExecInfo.nShow = SW_HIDE; | |
ShExecInfo.hInstApp = NULL; | |
BOOL ok = ShellExecuteEx(&ShExecInfo); | |
if (ok) { | |
WaitForSingleObject(ShExecInfo.hProcess, INFINITE); | |
CloseHandle(ShExecInfo.hProcess); | |
} | |
return ret; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pch.h" | |
#include "PgSQL_Dump.h" | |
#include <shellapi.h> | |
#include <list> | |
#include <atlbase.h> | |
#include <string> | |
using namespace std; | |
int PgSQL_Dump::WriteDump(ConnInfo* connInfo, string targetFileName) { | |
int ret = 0; | |
stringstream ss; | |
string connectionString; | |
for (auto& elemItem : connInfo->configInfo) { | |
if (elemItem.key.compare("CONFIG_STRING") == 0) { | |
connectionString = elemItem.value; | |
break; | |
} | |
} | |
string dumpFileName = targetFileName + "\\db.dump"; | |
ss << " --no-owner --dbname=" << connectionString << " --schema=\\\"" << connInfo->schema << "\\\" --file=" | |
<< dumpFileName << " --blobs --format=custom "; | |
string s = ss.str(); | |
char buff[100]; | |
strcpy(buff, "pg_dump.exe"); | |
char bufDirectory[255]; | |
strcpy(bufDirectory, connInfo->baseFolder.c_str()); | |
SHELLEXECUTEINFO ShExecInfo = { 0 }; | |
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); | |
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; | |
ShExecInfo.hwnd = NULL; | |
ShExecInfo.lpVerb = NULL; | |
ShExecInfo.lpFile = buff; | |
ShExecInfo.lpParameters = s.c_str(); | |
ShExecInfo.lpDirectory = bufDirectory; | |
ShExecInfo.nShow = SW_HIDE; | |
ShExecInfo.hInstApp = NULL; | |
BOOL ok = ShellExecuteEx(&ShExecInfo); | |
if (ok) { | |
WaitForSingleObject(ShExecInfo.hProcess, INFINITE); | |
CloseHandle(ShExecInfo.hProcess); | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment