summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/utils/dbUtils.cpp
blob: ae07ff42c1ac559c43db3e1bb1fc0a6900cd6659 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "dbUtils.h"
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/join.hpp>
#include <tablepatch.h>
#include <buffer.h>
#include <atomic>

namespace Gentoo {
	namespace Utils {
		namespace Database {
			bool
			bindOptionalsS(DB::Command * db, unsigned int c, const std::vector<boost::optional<Glib::ustring> > & vs)
			{
				for(const auto & v : vs) {
					if (v) {
						db->bindParamS(c, *v);
						return true;
					}
				}
				db->bindNull(c);
				return false;
			}

			void
			bindOptionalS(DB::Command * db, unsigned int c, const IceUtil::Optional<std::string> & v)
			{
				if (v) {
					db->bindParamS(c, *v);
				}
				else {
					db->bindNull(c);
				}
			}

			std::atomic<uint16_t> tempTableNumber;
			std::string
			tempTableName()
			{
				return stringbf("_tmpTable_%x", tempTableNumber++);
			}

			std::string
			createTempWith(DB::Connection * db, const std::string & sql, const std::set<std::string> & keys)
			{
				auto tempTable = tempTableName();
				db->execute("CREATE TEMPORARY TABLE " + tempTable + " AS  " + sql);
				db->execute("ALTER TABLE " + tempTable + " ADD CONSTRAINT pk" + tempTable + " PRIMARY KEY (" + boost::algorithm::join(keys, ",") + ")");
				return tempTable;
			}

			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;
			}

			std::pair<std::string, DB::ModifyCommandPtr>
			customTemp(DB::Connection * db, const std::map<std::string, const std::string> & cols)
			{
				return namedTemp(db, "tmp_" + boost::lexical_cast<std::string>(db), cols);
			}

			std::pair<std::string, DB::ModifyCommandPtr>
			namedTemp(DB::Connection * db, const std::string & tempTable, const std::map<std::string, const std::string> & cols)
			{
				std::set<std::string> keys;
				std::set<std::string> defs;
				for (auto c : cols) {
					keys.insert(c.first);
					defs.insert(c.first + " " + c.second);
				}
				db->execute("CREATE TEMPORARY TABLE " + tempTable + "(" +
						boost::join(defs, ",") + ")");
				return { tempTable, tablePatchInserter(db, tempTable, keys) };
			}

			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 tablePatchInserter(dbc, p.src, p.cols);
			}
			DB::ModifyCommandPtr
			tablePatchInserter(DB::Connection * dbc, const std::string & t, const std::set<std::string> & c)
			{
				return dbc->modify(
						"INSERT INTO " + t +
						"(" + boost::algorithm::join(c, ", ") +
						") VALUES(" + boost::algorithm::join(std::vector<std::string>(c.size(), "?"), ", ") + ")");
			}
		}
	}
}