diff --git a/CondCore/CondDB/src/DbCore.h b/CondCore/CondDB/src/DbCore.h index d2eb6833660a1..79602159818a5 100644 --- a/CondCore/CondDB/src/DbCore.h +++ b/CondCore/CondDB/src/DbCore.h @@ -39,11 +39,113 @@ #include #include #include +#include + // #include // macros for the schema definition +namespace condcore_detail { + consteval int string_size(char const* iSize) { return std::string_view(iSize).size(); } + + template + consteval std::array makeFullName(const char* n, const char* m) { + if (std::string_view(n).size() != N) { + throw "makeFullName first argument incorrect size"; + } + if (std::string_view(m).size() != M) { + throw "makeFullName second argument incorrect size"; + } + std::array ret = {}; + for (int i = 0; i < N; ++i) { + ret[i] = n[i]; + } + ret[N] = '.'; + for (int i = 0; i < M; ++i) { + ret[i + N + 1] = m[i]; + } + return ret; + } + + consteval void test_makeFullName() { + constexpr auto v = makeFullName<2, 3>("ab", "cde"); + static_assert(v.size() == 2 + 3 + 1 + 1); + static_assert('a' == v[0]); + static_assert('b' == v[1]); + static_assert('.' == v[2]); + static_assert('c' == v[3]); + static_assert('d' == v[4]); + static_assert('e' == v[5]); + static_assert(0 == v[6]); + } + + template + consteval std::array addMin(std::string_view n) { + if (n.size() != N) { + throw "addMin argument wrong size"; + } + std::array ret = {}; + ret[0] = 'M'; + ret[1] = 'I'; + ret[2] = 'N'; + ret[3] = '('; + for (int i = 0; i < N; ++i) { + ret[4 + i] = n[i]; + } + ret[N + 4] = ')'; + return ret; + } + + consteval void test_addMin() { + constexpr std::string_view foo("Foo"); + constexpr auto addMin_ = addMin<3>(foo); + static_assert(addMin_.size() == 9); + static_assert(addMin_[0] == 'M'); + static_assert(addMin_[1] == 'I'); + static_assert(addMin_[2] == 'N'); + static_assert(addMin_[3] == '('); + static_assert(addMin_[4] == 'F'); + static_assert(addMin_[5] == 'o'); + static_assert(addMin_[6] == 'o'); + static_assert(addMin_[7] == ')'); + static_assert(addMin_[8] == 0); + } + + template + consteval std::array addMax(std::string_view n) { + if (n.size() != N) { + throw "addMax argument wrong size"; + } + std::array ret = {}; + ret[0] = 'M'; + ret[1] = 'A'; + ret[2] = 'X'; + ret[3] = '('; + for (int i = 0; i < N; ++i) { + ret[4 + i] = n[i]; + } + ret[N + 4] = ')'; + return ret; + } + + consteval void test_addMax() { + constexpr std::string_view foo("Foo"); + constexpr auto addMin_ = addMax<3>(foo); + static_assert(addMin_.size() == 9); + static_assert(addMin_[0] == 'M'); + static_assert(addMin_[1] == 'A'); + static_assert(addMin_[2] == 'X'); + static_assert(addMin_[3] == '('); + static_assert(addMin_[4] == 'F'); + static_assert(addMin_[5] == 'o'); + static_assert(addMin_[6] == 'o'); + static_assert(addMin_[7] == ')'); + static_assert(addMin_[8] == 0); + } + +} // namespace condcore_detail + // table definition #define conddb_table(NAME) \ namespace NAME { \ @@ -54,13 +156,16 @@ // implementation for the column definition: // case with 3 params -#define FIXSIZE_COLUMN(NAME, TYPE, SIZE) \ - struct NAME { \ - static constexpr char const* name = #NAME; \ - typedef TYPE type; \ - static constexpr size_t size = SIZE; \ - static std::string tableName() { return std::string(tname); } \ - static std::string fullyQualifiedName() { return std::string(tname) + "." + std::string(name); } \ +#define FIXSIZE_COLUMN(NAME, TYPE, SIZE) \ + struct NAME { \ + static constexpr char const* name = #NAME; \ + typedef TYPE type; \ + static constexpr size_t size = SIZE; \ + static constexpr std::string_view tableName() { return std::string_view(tname); } \ + static constexpr auto fullyQualifiedName_ = \ + condcore_detail::makeFullName(tname, \ + name); \ + static constexpr std::string_view fullyQualifiedName() { return std::string_view(fullyQualifiedName_.data()); } \ }; // case with 2 params @@ -155,7 +260,7 @@ namespace cond { varId << Column::name << "_" << id; if (!whereClause.empty()) whereClause += " AND "; - whereClause += Column::fullyQualifiedName() + " " + condition + " :" + varId.str() + " "; + whereClause += std::string(Column::fullyQualifiedName()) + " " + condition + " :" + varId.str() + " "; //bool init = (id == 0); f_add_attribute(data, varId.str(), value); } @@ -331,7 +436,8 @@ namespace cond { template void setForeignKey(const std::string& name) { checkColumns<0, Column>(); - m_description.createForeignKey(name, Column::name, ReferencedColumn::tableName(), ReferencedColumn::name); + m_description.createForeignKey( + name, Column::name, std::string(ReferencedColumn::tableName()), ReferencedColumn::name); } const coral::TableDescription& get() { return m_description; } @@ -388,7 +494,7 @@ namespace cond { } }; template - struct GetFromRow > { + struct GetFromRow> { std::string operator()(const coral::AttributeList& row, const std::string& fullyQualifiedName) { std::string val = row[fullyQualifiedName].data(); if (val.size() != n) @@ -426,7 +532,7 @@ namespace cond { template typename T::type get() const { - return GetFromRow()(*m_currentRow, T::fullyQualifiedName()); + return GetFromRow()(*m_currentRow, std::string(T::fullyQualifiedName())); } auto operator*() -> decltype(std::make_tuple(this->get()...)) { return std::make_tuple(get()...); } @@ -492,7 +598,7 @@ namespace cond { } }; template - struct DefineQueryOutput > { + struct DefineQueryOutput> { static void make(coral::IQuery& query, const std::string& fullyQualifiedName) { query.addToOutputList(fullyQualifiedName); query.defineOutputType(fullyQualifiedName, coral::AttributeSpecification::typeNameForType()); @@ -514,8 +620,9 @@ namespace cond { template Query& addTable() { if (m_tables.find(Col::tableName()) == m_tables.end()) { - m_coralQuery->addToTableList(Col::tableName()); - m_tables.insert(Col::tableName()); + std::string const tb{Col::tableName()}; + m_coralQuery->addToTableList(std::string(tb)); + m_tables.insert(std::move(tb)); } return *this; } @@ -526,7 +633,7 @@ namespace cond { template void _Query() { addTable(); - DefineQueryOutput::make(*m_coralQuery, Arg1::fullyQualifiedName()); + DefineQueryOutput::make(*m_coralQuery, std::string(Arg1::fullyQualifiedName())); _Query(); } @@ -602,7 +709,7 @@ namespace cond { size_t m_retrievedRows = 0; coral::AttributeList m_whereData; std::string m_whereClause; - std::set m_tables; + std::set> m_tables; }; class UpdateBuffer { diff --git a/CondCore/CondDB/src/IOVSchema.h b/CondCore/CondDB/src/IOVSchema.h index a268795fd1dc0..a4bf12278a263 100644 --- a/CondCore/CondDB/src/IOVSchema.h +++ b/CondCore/CondDB/src/IOVSchema.h @@ -112,37 +112,43 @@ namespace cond { conddb_column(SINCE, cond::Time_t); conddb_column(PAYLOAD_HASH, std::string, PAYLOAD::PAYLOAD_HASH_SIZE); conddb_column(INSERTION_TIME, boost::posix_time::ptime); + static constexpr auto minSINCE_ = + condcore_detail::addMin(SINCE::fullyQualifiedName()); + static constexpr std::string_view minSINCE() { return std::string_view(minSINCE_.data()); } + static constexpr auto maxSINCE_ = + condcore_detail::addMax(SINCE::fullyQualifiedName()); + static constexpr std::string_view maxSINCE() { return std::string_view(maxSINCE_.data()); } struct SINCE_GROUP { typedef cond::Time_t type; static constexpr size_t size = 0; - static std::string tableName() { return SINCE::tableName(); } - static std::string fullyQualifiedName() { return "MIN(" + SINCE::fullyQualifiedName() + ")"; } + static constexpr std::string_view tableName() { return SINCE::tableName(); } + static constexpr std::string_view fullyQualifiedName() { return minSINCE(); } static std::string group(unsigned long long groupSize) { std::string sgroupSize = std::to_string(groupSize); - return "CAST(" + SINCE::fullyQualifiedName() + "/" + sgroupSize + " AS INT )*" + sgroupSize; + return "CAST(" + std::string(SINCE::fullyQualifiedName()) + "/" + sgroupSize + " AS INT )*" + sgroupSize; } }; struct SEQUENCE_SIZE { typedef unsigned int type; static constexpr size_t size = 0; - static std::string tableName() { return SINCE::tableName(); } - static std::string fullyQualifiedName() { return "COUNT(*)"; } + static constexpr std::string_view tableName() { return SINCE::tableName(); } + static constexpr std::string_view fullyQualifiedName() { return "COUNT(*)"; } }; struct MIN_SINCE { typedef cond::Time_t type; static constexpr size_t size = 0; - static std::string tableName() { return SINCE::tableName(); } - static std::string fullyQualifiedName() { return "MIN(" + SINCE::fullyQualifiedName() + ")"; } + static constexpr std::string_view tableName() { return SINCE::tableName(); } + static constexpr std::string_view fullyQualifiedName() { return minSINCE(); } }; struct MAX_SINCE { typedef cond::Time_t type; static constexpr size_t size = 0; - static std::string tableName() { return SINCE::tableName(); } - static std::string fullyQualifiedName() { return "MAX(" + SINCE::fullyQualifiedName() + ")"; } + static constexpr std::string_view tableName() { return SINCE::tableName(); } + static constexpr std::string_view fullyQualifiedName() { return maxSINCE(); } }; class Table : public IIOVTable { diff --git a/CondCore/CondDB/src/RunInfoSchema.h b/CondCore/CondDB/src/RunInfoSchema.h index 53c431e8a3515..842ab50dc90fc 100644 --- a/CondCore/CondDB/src/RunInfoSchema.h +++ b/CondCore/CondDB/src/RunInfoSchema.h @@ -18,22 +18,28 @@ namespace cond { struct MAX_RUN_NUMBER { typedef cond::Time_t type; static constexpr size_t size = 0; - static std::string tableName() { return RUN_NUMBER::tableName(); } - static std::string fullyQualifiedName() { return "MAX(" + RUN_NUMBER::fullyQualifiedName() + ")"; } + static constexpr std::string_view tableName() { return RUN_NUMBER::tableName(); } + static constexpr auto fullyQualifiedName_ = + condcore_detail::addMax(RUN_NUMBER::fullyQualifiedName()); + static constexpr std::string_view fullyQualifiedName() { return std::string_view(fullyQualifiedName_.data()); } }; struct MIN_RUN_NUMBER { typedef cond::Time_t type; static constexpr size_t size = 0; - static std::string tableName() { return RUN_NUMBER::tableName(); } - static std::string fullyQualifiedName() { return "MIN(" + RUN_NUMBER::fullyQualifiedName() + ")"; } + static constexpr std::string_view tableName() { return RUN_NUMBER::tableName(); } + static constexpr auto fullyQualifiedName_ = + condcore_detail::addMin(RUN_NUMBER::fullyQualifiedName()); + static constexpr std::string_view fullyQualifiedName() { return std::string_view(fullyQualifiedName_.data()); } }; struct MIN_START_TIME { typedef boost::posix_time::ptime type; static constexpr size_t size = 0; - static std::string tableName() { return START_TIME::tableName(); } - static std::string fullyQualifiedName() { return "MIN(" + START_TIME::fullyQualifiedName() + ")"; } + static constexpr std::string_view tableName() { return START_TIME::tableName(); } + static constexpr auto fullyQualifiedName_ = + condcore_detail::addMin(START_TIME::fullyQualifiedName()); + static constexpr std::string_view fullyQualifiedName() { return std::string_view(fullyQualifiedName_.data()); } }; class Table : public IRunInfoTable {