diff options
author | randomdan <randomdan@localhost> | 2011-12-20 00:30:20 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-12-20 00:30:20 +0000 |
commit | 378fb8c5c7fc306288b5a1cb48d642cbe57e32bd (patch) | |
tree | bd4191cdd1a2c84635c3c8a7d50fd2fd3723ffd6 /project2/sql/sqlTest.cpp | |
parent | Improved connection failure logging and instantiation (diff) | |
download | project2-378fb8c5c7fc306288b5a1cb48d642cbe57e32bd.tar.bz2 project2-378fb8c5c7fc306288b5a1cb48d642cbe57e32bd.tar.xz project2-378fb8c5c7fc306288b5a1cb48d642cbe57e32bd.zip |
Split check and test functionality... if and check now take tests as children, adds all, any, none and not as building blocks
Diffstat (limited to 'project2/sql/sqlTest.cpp')
-rw-r--r-- | project2/sql/sqlTest.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/project2/sql/sqlTest.cpp b/project2/sql/sqlTest.cpp new file mode 100644 index 0000000..3cf25b7 --- /dev/null +++ b/project2/sql/sqlTest.cpp @@ -0,0 +1,102 @@ +#include <pch.hpp> +#include "sqlTest.h" +#include "scriptLoader.h" +#include "selectcommand.h" +#include "column.h" +#include "rdbmsDataSource.h" +#include "commonObjects.h" +#include "sqlVariableBinder.h" +#include <boost/foreach.hpp> +#include <boost/date_time/posix_time/posix_time.hpp> + +DECLARE_LOADER("sqltest", SqlTest); + +class CantCompareNulls : public std::exception { }; + +SqlTest::SqlTest(ScriptNodePtr p) : + SourceObject(p), + Test(p), + dataSource(p, "datasource"), + filter(p, "filter", ""), + testOp(p, "testOp", "=="), + testValue(p, "testValue"), + sqlCommand(p->child("sql")) +{ +} + +SqlTest::~SqlTest() +{ +} + +void +SqlTest::loadComplete(const CommonObjects * co) +{ + db = co->dataSource<RdbmsDataSource>(dataSource()); +} + +class HandleDoCompare : public DB::HandleField { + public: + HandleDoCompare(const VariableType & tV, const std::string & tO) : + retVal(false), + testValue(tV), + testOp(tO) { + } + void null() { + throw CantCompareNulls(); + } + void string(const char *c , size_t l) { + doTest(Glib::ustring(c, c + l)); + } + void integer(int64_t val) { + doTest(val); + } + void floatingpoint(double val) { + doTest(val); + } + void timestamp(const struct tm & val) { + doTest(boost::posix_time::ptime_from_tm(val)); + } + bool operator()() const { + return retVal; + } + private: + template <typename TV> + void doTest(const TV & val) { + TV tv = testValue; + if ((testOp == "==" || testOp == "=") && val == tv) { + retVal = true; + } + else if (testOp == "<" && val < tv) { + retVal = true; + } + else if (testOp == ">" && val > tv) { + retVal = true; + } + else if (testOp == "!=" && val != tv) { + retVal = true; + } + else if ((testOp == "<=" || testOp == "=<") && val <= tv) { + retVal = true; + } + else if ((testOp == ">=" || testOp == "=>") && val >= tv) { + retVal = true; + } + } + bool retVal; + const VariableType & testValue; + std::string testOp; +}; +bool +SqlTest::passes() const +{ + boost::shared_ptr<DB::SelectCommand> query = boost::shared_ptr<DB::SelectCommand>( + db->getWritable().newSelectCommand(sqlCommand.getSqlFor(filter()))); + unsigned int offset = 0; + sqlCommand.bindParams(query.get(), offset); + HandleDoCompare h(testValue, testOp()); + while (query->fetch()) { + (*query)[0].apply(h); + } + return h(); +} + |