Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
Foundation/Database/Document/Connection.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Database_Document_Connection_h_
5#define _Stroika_Foundation_Database_Document_Connection_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <optional>
10
12#include "Stroika/Foundation/Containers/Set.h"
15
16/**
17 * \file
18 *
19 * The point of this module is to define a Connection abstraction that can be used for different kinds
20 * of connections (e.g. SQLite local database object, and mongoDB remote database object). This generic API
21 * can then be used in places where either backend database might be in use.
22 *
23 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
24 */
25
26namespace Stroika::Foundation::Database::Document {
27
28 using Characters::String;
29 using Containers::Set;
30
31 class Transaction;
32 class EngineProperties;
33
34}
35namespace Stroika::Foundation::Database::Document::Connection {
36
37 class IRep;
38 class Ptr;
39
40 /**
41 * Optionally passed to OpertionCallbackPtr for the purpose of logging
42 */
43 enum Operation {
44 eStartingRead,
45 eCompletedRead,
46 eStartingWrite,
47 eCompletedWrite,
48 eNotifyError
49 };
50
51 /**
52 * Optionally passed to SomeKindOfDocDB::Connection::New (its Options argument) for the purpose of logging;
53 *
54 * \note exception_ptr is only provided for eNotifyError, and is typically current_exception () but can be nullptr
55 * \note callback must be internally synchonized, and maybe called in nested fasion.
56 * \note OpertionCallbackPtr must be no-throw (sadly not capturable with std::function).
57 */
59 function<void (Operation op, const Ptr& documentDBConnection, const optional<String>& collectionName, const exception_ptr& e)>;
60
61 /**
62 *
63 */
64 struct Options {
65 /**
66 * @brief If specified for a Connection object, then the Add () API allows externally specified IDs (optionally)
67 *
68 * This defaults to true, the easiest to use and most permissive setting. Setting to false is more efficient.
69 *
70 * \note - for some database backends, this affects the layout, so it it is critical it agrees on all database connections
71 * if you have multiple connections to the same database file/object.
72 *
73 * \note some backends may impose special requirements on the format of externally provided IDs (e.g.
74 * assume they will be in the form of a guid, but such requirements will be documented/enforced on a
75 * backend-by-backend 'new' factory basis).
76 */
77 bool fAddAllowsExternallySpecifiedIDs{true};
78
79 /**
80 * Note - callback must be internally synchonized, and maybe called in nested fasion.
81 * Callback can be used to calculate usage statistics, and note database errors. Presence of
82 * the operation callback doesn't affect normal operation of the
83 */
84 OpertionCallbackPtr fOperationLoggingCallback{nullptr};
85 };
86
87 /**
88 * Connection::Ptr provides an API for accessing a document database.
89 *
90 * A new Connection::Ptr is typically created with SOME_SERVICE::Connection::New () (e.g. SQLite::Connection::New() or MongoDBClient::Connection::New ())
91 *
92 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety-For-Envelope-But-Ambiguous-Thread-Safety-For-Letter">C++-Standard-Thread-Safety-For-Envelope-But-Ambiguous-Thread-Safety-For-Letter/a>
93 * But though each connection can only be accessed from a single thread at a time, the underlying database may be
94 * threadsafe (even if accessed across processes).
95 *
96 * The Connection::Ptr itself is standardC++ thread safety. The thread-safety of the underlying database depends on how the underlying
97 * shared_ptr<IRep> was created.
98 */
99 class Ptr : public shared_ptr<IRep> {
100 private:
101 using inherited = shared_ptr<IRep>;
102
103 public:
104 /**
105 */
106 Ptr () noexcept = default;
107 Ptr (const Ptr& src) = default;
108 Ptr (Ptr&& src) noexcept = default;
109 Ptr (nullptr_t) noexcept;
110 Ptr (const inherited& i) noexcept;
111
112 public:
113 ~Ptr () = default;
114
115 public:
116 /**
117 */
118 nonvirtual Ptr& operator= (const Ptr& src) = default;
119 nonvirtual Ptr& operator= (Ptr&& src) noexcept = default;
120
121 public:
122 /**
123 */
124 nonvirtual shared_ptr<const EngineProperties> GetEngineProperties () const;
125
126 public:
127 /**
128 */
129 nonvirtual Options GetOptions () const;
130
131 public:
132 /**
133 * returns roughly the disk (or RAM if not stored on disk) space consumed by the database referenced in the Connection.
134 */
135 nonvirtual uintmax_t GetSpaceConsumed () const;
136
137 public:
138 /**
139 * @brief return the names of all collections in the referenced database
140 */
141 nonvirtual Set<String> GetCollections () const;
142
143 public:
144 /**
145 * Creates the (named) collection (aka table), and does nothing if the table/collection already exists.
146 *
147 * \see also GetCollection - much the same but creates first if doesnt exist
148 *
149 * \note This returns the same thing as GetCollection (name) after creating it, and its result can be safely
150 * ignored and fetched later.
151 */
152 nonvirtual Collection::Ptr CreateCollection (const String& name) const;
153
154 public:
155 /**
156 * \note not an error and ignored if the named collection does not exist.
157 * \note but this can create errors if the underlying database has problems (e.g. file system errors, permission errors, etc).
158 */
159 nonvirtual void DropCollection (const String& name) const;
160
161 public:
162 /**
163 * Returns a (shared) pointer to the named collection. If the named collection does not exist, it is an error (exception thrown).
164 *
165 * \see also CreateCollection
166 */
167 nonvirtual Collection::Ptr GetCollection (const String& name) const;
168
169 public:
170 /**
171 * Transaction object factory
172 *
173 * The reason you might use this instead of SQLite::Transaction{} - is in writing generic code
174 * that doesn't depend on the particular kind of SQL database you are connected to (e.g. that
175 * might be used for ODBC or SQLite).
176 */
177 nonvirtual Transaction mkTransaction () const;
178
179 public:
180 /**
181 * @see Characters::ToString ()
182 */
183 nonvirtual String ToString () const;
184
185 public:
186 nonvirtual bool operator== (const Ptr& rhs) const noexcept;
187 nonvirtual bool operator== (nullptr_t) const noexcept;
188
189 protected:
190 // note this protects the LETTER (smart_ptr) not the envelope
192 };
193
194 /**
195 * Connection::IRep provides an (abstract) API for accessing a Document database.
196 *
197 * \note \em Thread-Safety <a href="Thread-Safety.md#Thread-Safety-Rules-Depends-On-Subtype">Thread-Safety-Rules-Depends-On-Subtype</a>
198 */
199 class IRep : public enable_shared_from_this<IRep> {
200 public:
201 /**
202 */
203 virtual ~IRep () = default;
204
205 public:
206 /**
207 */
208 virtual shared_ptr<const EngineProperties> GetEngineProperties () const = 0;
209
210 public:
211 /**
212 */
213 virtual Options GetOptions () const = 0;
214
215 public:
216 /**
217 * Note only provides an estimate of size used.
218 */
219 virtual uintmax_t GetSpaceConsumed () const = 0;
220
221 public:
222 /**
223 */
224 virtual Set<String> GetCollections () = 0;
225
226 public:
227 /**
228 * Creates the (named) collection (aka table), and does nothing if the table/collection already exists.
229 */
230 virtual Collection::Ptr CreateCollection (const String& name) = 0;
231
232 public:
233 /**
234 */
235 virtual void DropCollection (const String& name) = 0;
236
237 public:
238 /**
239 */
240 virtual Collection::Ptr GetCollection (const String& name) = 0;
241
242 public:
243 /**
244 * Transaction object factory
245 *
246 * \note operations that happen OUTSIDE of a transaction are IMPLICITLY auto-committed immediately.
247 */
249 };
250
251}
252
253/*
254 ********************************************************************************
255 ***************************** Implementation Details ***************************
256 ********************************************************************************
257 */
258#include "Connection.inl"
259
260#endif /*_Stroika_Foundation_Database_Document_Connection_h_*/
function< void(Operation op, const Ptr &documentDBConnection, const optional< String > &collectionName, const exception_ptr &e)> OpertionCallbackPtr
#define qStroika_ATTRIBUTE_NO_UNIQUE_ADDRESS_VCFORCE
[[msvc::no_unique_address]] isn't always broken in MSVC. Annotate with this on things where its not b...
Definition StdCompat.h:445
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
Set<T> is a container of T, where once an item is added, additionally adds () do nothing.
virtual Collection::Ptr CreateCollection(const String &name)=0
nonvirtual Set< String > GetCollections() const
return the names of all collections in the referenced database
nonvirtual Collection::Ptr CreateCollection(const String &name) const
NOT a real mutex - just a debugging infrastructure support tool so in debug builds can be assured thr...