From 75dbb263aeab0f6ed2726d2a5375fbf1f7696ac6 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 25 Apr 2016 22:52:30 +0100 Subject: Add some db utils functions for easing patching --- gentoobrowse-api/service/utils/dbUtils.cpp | 31 ++++++++++++++++++++++++++++++ gentoobrowse-api/service/utils/dbUtils.h | 6 ++++++ 2 files changed, 37 insertions(+) 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 +#include +#include 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(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(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 +#include +#include namespace Gentoo { namespace Utils { namespace Database { bool bindOptionalsS(DB::Command * db, unsigned int c, const std::vector > & vs); + + std::string emptyClone(DB::Connection *, const std::string &); + void drop(DB::Connection *, const std::string &); + DB::ModifyCommandPtr tablePatchInserter(DB::Connection *, const DB::TablePatch &); } } } -- cgit v1.2.3