#include #include "sqlTest.h" #include "scriptLoader.h" #include "selectcommand.h" #include "column.h" #include "rdbmsDataSource.h" #include "commonObjects.h" #include "sqlVariableBinder.h" #include #include 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")), db(NULL) { } SqlTest::~SqlTest() { } void SqlTest::loadComplete(const CommonObjects * co) { db = co->dataSource(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 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 query = boost::shared_ptr( 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(); }