From 7144304c2373f16f48b3aba7d82d3580e82c2bc0 Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 17 Feb 2011 21:00:20 +0000 Subject: Add a Null variable type and support for it, this is now the default Allow all config options in the config file, even in debug (console and cgi support different things, would always break) Allow multiple query params to be set in console mode Allow url params to be set in console mode Call the new dbpp finish function to do some checking --- project2/console/consoleEnvironment.cpp | 24 +++++++++++++++--------- project2/console/consoleEnvironment.h | 4 +++- project2/environment.cpp | 8 +------- project2/fileStrmVarWriter.cpp | 3 +++ project2/fileStrmVarWriter.h | 1 + project2/rdbmsDataSource.cpp | 2 ++ project2/sqlRows.cpp | 4 +++- project2/sqlVariableBinder.cpp | 8 +++++++- project2/sqlVariableBinder.h | 2 ++ project2/variableConvert.cpp | 18 ++++++++++++++++++ project2/variables.h | 2 ++ 11 files changed, 57 insertions(+), 19 deletions(-) diff --git a/project2/console/consoleEnvironment.cpp b/project2/console/consoleEnvironment.cpp index 6b2a753..f2d4667 100644 --- a/project2/console/consoleEnvironment.cpp +++ b/project2/console/consoleEnvironment.cpp @@ -7,18 +7,18 @@ #include "../exceptions.h" #include #include +#include #include +#include namespace po = boost::program_options; template std::basic_istream<_CharT, _Traits> & -operator>>(std::basic_istream<_CharT, _Traits> & s, ConsoleEnvironment::QueryParams & q) +operator>>(std::basic_istream<_CharT, _Traits> & s, ConsoleEnvironment::QueryParams::value_type & q) { - std::string n, v; - std::getline(s, n, '='); - std::getline(s, v); - q.insert(ConsoleEnvironment::QueryParams::value_type(n, v)); + std::getline(s, q.first, '='); + std::getline(s, q.second); return s; } @@ -40,7 +40,8 @@ ConsoleEnvironment::addOptions(boost::program_options::positional_options_descri ("help,h", "Print usage and exit") ("syslogident,i", po::value(&scriptname)->default_value(scriptname), "Log to syslog with ident ") ("platform,p", po::value(&platform), "Platform") - ("queryparam,q", po::value(&queryParams), "Platform") + ("queryparam,q", po::value(&queryParams), "Query parameter") + ("uriparam,u", po::value(&uriParams), "URL paramater") ("file,f", po::value(&todolist), "File to process") ; poptions.add("file", -1); @@ -61,15 +62,20 @@ ConsoleEnvironment::postinit(const boost::program_options::options_description & } Glib::ustring -ConsoleEnvironment::getParamUri(unsigned int) const +ConsoleEnvironment::getParamUri(unsigned int idx) const { - throw NotSupported(__PRETTY_FUNCTION__); + if (idx < uriParams.size()) { + return uriParams[idx]; + } + throw UriElementOutOfRange(idx); } Glib::ustring ConsoleEnvironment::getParamQuery(const std::string & p) const { - QueryParams::const_iterator i = queryParams.find(p); + QueryParams::const_iterator i = std::find_if(queryParams.begin(), queryParams.end(), + boost::bind(&boost::algorithm::equals, p, + boost::bind(&ConsoleEnvironment::QueryParams::value_type::first, _1))); if (i != queryParams.end()) { return i->second; } diff --git a/project2/console/consoleEnvironment.h b/project2/console/consoleEnvironment.h index 82fe11f..c72eed5 100644 --- a/project2/console/consoleEnvironment.h +++ b/project2/console/consoleEnvironment.h @@ -9,7 +9,8 @@ class ConsoleEnvironment : public Environment { public: typedef std::vector ToDoList; - typedef std::map QueryParams; + typedef std::vector UriParams; + typedef std::vector > QueryParams; ConsoleEnvironment(int argc, char ** argv); virtual ~ConsoleEnvironment(); @@ -28,6 +29,7 @@ class ConsoleEnvironment : public Environment { std::string scriptname; std::string platform; QueryParams queryParams; + UriParams uriParams; ToDoList todolist; }; diff --git a/project2/environment.cpp b/project2/environment.cpp index 46ec6cf..d9e2687 100644 --- a/project2/environment.cpp +++ b/project2/environment.cpp @@ -39,13 +39,7 @@ Environment::init() // Allow anything in release, but valid the config file in debug // Some options only work in debug and you wouldn't want extra // config entries to break an install - po::store(po::parse_config_file(f, all, -#if NDEBUG - true -#else - false -#endif - ), settings); + po::store(po::parse_config_file(f, all, true), settings); } po::notify(settings); diff --git a/project2/fileStrmVarWriter.cpp b/project2/fileStrmVarWriter.cpp index 8b5b2ec..f2c8d7f 100644 --- a/project2/fileStrmVarWriter.cpp +++ b/project2/fileStrmVarWriter.cpp @@ -15,6 +15,9 @@ FileStreamVariableWriter::~FileStreamVariableWriter() { } +void FileStreamVariableWriter::operator()(const Null &) const { + fprintf(out, ""); +} void FileStreamVariableWriter::operator()(const long long int & i) const { fprintf(out, "%lld", i); } diff --git a/project2/fileStrmVarWriter.h b/project2/fileStrmVarWriter.h index ecd65b6..6b0a258 100644 --- a/project2/fileStrmVarWriter.h +++ b/project2/fileStrmVarWriter.h @@ -8,6 +8,7 @@ class FileStreamVariableWriter : public boost::static_visitor<> { FileStreamVariableWriter(FILE *, bool quoting); ~FileStreamVariableWriter(); + void operator()(const Null &) const; void operator()(const long long int & i) const; void operator()(const long int & i) const; void operator()(const int & i) const; diff --git a/project2/rdbmsDataSource.cpp b/project2/rdbmsDataSource.cpp index d7d77d3..8a095c7 100644 --- a/project2/rdbmsDataSource.cpp +++ b/project2/rdbmsDataSource.cpp @@ -129,6 +129,7 @@ RdbmsDataSource::commit() DBHosts::const_iterator m = dbhosts.find(masterDsn); if (m != dbhosts.end() && m->second->connection->inTx()) { m->second->connection->commitTx(); + m->second->connection->finish(); } } @@ -138,6 +139,7 @@ RdbmsDataSource::rollback() DBHosts::const_iterator m = dbhosts.find(masterDsn); if (m != dbhosts.end() && m->second->connection->inTx()) { m->second->connection->rollbackTx(); + m->second->connection->finish(); } changedDSNs.erase(name); } diff --git a/project2/sqlRows.cpp b/project2/sqlRows.cpp index bb94034..d33a09f 100644 --- a/project2/sqlRows.cpp +++ b/project2/sqlRows.cpp @@ -45,7 +45,9 @@ SqlRows::setFilter(const Glib::ustring & name) class HandleAsVariableType : public DB::HandleField { public: - void null() { } + void null() { + variable = Null(); + } void string(const char * c, size_t l) { variable = boost::shared_ptr(new Glib::ustring(c, c + l)); } diff --git a/project2/sqlVariableBinder.cpp b/project2/sqlVariableBinder.cpp index 22cf34a..02528d3 100644 --- a/project2/sqlVariableBinder.cpp +++ b/project2/sqlVariableBinder.cpp @@ -1,5 +1,6 @@ #include "sqlVariableBinder.h" #include "command.h" +#include "variables.h" #include SqlVariableBinder::SqlVariableBinder(DB::Command * c, unsigned int i) : @@ -8,6 +9,11 @@ SqlVariableBinder::SqlVariableBinder(DB::Command * c, unsigned int i) : { } void +SqlVariableBinder::operator()(const Null &) const +{ + cmd->bindNull(idx); +} +void SqlVariableBinder::operator()(const Glib::ustring & i) const { cmd->bindParamS(idx, i); @@ -16,7 +22,7 @@ void SqlVariableBinder::operator()(const boost::shared_ptr & i) const { if (!i) return cmd->bindNull(idx); - (*this)(*i); + cmd->bindParamS(idx, *i); } void SqlVariableBinder::operator()(const long long unsigned int & i) const diff --git a/project2/sqlVariableBinder.h b/project2/sqlVariableBinder.h index 2839e34..42a4567 100644 --- a/project2/sqlVariableBinder.h +++ b/project2/sqlVariableBinder.h @@ -9,9 +9,11 @@ namespace DB { class Command; } +class Null; class SqlVariableBinder : public boost::static_visitor<> { public: SqlVariableBinder(DB::Command * c, unsigned int i); + void operator()(const Null & i) const; void operator()(const Glib::ustring & i) const; void operator()(const boost::shared_ptr & i) const; void operator()(const long long unsigned int & i) const; diff --git a/project2/variableConvert.cpp b/project2/variableConvert.cpp index a084639..1ac2253 100644 --- a/project2/variableConvert.cpp +++ b/project2/variableConvert.cpp @@ -25,6 +25,12 @@ set(const VariableType * var, const T * t) return set(var, t, deleter); } +class NullVariable : std::runtime_error { + public: + NullVariable() : std::runtime_error("Variable has null value where one is required") { + } +}; + // Convert to Glib::ustring class ConvertVisitorGlibUstring : public boost::static_visitor { public: @@ -42,6 +48,9 @@ class ConvertVisitorGlibUstring : public boost::static_visitor & r) const { return *set(var, new Glib::ustring(boost::posix_time::to_iso_extended_string(*r))); } + const Glib::ustring & operator()(const Null &) const { + throw NullVariable(); + } template const Glib::ustring & operator()(const T & r) const { return *set(var, new Glib::ustring(boost::lexical_cast(r))); @@ -66,6 +75,9 @@ class ConvertVisitorStdString : public boost::static_visitor & r) const { return *set(var, new std::string(boost::posix_time::to_iso_extended_string(*r))); } + const std::string & operator()(const Null &) const { + throw NullVariable(); + } template const std::string & operator()(const T & r) const { return *set(var, new std::string(boost::lexical_cast(r))); @@ -90,6 +102,9 @@ class ConvertVisitorCharStar : public boost::static_visitor { const char * operator()(const boost::shared_ptr & r) const { return set(var, new std::string(boost::posix_time::to_iso_extended_string(*r)))->c_str(); } + const char * operator()(const Null &) const { + return set(var, NULL, NULL); + } template const char * operator()(const T & r) const { return set(var, new std::string(boost::lexical_cast(r)))->c_str(); @@ -116,6 +131,9 @@ class ConvertVisitorUCharStar : public boost::static_visitor( set(var, new std::string(boost::posix_time::to_iso_extended_string(*r)))->c_str()); } + const unsigned char * operator()(const Null &) const { + return set(var, NULL, NULL); + } template const unsigned char * operator()(const T & r) const { return set(var, new std::basic_string(boost::lexical_cast >(r)))->c_str(); diff --git a/project2/variables.h b/project2/variables.h index 7303e5a..ecb8268 100644 --- a/project2/variables.h +++ b/project2/variables.h @@ -13,7 +13,9 @@ #include class RowUser; +class Null { }; typedef boost::variant< + Null, // Strings Glib::ustring, boost::shared_ptr, -- cgit v1.2.3