summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/filter.cpp34
-rw-r--r--lib/filter.h82
-rw-r--r--lib/statement.cpp2
3 files changed, 76 insertions, 42 deletions
diff --git a/lib/filter.cpp b/lib/filter.cpp index 1af7314..17309f1 100644 --- a/lib/filter.cpp +++ b/lib/filter.cpp
@@ -871,7 +871,7 @@ namespace verbly {
871 throw std::domain_error("This filter has no children"); 871 throw std::domain_error("This filter has no children");
872 } 872 }
873 } 873 }
874 874
875 filter filter::operator!() const 875 filter filter::operator!() const
876 { 876 {
877 switch (type_) 877 switch (type_)
@@ -1371,4 +1371,36 @@ namespace verbly {
1371 } 1371 }
1372 } 1372 }
1373 1373
1374 filter filter::compact() const
1375 {
1376 switch (type_)
1377 {
1378 case type::empty:
1379 case type::singleton:
1380 {
1381 return *this;
1382 }
1383
1384 case type::group:
1385 {
1386 filter result(group_.orlogic);
1387 for (const filter& child : group_.children)
1388 {
1389 filter compactedChild = child.compact();
1390 if (compactedChild.type_ != type::empty)
1391 {
1392 result += child;
1393 }
1394 }
1395
1396 if (group_.children.empty())
1397 {
1398 result = {};
1399 }
1400
1401 return result;
1402 }
1403 }
1404 }
1405
1374}; 1406};
diff --git a/lib/filter.h b/lib/filter.h index d213d7a..dcadf95 100644 --- a/lib/filter.h +++ b/lib/filter.h
@@ -8,7 +8,7 @@
8#include "enums.h" 8#include "enums.h"
9 9
10namespace verbly { 10namespace verbly {
11 11
12 class filter { 12 class filter {
13 public: 13 public:
14 enum class type { 14 enum class type {
@@ -16,7 +16,7 @@ namespace verbly {
16 singleton, 16 singleton,
17 group 17 group
18 }; 18 };
19 19
20 enum class comparison { 20 enum class comparison {
21 int_equals, 21 int_equals,
22 int_does_not_equal, 22 int_does_not_equal,
@@ -36,87 +36,89 @@ namespace verbly {
36 hierarchally_matches, 36 hierarchally_matches,
37 does_not_hierarchally_match 37 does_not_hierarchally_match
38 }; 38 };
39 39
40 // Copy and move constructors 40 // Copy and move constructors
41 41
42 filter(const filter& other); 42 filter(const filter& other);
43 filter(filter&& other); 43 filter(filter&& other);
44 44
45 // Assignment 45 // Assignment
46 46
47 filter& operator=(filter other); 47 filter& operator=(filter other);
48 48
49 // Swap 49 // Swap
50 50
51 friend void swap(filter& first, filter& second); 51 friend void swap(filter& first, filter& second);
52 52
53 // Destructor 53 // Destructor
54 54
55 ~filter(); 55 ~filter();
56 56
57 // Accessors 57 // Accessors
58 58
59 type getType() const 59 type getType() const
60 { 60 {
61 return type_; 61 return type_;
62 } 62 }
63 63
64 // Empty 64 // Empty
65 65
66 filter(); 66 filter();
67 67
68 // Singleton 68 // Singleton
69 69
70 filter(field filterField, comparison filterType, int filterValue); 70 filter(field filterField, comparison filterType, int filterValue);
71 filter(field filterField, comparison filterType, std::string filterValue); 71 filter(field filterField, comparison filterType, std::string filterValue);
72 filter(field filterField, comparison filterType, bool filterValue); 72 filter(field filterField, comparison filterType, bool filterValue);
73 filter(field filterField, comparison filterType); 73 filter(field filterField, comparison filterType);
74 filter(field joinOn, comparison filterType, filter joinCondition); 74 filter(field joinOn, comparison filterType, filter joinCondition);
75 75
76 field getField() const; 76 field getField() const;
77 77
78 comparison getComparison() const; 78 comparison getComparison() const;
79 79
80 filter getJoinCondition() const; 80 filter getJoinCondition() const;
81 81
82 std::string getStringArgument() const; 82 std::string getStringArgument() const;
83 83
84 int getIntegerArgument() const; 84 int getIntegerArgument() const;
85 85
86 bool getBooleanArgument() const; 86 bool getBooleanArgument() const;
87 87
88 // Group 88 // Group
89 89
90 explicit filter(bool orlogic); 90 explicit filter(bool orlogic);
91 91
92 bool getOrlogic() const; 92 bool getOrlogic() const;
93 93
94 filter operator+(filter condition) const; 94 filter operator+(filter condition) const;
95 95
96 filter& operator+=(filter condition); 96 filter& operator+=(filter condition);
97 97
98 using const_iterator = std::list<filter>::const_iterator; 98 using const_iterator = std::list<filter>::const_iterator;
99 99
100 const_iterator begin() const; 100 const_iterator begin() const;
101 101
102 const_iterator end() const; 102 const_iterator end() const;
103 103
104 // Negation 104 // Negation
105 105
106 filter operator!() const; 106 filter operator!() const;
107 107
108 // Groupifying 108 // Groupifying
109 109
110 filter operator&&(filter condition) const; 110 filter operator&&(filter condition) const;
111 filter operator||(filter condition) const; 111 filter operator||(filter condition) const;
112 112
113 filter& operator&=(filter condition); 113 filter& operator&=(filter condition);
114 filter& operator|=(filter condition); 114 filter& operator|=(filter condition);
115 115
116 // Utility 116 // Utility
117 117
118 filter normalize(object context) const; 118 filter normalize(object context) const;
119 119
120 filter compact() const;
121
120 private: 122 private:
121 union { 123 union {
122 struct { 124 struct {
@@ -135,9 +137,9 @@ namespace verbly {
135 } group_; 137 } group_;
136 }; 138 };
137 type type_ = type::empty; 139 type type_ = type::empty;
138 140
139 }; 141 };
140 142
141}; 143};
142 144
143#endif /* end of include guard: FILTER_H_932BA9C6 */ 145#endif /* end of include guard: FILTER_H_932BA9C6 */
diff --git a/lib/statement.cpp b/lib/statement.cpp index 64a4311..222a8eb 100644 --- a/lib/statement.cpp +++ b/lib/statement.cpp
@@ -16,7 +16,7 @@ namespace verbly {
16 statement::statement( 16 statement::statement(
17 object context, 17 object context,
18 filter queryFilter) : 18 filter queryFilter) :
19 statement(getTableForContext(context), queryFilter.normalize(context)) 19 statement(getTableForContext(context), queryFilter.compact().normalize(context))
20 { 20 {
21 } 21 }
22 22