From 549ecb2d2d4c10b61fc6f5b41ef4e0bc5302f098 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 25 Apr 2016 02:15:28 +0100 Subject: Refactor utils --- gentoobrowse-api/service/dbUtils.cpp | 21 ----- gentoobrowse-api/service/dbUtils.h | 15 --- gentoobrowse-api/service/fileUtils.cpp | 60 ------------ gentoobrowse-api/service/fileUtils.h | 45 --------- .../service/maintenance/categoryMetaProcessor.cpp | 6 +- .../service/maintenance/ebuildMetaProcessor.cpp | 105 +++------------------ .../service/maintenance/ebuildMetaProcessor.h | 5 +- .../maintenance/packageManifestProcessor.cpp | 2 +- .../service/maintenance/packageMetaProcessor.cpp | 6 +- gentoobrowse-api/service/utils/dbUtils.cpp | 21 +++++ gentoobrowse-api/service/utils/dbUtils.h | 15 +++ .../service/utils/ebuildCacheParser.cpp | 34 +++++++ gentoobrowse-api/service/utils/ebuildCacheParser.h | 28 ++++++ .../service/utils/entityWhereFilter.cpp | 25 +++++ gentoobrowse-api/service/utils/entityWhereFilter.h | 22 +++++ gentoobrowse-api/service/utils/fileUtils.cpp | 60 ++++++++++++ gentoobrowse-api/service/utils/fileUtils.h | 45 +++++++++ .../service/utils/splitEbuildProps.cpp | 29 ++++++ gentoobrowse-api/service/utils/splitEbuildProps.h | 25 +++++ gentoobrowse-api/service/utils/xmlUtils.cpp | 26 +++++ gentoobrowse-api/service/utils/xmlUtils.h | 20 ++++ gentoobrowse-api/service/xmlUtils.cpp | 26 ----- gentoobrowse-api/service/xmlUtils.h | 20 ---- 23 files changed, 372 insertions(+), 289 deletions(-) delete mode 100644 gentoobrowse-api/service/dbUtils.cpp delete mode 100644 gentoobrowse-api/service/dbUtils.h delete mode 100644 gentoobrowse-api/service/fileUtils.cpp delete mode 100644 gentoobrowse-api/service/fileUtils.h create mode 100644 gentoobrowse-api/service/utils/dbUtils.cpp create mode 100644 gentoobrowse-api/service/utils/dbUtils.h create mode 100644 gentoobrowse-api/service/utils/ebuildCacheParser.cpp create mode 100644 gentoobrowse-api/service/utils/ebuildCacheParser.h create mode 100644 gentoobrowse-api/service/utils/entityWhereFilter.cpp create mode 100644 gentoobrowse-api/service/utils/entityWhereFilter.h create mode 100644 gentoobrowse-api/service/utils/fileUtils.cpp create mode 100644 gentoobrowse-api/service/utils/fileUtils.h create mode 100644 gentoobrowse-api/service/utils/splitEbuildProps.cpp create mode 100644 gentoobrowse-api/service/utils/splitEbuildProps.h create mode 100644 gentoobrowse-api/service/utils/xmlUtils.cpp create mode 100644 gentoobrowse-api/service/utils/xmlUtils.h delete mode 100644 gentoobrowse-api/service/xmlUtils.cpp delete mode 100644 gentoobrowse-api/service/xmlUtils.h diff --git a/gentoobrowse-api/service/dbUtils.cpp b/gentoobrowse-api/service/dbUtils.cpp deleted file mode 100644 index f2b7f4d..0000000 --- a/gentoobrowse-api/service/dbUtils.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "dbUtils.h" - -namespace Gentoo { - namespace Utils { - namespace Database { - bool - bindOptionalsS(DB::Command * db, unsigned int c, const std::vector > & vs) - { - for(const auto & v : vs) { - if (v) { - db->bindParamS(c, *v); - return true; - } - } - db->bindNull(c); - return false; - } - } - } -} - diff --git a/gentoobrowse-api/service/dbUtils.h b/gentoobrowse-api/service/dbUtils.h deleted file mode 100644 index e609c2d..0000000 --- a/gentoobrowse-api/service/dbUtils.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef GENTOOBROWSE_API_SERVICE_DBUTILS_H -#define GENTOOBROWSE_API_SERVICE_DBUTILS_H - -#include - -namespace Gentoo { - namespace Utils { - namespace Database { - bool bindOptionalsS(DB::Command * db, unsigned int c, const std::vector > & vs); - } - } -} - -#endif - diff --git a/gentoobrowse-api/service/fileUtils.cpp b/gentoobrowse-api/service/fileUtils.cpp deleted file mode 100644 index 1446b9f..0000000 --- a/gentoobrowse-api/service/fileUtils.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "fileUtils.h" -#include -#include -#include - -namespace Gentoo { - namespace Utils { - namespace File { - boost::filesystem::path operator/(const boost::filesystem::path & p, unsigned int n) - { - auto pp = p.begin(); - while (n--) ++pp; - return *pp; - } - } - - FileHandle::FileHandle(const boost::filesystem::path & path) : - fh(open(path.c_str(), O_RDONLY)) - { - if (fh < 0) { - throw std::runtime_error("Failed to open " + path.string()); - } - } - - FileHandle::~FileHandle() - { - close(fh); - } - - FileHandleStat::FileHandleStat(const boost::filesystem::path & path) : - FileHandle(path) - { - if (fstat(fh, &st)) { - throw std::runtime_error("Failed to stat " + path.string()); - } - } - - const struct stat & - FileHandleStat::getStat() const - { - return st; - } - - MemMap::MemMap(const boost::filesystem::path & path) : - FileHandleStat(path), - data(mmap(0, st.st_size, PROT_READ, MAP_SHARED, fh, 0)) - { - if (data == (void*)-1) { - throw std::runtime_error("Failed to mmap " + path.string()); - } - } - - MemMap::~MemMap() - { - munmap(data, st.st_size); - } - - } -} - diff --git a/gentoobrowse-api/service/fileUtils.h b/gentoobrowse-api/service/fileUtils.h deleted file mode 100644 index d73b1ef..0000000 --- a/gentoobrowse-api/service/fileUtils.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef GENTOOBROWSE_API_SERVICE_FILEUTILS_H -#define GENTOOBROWSE_API_SERVICE_FILEUTILS_H - -#include -#include - -namespace Gentoo { - namespace Utils { - namespace File { - boost::filesystem::path operator/(const boost::filesystem::path & p, unsigned int n); - } - - class FileHandle { - public: - FileHandle(const boost::filesystem::path & path); - virtual ~FileHandle(); - - protected: - const int fh; - }; - - class FileHandleStat : public FileHandle { - public: - FileHandleStat(const boost::filesystem::path & path); - - const struct stat & getStat() const; - - protected: - struct stat st; - }; - - class MemMap : public FileHandleStat { - public: - MemMap(const boost::filesystem::path & path); - ~MemMap(); - - void * const data; - }; - - } -} - -#endif - - diff --git a/gentoobrowse-api/service/maintenance/categoryMetaProcessor.cpp b/gentoobrowse-api/service/maintenance/categoryMetaProcessor.cpp index 781526a..31d2508 100644 --- a/gentoobrowse-api/service/maintenance/categoryMetaProcessor.cpp +++ b/gentoobrowse-api/service/maintenance/categoryMetaProcessor.cpp @@ -1,8 +1,8 @@ #include "categoryMetaProcessor.h" #include -#include "fileUtils.h" -#include "xmlUtils.h" -#include "dbUtils.h" +#include "utils/fileUtils.h" +#include "utils/xmlUtils.h" +#include "utils/dbUtils.h" #include namespace U = Gentoo::Utils; diff --git a/gentoobrowse-api/service/maintenance/ebuildMetaProcessor.cpp b/gentoobrowse-api/service/maintenance/ebuildMetaProcessor.cpp index 1bd8c6c..dbb3ea3 100644 --- a/gentoobrowse-api/service/maintenance/ebuildMetaProcessor.cpp +++ b/gentoobrowse-api/service/maintenance/ebuildMetaProcessor.cpp @@ -6,9 +6,11 @@ #include #include #include -#include "fileUtils.h" -#include "xmlUtils.h" -#include "dbUtils.h" +#include "utils/fileUtils.h" +#include "utils/dbUtils.h" +#include "utils/ebuildCacheParser.h" +#include "utils/entityWhereFilter.h" +#include "utils/splitEbuildProps.h" #include #include #include @@ -25,65 +27,6 @@ static Glib::RefPtr packageVersion = Glib::Regex::create("^(.+)-([0 namespace Gentoo { namespace Service { - class EbuildCacheParser : public U::MemMap { - public: - typedef std::pair Range; - typedef std::map KVs; - - EbuildCacheParser(const boost::filesystem::path & p) : - U::MemMap(p) - { - const char * chardata = (const char *)this->data; - while (const char * eq = strchr(chardata, '=')) { - if (const char * nl = strchr(eq + 1, '\n')) { - kvs.insert({ std::string(chardata, eq), { eq + 1, nl } }); - chardata = nl + 1; - } - else { - kvs.insert({ std::string(chardata, eq), { eq + 1, (const char *)this->data + st.st_size } }); - return; - } - } - } - - boost::optional get(const std::string & key) const - { - auto kvi = kvs.find(key); - if (kvi == kvs.end()) { - return boost::optional(); - } - return Glib::ustring(kvi->second.first, kvi->second.second); - } - - private: - KVs kvs; - }; - - class SplitEbuildProps : public DB::SqlWriter { - public: - SplitEbuildProps(const std::string & ce, int e, const std::string & cp, const boost::optional & p) : - entityId(e), - colEntityName(ce), - colPropName(cp), - props(p) - { - } - - void writeSql(AdHoc::Buffer & sql) override - { - sql.appendbf("(SELECT DISTINCT ?::int %s, trim(regexp_split_to_table(?, '\\s+'), '+') %s)", colEntityName, colPropName); - } - - void bindParams(DB::Command * c, unsigned int & offset) override - { - c->bindParamI(offset++, entityId); - Utils::Database::bindOptionalsS(c, offset++, { props }); - } - - const int entityId; - const std::string colEntityName, colPropName; - const boost::optional props; - }; const int EbuildMetaProcessor::FILETYPEID = 1; @@ -93,7 +36,7 @@ namespace Gentoo { Glib::MatchInfo matches; const Glib::ustring pv = (fn / 4).string(); if (packageVersion->match(pv, matches)) { - EbuildCacheParser ecp(path); + U::EbuildCacheParser ecp(path); const std::string repoName = (fn / 0).string(); const std::string categoryName = (fn / 3).string(); const std::string packageName = matches.fetch(1); @@ -135,7 +78,7 @@ namespace Gentoo { const Glib::ustring pv = (fn / 4).string(); if (packageVersion->match(pv, matches)) { auto m = dbc->select(sql::maintenance::ebuildUpdate::sql); - EbuildCacheParser ecp(path); + U::EbuildCacheParser ecp(path); Utils::Database::bindOptionalsS(m.get(), 0, { ecp.get("SLOT") }); Utils::Database::bindOptionalsS(m.get(), 1, { ecp.get("LICENSE") }); m->bindParamT(2, boost::posix_time::from_time_t(ecp.getStat().st_mtim.tv_sec)); @@ -150,35 +93,13 @@ namespace Gentoo { } } - class EntityWhereFilter : public DB::SqlWriter { - public: - EntityWhereFilter(const std::string & en, int64_t e) : - entityId(e), - entityColName(en) - { - } - - void writeSql(AdHoc::Buffer & sql) override - { - sql.appendbf("b.%s = ?", entityColName); - } - - void bindParams(DB::Command * c, unsigned int & offset) override - { - c->bindParamI(offset++, entityId); - } - - const int64_t entityId; - const std::string entityColName; - }; - void - EbuildMetaProcessor::perEbuildUpdates(DB::Connection * dbc, const EbuildCacheParser & ecp, int64_t ebuildId, bool newest, int64_t packageId) const + EbuildMetaProcessor::perEbuildUpdates(DB::Connection * dbc, const U::EbuildCacheParser & ecp, int64_t ebuildId, bool newest, int64_t packageId) const { - EntityWhereFilter ewf("ebuildId", ebuildId); + U::EntityWhereFilter ewf("ebuildId", ebuildId); // IUSE DB::TablePatch t; - SplitEbuildProps sep_use("ebuildId", ebuildId, "use", ecp.get("IUSE")); + U::SplitEbuildProps sep_use("ebuildId", ebuildId, "use", ecp.get("IUSE")); t.dest = "gentoobrowse.ebuild_uses"; t.srcExpr = &sep_use; t.pk = { "ebuildid", "use" }; @@ -186,7 +107,7 @@ namespace Gentoo { t.where = &ewf; dbc->patchTable(&t); // KEYWORDS - SplitEbuildProps sep_keywords("ebuildId", ebuildId, "arch", ecp.get("KEYWORDS")); + U::SplitEbuildProps sep_keywords("ebuildId", ebuildId, "arch", ecp.get("KEYWORDS")); t.dest = "gentoobrowse.ebuild_archs"; t.srcExpr = &sep_keywords; t.pk = { "ebuildid", "arch" }; @@ -195,8 +116,8 @@ namespace Gentoo { dbc->patchTable(&t); if (newest) { // HOMEPAGE - EntityWhereFilter pwf("packageId", 1); - SplitEbuildProps sep_homepage("packageId", packageId, "url", ecp.get("HOMEPAGE")); + U::EntityWhereFilter pwf("packageId", 1); + U::SplitEbuildProps sep_homepage("packageId", packageId, "url", ecp.get("HOMEPAGE")); t.dest = "gentoobrowse.package_urls"; t.srcExpr = &sep_homepage; t.pk = { "packageId", "url" }; diff --git a/gentoobrowse-api/service/maintenance/ebuildMetaProcessor.h b/gentoobrowse-api/service/maintenance/ebuildMetaProcessor.h index 42b538d..3cd6ba9 100644 --- a/gentoobrowse-api/service/maintenance/ebuildMetaProcessor.h +++ b/gentoobrowse-api/service/maintenance/ebuildMetaProcessor.h @@ -2,14 +2,13 @@ #define GENTOOBROWSE_API_SERVICE_MAINTENANCE_EBUILDMETAPROC_H #include "../maintenanceimpl.h" +#include "utils/ebuildCacheParser.h" #include #include #include namespace Gentoo { namespace Service { - class EbuildCacheParser; - class EbuildMetaProcessor : public Maintenance::FileProcessor { public: static const int FILETYPEID; @@ -19,7 +18,7 @@ namespace Gentoo { void deleted(DB::Connection * dbc, const boost::filesystem::path & fn) const; private: - void perEbuildUpdates(DB::Connection * dbc, const EbuildCacheParser & ecp, int64_t ebuildId, bool newest, int64_t packageId) const; + void perEbuildUpdates(DB::Connection * dbc, const Utils::EbuildCacheParser & ecp, int64_t ebuildId, bool newest, int64_t packageId) const; }; } } diff --git a/gentoobrowse-api/service/maintenance/packageManifestProcessor.cpp b/gentoobrowse-api/service/maintenance/packageManifestProcessor.cpp index a84f4a4..e22ce3b 100644 --- a/gentoobrowse-api/service/maintenance/packageManifestProcessor.cpp +++ b/gentoobrowse-api/service/maintenance/packageManifestProcessor.cpp @@ -1,6 +1,6 @@ #include "packageManifestProcessor.h" #include -#include "fileUtils.h" +#include "utils/fileUtils.h" using namespace Gentoo::Utils::File; diff --git a/gentoobrowse-api/service/maintenance/packageMetaProcessor.cpp b/gentoobrowse-api/service/maintenance/packageMetaProcessor.cpp index 9dcf709..5c67774 100644 --- a/gentoobrowse-api/service/maintenance/packageMetaProcessor.cpp +++ b/gentoobrowse-api/service/maintenance/packageMetaProcessor.cpp @@ -1,8 +1,8 @@ #include "packageMetaProcessor.h" #include -#include "fileUtils.h" -#include "xmlUtils.h" -#include "dbUtils.h" +#include "utils/fileUtils.h" +#include "utils/xmlUtils.h" +#include "utils/dbUtils.h" #include namespace U = Gentoo::Utils; diff --git a/gentoobrowse-api/service/utils/dbUtils.cpp b/gentoobrowse-api/service/utils/dbUtils.cpp new file mode 100644 index 0000000..f2b7f4d --- /dev/null +++ b/gentoobrowse-api/service/utils/dbUtils.cpp @@ -0,0 +1,21 @@ +#include "dbUtils.h" + +namespace Gentoo { + namespace Utils { + namespace Database { + bool + bindOptionalsS(DB::Command * db, unsigned int c, const std::vector > & vs) + { + for(const auto & v : vs) { + if (v) { + db->bindParamS(c, *v); + return true; + } + } + db->bindNull(c); + return false; + } + } + } +} + diff --git a/gentoobrowse-api/service/utils/dbUtils.h b/gentoobrowse-api/service/utils/dbUtils.h new file mode 100644 index 0000000..e609c2d --- /dev/null +++ b/gentoobrowse-api/service/utils/dbUtils.h @@ -0,0 +1,15 @@ +#ifndef GENTOOBROWSE_API_SERVICE_DBUTILS_H +#define GENTOOBROWSE_API_SERVICE_DBUTILS_H + +#include + +namespace Gentoo { + namespace Utils { + namespace Database { + bool bindOptionalsS(DB::Command * db, unsigned int c, const std::vector > & vs); + } + } +} + +#endif + diff --git a/gentoobrowse-api/service/utils/ebuildCacheParser.cpp b/gentoobrowse-api/service/utils/ebuildCacheParser.cpp new file mode 100644 index 0000000..ad894db --- /dev/null +++ b/gentoobrowse-api/service/utils/ebuildCacheParser.cpp @@ -0,0 +1,34 @@ +#include "ebuildCacheParser.h" + +namespace U = Gentoo::Utils; + +namespace Gentoo { + namespace Utils { + EbuildCacheParser::EbuildCacheParser(const boost::filesystem::path & p) : + U::MemMap(p) + { + const char * chardata = (const char *)this->data; + while (const char * eq = strchr(chardata, '=')) { + if (const char * nl = strchr(eq + 1, '\n')) { + kvs.insert({ std::string(chardata, eq), { eq + 1, nl } }); + chardata = nl + 1; + } + else { + kvs.insert({ std::string(chardata, eq), { eq + 1, (const char *)this->data + st.st_size } }); + return; + } + } + } + + boost::optional + EbuildCacheParser::get(const std::string & key) const + { + auto kvi = kvs.find(key); + if (kvi == kvs.end()) { + return boost::optional(); + } + return Glib::ustring(kvi->second.first, kvi->second.second); + } + } +} + diff --git a/gentoobrowse-api/service/utils/ebuildCacheParser.h b/gentoobrowse-api/service/utils/ebuildCacheParser.h new file mode 100644 index 0000000..e9f1dfc --- /dev/null +++ b/gentoobrowse-api/service/utils/ebuildCacheParser.h @@ -0,0 +1,28 @@ +#ifndef GENTOOBROWSE_API_SERVICE_MAINTENANCE_EBUILDCACHEPARSER_H +#define GENTOOBROWSE_API_SERVICE_MAINTENANCE_EBUILDCACHEPARSER_H + +#include "fileUtils.h" +#include +#include +#include +#include + +namespace Gentoo { + namespace Utils { + class EbuildCacheParser : public Gentoo::Utils::MemMap { + public: + typedef std::pair Range; + typedef std::map KVs; + + EbuildCacheParser(const boost::filesystem::path & p); + + boost::optional get(const std::string & key) const; + + private: + KVs kvs; + }; + } +} + +#endif + diff --git a/gentoobrowse-api/service/utils/entityWhereFilter.cpp b/gentoobrowse-api/service/utils/entityWhereFilter.cpp new file mode 100644 index 0000000..8e29cfe --- /dev/null +++ b/gentoobrowse-api/service/utils/entityWhereFilter.cpp @@ -0,0 +1,25 @@ +#include "entityWhereFilter.h" +#include + +namespace Gentoo { + namespace Utils { + EntityWhereFilter::EntityWhereFilter(const std::string & en, int64_t e) : + entityId(e), + entityColName(en) + { + } + + void + EntityWhereFilter::writeSql(AdHoc::Buffer & sql) + { + sql.appendbf("b.%s = ?", entityColName); + } + + void + EntityWhereFilter::bindParams(DB::Command * c, unsigned int & offset) + { + c->bindParamI(offset++, entityId); + } + } +} + diff --git a/gentoobrowse-api/service/utils/entityWhereFilter.h b/gentoobrowse-api/service/utils/entityWhereFilter.h new file mode 100644 index 0000000..20420c3 --- /dev/null +++ b/gentoobrowse-api/service/utils/entityWhereFilter.h @@ -0,0 +1,22 @@ +#ifndef GENTOOBROWSE_API_SERVICE_MAINTENANCE_ENTITYWHEREFILTER_H +#define GENTOOBROWSE_API_SERVICE_MAINTENANCE_ENTITYWHEREFILTER_H + +#include + +namespace Gentoo { + namespace Utils { + class EntityWhereFilter : public DB::SqlWriter { + public: + EntityWhereFilter(const std::string & en, int64_t e); + + void writeSql(AdHoc::Buffer & sql) override; + void bindParams(DB::Command * c, unsigned int & offset) override; + + const int64_t entityId; + const std::string entityColName; + }; + } +} + +#endif + diff --git a/gentoobrowse-api/service/utils/fileUtils.cpp b/gentoobrowse-api/service/utils/fileUtils.cpp new file mode 100644 index 0000000..1446b9f --- /dev/null +++ b/gentoobrowse-api/service/utils/fileUtils.cpp @@ -0,0 +1,60 @@ +#include "fileUtils.h" +#include +#include +#include + +namespace Gentoo { + namespace Utils { + namespace File { + boost::filesystem::path operator/(const boost::filesystem::path & p, unsigned int n) + { + auto pp = p.begin(); + while (n--) ++pp; + return *pp; + } + } + + FileHandle::FileHandle(const boost::filesystem::path & path) : + fh(open(path.c_str(), O_RDONLY)) + { + if (fh < 0) { + throw std::runtime_error("Failed to open " + path.string()); + } + } + + FileHandle::~FileHandle() + { + close(fh); + } + + FileHandleStat::FileHandleStat(const boost::filesystem::path & path) : + FileHandle(path) + { + if (fstat(fh, &st)) { + throw std::runtime_error("Failed to stat " + path.string()); + } + } + + const struct stat & + FileHandleStat::getStat() const + { + return st; + } + + MemMap::MemMap(const boost::filesystem::path & path) : + FileHandleStat(path), + data(mmap(0, st.st_size, PROT_READ, MAP_SHARED, fh, 0)) + { + if (data == (void*)-1) { + throw std::runtime_error("Failed to mmap " + path.string()); + } + } + + MemMap::~MemMap() + { + munmap(data, st.st_size); + } + + } +} + diff --git a/gentoobrowse-api/service/utils/fileUtils.h b/gentoobrowse-api/service/utils/fileUtils.h new file mode 100644 index 0000000..d73b1ef --- /dev/null +++ b/gentoobrowse-api/service/utils/fileUtils.h @@ -0,0 +1,45 @@ +#ifndef GENTOOBROWSE_API_SERVICE_FILEUTILS_H +#define GENTOOBROWSE_API_SERVICE_FILEUTILS_H + +#include +#include + +namespace Gentoo { + namespace Utils { + namespace File { + boost::filesystem::path operator/(const boost::filesystem::path & p, unsigned int n); + } + + class FileHandle { + public: + FileHandle(const boost::filesystem::path & path); + virtual ~FileHandle(); + + protected: + const int fh; + }; + + class FileHandleStat : public FileHandle { + public: + FileHandleStat(const boost::filesystem::path & path); + + const struct stat & getStat() const; + + protected: + struct stat st; + }; + + class MemMap : public FileHandleStat { + public: + MemMap(const boost::filesystem::path & path); + ~MemMap(); + + void * const data; + }; + + } +} + +#endif + + diff --git a/gentoobrowse-api/service/utils/splitEbuildProps.cpp b/gentoobrowse-api/service/utils/splitEbuildProps.cpp new file mode 100644 index 0000000..8116742 --- /dev/null +++ b/gentoobrowse-api/service/utils/splitEbuildProps.cpp @@ -0,0 +1,29 @@ +#include "splitEbuildProps.h" +#include +#include "dbUtils.h" + +namespace Gentoo { + namespace Utils { + SplitEbuildProps::SplitEbuildProps(const std::string & ce, int e, const std::string & cp, const boost::optional & p) : + entityId(e), + colEntityName(ce), + colPropName(cp), + props(p) + { + } + + void + SplitEbuildProps::writeSql(AdHoc::Buffer & sql) + { + sql.appendbf("(SELECT DISTINCT ?::int %s, trim(regexp_split_to_table(?, '\\s+'), '+') %s)", colEntityName, colPropName); + } + + void + SplitEbuildProps::bindParams(DB::Command * c, unsigned int & offset) + { + c->bindParamI(offset++, entityId); + Gentoo::Utils::Database::bindOptionalsS(c, offset++, { props }); + } + } +} + diff --git a/gentoobrowse-api/service/utils/splitEbuildProps.h b/gentoobrowse-api/service/utils/splitEbuildProps.h new file mode 100644 index 0000000..4739b7e --- /dev/null +++ b/gentoobrowse-api/service/utils/splitEbuildProps.h @@ -0,0 +1,25 @@ +#ifndef GENTOOBROWSE_API_SERVICE_MAINTENANCE_SPLITEBUILDPROPS_H +#define GENTOOBROWSE_API_SERVICE_MAINTENANCE_SPLITEBUILDPROPS_H + +#include +#include +#include + +namespace Gentoo { + namespace Utils { + class SplitEbuildProps : public DB::SqlWriter { + public: + SplitEbuildProps(const std::string & ce, int e, const std::string & cp, const boost::optional & p); + + void writeSql(AdHoc::Buffer & sql) override; + void bindParams(DB::Command * c, unsigned int & offset) override; + + const int entityId; + const std::string colEntityName, colPropName; + const boost::optional props; + }; + } +} + +#endif + diff --git a/gentoobrowse-api/service/utils/xmlUtils.cpp b/gentoobrowse-api/service/utils/xmlUtils.cpp new file mode 100644 index 0000000..3fc3b44 --- /dev/null +++ b/gentoobrowse-api/service/utils/xmlUtils.cpp @@ -0,0 +1,26 @@ +#include "xmlUtils.h" + +namespace Gentoo { + namespace Utils { + XmlDoc::XmlDoc(const boost::filesystem::path & path) : + xmlpp::DomParser(path.string()) + { + } + + boost::optional + XmlDoc::getXPathValue(const Glib::ustring & xp) + { + auto ns = get_document()->get_root_node()->find(xp); + if (ns.size() >= 1) { + if (auto cn = dynamic_cast(ns.front())) { + return cn->get_content(); + } + } + else if (ns.size() > 1) { + throw std::logic_error("Ambiguous xpath " + xp); + } + return boost::optional(); + } + } +} + diff --git a/gentoobrowse-api/service/utils/xmlUtils.h b/gentoobrowse-api/service/utils/xmlUtils.h new file mode 100644 index 0000000..158e134 --- /dev/null +++ b/gentoobrowse-api/service/utils/xmlUtils.h @@ -0,0 +1,20 @@ +#ifndef GENTOOBROWSE_API_SERVICE_XMLUTILS_H +#define GENTOOBROWSE_API_SERVICE_XMLUTILS_H + +#include +#include +#include + +namespace Gentoo { + namespace Utils { + class XmlDoc : public xmlpp::DomParser { + public: + XmlDoc(const boost::filesystem::path &); + + boost::optional getXPathValue(const Glib::ustring &); + }; + } +} + +#endif + diff --git a/gentoobrowse-api/service/xmlUtils.cpp b/gentoobrowse-api/service/xmlUtils.cpp deleted file mode 100644 index 3fc3b44..0000000 --- a/gentoobrowse-api/service/xmlUtils.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "xmlUtils.h" - -namespace Gentoo { - namespace Utils { - XmlDoc::XmlDoc(const boost::filesystem::path & path) : - xmlpp::DomParser(path.string()) - { - } - - boost::optional - XmlDoc::getXPathValue(const Glib::ustring & xp) - { - auto ns = get_document()->get_root_node()->find(xp); - if (ns.size() >= 1) { - if (auto cn = dynamic_cast(ns.front())) { - return cn->get_content(); - } - } - else if (ns.size() > 1) { - throw std::logic_error("Ambiguous xpath " + xp); - } - return boost::optional(); - } - } -} - diff --git a/gentoobrowse-api/service/xmlUtils.h b/gentoobrowse-api/service/xmlUtils.h deleted file mode 100644 index 158e134..0000000 --- a/gentoobrowse-api/service/xmlUtils.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef GENTOOBROWSE_API_SERVICE_XMLUTILS_H -#define GENTOOBROWSE_API_SERVICE_XMLUTILS_H - -#include -#include -#include - -namespace Gentoo { - namespace Utils { - class XmlDoc : public xmlpp::DomParser { - public: - XmlDoc(const boost::filesystem::path &); - - boost::optional getXPathValue(const Glib::ustring &); - }; - } -} - -#endif - -- cgit v1.2.3