1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#include "dbUtils.h"
#include <boost/algorithm/string/join.hpp>
#include <connection.h>
#include <format>
#include <tablepatch.h>
#include <unistd.h>
#include <vector>
namespace Gentoo::Utils::Database {
std::string
emptyClone(DB::Connection * dbc, const std::string & orig)
{
auto tempTable = orig;
auto dot = tempTable.rfind('.');
if (dot != std::string::npos) {
tempTable = tempTable.substr(dot + 1);
}
tempTable += std::format("_clone_{}{}", getpid(), static_cast<void *>(dbc));
dbc->execute(std::format("CREATE TEMPORARY TABLE {} AS SELECT * FROM {} WHERE false", tempTable, orig));
return tempTable;
}
std::pair<std::string, DB::ModifyCommandPtr>
namedTemp(
DB::Connection * dbc, const std::string & tempTable, const std::map<std::string, const std::string> & cols)
{
std::set<std::string> keys;
std::set<std::string> defs;
for (const auto & col : cols) {
keys.insert(col.first);
defs.insert(std::format("{} {}", col.first, col.second));
}
dbc->execute(std::format("CREATE TEMPORARY TABLE {}({})", tempTable, boost::join(defs, ",")));
return {tempTable, tablePatchInserter(dbc, tempTable, keys)};
}
void
drop(DB::Connection * dbc, const std::string & table)
{
dbc->execute(std::format("DROP TABLE {}", table));
}
DB::ModifyCommandPtr
tablePatchInserter(DB::Connection * dbc, const DB::TablePatch & patch)
{
return tablePatchInserter(dbc, patch.src, patch.cols);
}
DB::ModifyCommandPtr
tablePatchInserter(DB::Connection * dbc, const std::string & table, const std::set<std::string> & columns)
{
return dbc->modify(std::format("INSERT INTO {}({}) VALUES({})", table, boost::join(columns, ", "),
boost::join(std::vector<std::string>(columns.size(), "?"), ", ")));
}
}
|