#include "sqlCheck.h" #include "xmlObjectLoader.h" #include "selectcommand.h" #include "column.h" #include "rdbmsDataSource.h" #include #include "commonObjects.h" #include "sqlVariableBinder.h" #include #include DECLARE_LOADER("sqlcheck", SqlCheck); class CantCompareNulls : public std::exception { }; SqlCheck::SqlCheck(const xmlpp::Element * p) : IHaveParameters(p), ParamChecker(p), dataSource(p, "datasource"), sql(xmlChildText(p, "sql")), testOp(p->get_attribute_value("testOp")), testValue(atof(p->get_attribute_value("testValue").c_str())), query(NULL) { } SqlCheck::~SqlCheck() { delete query; } void SqlCheck::loadComplete(const CommonObjects * co) { query = co->dataSource(dataSource())->getReadonly().newSelectCommand(sql); } 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 SqlCheck::performCheck() const { BOOST_FOREACH(Parameters::value_type p, parameters) { boost::apply_visitor(SqlVariableBinder(query, atoi(p.second->name.c_str())), p.second->value); } HandleDoCompare h(testValue, testOp); while (query->fetch()) { (*query)[0].apply(h); } return h(); }