summaryrefslogtreecommitdiff
path: root/project2/sql/sqlTest.cpp
blob: 1bb66780210a8912bc6d29169ec011d2ad785706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#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/date_time/posix_time/posix_time.hpp>

NAMEDFACTORY("sqltest", SqlTest, TestFactory);

class CantCompareNulls : public std::exception { };

SqlTest::SqlTest(ScriptNodePtr p) :
	SourceObject(p),
	Test(p),
	SqlBase(p),
	filter(p, "filter", ""),
	testOp(p, "testOp", "=="),
	testValue(p, "testValue"),
	sqlCommand(p->child("sql"))
{
}

SqlTest::~SqlTest()
{
}

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 boolean(bool val) {
			doTest(val);
		}
		void floatingpoint(double val) {
			doTest(val);
		}
		void interval(const boost::posix_time::time_duration & val) {
			doTest(val);
		}
		void timestamp(const boost::posix_time::ptime & val) {
			doTest(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(ExecContext * ec) const
{
	auto con = db->getReadonly();
	DynamicSql::SqlWriterWrapper sww(ec, &sqlCommand);
	auto query = sww.getCommandFor<DB::SelectCommand>(con.get(), filter(ec));
	HandleDoCompare h(testValue(ec), testOp(ec));
	while (query->fetch()) {
		(*query)[0].apply(h);
	}
	return h();
}