summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2017-01-08 17:12:00 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2017-01-08 20:48:35 +0000
commita38ec1687d0f99ac18a826f57a88a4886f352c24 (patch)
tree642006021decc37dc9cced4856438eee6aa7e916
parentReplace messy m4 scripts with a proper C++ app for creating .cpp and .h for S... (diff)
downloadicetray-a38ec1687d0f99ac18a826f57a88a4886f352c24.tar.bz2
icetray-a38ec1687d0f99ac18a826f57a88a4886f352c24.tar.xz
icetray-a38ec1687d0f99ac18a826f57a88a4886f352c24.zip
Read DB command options from .sql file commentsicetray-0.1.3
-rw-r--r--icetray/icetray/abstractCachingDatabaseClient.h2
-rw-r--r--icetray/icetray/sqlSource.cpp12
-rw-r--r--icetray/icetray/sqlSource.h17
-rw-r--r--icetray/icetray/staticSqlSource.cpp20
-rw-r--r--icetray/icetray/staticSqlSource.h9
-rw-r--r--icetray/tool/icetray.jam3
-rw-r--r--icetray/tool/icetraySql.cpp41
-rw-r--r--icetray/unittests/testIceTray.cpp1
-rw-r--r--icetray/unittests/testIceTrayServiceTestSql.sql3
9 files changed, 75 insertions, 33 deletions
diff --git a/icetray/icetray/abstractCachingDatabaseClient.h b/icetray/icetray/abstractCachingDatabaseClient.h
index 4abeba7..f910626 100644
--- a/icetray/icetray/abstractCachingDatabaseClient.h
+++ b/icetray/icetray/abstractCachingDatabaseClient.h
@@ -22,7 +22,7 @@ namespace IceTray {
{
CacheKey key;
key.reserve(sizeof...(Params) + 2);
- key.push_back(sql.getSqlHash());
+ key.push_back(*sql.getCommandOptions()->hash);
key.push_back(typeid(Domain).hash_code());
keyPushParams(key, params...);
if (auto cached = cache.get(key)) {
diff --git a/icetray/icetray/sqlSource.cpp b/icetray/icetray/sqlSource.cpp
index b7d705e..de970bb 100644
--- a/icetray/icetray/sqlSource.cpp
+++ b/icetray/icetray/sqlSource.cpp
@@ -1,10 +1,16 @@
#include "sqlSource.h"
namespace IceTray {
- std::size_t
- SqlSource::getSqlHash() const
+ DB::ModifyCommandPtr
+ SqlSource::modify(DB::Connection * db) const
{
- return std::hash<std::string>()(getSql());
+ return db->modify(getSql(), getCommandOptions());
+ }
+
+ DB::SelectCommandPtr
+ SqlSource::select(DB::Connection * db) const
+ {
+ return db->select(getSql(), getCommandOptions());
}
}
diff --git a/icetray/icetray/sqlSource.h b/icetray/icetray/sqlSource.h
index a0e8ea1..70afe8c 100644
--- a/icetray/icetray/sqlSource.h
+++ b/icetray/icetray/sqlSource.h
@@ -14,21 +14,10 @@ namespace IceTray {
virtual ~SqlSource() = default;
virtual const std::string & getSql() const = 0;
- virtual std::size_t getSqlHash() const;
+ virtual const DB::CommandOptions * getCommandOptions() const = 0;
- template<typename OptsType = DB::CommandOptions, typename ... Opts>
- DB::ModifyCommandPtr modify(DB::Connection * db, const Opts & ... opts) const
- {
- OptsType o(getSqlHash(), opts...);
- return db->modify(getSql(), &o);
- }
-
- template<typename OptsType = DB::CommandOptions, typename ... Opts>
- DB::SelectCommandPtr select(DB::Connection * db, const Opts & ... opts) const
- {
- OptsType o(getSqlHash(), opts...);
- return db->select(getSql(), &o);
- }
+ DB::ModifyCommandPtr modify(DB::Connection * db) const;
+ DB::SelectCommandPtr select(DB::Connection * db) const;
};
}
diff --git a/icetray/icetray/staticSqlSource.cpp b/icetray/icetray/staticSqlSource.cpp
index 3c404e3..7e3570f 100644
--- a/icetray/icetray/staticSqlSource.cpp
+++ b/icetray/icetray/staticSqlSource.cpp
@@ -3,20 +3,32 @@
namespace IceTray {
StaticSqlSource::StaticSqlSource(const std::string & s) :
sql(s),
- hash(std::hash<std::string>()(sql))
+ opts(new DB::CommandOptions(std::hash<std::string>()(sql)))
{
}
+ StaticSqlSource::StaticSqlSource(const std::string & s, const std::string & optsName, const DB::CommandOptionsMap & map) :
+ sql(s),
+ opts(DB::CommandOptionsFactory::createNew(optsName, std::hash<std::string>()(sql), map))
+ {
+
+ }
+
+ StaticSqlSource::~StaticSqlSource()
+ {
+ delete opts;
+ }
+
const std::string &
StaticSqlSource::getSql() const
{
return sql;
}
- std::size_t
- StaticSqlSource::getSqlHash() const
+ const DB::CommandOptions *
+ StaticSqlSource::getCommandOptions() const
{
- return hash;
+ return opts;
}
}
diff --git a/icetray/icetray/staticSqlSource.h b/icetray/icetray/staticSqlSource.h
index 5b5658f..84fd402 100644
--- a/icetray/icetray/staticSqlSource.h
+++ b/icetray/icetray/staticSqlSource.h
@@ -2,18 +2,21 @@
#define ICETRAY_STATICSQLSOURCE_H
#include "sqlSource.h"
+#include <command.h>
namespace IceTray {
class DLL_PUBLIC StaticSqlSource : public SqlSource {
public:
- StaticSqlSource(const std::string &);
+ StaticSqlSource(const std::string & sql);
+ StaticSqlSource(const std::string & sql, const std::string & optsName, const DB::CommandOptionsMap &);
+ virtual ~StaticSqlSource();
const std::string & getSql() const override;
- std::size_t getSqlHash() const override;
+ const DB::CommandOptions * getCommandOptions() const override;
protected:
const std::string sql;
- const std::size_t hash;
+ const DB::CommandOptions * opts;
};
}
diff --git a/icetray/tool/icetray.jam b/icetray/tool/icetray.jam
index 1b40c0e..bc0b861 100644
--- a/icetray/tool/icetray.jam
+++ b/icetray/tool/icetray.jam
@@ -8,6 +8,7 @@ project.initialize $(__name__) ;
project icetray ;
feature.feature icetray.sql.namespace : : free ;
+feature.feature icetray.sql.connector : : free ;
feature.feature icetray.sql.basedir : : free path ;
type.register SQL : sql ;
toolset.flags icetray.c++.sql CONNECTOR <icetray.sql.connector> ;
@@ -17,7 +18,7 @@ generators.register-standard icetray.c++.sql : SQL : CPP(%.sql) H(%.sql) ;
actions icetray.c++.sql
{
- icetray-sql --basedir="$(BASEDIR)" --namespace="$(NAMESPACE)" "$(2[1])" "$(1[1])" "$(1[2])"
+ icetray-sql --basedir="$(BASEDIR)" --connector="$(CONNECTOR)" --namespace="$(NAMESPACE)" "$(2[1])" "$(1[1])" "$(1[2])"
}
IMPORT $(__name__) : icetray.c++.sql : : icetray.c++.sql ;
diff --git a/icetray/tool/icetraySql.cpp b/icetray/tool/icetraySql.cpp
index 5817ca8..8663c8b 100644
--- a/icetray/tool/icetraySql.cpp
+++ b/icetray/tool/icetraySql.cpp
@@ -5,6 +5,7 @@
#include <boost/filesystem/convenience.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
+#include <boost/algorithm/string/predicate.hpp>
#include <compileTimeFormatter.h>
namespace po = boost::program_options;
@@ -15,9 +16,13 @@ AdHocFormatter(CPPHeader,
const IceTray::StaticSqlSource )C");
AdHocFormatter(CPPNS, "%?::");
-AdHocFormatter(CPPOpen, R"C(%? (
-R"SQL()C");
-AdHocFormatter(CPPFooter, R"C()SQL");
+AdHocFormatter(CPPOpen, R"C(%?
+__attribute__((init_priority(209))) (R"SQL()C");
+AdHocFormatter(CPPConnectorOpts, R"C(, "%?", {%?})C");
+AdHocFormatter(CPPConnectorOpt, R"C({ "%?", "%?" },)C");
+AdHocFormatter(CPPConnectorOptFlag, R"C({ "%?", "" },)C");
+AdHocFormatter(CPPClose, R"C()SQL")C");
+AdHocFormatter(CPPFooter, R"C();
)C");
AdHocFormatter(HHeader,
@@ -35,7 +40,7 @@ main(int argc, char ** argv)
{
po::options_description opts("IceTray SQL-to-C++ precompiler");
fs::path sql, cpp, h, base;
- std::string sqlns;
+ std::string sqlns, connector;
opts.add_options()
("help,h", "Show this help message")
@@ -44,6 +49,7 @@ main(int argc, char ** argv)
("h", po::value(&h)->required(), "Path of header file to write")
("basedir,d", po::value(&base)->default_value(fs::current_path()), "Base directory of SQL scripts (namespaces are created relative to here)")
("namespace", po::value(&sqlns), "Namespace to create SqlSource in")
+ ("connector,c", po::value(&connector), "Specifiy a default connector name")
;
po::positional_options_description p;
@@ -76,9 +82,30 @@ main(int argc, char ** argv)
CPPNS::write(cppout, nsp);
});
CPPOpen::write(cppout, sql.stem().string());
- std::copy(std::istreambuf_iterator<char>(sqlin),
- std::istreambuf_iterator<char>(),
- std::ostreambuf_iterator<char>(cppout));
+ std::string buf;
+ std::stringstream map;
+ while (!std::getline(sqlin, buf).eof()) {
+ if (boost::algorithm::starts_with(buf, "-- libdbpp-")) {
+ connector = buf.substr(11);
+ }
+ else if (boost::algorithm::starts_with(buf, "-- libdbpp:")) {
+ auto opt = buf.substr(11);
+ auto colon = opt.find(':');
+ if (colon != std::string::npos) {
+ CPPConnectorOpt::write(map, opt.substr(0, colon), opt.substr(colon + 1));
+ }
+ else {
+ CPPConnectorOptFlag::write(map, opt);
+ }
+ }
+ else {
+ cppout << buf << std::endl;
+ }
+ }
+ CPPClose::write(cppout);
+ if (!connector.empty() && !map.str().empty()) {
+ CPPConnectorOpts::write(cppout, connector, map.str());
+ }
CPPFooter::write(cppout);
HHeader::write(hout);
diff --git a/icetray/unittests/testIceTray.cpp b/icetray/unittests/testIceTray.cpp
index 20beeb8..48bd8f1 100644
--- a/icetray/unittests/testIceTray.cpp
+++ b/icetray/unittests/testIceTray.cpp
@@ -4,6 +4,7 @@
#include <dryice.h>
#include <Ice/Communicator.h>
#include "testIceTrayServiceI.h"
+#include <staticSqlSource.h>
#include <pq-mock.h>
#include <definedDirs.h>
diff --git a/icetray/unittests/testIceTrayServiceTestSql.sql b/icetray/unittests/testIceTrayServiceTestSql.sql
index 162c801..83112a7 100644
--- a/icetray/unittests/testIceTrayServiceTestSql.sql
+++ b/icetray/unittests/testIceTrayServiceTestSql.sql
@@ -1,3 +1,6 @@
+-- libdbpp-postgresql
+-- libdbpp:no-cursor
+-- libdbpp:page-size:12
SELECT COUNT(*)
FROM testTable
WHERE id = ?