From a10dde58066098f9fcfd87e2487a16c8d25cb4da Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 24 May 2016 22:09:25 +0100 Subject: Extend EntityWhereFilter to operate on a set of Ids --- .../service/utils/entityWhereFilter.cpp | 28 +++++++++++++++++++--- gentoobrowse-api/service/utils/entityWhereFilter.h | 7 +++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/gentoobrowse-api/service/utils/entityWhereFilter.cpp b/gentoobrowse-api/service/utils/entityWhereFilter.cpp index d6189bd..be80971 100644 --- a/gentoobrowse-api/service/utils/entityWhereFilter.cpp +++ b/gentoobrowse-api/service/utils/entityWhereFilter.cpp @@ -3,8 +3,19 @@ namespace Gentoo { namespace Utils { + EntityWhereFilter::EntityWhereFilter(const std::string & en) : + entityColName(en) + { + } + EntityWhereFilter::EntityWhereFilter(const std::string & en, int64_t e) : - entityId(e), + entityColName(en) + { + entityIds.insert(e); + } + + EntityWhereFilter::EntityWhereFilter(const std::string & en, const EntityIds & e) : + entityIds(e), entityColName(en) { } @@ -12,13 +23,24 @@ namespace Gentoo { void EntityWhereFilter::writeSql(AdHoc::Buffer & sql) { - sql.appendbf("a.%s = ?", entityColName); + sql.appendbf("a.%s IN (", entityColName); + for (EntityIds::const_iterator ei = entityIds.begin(); ei != entityIds.end(); ++ei) { + if (ei != entityIds.begin()) { + sql.append(", ?"); + } + else { + sql.append("?"); + } + } + sql.append(")"); } void EntityWhereFilter::bindParams(DB::Command * c, unsigned int & offset) { - c->bindParamI(offset++, entityId); + for (const auto & entityId : entityIds) { + c->bindParamI(offset++, entityId); + } } } } diff --git a/gentoobrowse-api/service/utils/entityWhereFilter.h b/gentoobrowse-api/service/utils/entityWhereFilter.h index 20420c3..a637288 100644 --- a/gentoobrowse-api/service/utils/entityWhereFilter.h +++ b/gentoobrowse-api/service/utils/entityWhereFilter.h @@ -2,17 +2,22 @@ #define GENTOOBROWSE_API_SERVICE_MAINTENANCE_ENTITYWHEREFILTER_H #include +#include namespace Gentoo { namespace Utils { class EntityWhereFilter : public DB::SqlWriter { public: + typedef std::set EntityIds; + + EntityWhereFilter(const std::string & en); EntityWhereFilter(const std::string & en, int64_t e); + EntityWhereFilter(const std::string & en, const EntityIds & e); void writeSql(AdHoc::Buffer & sql) override; void bindParams(DB::Command * c, unsigned int & offset) override; - const int64_t entityId; + EntityIds entityIds; const std::string entityColName; }; } -- cgit v1.2.3