Skip to content

Instantly share code, notes, and snippets.

@Vladimir-Novick
Last active March 22, 2021 14:39
Show Gist options
  • Save Vladimir-Novick/a109d6987bf834f9cb1dd4bd7340a81c to your computer and use it in GitHub Desktop.
Save Vladimir-Novick/a109d6987bf834f9cb1dd4bd7340a81c to your computer and use it in GitHub Desktop.
PostgreSQL - Dump and restore schema from c++ application
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;
}
#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