summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <daniel.goodliffe@pressassociation.com>2015-05-07 16:11:37 +0100
committerDan Goodliffe <daniel.goodliffe@pressassociation.com>2015-05-07 16:11:37 +0100
commitbfc3b28e596166f2b82d917baaed8bbb17fb687b (patch)
treed226326be6475f797b89df134c1c3f23e1c9c923
parentAdd target for installing the UT support libs (diff)
downloadproject2-bfc3b28e596166f2b82d917baaed8bbb17fb687b.tar.bz2
project2-bfc3b28e596166f2b82d917baaed8bbb17fb687b.tar.xz
project2-bfc3b28e596166f2b82d917baaed8bbb17fb687b.zip
Persist a table containing some environmental things during mock setup
-rw-r--r--project2/sql/Jamfile.jam1
-rw-r--r--project2/sql/mockDatabase.cpp63
-rw-r--r--project2/sql/mockDatabase.h6
3 files changed, 59 insertions, 11 deletions
diff --git a/project2/sql/Jamfile.jam b/project2/sql/Jamfile.jam
index 01ba44b..0af9f5a 100644
--- a/project2/sql/Jamfile.jam
+++ b/project2/sql/Jamfile.jam
@@ -69,6 +69,7 @@ lib p2sql :
:
<include>.
<library>glibmm
+ <library>boost_filesystem
<library>../common//p2common
<library>../lib//p2lib
<include>../../libmisc
diff --git a/project2/sql/mockDatabase.cpp b/project2/sql/mockDatabase.cpp
index 80322cf..2f26282 100644
--- a/project2/sql/mockDatabase.cpp
+++ b/project2/sql/mockDatabase.cpp
@@ -3,6 +3,7 @@
#include <misc.h>
#include <logger.h>
#include <fstream>
+#include <modifycommand.h>
MockDatabase::MockDatabase(const std::string & name) :
mockName(name)
@@ -18,20 +19,13 @@ MockDatabase::PlaySchemaScripts(const std::vector<boost::filesystem::path> & ss)
{
DB::Connection * conn = openConnection();
try {
+ CreateStatusTable(conn);
for (auto s : ss) {
conn->beginTx();
- Logger()->messagebf(LOG_DEBUG, "%s << %s", mockName, s);
- std::ifstream f;
- f.open(s.string());
- while (!f.eof()) {
- char buf[BUFSIZ];
- f.getline(buf, BUFSIZ, ';');
- if (!f.eof())
- conn->execute(buf);
- }
- f.close();
+ PlaySchemaScript(conn, s);
conn->commitTx();
}
+ DropStatusTable(conn);
delete conn;
Logger()->messagebf(LOG_DEBUG, "%s initialized", mockName);
MockConnectionLoader::mocks[mockName] = this;
@@ -44,7 +38,54 @@ MockDatabase::PlaySchemaScripts(const std::vector<boost::filesystem::path> & ss)
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());
+ while (!f.eof()) {
+ char buf[BUFSIZ];
+ f.getline(buf, BUFSIZ, ';');
+ if (!f.eof())
+ conn->execute(buf);
+ }
+ 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) :
diff --git a/project2/sql/mockDatabase.h b/project2/sql/mockDatabase.h
index 5cf42f2..9c00420 100644
--- a/project2/sql/mockDatabase.h
+++ b/project2/sql/mockDatabase.h
@@ -16,9 +16,15 @@ class MockDatabase {
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 {