summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/maintenance/useGroupProcessor.cpp
blob: 9749ba7c4427e947bd327720a1586d61e9fc60fb (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
#include "useGroupProcessor.h"
#include <modifycommand.h>
#include <selectcommandUtil.impl.h>
#include <tablepatch.h>
#include "utils/fileUtils.h"
#include "utils/dbUtils.h"
#include "utils/entityWhereFilter.h"
#include <glibmm/regex.h>
#include "sql/maintenance/useGroupsDelete.sql.h"
#include "sql/maintenance/useGroupsInsert.sql.h"
#include "sql/maintenance/useGroupsGetId.sql.h"

namespace U = Gentoo::Utils;

static Glib::RefPtr<Glib::Regex> useDesc = Glib::Regex::create("^([^#\\s][^ ]*)\\s+-\\s+(.*)$", Glib::RegexCompileFlags::REGEX_MULTILINE);

namespace Gentoo {
	namespace Service {
		const int UseGroupProcessor::FILETYPEID = 9;

		void
		UseGroupProcessor::created(DB::Connection * dbc, const boost::filesystem::path & fn, const boost::filesystem::path & path)
		{
			auto m = dbc->select(sql::maintenance::useGroupsInsert.getSql());
			m->bindParamS(0, fn.stem().string());
			m->forEachRow<int64_t>([this, dbc, &path](auto useGroupId) {
					this->mergeContent(dbc, path, useGroupId);
				});
		}

		void
		UseGroupProcessor::modified(DB::Connection * dbc, const boost::filesystem::path & fn, const boost::filesystem::path & path)
		{
			auto m = dbc->select(sql::maintenance::useGroupsGetId.getSql());
			m->bindParamS(0, fn.stem().string());
			m->forEachRow<int64_t>([this, dbc, &path](auto useGroupId) {
					this->mergeContent(dbc, path, useGroupId);
				});
		}

		void
		UseGroupProcessor::mergeContent(DB::Connection * dbc, const boost::filesystem::path & path, int64_t useGroupId)
		{
			DB::TablePatch p;
			p.dest = "gentoobrowse.use_group";
			U::EntityWhereFilter gwf("useGroupId", useGroupId);
			p.src = Utils::Database::emptyClone(dbc, "gentoobrowse.use_group");
			p.pk = { "useGroupId", "use" };
			p.cols = { "useGroupId", "use", "description" };
			p.where = &gwf;

			Utils::MemMap u(path);
			Glib::ustring d(std::string(reinterpret_cast<const char *>(u.data), u.getStat().st_size));
			Glib::MatchInfo matches;
			auto i = Utils::Database::tablePatchInserter(dbc, p);
			i->bindParamI(2, useGroupId);
			// Dirty, but dupes exist
			std::set<std::string> uses;
			for (useDesc->match(d, matches); matches.get_match_count() == 3; matches.next()) {
				if (uses.find(matches.fetch(1)) == uses.end()) {
					i->bindParamS(1, matches.fetch(1));
					i->bindParamS(0, matches.fetch(2));
					i->execute();
					uses.insert(matches.fetch(1));
				}
			}
			dbc->patchTable(&p);

			Utils::Database::drop(dbc, p.src);
		}

		void
		UseGroupProcessor::deleted(DB::Connection * dbc, const boost::filesystem::path & fn)
		{
			auto m = dbc->modify(sql::maintenance::useGroupsDelete.getSql());
			m->bindParamS(0, fn.stem().string());
			m->execute();
		}
	}
}