Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
Database/Document/Collection.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
5#include "Stroika/Foundation/Execution/Activity.h"
7
8namespace Stroika::Foundation::Database::Document::Collection {
9
10 /*
11 ********************************************************************************
12 ************************* Document::Connection::Ptr ****************************
13 ********************************************************************************
14 */
15 inline Ptr::Ptr (const shared_ptr<IRep>& src)
16 : inherited{src}
17 {
18 }
19 inline Ptr& Ptr::operator= (const Ptr& src)
20 {
21 inherited::operator= (src);
22 return *this;
23 }
24 inline Ptr& Ptr::operator= (Ptr&& src) noexcept
25 {
26 inherited::operator= (src);
27 return *this;
28 }
29 inline auto Ptr::operator== (const Ptr& rhs) const
30 {
31 return get () == rhs.get ();
32 }
33 inline bool Ptr::operator== (nullptr_t) const noexcept
34 {
35 return this->get () == nullptr;
36 }
37 inline String Ptr::GetName () const
38 {
39 using namespace Characters;
40 return this->get ()->GetName ();
41 }
42 inline String Ptr::ToString () const
43 {
44 using namespace Characters;
45 return Format ("{}"_f, static_cast<const void*> (this->get ()));
46 }
47 inline IDType Ptr::Add (const Document& v) const
48 {
49 return this->get ()->Add (v);
50 }
51 inline optional<Document> Ptr::Get (const IDType& id, const optional<Projection>& projection) const
52 {
53 using namespace Characters::Literals;
54 auto activity =
55 Execution::LazyEvalActivity{[&] () -> String { return "getting document from database collection '{}'"_f(GetName ()); }};
56 Execution::DeclareActivity da{&activity};
57 return this->get ()->Get (id, projection);
58 }
59 inline optional<Document> Ptr::Get (const Filter& filter, const optional<Projection>& projection) const
60 {
61 auto r = this->GetAll (filter, projection);
62 if (r.size () > 1) {
63 static auto kTooManyResultsException_ = Execution::RuntimeErrorException{"too many results from DocumentDB Get() call"sv};
64 Execution::Throw (kTooManyResultsException_);
65 }
66 if (r.size () == 0) {
67 return nullopt;
68 }
69 return r[0];
70 }
71 inline Document Ptr::GetOrThrow (const IDType& id, const optional<Projection>& projection) const
72 {
73 static const auto kExcept_ = Execution::RuntimeErrorException{"no such id"sv};
74 return Memory::ValueOfOrThrow (Get (id, projection), kExcept_);
75 }
76 inline Document Ptr::GetOrThrow (const Filter& filter, const optional<Projection>& projection) const
77 {
78 static const auto kExcept_ = Execution::RuntimeErrorException{"no such document"sv};
79 return Memory::ValueOfOrThrow (Get (filter, projection), kExcept_);
80 }
81 inline Sequence<Document> Ptr::GetAll (const optional<Filter>& filter, const optional<Projection>& projection) const
82 {
83 using namespace Characters::Literals;
84 auto activity = Execution::LazyEvalActivity{
85 [&] () -> String { return "getting all matching documents from database collection '{}'"_f(GetName ()); }};
86 Execution::DeclareActivity da{&activity};
87 return this->get ()->GetAll (filter, projection);
88 }
89 inline Sequence<IDType> Ptr::GetAllIDs (const optional<Filter>& filter) const
90 {
91 return this->GetAll (filter, kOnlyIDs).Map<Sequence<IDType>> ([] (const Document& d) -> IDType {
92 static const auto kExcept_ = Execution::RuntimeErrorException{"no such id"sv};
93 return d.LookupChecked (kID, kExcept_).As<String> ();
94 });
95 }
96 inline void Ptr::Replace (const Document& newV) const
97 {
98 Replace (Memory::ValueOf (newV.Lookup (kID)).As<String> (), newV);
99 }
100 inline void Ptr::Replace (const IDType& id, const Document& newV) const
101 {
102 using namespace Characters::Literals;
103 auto activity =
104 Execution::LazyEvalActivity{[&] () -> String { return "updating document in database collection '{}'"_f(GetName ()); }};
105 Execution::DeclareActivity da{&activity};
106 this->get ()->Update (id, newV, nullopt);
107 }
108 inline void Ptr::Update (const Document& newV) const
109 {
110 Update (Memory::ValueOf (newV.Lookup (kID)).As<String> (), newV, Set<String>{newV.Keys ()});
111 }
112 inline void Ptr::Update (const Document& newV, const Set<String>& onlyTheseFields) const
113 {
114 Update (Memory::ValueOf (newV.Lookup (kID)).As<String> (), newV, onlyTheseFields);
115 }
116 inline void Ptr::Update (const IDType& id, const Document& newV) const
117 {
118 using namespace Characters::Literals;
119 auto activity =
120 Execution::LazyEvalActivity{[&] () -> String { return "updating document in database collection '{}'"_f(GetName ()); }};
121 Execution::DeclareActivity da{&activity};
122 this->get ()->Update (id, newV, Set<String>{newV.Keys ()});
123 }
124 inline void Ptr::Update (const IDType& id, const Document& newV, const Set<String>& onlyTheseFields) const
125 {
126 using namespace Characters::Literals;
127 auto activity = Execution::LazyEvalActivity{[&] () -> String { return "updating document in database collection {}"_f(GetName ()); }};
128 Execution::DeclareActivity da{&activity};
129 this->get ()->Update (id, newV, onlyTheseFields);
130 }
131 inline void Ptr::Remove (const IDType& id) const
132 {
133 using namespace Characters::Literals;
134 auto activity =
135 Execution::LazyEvalActivity{[&] () -> String { return "removing document from database collection {}"_f(GetName ()); }};
136 Execution::DeclareActivity da{&activity};
137 this->get ()->Remove (id);
138 }
139 inline IDType Ptr::AddOrUpdate (const Document& v) const
140 {
141 if (auto oid = v.Lookup (kID)) {
142 if (Get (oid->As<String> ())) {
143 Replace (v);
144 return oid->As<String> ();
145 }
146 }
147 return Add (v);
148 }
150}
Mapping< String, VariantValue > Document
Definition Document.h:28
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual optional< mapped_type > Lookup(ArgByValueType< key_type > key) const
Definition Mapping.inl:142
nonvirtual Iterable< key_type > Keys() const
Definition Mapping.inl:111
A generalization of a vector: a container whose elements are keyed by the natural numbers.
nonvirtual RESULT_CONTAINER Map(ELEMENT_MAPPER &&elementMapper) const
'override' Iterable<>::Map () function so RESULT_CONTAINER defaults to Sequence, and improve that cas...
Definition Sequence.inl:174
Set<T> is a container of T, where once an item is added, additionally adds () do nothing.