AnnaDB 1.0
Loading...
Searching...
No Matches
query.hpp
1//
2// Created by felix on 16.02.23.
3//
4
5#ifndef ANNADB_DRIVER_QUERY_HPP
6#define ANNADB_DRIVER_QUERY_HPP
7
8#include <string>
9#include <cstdarg>
10#include <utility>
11#include "TySON.hpp"
12#include "query_comparision.hpp"
13
15{
16
23 enum class UpdateType
24 {
25 Inc = 0,
26 Set = 1,
27 };
28
29 struct SortCmd
30 {
31 virtual ~SortCmd() = default;
32 virtual std::string data() const = 0;
33 };
34
35 class Asc : public SortCmd
36 {
37 std::string field_;
38
39 public:
40
46 explicit Asc(std::string_view field) noexcept : field_(field) {}
47
48 [[nodiscard]] std::string data() const override
49 {
50 return "asc(value|" + field_ + "|)";
51 }
52 };
53
54 class Desc : public SortCmd
55 {
56 std::string field_;
57
58 public:
59
65 explicit Desc(std::string_view field) noexcept : field_(field) {}
66
67 [[nodiscard]] std::string data() const override
68 {
69 return "desc(value|" + field_ + "|)";
70 }
71 };
72
73 class QueryCmd;
74
76 {
77 std::string name_;
78 bool start_cmd_ = false;
79 [[nodiscard]] virtual std::string annadb_query() = 0;
80 [[nodiscard]] virtual std::vector<std::string> previous_steps_() = 0;
81 [[nodiscard]] virtual std::vector<std::string> next_steps_() = 0;
82
83 public:
84 QueryCmd() = default;
85 explicit QueryCmd(std::string_view name, bool start_cmd) : name_(name), start_cmd_(start_cmd) {}
86 virtual ~QueryCmd() = default;
87
88 [[nodiscard]] std::string name() noexcept
89 {
90 return this->name_;
91 }
92
93 [[nodiscard]] bool can_start_pipeline() const noexcept
94 {
95 return start_cmd_;
96 }
97
98 [[nodiscard]] virtual std::string query() noexcept
99 {
100 return this->annadb_query();
101 }
102
103 [[nodiscard]] bool next_step_allowed(const std::string &cmdName) noexcept
104 {
105 if (next_steps_().empty())
106 {
107 return false;
108 }
109
110 auto res = std::find(next_steps_().begin(), next_steps_().end(), cmdName);
111 return res != next_steps_().end();
112 }
113
114 [[nodiscard]] bool previous_step_allowed(const std::string &cmdName) noexcept
115 {
116 if (previous_steps_().empty())
117 {
118 return false;
119 }
120
121 auto res = std::find(previous_steps_().begin(), previous_steps_().end(), cmdName);
122 return res != next_steps_().end();
123 }
124 };
125
126
127 class Insert : public QueryCmd
128 {
129 std::vector<tyson::TySonObject> values_;
130
131 std::string annadb_query() override
132 {
133 std::stringstream sstream;
134 sstream << "insert[";
135 std::for_each(values_.begin(), values_.end(),
136 [&sstream](auto &val){ sstream << val << ",";});
137 sstream << "]";
138 return sstream.str();
139 }
140
141 [[nodiscard]] std::vector<std::string> previous_steps_() noexcept override
142 {
143 return {};
144 };
145 [[nodiscard]] std::vector<std::string> next_steps_() noexcept override
146 {
147 return {};
148 }
149
150 public:
151
158 explicit Insert(tyson::TySonObject &obj) noexcept : QueryCmd("insert", true)
159 {
160 values_.emplace_back(std::move(obj));
161 }
162
163 template<typename ...T>
164 explicit Insert(T&& ...values) noexcept : QueryCmd("insert", true)
165 {
166 std::vector<tyson::TySonObject> objs {};
167 objs.reserve(sizeof...(values));
168 (objs.emplace_back(values), ...);
169
170 values_ = std::move(objs);
171 }
172
179 explicit Insert(std::vector<tyson::TySonObject> &objs) noexcept : QueryCmd("insert", true), values_(std::move(objs)) {}
180
181 };
182
183
184 class Get : public QueryCmd
185 {
186 std::vector<tyson::TySonObject> values_;
187
188 std::string annadb_query() override
189 {
190 std::stringstream sstream;
191 sstream << "get[";
192 std::for_each(values_.begin(), values_.end(),
193 [&sstream](auto &val){ sstream << val << ",";});
194 sstream << "]";
195 return sstream.str();
196 }
197
198 [[nodiscard]] std::vector<std::string> previous_steps_() noexcept override
199 {
200 return { "find", "get", "sort", "limit", "offset"};
201 };
202 [[nodiscard]] std::vector<std::string> next_steps_() noexcept override
203 {
204 return {"find", "get", "sort", "limit", "offset", "update", "delete"};
205 }
206
207 public:
208
217 Get(tyson::TySonObject &obj) : QueryCmd("get", true)
218 {
219 if (obj.type() != tyson::TySonType::Link)
220 {
221 throw std::invalid_argument(".get can only be used with a TySonObject of TySonType::Link.");
222 }
223
224 values_.emplace_back(obj);
225 }
226
235 Get(std::vector<tyson::TySonObject> &objs) : QueryCmd("get", true)
236 {
237 auto all_Links = std::all_of(objs.cbegin(), objs.cend(), [](const tyson::TySonObject val)
238 {
239 return val.type() == tyson::TySonType::Link;
240 });
241
242 if (!all_Links)
243 {
244 throw std::invalid_argument(".get can only be used with a TySonObject of TySonType::Link.");
245 }
246
247 values_ = std::move(objs);
248 }
249
250 template<typename ...T>
251 explicit Get(T &&...values) : QueryCmd("get", true)
252 {
253 std::vector<tyson::TySonObject> objs {};
254 objs.reserve(sizeof...(values));
255 (objs.emplace_back(values), ...);
256
257 auto all_Links = std::all_of(objs.cbegin(), objs.cend(), [](const tyson::TySonObject val)
258 {
259 return val.type() == tyson::TySonType::Link;
260 });
261
262 if (!all_Links)
263 {
264 throw std::invalid_argument(".get can only be used with a TySonObject of TySonType::Link.");
265 }
266
267 values_ = std::move(objs);
268 }
269 };
270
271
272 class Find : public QueryCmd
273 {
274 std::vector<std::unique_ptr<Comparison>> comparators_;
275 std::string annadb_query() override
276 {
277 std::stringstream sstream;
278 sstream << "find[";
279 for (auto &val : comparators_)
280 {
281 sstream << val->str() << ",";
282 }
283 sstream << "]";
284 return sstream.str();
285 }
286
287 [[nodiscard]] std::vector<std::string> previous_steps_() noexcept override
288 {
289 return { "find", "get", "sort", "limit", "offset"};
290 };
291 [[nodiscard]] std::vector<std::string> next_steps_() noexcept override
292 {
293 return {"find", "get", "sort", "limit", "offset", "update", "delete"};
294 }
295
296 public:
297 Find() noexcept : QueryCmd("find", true) {};
298 Find(Find &&rhs) = default;
299
300 Find& eq(tyson::TySonObject &value) noexcept
301 {
302 comparators_.emplace_back(std::make_unique<Eq>(value));
303 return *this;
304 }
305
306 Find& eq(std::string_view path_to_field, tyson::TySonObject &value) noexcept
307 {
308 comparators_.emplace_back(std::make_unique<Eq>(path_to_field, value));
309 return *this;
310 }
311
318 static Find EQ(tyson::TySonObject &value) noexcept
319 {
320 Find find {};
321 find.eq(value);
322 return find;
323 }
324
325 Find& neq(tyson::TySonObject &value) noexcept
326 {
327 comparators_.emplace_back(std::make_unique<Neq>(value));
328 return *this;
329 }
330
331 Find& neq(std::string_view path_to_field, tyson::TySonObject &value) noexcept
332 {
333 comparators_.emplace_back(std::make_unique<Neq>(path_to_field, value));
334 return *this;
335 }
336
343 static Find NEQ(tyson::TySonObject &value) noexcept
344 {
345 Find find {};
346 find.neq(value);
347 return find;
348 }
349
350 template<std::convertible_to<tyson::TySonObject> T>
351 Find& gt(T &&value) noexcept
352 {
353 comparators_.emplace_back(std::make_unique<Gt>(std::forward<tyson::TySonObject>(value)));
354 return *this;
355 }
356
357 Find& gt(tyson::TySonObject &value) noexcept
358 {
359 comparators_.emplace_back(std::make_unique<Gt>(value));
360 return *this;
361 }
362
363 template<std::convertible_to<tyson::TySonObject> T>
364 Find& gt(std::string_view path_to_field, T &&value) noexcept
365 {
366 comparators_.emplace_back(std::make_unique<Gt>(path_to_field, std::forward<tyson::TySonObject>(value)));
367 return *this;
368 }
369
370 Find& gt(std::string_view path_to_field, tyson::TySonObject &value) noexcept
371 {
372 comparators_.emplace_back(std::make_unique<Gt>(path_to_field, value));
373 return *this;
374 }
375
382 template<std::convertible_to<tyson::TySonObject> T>
383 static Find GT(T &&value) noexcept
384 {
385 Find find {};
386 find.gt(value);
387 return find;
388 }
389
390 template<std::convertible_to<tyson::TySonObject> T>
391 Find& gte(T &&value) noexcept
392 {
393 comparators_.emplace_back(std::make_unique<Gte>(std::forward<tyson::TySonObject>(value)));
394 return *this;
395 }
396
397 template<std::convertible_to<tyson::TySonObject> T>
398 Find& gte(std::string_view path_to_field, T &&value) noexcept
399 {
400 comparators_.emplace_back(std::make_unique<Gte>(path_to_field, std::forward<tyson::TySonObject>(value)));
401 return *this;
402 }
403
410 template<std::convertible_to<tyson::TySonObject> T>
411 static Find GTE(T value) noexcept
412 {
413 Find find {};
414 find.gte(std::forward<tyson::TySonObject>(value));
415 return find;
416 }
417
418 template<std::convertible_to<tyson::TySonObject> T>
419 Find& lt(T value) noexcept
420 {
421 comparators_.emplace_back(std::make_unique<Lt>(std::forward<tyson::TySonObject>(value)));
422 return *this;
423 }
424
425 template<std::convertible_to<tyson::TySonObject> T>
426 Find& lt(std::string_view path_to_field, T value) noexcept
427 {
428 comparators_.emplace_back(std::make_unique<Lt>(path_to_field, std::forward<tyson::TySonObject>(value)));
429 return *this;
430 }
431
438 template<std::convertible_to<tyson::TySonObject> T>
439 static Find LT(T value) noexcept
440 {
441 Find find {};
442 find.lt(std::forward<tyson::TySonObject>(value));
443 return find;
444 }
445
446 template<std::convertible_to<tyson::TySonObject> T>
447 Find& lte(T value) noexcept
448 {
449 comparators_.emplace_back(std::make_unique<Lte>(std::forward<tyson::TySonObject>(value)));
450 return *this;
451 }
452
453 template<std::convertible_to<tyson::TySonObject> T>
454 Find& lte(std::string_view path_to_field, T value) noexcept
455 {
456 comparators_.emplace_back(std::make_unique<Lte>(path_to_field, std::forward<tyson::TySonObject>(value)));
457 return *this;
458 }
459
466 static Find LTE(tyson::TySonObject &value) noexcept
467 {
468 Find find {};
469 find.lte(value);
470 return find;
471 }
472
473 Find& q(And &value) noexcept
474 {
475 comparators_.emplace_back(std::make_unique<And>(value));
476 return *this;
477 }
478
486 template<std::convertible_to<Comparison> ...Comps>
487 static Find AND(Comps ...comps) noexcept
488 {
489 And and_ {comps ...};
490 Find find {};
491 find.q(and_);
492 return find;
493 }
494
495 Find& q(Or &value) noexcept
496 {
497 comparators_.emplace_back(std::make_unique<Or>(value));
498 return *this;
499 }
500
507 template<std::convertible_to<Comparison> ...Comps>
508 static Find OR(Comps ...comps) noexcept
509 {
510 Or or_ {comps ...};
511 Find find {};
512 find.q(or_);
513 return find;
514 }
515
516 Find& q(Not &value) noexcept
517 {
518 comparators_.emplace_back(std::make_unique<Not>(value));
519 return *this;
520 }
521
528 static Find NOT(std::string_view field) noexcept
529 {
530 Not not_ {field};
531 Find find {};
532 find.q(not_);
533 return find;
534 }
535
536 };
537
538
539 class Sort : public QueryCmd
540 {
541 std::vector<std::unique_ptr<annadb::Query::SortCmd>> cmds_ {};
542
543 std::string annadb_query() override
544 {
545 std::string query = "sort[";
546 std::for_each(cmds_.begin(), cmds_.end(), [&query](std::unique_ptr<annadb::Query::SortCmd> &val)
547 {
548 query += val->data() + ",";
549 });
550 query += "]";
551
552 return query;
553 }
554
555 [[nodiscard]] std::vector<std::string> previous_steps_() noexcept override
556 {
557 return { "find", "get", "sort", "limit", "offset"};
558 };
559 [[nodiscard]] std::vector<std::string> next_steps_() noexcept override
560 {
561 return {"find", "get", "sort", "limit", "offset", "update", "delete"};
562 }
563
564 public:
565 explicit Sort(std::vector<std::unique_ptr<annadb::Query::SortCmd>> &&cmds) noexcept : QueryCmd("sort", false), cmds_(std::move(cmds)) {}
566
567 template<std::convertible_to<std::string> ...T>
568 static Sort ASC(T&& ...fields) noexcept
569 {
570 std::vector<std::unique_ptr<annadb::Query::SortCmd>> cmds {};
571 cmds.reserve(sizeof...(fields));
572
573 (cmds.push_back(
574 std::make_unique<Asc>(annadb::Query::Asc(fields)
575 )), ...);
576
577 return Sort(std::move(cmds));
578 }
579
580 template<std::convertible_to<std::string> ...T>
581 static Sort DESC(T&& ...fields) noexcept
582 {
583 std::vector<std::unique_ptr<annadb::Query::SortCmd>> cmds = {};
584 (
585 cmds.push_back(
586 std::make_unique<Desc>(annadb::Query::Desc(fields))
587 ), ...
588 );
589
590 return Sort(std::move(cmds));
591 }
592 };
593
594
595 class Limit : public QueryCmd
596 {
597 std::string data_;
598 std::string annadb_query() override
599 {
600 return "limit(n|" + data_ + "|)";
601 }
602
603 [[nodiscard]] std::vector<std::string> previous_steps_() noexcept override
604 {
605 return { "find", "get", "sort", "limit", "offset"};
606 };
607 [[nodiscard]] std::vector<std::string> next_steps_() noexcept override
608 {
609 return {"find", "get", "sort", "limit", "offset", "update", "delete"};
610 }
611
612 public:
613 Limit(const Limit &rhs) noexcept : QueryCmd("delete", true), data_(rhs.data_) {};
614
615 explicit Limit(short data) : QueryCmd("limit", false), data_(std::to_string(data)) {}
616 explicit Limit(unsigned short data) : QueryCmd("limit", false), data_(std::to_string(data)) {}
617 explicit Limit(int data) : QueryCmd("limit", false), data_(std::to_string(data)) {}
618 explicit Limit(unsigned int data) : QueryCmd("limit", false), data_(std::to_string(data)) {}
619 explicit Limit(long data) : QueryCmd("limit", false), data_(std::to_string(data)) {}
620 explicit Limit(unsigned long data) : QueryCmd("limit", false), data_(std::to_string(data)) {}
621 explicit Limit(long long data) : QueryCmd("limit", false), data_(std::to_string(data)) {}
622 explicit Limit(unsigned long long data) : QueryCmd("limit", false), data_(std::to_string(data)) {}
623
624 };
625
626
627 class Offset : public QueryCmd
628 {
629 std::string data_;
630
631 std::string annadb_query() override
632 {
633 return "offset(n|" + data_ + "|)";
634 }
635
636 [[nodiscard]] std::vector<std::string> previous_steps_() noexcept override
637 {
638 return { "find", "get", "sort", "limit", "offset"};
639 };
640 [[nodiscard]] std::vector<std::string> next_steps_() noexcept override
641 {
642 return {"find", "get", "sort", "limit", "offset", "update", "delete"};
643 }
644
645 public:
646 Offset(const Offset &rhs) : QueryCmd("delete", true), data_(rhs.data_) {};
647
648 explicit Offset(short data) : QueryCmd("offset", false), data_(std::to_string(data)) {}
649 explicit Offset(unsigned short data) : QueryCmd("offset", false), data_(std::to_string(data)) {}
650 explicit Offset(int data) : QueryCmd("offset", false), data_(std::to_string(data)) {}
651 explicit Offset(unsigned int data) : QueryCmd("offset", false), data_(std::to_string(data)) {}
652 explicit Offset(long data) : QueryCmd("offset", false), data_(std::to_string(data)) {}
653 explicit Offset(unsigned long data) : QueryCmd("offset", false), data_(std::to_string(data)) {}
654 explicit Offset(long long data) : QueryCmd("offset", false), data_(std::to_string(data)) {}
655 explicit Offset(unsigned long long data) : QueryCmd("offset", false), data_(std::to_string(data)) {}
656
657 };
658
659
660 class Update : public QueryCmd
661 {
662 std::vector<std::tuple<UpdateType, tyson::TySonObject>> values_ {};
663
664 std::string annadb_query() override
665 {
666 std::stringstream sstream;
667 sstream << "update[";
668 for (auto &val : values_)
669 {
670 auto type = std::get<0>(val);
671 auto obj = std::get<1>(val);
672
673 if (type == UpdateType::Set)
674 {
675 sstream << "set{" << obj << "},";
676 }
677 else
678 {
679 sstream << "inc{" << obj << "},";
680 }
681 }
682
683 sstream << "]";
684
685 return sstream.str();
686 }
687
688 [[nodiscard]] std::vector<std::string> previous_steps_() noexcept override
689 {
690 return { "find", "get", "sort", "limit", "offset"};
691 };
692 [[nodiscard]] std::vector<std::string> next_steps_() noexcept override
693 {
694 return {};
695 }
696
697 public:
698
699 Update(tyson::TySonObject &val, UpdateType &type) noexcept : QueryCmd("update", false)
700 {
701 values_.emplace_back(type, val);
702 }
703 Update(std::vector<tyson::TySonObject> &values, UpdateType &type) noexcept : QueryCmd("update", false)
704 {
705 std::for_each(values.begin(), values.end(),
706 [this, &type](auto val)
707 {
708 this->values_.emplace_back(type, val);
709 });
710 }
711 Update(std::vector<std::tuple<UpdateType, tyson::TySonObject>> &&values) noexcept : QueryCmd("update", false), values_(values) {}
712 };
713
714
715 class Delete : public QueryCmd
716 {
717 std::string annadb_query() override
718 {
719 return "delete";
720 }
721
722 [[nodiscard]] std::vector<std::string> previous_steps_() noexcept override
723 {
724 return { "find", "get", "sort", "limit", "offset"};
725 };
726 [[nodiscard]] std::vector<std::string> next_steps_() noexcept override
727 {
728 return {};
729 }
730
731 public:
732 Delete() = default;
733 Delete(const Delete &) : QueryCmd("delete", false) {};
734 };
735
736 class Project : public QueryCmd
737 {
738 std::vector<std::pair<std::string, tyson::TySonObject>> values_;
739
740 std::string annadb_query() override
741 {
742 std::stringstream sstream;
743 sstream << "project{";
744 std::for_each(values_.begin(), values_.end(),
745 [&sstream](auto &val)
746 {
747 sstream << "s|" <<std::get<0>(val) << "|:" << std::get<1>(val) << ",";
748 });
749 sstream << "}";
750 return sstream.str();
751 }
752
753 [[nodiscard]] std::vector<std::string> previous_steps_() noexcept override
754 {
755 return { "get", "find", "sort", "limit", "offset"};
756 };
757
758 [[nodiscard]] std::vector<std::string> next_steps_() noexcept override
759 {
760 return {};
761 }
762
763 public:
764
765 template<std::convertible_to<std::pair<std::string, tyson::TySonObject>> ...T>
766 explicit Project(T && ... objs)
767 {
768 values_.reserve(sizeof...(objs));
769 (values_.emplace_back(objs), ...);
770 }
771 };
772
773
774 class Query
775 {
776 std::string collection_name_;
777 std::vector<std::unique_ptr<annadb::Query::QueryCmd>> cmds_ {};
778 void add_to_cmds(std::unique_ptr<QueryCmd> queryCmd)
779 {
780 if (cmds_.empty() && !queryCmd->can_start_pipeline())
781 {
782 throw std::invalid_argument(queryCmd->name() + " can not be used to start a new query pipeline.");
783 }
784
785 if (cmds_.empty())
786 {
787 this->cmds_.emplace_back(std::move(queryCmd));
788 }
789 else
790 {
791 auto latest_cmd = cmds_.size() - 1;
792 if (cmds_.at(latest_cmd)->next_step_allowed(queryCmd->name()) &&
793 queryCmd->previous_step_allowed(cmds_.at(latest_cmd)->name()))
794 {
795 this->cmds_.emplace_back(std::move(queryCmd));
796 }
797 else
798 {
799 throw std::invalid_argument(queryCmd->name() + " can not be used before/after " + cmds_.at(latest_cmd)->name());
800 }
801 }
802 }
803
804 friend std::ostream& operator<<(std::ostream &out, Query &query) noexcept
805 {
806 std::stringstream sstream;
807 sstream << "collection|" << query.collection_name_ << "|";
808
809 if (query.cmds_.size() == 1)
810 {
811 sstream << ":" << query.cmds_[0]->query() << ";";
812 }
813 else
814 {
815 sstream << ":q[";
816 for (auto &cmd: query.cmds_)
817 {
818 sstream << cmd->query() << ",";
819 }
820 sstream << "];";
821 }
822 return out << sstream.str();
823 }
824
825 public:
831 explicit Query(std::string collection_name) : collection_name_(std::move(collection_name)) {};
832 ~Query() = default;
833
839 void insert(Insert &insert) noexcept
840 {
841 this->add_to_cmds(std::make_unique<Insert>(std::move(insert)));
842 }
843
850 {
851 this->add_to_cmds(std::make_unique<Insert>(insert));
852 }
853
859 void insert(std::vector<tyson::TySonObject> &insert) noexcept
860 {
861 this->add_to_cmds(std::make_unique<Insert>(insert));
862 }
863
869 template<typename ...T>
870 void insert(T&& ...insert) noexcept
871 {
872 this->add_to_cmds(std::make_unique<Insert>(insert...));
873 }
874
881 Query& get(Get &get) noexcept
882 {
883 this->add_to_cmds(std::make_unique<Get>(std::move(get)));
884 return *this;
885 }
886
894 {
895 this->add_to_cmds(std::make_unique<Get>(get));
896 return *this;
897 }
898
905 Query& get(std::vector<tyson::TySonObject> &get) noexcept
906 {
907 this->add_to_cmds(std::make_unique<Get>(get));
908 return *this;
909 }
910
917 template<std::convertible_to<tyson::TySonObject> ...T>
918 Query& get(T &&...values) noexcept
919 {
920 this->add_to_cmds(std::make_unique<Get>(values...));
921 return *this;
922 }
923
930 Query& find(Find &&find) noexcept
931 {
932 this->add_to_cmds(std::make_unique<Find>(std::move(find)));
933 return *this;
934 }
935
942 Query& sort(Sort &&sort) noexcept
943 {
944 this->add_to_cmds(std::make_unique<Sort>(std::move(sort)));
945 return *this;
946 }
947
954 Query& limit(Limit &limit) noexcept
955 {
956 this->add_to_cmds(std::make_unique<Limit>(std::move(limit)));
957 return *this;
958 }
959
967 template<typename T>
968 requires std::is_integral_v<T>
969 Query& limit(T &&limit) noexcept
970 {
971 this->add_to_cmds(std::make_unique<Limit>(limit));
972 return *this;
973 }
974
982 {
983 this->add_to_cmds(std::make_unique<Offset>(offset));
984 return *this;
985 }
986
994 template<typename T>
995 requires std::is_integral_v<T>
996 Query& offset(T &&offset) noexcept
997 {
998 this->add_to_cmds(std::make_unique<Offset>(offset));
999 return *this;
1000 }
1001
1009 {
1010 if (value.type() == tyson::TySonType::Value)
1011 {
1012 this->add_to_cmds(std::make_unique<Update>(value, kind));
1013 }
1014 else
1015 {
1016 throw std::invalid_argument("Only `tyson::TySonType::Value` are allowed.");
1017 }
1018 }
1019
1026 void update(UpdateType &kind, std::vector<tyson::TySonObject> &values)
1027 {
1028 if (std::all_of(values.begin(), values.end(),
1029 [](const auto &val){ return val.type() == tyson::TySonType::Value;}))
1030 {
1031 this->add_to_cmds(std::make_unique<Update>(values, kind));
1032 }
1033 else
1034 {
1035 throw std::invalid_argument("Only `tyson::TySonType::Values` are allowed.");
1036 }
1037 }
1038
1039 template<std::convertible_to<tyson::TySonObject> ...T>
1040 void update(UpdateType &kind, T&& ...values)
1041 {
1042 std::vector<tyson::TySonObject> objects {};
1043 objects.reserve(sizeof...(values));
1044 (objects.emplace_back(values), ...);
1045
1046 if (std::all_of(objects.begin(), objects.end(),
1047 [](const auto &val){ return val.type() == tyson::TySonType::Value;}))
1048 {
1049 this->add_to_cmds(std::make_unique<Update>(objects, kind));
1050 }
1051 else
1052 {
1053 throw std::invalid_argument("Only `tyson::TySonType::Values` are allowed.");
1054 }
1055 }
1056
1064 void update(std::vector<std::tuple<UpdateType, tyson::TySonObject>> &values)
1065 {
1066 if (std::all_of(values.begin(), values.end(),
1067 [](const auto &val){ return std::get<1>(val).type() == tyson::TySonType::Value;}))
1068 {
1069 this->add_to_cmds(std::make_unique<Update>(std::move(values)));
1070 }
1071 else
1072 {
1073 throw std::invalid_argument("Only `tyson::TySonType::Values` are allowed.");
1074 }
1075 }
1076
1081 void delete_q() noexcept
1082 {
1083 this->add_to_cmds(std::make_unique<Delete>(Delete()));
1084 }
1085
1093 template<std::convertible_to<std::pair<std::string, tyson::TySonObject>> ...T>
1094 Query& project(T &&...values) noexcept
1095 {
1096 this->add_to_cmds(std::make_unique<Project>(values...));
1097 return *this;
1098 }
1099 };
1100}
1101
1102#endif //ANNADB_DRIVER_QUERY_HPP
Definition: query_comparision.hpp:231
Definition: query.hpp:36
Asc(std::string_view field) noexcept
Definition: query.hpp:46
Definition: query.hpp:716
Definition: query.hpp:55
Desc(std::string_view field) noexcept
Definition: query.hpp:65
Definition: query.hpp:273
static Find AND(Comps ...comps) noexcept
Definition: query.hpp:487
static Find GTE(T value) noexcept
Definition: query.hpp:411
static Find OR(Comps ...comps) noexcept
Definition: query.hpp:508
static Find LTE(tyson::TySonObject &value) noexcept
Definition: query.hpp:466
static Find GT(T &&value) noexcept
Definition: query.hpp:383
static Find NEQ(tyson::TySonObject &value) noexcept
Definition: query.hpp:343
static Find LT(T value) noexcept
Definition: query.hpp:439
static Find EQ(tyson::TySonObject &value) noexcept
Definition: query.hpp:318
static Find NOT(std::string_view field) noexcept
Definition: query.hpp:528
Definition: query.hpp:185
Get(tyson::TySonObject &obj)
Definition: query.hpp:217
Get(std::vector< tyson::TySonObject > &objs)
Definition: query.hpp:235
Definition: query.hpp:128
Insert(std::vector< tyson::TySonObject > &objs) noexcept
Definition: query.hpp:179
Insert(tyson::TySonObject &obj) noexcept
Definition: query.hpp:158
Definition: query.hpp:596
Definition: query_comparision.hpp:291
Definition: query.hpp:628
Definition: query_comparision.hpp:261
Definition: query.hpp:737
Definition: query.hpp:76
Definition: query.hpp:775
void update(UpdateType &&kind, tyson::TySonObject &value)
Definition: query.hpp:1008
void delete_q() noexcept
Definition: query.hpp:1081
void insert(std::vector< tyson::TySonObject > &insert) noexcept
Definition: query.hpp:859
Query & find(Find &&find) noexcept
Definition: query.hpp:930
Query & offset(T &&offset) noexcept
Definition: query.hpp:996
void update(std::vector< std::tuple< UpdateType, tyson::TySonObject > > &values)
Definition: query.hpp:1064
Query & get(tyson::TySonObject &get) noexcept
Definition: query.hpp:893
Query & get(std::vector< tyson::TySonObject > &get) noexcept
Definition: query.hpp:905
void insert(Insert &insert) noexcept
Definition: query.hpp:839
Query(std::string collection_name)
Definition: query.hpp:831
Query & get(Get &get) noexcept
Definition: query.hpp:881
Query & offset(Offset &offset) noexcept
Definition: query.hpp:981
Query & project(T &&...values) noexcept
Definition: query.hpp:1094
void insert(tyson::TySonObject &insert) noexcept
Definition: query.hpp:849
Query & limit(Limit &limit) noexcept
Definition: query.hpp:954
void update(UpdateType &kind, std::vector< tyson::TySonObject > &values)
Definition: query.hpp:1026
Query & limit(T &&limit) noexcept
Definition: query.hpp:969
Query & get(T &&...values) noexcept
Definition: query.hpp:918
Query & sort(Sort &&sort) noexcept
Definition: query.hpp:942
void insert(T &&...insert) noexcept
Definition: query.hpp:870
Definition: query.hpp:540
Definition: query.hpp:661
Definition: TySON.hpp:86
TySonType type() const noexcept
Definition: TySON.hpp:576
Definition: query.hpp:15
UpdateType
Definition: query.hpp:24
Definition: query.hpp:30