summaryrefslogtreecommitdiff
path: root/libpqpp/pq-mock.cpp
blob: 30f14aa912c81dc202755d84f07aae5576ff0048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "pq-mock.h"
#include "pq-connection.h"
#include <buffer.h>
#include <selectcommand.h>
#include <selectcommandUtil.impl.h>

namespace PQ {

Mock::Mock(const std::string & masterdb, const std::string & name, const std::vector<boost::filesystem::path> & ss) :
	MockServerDatabase(masterdb, name, "postgresql")
{
	CreateNewDatabase();
	PlaySchemaScripts(ss);
	SetTablesToUnlogged();
}

DB::Connection *
Mock::openConnection() const
{
	return new Connection(stringbf("user=postgres dbname=%s", testDbName));
}

void
Mock::SetTablesToUnlogged() const
{
	auto c = DB::ConnectionPtr(openConnection());
	auto s = c->select("SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE schemaname NOT IN (?, ?)");
	s->bindParamS(0, "pg_catalog");
	s->bindParamS(1, "information_schema");
	for (const auto & t : s->as<std::string, std::string>()) {
		c->execute("ALTER TABLE " + t.value<0>() + "." + t.value<1>() + " SET UNLOGGED");
	}
}

Mock::~Mock()
{
	DropDatabase();
}

void
Mock::DropDatabase() const
{
	master->execute("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '" + testDbName + "'");
	MockServerDatabase::DropDatabase();
}

}