summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-04-30 00:56:25 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2016-04-30 00:56:25 +0100
commit82e49f12563c6af9e9b3109a996537f6613d5ce2 (patch)
tree6d71ba8b820d01a619a6f2166d5f124c2065db26
parentTidy up and provide improved variable/SQL binding (diff)
downloadicetray-82e49f12563c6af9e9b3109a996537f6613d5ce2.tar.bz2
icetray-82e49f12563c6af9e9b3109a996537f6613d5ce2.tar.xz
icetray-82e49f12563c6af9e9b3109a996537f6613d5ce2.zip
More sensible SQL script embeddingicetray-0.1
-rw-r--r--icetray/icetray/Jamfile.jam2
-rw-r--r--icetray/icetray/abstractCachingDatabaseClient.h8
-rw-r--r--icetray/icetray/abstractDatabaseClient.h7
-rw-r--r--icetray/icetray/sqlSource.cpp10
-rw-r--r--icetray/icetray/sqlSource.h19
-rw-r--r--icetray/icetray/staticSqlSource.cpp22
-rw-r--r--icetray/icetray/staticSqlSource.h22
-rw-r--r--icetray/tool/embed.sql.cpp.m43
-rw-r--r--icetray/tool/embed.sql.h.m49
-rw-r--r--icetray/unittests/testIceTrayServiceI.cpp6
10 files changed, 89 insertions, 19 deletions
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<typename Domain, typename Sql, typename ... Params>
+ template<typename Domain, typename ... Params>
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<Domain>(*cached);
}
- auto d(fetch<Domain, Sql, Params...>(params...));
+ auto d(fetch<Domain, Params...>(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 <db/sqlSelectDeserializer.h>
#include <slicer/slicer.h>
#include <visibility.h>
@@ -11,13 +12,13 @@ namespace IceTray {
protected:
AbstractDatabaseClient(DatabasePoolPtr d);
- template<typename Domain, typename Sql, typename ... Params>
+ template<typename Domain, typename ... Params>
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<Slicer::SqlSelectDeserializer, Domain>(*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<std::string>()(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 <functional>
+#include <string>
+#include <visibility.h>
+
+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<std::string>()(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<std::string>()(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 <functional>
-#include <string>
+#include <staticSqlSource.h>
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<int, sql::subdir::some>();
- fetch<int, sql::subdir::a::more>();
+ fetch<int>(sql::subdir::some);
+ fetch<int>(sql::subdir::a::more);
}
void TestIceTrayServiceI::method2(Ice::Int id, const std::string & name, const Ice::Current &)
{
- BOOST_VERIFY((fetchCache<int, sql::testIceTrayServiceTestSql>(10, id, name)) == (fetchCache<int, sql::testIceTrayServiceTestSql>(10, id, name)));
+ BOOST_VERIFY((fetchCache<int>(sql::testIceTrayServiceTestSql, 10, id, name)) == (fetchCache<int>(sql::testIceTrayServiceTestSql, 10, id, name)));
}
void TestService::addObjects(const std::string &, const Ice::CommunicatorPtr & ic, const Ice::StringSeq &, const Ice::ObjectAdapterPtr & adp)