summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/utils/dbUtils.cpp
blob: 4ec0f54275fd899cc72b7a716595d1367aa6b0a4 (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
#include "dbUtils.h"
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/join.hpp>
#include <tablepatch.h>

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

			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(), "?"), ", ") + ")");
			}
		}
	}
}