summaryrefslogtreecommitdiff
path: root/libpqpp/pq-mock.cpp
blob: f72c01f3b4744cfc411847fb020507d5da603497 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "pq-mock.h"
#include "pq-connection.h"
#include <compileTimeFormatter.h>
#include <modifycommand.h>
#include <selectcommand.h>
#include <selectcommandUtil.impl.h>

NAMEDFACTORY("postgresql", PQ::Mock, DB::MockDatabaseFactory);

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();
}

AdHocFormatter(MockConnStr, "user=postgres dbname=%?");
DB::Connection *
Mock::openConnection() const
{
	return new Connection(MockConnStr::get(testDbName));
}

AdHocFormatter(MockSetUnlogged, "ALTER TABLE %?.%? SET UNLOGGED");
void
Mock::SetTablesToUnlogged() const
{
	auto c = DB::ConnectionPtr(openConnection());
	auto s = c->select(R"SQL( 
SELECT n.nspname, c.relname
FROM pg_class c, pg_namespace n
WHERE c.relkind = 'r'
AND n.nspname not in (?, ?)
AND c.relpersistence = 'p'
AND NOT EXISTS (
	SELECT from pg_constraint fk, pg_class ck
	WHERE fk.contype = 'f'
	AND fk.confrelid = c.oid
	AND fk.conrelid = ck.oid
	AND ck.oid != c.oid
	AND ck.relpersistence = 'p')
AND n.oid = c.relnamespace
ORDER BY 1, 2)SQL");
	s->bindParamS(0, "pg_catalog");
	s->bindParamS(1, "information_schema");
	unsigned int n = 0;
	do {
		n = 0;
		for (const auto & t : s->as<std::string, std::string>()) {
			c->execute(MockSetUnlogged::get(t.value<0>(), t.value<1>()));
			n += 1;
		}
	} while(n);
}

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

void
Mock::DropDatabase() const
{
	auto t = master->modify("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = ?");
	t->bindParamS(0, testDbName);
	t->execute();
	MockServerDatabase::DropDatabase();
}

}