summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-09-22 21:36:18 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2015-09-22 21:36:18 +0100
commit1d5af8554d73f02f8a1eba8a19046a6762f6f14f (patch)
treeb2ce9bfc6108647c5c25da93df2efd0d9ae32bda
parentMigrate SQL script parser and add support for executing a script on a connection (diff)
downloadproject2-1d5af8554d73f02f8a1eba8a19046a6762f6f14f.tar.bz2
project2-1d5af8554d73f02f8a1eba8a19046a6762f6f14f.tar.xz
project2-1d5af8554d73f02f8a1eba8a19046a6762f6f14f.zip
Database mocking base classes migrated in.
-rw-r--r--project2/sql/mockDatabase.cpp119
-rw-r--r--project2/sql/mockDatabase.h44
2 files changed, 0 insertions, 163 deletions
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 <buffer.h>
-#include <logger.h>
-#include <fstream>
-#include <modifycommand.h>
-#include <FlexLexer.h>
-#include "sqlFlexLexer.h"
-
-MockDatabase::MockDatabase(const std::string & name) :
- mockName(name)
-{
-}
-
-MockDatabase::~MockDatabase()
-{
-}
-
-void
-MockDatabase::PlaySchemaScripts(const std::vector<boost::filesystem::path> & 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<FlexLexer>(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<ConnectionLoader, std::string>::Get<std::invalid_argument>(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 <string>
-#include <vector>
-#include <boost/filesystem/path.hpp>
-#include <connection.h>
-
-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<boost::filesystem::path> & 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
-