From 82e49f12563c6af9e9b3109a996537f6613d5ce2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 30 Apr 2016 00:56:25 +0100 Subject: More sensible SQL script embedding --- icetray/icetray/Jamfile.jam | 2 ++ icetray/icetray/abstractCachingDatabaseClient.h | 8 ++++---- icetray/icetray/abstractDatabaseClient.h | 7 ++++--- icetray/icetray/sqlSource.cpp | 10 ++++++++++ icetray/icetray/sqlSource.h | 19 +++++++++++++++++++ icetray/icetray/staticSqlSource.cpp | 22 ++++++++++++++++++++++ icetray/icetray/staticSqlSource.h | 22 ++++++++++++++++++++++ icetray/tool/embed.sql.cpp.m4 | 3 +-- icetray/tool/embed.sql.h.m4 | 9 ++------- icetray/unittests/testIceTrayServiceI.cpp | 6 +++--- 10 files changed, 89 insertions(+), 19 deletions(-) create mode 100644 icetray/icetray/sqlSource.cpp create mode 100644 icetray/icetray/sqlSource.h create mode 100644 icetray/icetray/staticSqlSource.cpp create mode 100644 icetray/icetray/staticSqlSource.h diff --git a/icetray/icetray/Jamfile.jam b/icetray/icetray/Jamfile.jam index 2c62f6c..cc7d204 100644 --- a/icetray/icetray/Jamfile.jam +++ b/icetray/icetray/Jamfile.jam @@ -1,6 +1,8 @@ import package ; lib icetray : + sqlSource.cpp + staticSqlSource.cpp abstractDatabaseClient.cpp abstractCachingDatabaseClient.cpp defaultPool.cpp diff --git a/icetray/icetray/abstractCachingDatabaseClient.h b/icetray/icetray/abstractCachingDatabaseClient.h index 5a59867..4abeba7 100644 --- a/icetray/icetray/abstractCachingDatabaseClient.h +++ b/icetray/icetray/abstractCachingDatabaseClient.h @@ -15,20 +15,20 @@ namespace IceTray { protected: AbstractCachingDatabaseClient(DatabasePoolPtr d); - template + template inline Domain - fetchCache(time_t cacheTime, const Params & ... params) + fetchCache(const SqlSource & sql, time_t cacheTime, const Params & ... params) { CacheKey key; key.reserve(sizeof...(Params) + 2); - key.push_back(Sql::hash); + key.push_back(sql.getSqlHash()); key.push_back(typeid(Domain).hash_code()); keyPushParams(key, params...); if (auto cached = cache.get(key)) { return boost::any_cast(*cached); } - auto d(fetch(params...)); + auto d(fetch(sql, params...)); cache.add(key, CacheItem(d), time(NULL) + cacheTime); return d; } diff --git a/icetray/icetray/abstractDatabaseClient.h b/icetray/icetray/abstractDatabaseClient.h index 0661e24..50a7cda 100644 --- a/icetray/icetray/abstractDatabaseClient.h +++ b/icetray/icetray/abstractDatabaseClient.h @@ -2,6 +2,7 @@ #define ICETRAY_ABSTRACTDATABASECLIENT_H #include "database.h" +#include "sqlSource.h" #include #include #include @@ -11,13 +12,13 @@ namespace IceTray { protected: AbstractDatabaseClient(DatabasePoolPtr d); - template + template inline Domain - fetch(const Params & ... params) + fetch(const SqlSource & sql, const Params & ... params) { auto c = db->get(); - auto s = DB::SelectCommandPtr(c->newSelectCommand(Sql::sql)); + auto s = c->select(sql.getSql()); bind(0, s.get(), params...); return Slicer::DeserializeAny(*s); } diff --git a/icetray/icetray/sqlSource.cpp b/icetray/icetray/sqlSource.cpp new file mode 100644 index 0000000..b7d705e --- /dev/null +++ b/icetray/icetray/sqlSource.cpp @@ -0,0 +1,10 @@ +#include "sqlSource.h" + +namespace IceTray { + std::size_t + SqlSource::getSqlHash() const + { + return std::hash()(getSql()); + } +} + diff --git a/icetray/icetray/sqlSource.h b/icetray/icetray/sqlSource.h new file mode 100644 index 0000000..25ed933 --- /dev/null +++ b/icetray/icetray/sqlSource.h @@ -0,0 +1,19 @@ +#ifndef ICETRAY_SQLSOURCE_H +#define ICETRAY_SQLSOURCE_H + +#include +#include +#include + +namespace IceTray { + class DLL_PUBLIC SqlSource { + public: + virtual ~SqlSource() = default; + + virtual const std::string & getSql() const = 0; + virtual std::size_t getSqlHash() const; + }; +} + +#endif + diff --git a/icetray/icetray/staticSqlSource.cpp b/icetray/icetray/staticSqlSource.cpp new file mode 100644 index 0000000..3c404e3 --- /dev/null +++ b/icetray/icetray/staticSqlSource.cpp @@ -0,0 +1,22 @@ +#include "staticSqlSource.h" + +namespace IceTray { + StaticSqlSource::StaticSqlSource(const std::string & s) : + sql(s), + hash(std::hash()(sql)) + { + } + + const std::string & + StaticSqlSource::getSql() const + { + return sql; + } + + std::size_t + StaticSqlSource::getSqlHash() const + { + return hash; + } +} + diff --git a/icetray/icetray/staticSqlSource.h b/icetray/icetray/staticSqlSource.h new file mode 100644 index 0000000..5b5658f --- /dev/null +++ b/icetray/icetray/staticSqlSource.h @@ -0,0 +1,22 @@ +#ifndef ICETRAY_STATICSQLSOURCE_H +#define ICETRAY_STATICSQLSOURCE_H + +#include "sqlSource.h" + +namespace IceTray { + class DLL_PUBLIC StaticSqlSource : public SqlSource { + public: + StaticSqlSource(const std::string &); + + const std::string & getSql() const override; + std::size_t getSqlHash() const override; + + protected: + const std::string sql; + const std::size_t hash; + }; +} + +#endif + + diff --git a/icetray/tool/embed.sql.cpp.m4 b/icetray/tool/embed.sql.cpp.m4 index c150b4d..28a9747 100644 --- a/icetray/tool/embed.sql.cpp.m4 +++ b/icetray/tool/embed.sql.cpp.m4 @@ -1,7 +1,6 @@ changecom(`@@') #include "NAME.sql.h" -const std::string NAMESPACE::NAME::sql({ +const IceTray::StaticSqlSource NAMESPACE::NAME ({ patsubst(esyscmd(`xxd -p -c 12 ' SQL), `\(..\)', `0x\1, ')0x0a}); -const std::size_t NAMESPACE::NAME::hash(std::hash()(sql)); diff --git a/icetray/tool/embed.sql.h.m4 b/icetray/tool/embed.sql.h.m4 index fbabf62..e8ccee7 100644 --- a/icetray/tool/embed.sql.h.m4 +++ b/icetray/tool/embed.sql.h.m4 @@ -2,14 +2,9 @@ define(`foreach',`ifelse(eval($#>2),1, `pushdef(`$1',`$3')$2`'popdef(`$1') `'ifelse(eval($#>3),1,`$0(`$1',`$2',shift(shift(shift($@))))')')') define(NAMESPACEC,`patsubst(NAMESPACE,`::', `,')') -#include -#include +#include foreach(`ns', `namespace ns { ', NAMESPACEC) - class NAME { - public: - static const std::string sql; - static const std::size_t hash; - }; + extern const IceTray::StaticSqlSource NAME; foreach(`ns', `}', NAMESPACEC) diff --git a/icetray/unittests/testIceTrayServiceI.cpp b/icetray/unittests/testIceTrayServiceI.cpp index dde1d92..5ab0fe4 100644 --- a/icetray/unittests/testIceTrayServiceI.cpp +++ b/icetray/unittests/testIceTrayServiceI.cpp @@ -15,13 +15,13 @@ namespace TestIceTray { void TestIceTrayServiceI::method1(const Ice::Current &) { - fetch(); - fetch(); + fetch(sql::subdir::some); + fetch(sql::subdir::a::more); } void TestIceTrayServiceI::method2(Ice::Int id, const std::string & name, const Ice::Current &) { - BOOST_VERIFY((fetchCache(10, id, name)) == (fetchCache(10, id, name))); + BOOST_VERIFY((fetchCache(sql::testIceTrayServiceTestSql, 10, id, name)) == (fetchCache(sql::testIceTrayServiceTestSql, 10, id, name))); } void TestService::addObjects(const std::string &, const Ice::CommunicatorPtr & ic, const Ice::StringSeq &, const Ice::ObjectAdapterPtr & adp) -- cgit v1.2.3