diff options
Diffstat (limited to 'lib/adjective_query.cpp')
-rw-r--r-- | lib/adjective_query.cpp | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/lib/adjective_query.cpp b/lib/adjective_query.cpp index ec100e3..283fdca 100644 --- a/lib/adjective_query.cpp +++ b/lib/adjective_query.cpp | |||
@@ -53,6 +53,29 @@ namespace verbly { | |||
53 | return *this; | 53 | return *this; |
54 | } | 54 | } |
55 | 55 | ||
56 | adjective_query& adjective_query::with_prefix(filter<std::string> _f) | ||
57 | { | ||
58 | _f.clean(); | ||
59 | _with_prefix = _f; | ||
60 | |||
61 | return *this; | ||
62 | } | ||
63 | |||
64 | adjective_query& adjective_query::with_suffix(filter<std::string> _f) | ||
65 | { | ||
66 | _f.clean(); | ||
67 | _with_suffix = _f; | ||
68 | |||
69 | return *this; | ||
70 | } | ||
71 | |||
72 | adjective_query& adjective_query::with_complexity(int _arg) | ||
73 | { | ||
74 | _with_complexity = _arg; | ||
75 | |||
76 | return *this; | ||
77 | } | ||
78 | |||
56 | adjective_query& adjective_query::is_variant() | 79 | adjective_query& adjective_query::is_variant() |
57 | { | 80 | { |
58 | this->_is_variant = true; | 81 | this->_is_variant = true; |
@@ -231,6 +254,85 @@ namespace verbly { | |||
231 | case adjective::positioning::undefined: break; | 254 | case adjective::positioning::undefined: break; |
232 | } | 255 | } |
233 | 256 | ||
257 | if (!_with_prefix.empty()) | ||
258 | { | ||
259 | std::function<std::string (filter<std::string>, bool)> recur = [&] (filter<std::string> f, bool notlogic) -> std::string { | ||
260 | switch (f.get_type()) | ||
261 | { | ||
262 | case filter<std::string>::type::singleton: | ||
263 | { | ||
264 | if (notlogic == f.get_notlogic()) | ||
265 | { | ||
266 | return "base_form LIKE @PREFIX"; | ||
267 | } else { | ||
268 | return "base_form NOT LIKE @PREFIX"; | ||
269 | } | ||
270 | } | ||
271 | |||
272 | case filter<std::string>::type::group: | ||
273 | { | ||
274 | bool truelogic = notlogic != f.get_notlogic(); | ||
275 | |||
276 | std::list<std::string> clauses; | ||
277 | std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<std::string> f2) { | ||
278 | return recur(f2, truelogic); | ||
279 | }); | ||
280 | |||
281 | if (truelogic == f.get_orlogic()) | ||
282 | { | ||
283 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; | ||
284 | } else { | ||
285 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
286 | } | ||
287 | } | ||
288 | } | ||
289 | }; | ||
290 | |||
291 | conditions.push_back(recur(_with_prefix, false)); | ||
292 | } | ||
293 | |||
294 | if (!_with_suffix.empty()) | ||
295 | { | ||
296 | std::function<std::string (filter<std::string>, bool)> recur = [&] (filter<std::string> f, bool notlogic) -> std::string { | ||
297 | switch (f.get_type()) | ||
298 | { | ||
299 | case filter<std::string>::type::singleton: | ||
300 | { | ||
301 | if (notlogic == f.get_notlogic()) | ||
302 | { | ||
303 | return "base_form LIKE @SUFFIX"; | ||
304 | } else { | ||
305 | return "base_form NOT LIKE @SUFFIX"; | ||
306 | } | ||
307 | } | ||
308 | |||
309 | case filter<std::string>::type::group: | ||
310 | { | ||
311 | bool truelogic = notlogic != f.get_notlogic(); | ||
312 | |||
313 | std::list<std::string> clauses; | ||
314 | std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<std::string> f2) { | ||
315 | return recur(f2, truelogic); | ||
316 | }); | ||
317 | |||
318 | if (truelogic == f.get_orlogic()) | ||
319 | { | ||
320 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; | ||
321 | } else { | ||
322 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
323 | } | ||
324 | } | ||
325 | } | ||
326 | }; | ||
327 | |||
328 | conditions.push_back(recur(_with_suffix, false)); | ||
329 | } | ||
330 | |||
331 | if (_with_complexity != unlimited) | ||
332 | { | ||
333 | conditions.push_back("complexity = @COMPLEX"); | ||
334 | } | ||
335 | |||
234 | if (_is_variant) | 336 | if (_is_variant) |
235 | { | 337 | { |
236 | conditions.push_back("adjective_id IN (SELECT adjective_id FROM variation)"); | 338 | conditions.push_back("adjective_id IN (SELECT adjective_id FROM variation)"); |
@@ -691,6 +793,23 @@ namespace verbly { | |||
691 | sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@EXCID"), except._id); | 793 | sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@EXCID"), except._id); |
692 | } | 794 | } |
693 | 795 | ||
796 | for (auto prefix : _with_prefix.inorder_flatten()) | ||
797 | { | ||
798 | std::string pfat = prefix + "%"; | ||
799 | sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@PREFIX"), pfat.c_str(), pfat.length(), SQLITE_STATIC); | ||
800 | } | ||
801 | |||
802 | for (auto suffix : _with_suffix.inorder_flatten()) | ||
803 | { | ||
804 | std::string pfat = "%" + suffix; | ||
805 | sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@SUFFIX"), pfat.c_str(), pfat.length(), SQLITE_STATIC); | ||
806 | } | ||
807 | |||
808 | if (_with_complexity != unlimited) | ||
809 | { | ||
810 | sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@COMPLEX"), _with_complexity); | ||
811 | } | ||
812 | |||
694 | for (auto attribute : _variant_of.inorder_flatten()) | 813 | for (auto attribute : _variant_of.inorder_flatten()) |
695 | { | 814 | { |
696 | sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@ATTRID"), attribute._id); | 815 | sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@ATTRID"), attribute._id); |