summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-04-25 22:52:30 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2016-04-25 22:52:30 +0100
commit1c16414f565ed1f3d482f71f8630c78fa1041ef0 (patch)
treeec78e5aeed9948913137960a217fc40d92cf94ac
parentRefactor utils (diff)
downloadgentoobrowse-api-1c16414f565ed1f3d482f71f8630c78fa1041ef0.tar.bz2
gentoobrowse-api-1c16414f565ed1f3d482f71f8630c78fa1041ef0.tar.xz
gentoobrowse-api-1c16414f565ed1f3d482f71f8630c78fa1041ef0.zip
Add some db utils functions for easing patching
-rw-r--r--gentoobrowse-api/service/utils/dbUtils.cpp31
-rw-r--r--gentoobrowse-api/service/utils/dbUtils.h6
2 files changed, 37 insertions, 0 deletions
diff --git a/gentoobrowse-api/service/utils/dbUtils.cpp b/gentoobrowse-api/service/utils/dbUtils.cpp
index f2b7f4d..4ec0f54 100644
--- a/gentoobrowse-api/service/utils/dbUtils.cpp
+++ b/gentoobrowse-api/service/utils/dbUtils.cpp
@@ -1,4 +1,7 @@
#include "dbUtils.h"
+#include <boost/lexical_cast.hpp>
+#include <boost/algorithm/string/join.hpp>
+#include <tablepatch.h>
namespace Gentoo {
namespace Utils {
@@ -15,6 +18,34 @@ namespace Gentoo {
db->bindNull(c);
return false;
}
+
+ std::string
+ emptyClone(DB::Connection * db, const std::string & orig)
+ {
+ auto tempTable = orig;
+ auto dot = tempTable.rfind('.');
+ if (dot != std::string::npos) {
+ tempTable = tempTable.substr(dot + 1);
+ }
+ tempTable += "_clone_" + boost::lexical_cast<std::string>(db);
+ db->execute("CREATE TEMPORARY TABLE " + tempTable + " AS SELECT * FROM " + orig + " WHERE false");
+ return tempTable;
+ }
+
+ void
+ drop(DB::Connection * db, const std::string & table)
+ {
+ db->execute("DROP TABLE " + table);
+ }
+
+ DB::ModifyCommandPtr
+ tablePatchInserter(DB::Connection * dbc, const DB::TablePatch & p)
+ {
+ return dbc->modify(
+ "INSERT INTO " + p.src +
+ "(" + boost::algorithm::join(p.cols, ", ") +
+ ") VALUES(" + boost::algorithm::join(std::vector<std::string>(p.cols.size(), "?"), ", ") + ")");
+ }
}
}
}
diff --git a/gentoobrowse-api/service/utils/dbUtils.h b/gentoobrowse-api/service/utils/dbUtils.h
index e609c2d..57554e3 100644
--- a/gentoobrowse-api/service/utils/dbUtils.h
+++ b/gentoobrowse-api/service/utils/dbUtils.h
@@ -2,11 +2,17 @@
#define GENTOOBROWSE_API_SERVICE_DBUTILS_H
#include <command.h>
+#include <modifycommand.h>
+#include <connection.h>
namespace Gentoo {
namespace Utils {
namespace Database {
bool bindOptionalsS(DB::Command * db, unsigned int c, const std::vector<boost::optional<Glib::ustring> > & vs);
+
+ std::string emptyClone(DB::Connection *, const std::string &);
+ void drop(DB::Connection *, const std::string &);
+ DB::ModifyCommandPtr tablePatchInserter(DB::Connection *, const DB::TablePatch &);
}
}
}