From 1d5af8554d73f02f8a1eba8a19046a6762f6f14f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 22 Sep 2015 21:36:18 +0100 Subject: Database mocking base classes migrated in. --- project2/sql/mockDatabase.cpp | 119 ------------------------------------------ project2/sql/mockDatabase.h | 44 ---------------- 2 files changed, 163 deletions(-) delete mode 100644 project2/sql/mockDatabase.cpp delete mode 100644 project2/sql/mockDatabase.h diff --git a/project2/sql/mockDatabase.cpp b/project2/sql/mockDatabase.cpp deleted file mode 100644 index 3ad409d..0000000 --- a/project2/sql/mockDatabase.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include "mockDatabase.h" -#include "mockDatasource.h" -#include -#include -#include -#include -#include -#include "sqlFlexLexer.h" - -MockDatabase::MockDatabase(const std::string & name) : - mockName(name) -{ -} - -MockDatabase::~MockDatabase() -{ -} - -void -MockDatabase::PlaySchemaScripts(const std::vector & ss) const -{ - DB::Connection * conn = openConnection(); - try { - CreateStatusTable(conn); - for (auto s : ss) { - conn->beginTx(); - PlaySchemaScript(conn, s); - conn->commitTx(); - } - DropStatusTable(conn); - delete conn; - Logger()->messagebf(LOG_DEBUG, "%s initialized", mockName); - MockConnectionLoader::mocks[mockName] = this; - } - catch (...) { - if (conn->inTx()) { - conn->rollbackTx(); - } - delete conn; - DropDatabase(); - throw; - } -} - -void -MockDatabase::PlaySchemaScript(DB::Connection * conn, const boost::filesystem::path & s) const -{ - UpdateStatusTable(conn, s); - Logger()->messagebf(LOG_DEBUG, "%s << %s", mockName, s); - std::ifstream f; - f.open(s.string()); - if (!f.good()) { - throw std::runtime_error("Failed to open mock script: " + s.string()); - } - auto lexer = boost::shared_ptr(new sqlFlexLexer(s.parent_path(), f, conn)); - while(lexer->yylex() != 0) ; - f.close(); -} - -void -MockDatabase::CreateStatusTable(DB::Connection * conn) const -{ - conn->execute( - "CREATE TABLE _p2_teststatus( \ - pid int, \ - script varchar(256), \ - scriptdir varchar(256))"); - auto ins = conn->newModifyCommand( - "INSERT INTO _p2_teststatus(pid) VALUES(?)"); - ins->bindParamI(0, getpid()); - ins->execute(); - delete ins; -} - -void -MockDatabase::DropStatusTable(DB::Connection * conn) const -{ - conn->execute("DROP TABLE _p2_teststatus"); -} - -void -MockDatabase::UpdateStatusTable(DB::Connection * conn, const boost::filesystem::path & script) const -{ - auto upd = conn->newModifyCommand( - "UPDATE _p2_teststatus SET script = ?, scriptdir = ?"); - upd->bindParamS(0, script.string()); - upd->bindParamS(1, script.parent_path().string()); - upd->execute(); - delete upd; -} - -MockServerDatabase::MockServerDatabase(const std::string & masterdb, const std::string & name, const std::string & type) : - MockDatabase(name), - master(InstanceMap::Get(type)->create(masterdb)), - testDbName(stringbf("test_%d_%d", getpid(), ++MockConnectionLoader::mocked)) -{ -} - -MockServerDatabase::~MockServerDatabase() -{ - delete master; -} - -void -MockServerDatabase::CreateNewDatabase() const -{ - DropDatabase(); - Logger()->messagebf(LOG_INFO, "Creating new database %s", testDbName); - master->execute("CREATE DATABASE " + testDbName); -} - -void -MockServerDatabase::DropDatabase() const -{ - Logger()->messagebf(LOG_INFO, "Droping database %s", testDbName); - master->execute("DROP DATABASE IF EXISTS " + testDbName); - MockConnectionLoader::mocks.erase(mockName); -} - diff --git a/project2/sql/mockDatabase.h b/project2/sql/mockDatabase.h deleted file mode 100644 index 9c00420..0000000 --- a/project2/sql/mockDatabase.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef MOCKDATABASE_H -#define MOCKDATABASE_H - -#include -#include -#include -#include - -class MockDatabase { - public: - MockDatabase(const std::string & mockName); - virtual ~MockDatabase(); - - virtual DB::Connection * openConnection() const = 0; - - protected: - virtual void CreateNewDatabase() const = 0; - virtual void PlaySchemaScripts(const std::vector & ss) const; - virtual void PlaySchemaScript(DB::Connection *, const boost::filesystem::path & s) const; - virtual void DropDatabase() const = 0; - virtual void UpdateStatusTable(DB::Connection *, const boost::filesystem::path &) const; - - const std::string mockName; - - private: - void CreateStatusTable(DB::Connection *) const; - void DropStatusTable(DB::Connection *) const; -}; - -class MockServerDatabase : public MockDatabase { - public: - MockServerDatabase(const std::string & masterdb, const std::string & name, const std::string & type); - virtual ~MockServerDatabase(); - - protected: - virtual void CreateNewDatabase() const override; - virtual void DropDatabase() const override; - - DB::Connection * master; - const std::string testDbName; -}; - -#endif - -- cgit v1.2.3