forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRowFilter.cpp
More file actions
217 lines (181 loc) · 4.98 KB
/
RowFilter.cpp
File metadata and controls
217 lines (181 loc) · 4.98 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// RowFilter.cpp
//
// $Id: //poco/Main/Data/src/RowFilter.cpp#1 $
//
// Library: Data
// Package: DataCore
// Module: RowFilter
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/RowFilter.h"
#include "Poco/Data/RecordSet.h"
#include "Poco/String.h"
#include "Poco/Exception.h"
#include <functional>
namespace Poco {
namespace Data {
RowFilter::RowFilter(RecordSet* pRecordSet): _pRecordSet(pRecordSet), _not(false)
{
poco_check_ptr(pRecordSet);
init();
_pRecordSet->filter(this);
}
RowFilter::RowFilter(Ptr pParent, LogicOperator op): _pRecordSet(0),
_pParent(pParent),
_not(false)
{
poco_check_ptr(_pParent.get());
init();
_pParent->addFilter(this, op);
}
void RowFilter::init()
{
_comparisons.insert(Comparisons::value_type("<", VALUE_LESS_THAN));
_comparisons.insert(Comparisons::value_type("<=", VALUE_LESS_THAN_OR_EQUAL));
_comparisons.insert(Comparisons::value_type("=", VALUE_EQUAL));
_comparisons.insert(Comparisons::value_type("==", VALUE_EQUAL));
_comparisons.insert(Comparisons::value_type(">", VALUE_GREATER_THAN));
_comparisons.insert(Comparisons::value_type(">=", VALUE_GREATER_THAN_OR_EQUAL));
_comparisons.insert(Comparisons::value_type("<>", VALUE_NOT_EQUAL));
_comparisons.insert(Comparisons::value_type("!=", VALUE_NOT_EQUAL));
_comparisons.insert(Comparisons::value_type("IS NULL", VALUE_IS_NULL));
duplicate();
}
RowFilter::~RowFilter()
{
try
{
release();
if (_pRecordSet) _pRecordSet->filter(0);
if (_pParent.get()) _pParent->removeFilter(this);
}
catch (...)
{
poco_unexpected();
}
}
bool RowFilter::isAllowed(std::size_t row) const
{
Poco::Dynamic::Var retVal;
const RecordSet& rs = recordSet();
std::size_t columns = rs.columnCount();
ComparisonMap::const_iterator it = _comparisonMap.begin();
ComparisonMap::const_iterator end = _comparisonMap.end();
for (; it != end; ++it)
{
for (std::size_t col = 0; col < columns; ++col)
{
const std::string name = toUpper(rs.metaColumn(static_cast<UInt32>(col)).name());
if (_comparisonMap.find(name) == _comparisonMap.end()) continue;
Poco::Dynamic::Var ret;
CompT compOp = 0;
Poco::Dynamic::Var val = rs.value(col, row, false);
switch (it->second.get<1>())
{
case VALUE_LESS_THAN:
compOp = less; break;
case VALUE_LESS_THAN_OR_EQUAL:
compOp = lessOrEqual; break;
case VALUE_EQUAL:
compOp = equal; break;
case VALUE_GREATER_THAN:
compOp = greater; break;
case VALUE_GREATER_THAN_OR_EQUAL:
compOp = greaterOrEqual; break;
case VALUE_NOT_EQUAL:
compOp = notEqual; break;
case VALUE_IS_NULL:
compOp = isNull; break;
default:
throw IllegalStateException("Unsupported comparison criteria.");
}
doCompare(ret, val, compOp, it->second);
if (retVal.isEmpty()) retVal = ret;
else retVal = retVal || ret;
}
}
// iterate through children
FilterMap::const_iterator fIt = _filterMap.begin();
FilterMap::const_iterator fEnd = _filterMap.end();
for (; fIt != fEnd; ++fIt)
{
if (OP_OR == fIt->second)
{
if (retVal.isEmpty())
retVal = fIt->first->isAllowed(row);
else
retVal = retVal || fIt->first->isAllowed(row);
}
else if (OP_AND == fIt->second)
{
if (retVal.isEmpty())
retVal = fIt->first->isAllowed(row);
else
retVal = retVal && fIt->first->isAllowed(row);
}
else
throw IllegalStateException("Unknown logical operation.");
}
if (retVal.isEmpty()) retVal = true; // no filtering found
return (!_not) && retVal.extract<bool>();
}
int RowFilter::remove(const std::string& name)
{
poco_check_ptr (_pRecordSet);
_pRecordSet->moveFirst();
return static_cast<int>(_comparisonMap.erase(toUpper(name)));
}
RowFilter::Comparison RowFilter::getComparison(const std::string& comp) const
{
Comparisons::const_iterator it = _comparisons.find(toUpper(comp));
if (it == _comparisons.end())
throw NotFoundException("Comparison not found", comp);
return it->second;
}
void RowFilter::addFilter(const Ptr& pFilter, LogicOperator comparison)
{
poco_check_ptr (_pRecordSet);
pFilter->_pRecordSet = _pRecordSet;
_pRecordSet->moveFirst();
_filterMap.insert(FilterMap::value_type(pFilter, comparison));
}
void RowFilter::removeFilter(const Ptr& pFilter)
{
poco_check_ptr (_pRecordSet);
pFilter->_pRecordSet = 0;
_pRecordSet->moveFirst();
_filterMap.erase(pFilter);
}
void RowFilter::doCompare(Poco::Dynamic::Var& ret,
Poco::Dynamic::Var& val,
CompT comp,
const ComparisonEntry& ce)
{
if (ret.isEmpty()) ret = comp(val, ce.get<0>());
else
{
if (ce.get<2>() == OP_OR)
ret = ret || comp(val, ce.get<0>());
else if (ce.get<2>() == OP_AND)
ret = ret && comp(val, ce.get<0>());
else
throw IllegalStateException("Unknown logical operation.");
}
}
RecordSet& RowFilter::recordSet() const
{
if (!_pRecordSet)
{
Ptr pParent = _pParent;
while (pParent && !_pRecordSet)
_pRecordSet = pParent->_pRecordSet;
}
poco_check_ptr (_pRecordSet);
return *_pRecordSet;
}
} } // namespace Poco::Data