From 70c1cdfd8f34472761fe5ec97f0713990c1b4f5b Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 5 Sep 2018 21:23:41 +0300 Subject: Add multi-tenancy support --- libbrep/common.hxx | 91 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 9 deletions(-) (limited to 'libbrep/common.hxx') diff --git a/libbrep/common.hxx b/libbrep/common.hxx index 0163ca0..b9adee8 100644 --- a/libbrep/common.hxx +++ b/libbrep/common.hxx @@ -203,12 +203,14 @@ namespace brep #pragma db value struct package_id { + string tenant; package_name name; canonical_version version; package_id () = default; - package_id (package_name n, const brep::version& v) - : name (move (n)), + package_id (string t, package_name n, const brep::version& v) + : tenant (move (t)), + name (move (n)), version { v.epoch, v.canonical_upstream, v.canonical_release, v.revision} { @@ -252,6 +254,19 @@ namespace brep : (?).type ()}) \ from(brep::repository_location (std::move ((?).url), (?).type)) + // repository_id + // + #pragma db value + struct repository_id + { + string tenant; + string canonical_name; + + repository_id () = default; + repository_id (string t, string n) + : tenant (move (t)), canonical_name (move (n)) {} + }; + // Version comparison operators. // // They allow comparing objects that have epoch, canonical_upstream, @@ -381,35 +396,93 @@ namespace brep + x.revision + "DESC"; } + template + inline auto + order_by_version ( + const T& x, + bool first = true) -> //decltype ("ORDER BY" + x.epoch) + decltype (x.epoch == 0) + { + return (first ? "ORDER BY" : ", ") + + x.epoch + "," + + x.canonical_upstream + "," + + x.canonical_release + "," + + x.revision; + } + // Package id comparison operators. // inline bool operator< (const package_id& x, const package_id& y) { + if (int r = x.tenant.compare (y.tenant)) + return r < 0; + if (int r = x.name.compare (y.name)) return r < 0; return compare_version_lt (x.version, y.version, true); } - // They allow comparing objects that have name and version data members. The - // idea is that this works for both query members of package id types (in - // particular in join conditions) as well as for values of package_id type. + // They allow comparing objects that have tenant, name, and version data + // members. The idea is that this works for both query members of package id + // types (in particular in join conditions) as well as for values of + // package_id type. + // + template + inline auto + operator== (const T1& x, const T2& y) + -> decltype (x.tenant == y.tenant && + x.name == y.name && + x.version.epoch == y.version.epoch) + { + return x.tenant == y.tenant && + x.name == y.name && + compare_version_eq (x.version, y.version, true); + } + + template + inline auto + operator!= (const T1& x, const T2& y) + -> decltype (x.tenant == y.tenant && + x.name == y.name && + x.version.epoch == y.version.epoch) + { + return x.tenant != y.tenant || + x.name != y.name || + compare_version_ne (x.version, y.version, true); + } + + // Repository id comparison operators. + // + inline bool + operator< (const repository_id& x, const repository_id& y) + { + if (int r = x.tenant.compare (y.tenant)) + return r < 0; + + return x.canonical_name.compare (y.canonical_name) < 0; + } + + // They allow comparing objects that have tenant and canonical_name data + // members. The idea is that this works for both query members of repository + // id types (in particular in join conditions) as well as for values of + // repository_id type. // template inline auto operator== (const T1& x, const T2& y) - -> decltype (x.name == y.name && x.version.epoch == y.version.epoch) + -> decltype (x.tenant == y.tenant && x.canonical_name == y.canonical_name) { - return x.name == y.name && compare_version_eq (x.version, y.version, true); + return x.tenant == y.tenant && x.canonical_name == y.canonical_name; } template inline auto operator!= (const T1& x, const T2& y) - -> decltype (x.name == y.name && x.version.epoch == y.version.epoch) + -> decltype (x.tenant == y.tenant && x.canonical_name == y.canonical_name) { - return x.name != y.name || compare_version_ne (x.version, y.version, true); + return x.tenant != y.tenant || x.canonical_name != y.canonical_name; } } -- cgit v1.1