summaryrefslogtreecommitdiff
path: root/libmysqlpp/embeddedmy-mock.cpp
blob: 0ad0cc53d88ea693024c91472f79d0f7a3c7952d (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
#include "embeddedmy-connection.h"
#include "embeddedmy-mock.h"
#include <boost/filesystem/convenience.hpp>
#include <buffer.h>

namespace MySQL {
	namespace Embedded {

		Mock::Mock(const std::string & name, const std::vector<boost::filesystem::path> & ss) :
			MockDatabase(name),
			testDbPath(boost::filesystem::path("/tmp") / stringbf("embeddedmysql-%d-%d", getpid(), ++DB::MockDatabase::mocked))
		{
			CreateNewDatabase();
			PlaySchemaScripts(ss);
		}

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

		DB::Connection *
		Mock::openConnection() const
		{
			return new Connection(testDbPath.string());
		}

		void
		Mock::CreateNewDatabase() const
		{
			boost::filesystem::create_directories(testDbPath);
			const auto datadir = stringbf("--datadir=%s", testDbPath.string());
			static const char * opts[] = {
				typeid(this).name(),
				datadir.c_str(),
				"--bootstrap",
				NULL
			};
			static const char * groups[] = { NULL };
			mysql_library_init(3, (char**)opts, (char**)groups);
			sleep(20);
		}

		void
		Mock::DropDatabase() const
		{
			mysql_library_end();
			boost::filesystem::remove_all(testDbPath);
		}
	}
}