Stroika Library 3.0d23x
 
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
39 /**
40 *
41 */
42 struct Options {
43 /**
44 * @brief If specified for a Connection object, then the Add () API allows externally specified IDs (optionally)
45 *
46 * This defaults to true, the easiest to use and most permissive setting. Setting to false is more efficient.
47 *
48 * \note - for some database backends, this affects the layout, so it it is critical it agrees on all database connections
49 * if you have multiple connections to the same database file/object.
50 *
51 * \note some backends may impose special requirements on the format of externally provided IDs (e.g.
52 * the Stroika wrappers for mongo-db assume they will be in the form of a guid).
53 */
54 bool fAddAllowsExternallySpecifiedIDs{true};
55 };
56
57 /**
58 * Connection::Ptr provides an API for accessing a document database.
59 *
60 * A new Connection::Ptr is typically created with SOME_SERVICE::Connection::New () (e.g. SQLite::Connection::New() or MongoDBClient::Connection::New ())
61 *
62 * \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>
63 * But though each connection can only be accessed from a single thread at a time, the underlying database may be
64 * threadsafe (even if accessed across processes).
65 *
66 * The Connection::Ptr itself is standardC++ thread safety. The thread-safety of the underlying database depends on how the underlying
67 * shared_ptr<IRep> was created.
68 */
69 class Ptr : public shared_ptr<IRep> {
70 private:
71 using inherited = shared_ptr<IRep>;
72
73 public:
74 /**
75 */
76 Ptr (const Ptr& src) = default;
77 Ptr (Ptr&& src) noexcept = default;
78 using inherited::inherited;
79
80 public:
81 ~Ptr () = default;
82
83 public:
84 /**
85 */
86 nonvirtual Ptr& operator= (const Ptr& src) = default;
87 nonvirtual Ptr& operator= (Ptr&& src) noexcept = default;
88
89 public:
90 /**
91 */
92 nonvirtual shared_ptr<const EngineProperties> GetEngineProperties () const;
93
94 public:
95 /**
96 */
97 nonvirtual Options GetOptions () const;
98
99 public:
100 /**
101 * @brief return the names of all collections
102 */
103 nonvirtual Set<String> GetCollections () const;
104
105 public:
106 /**
107 * Creates the (named) collection (aka table), and does nothing if the table/collection already exists.
108 *
109 * \see also GetCollection - much the same but creates first if doesnt exist
110 *
111 * \note This returns the same thing as GetCollection (name) after creating it, and its result can be safely
112 * ignored and fetched later.
113 */
114 nonvirtual Collection::Ptr CreateCollection (const String& name) const;
115
116 public:
117 /**
118 * \note not an error and ignored if the named collection does not exist.
119 * \note but this can create errors if the underlying database has problems (e.g. file system errors, permission errors, etc).
120 */
121 nonvirtual void DropCollection (const String& name) const;
122
123 public:
124 /**
125 * Returns a (shared) pointer to the named collection. If the named collection does not exist, it is an error.
126 *
127 * \see also CreateCollection
128 */
129 nonvirtual Collection::Ptr GetCollection (const String& name) const;
130
131 public:
132 /**
133 * Transaction object factory
134 *
135 * The reason you might use this instead of SQLite::Transaction{} - is in writing generic code
136 * that doesn't depend on the particular kind of SQL database you are connected to (e.g. that
137 * might be used for ODBC or SQLite).
138 */
139 nonvirtual Transaction mkTransaction () const;
140
141 public:
142 /**
143 * @see Characters::ToString ()
144 */
145 nonvirtual String ToString () const;
146
147 public:
148 nonvirtual bool operator== (const Ptr& rhs) const noexcept;
149 nonvirtual bool operator== (nullptr_t) const noexcept;
150
151 protected:
152 [[no_unique_address]] Debug::AssertExternallySynchronizedMutex _fAssertExternallySynchronizedMutex;
153 };
154
155 /**
156 * Connection::IRep provides an (abstract) API for accessing a Document database.
157 *
158 * \note \em Thread-Safety <a href="Thread-Safety.md#Thread-Safety-Rules-Depends-On-Subtype">Thread-Safety-Rules-Depends-On-Subtype</a>
159 */
160 class IRep : public enable_shared_from_this<IRep> {
161 public:
162 /**
163 */
164 virtual ~IRep () = default;
165
166 public:
167 /**
168 */
169 virtual shared_ptr<const EngineProperties> GetEngineProperties () const = 0;
170
171 public:
172 /**
173 */
174 virtual Options GetOptions () const = 0;
175
176 public:
177 /**
178 */
179 virtual Set<String> GetCollections () = 0;
180
181 public:
182 /**
183 * Creates the (named) collection (aka table), and does nothing if the table/collection already exists.
184 */
185 virtual Collection::Ptr CreateCollection (const String& name) = 0;
186
187 public:
188 /**
189 */
190 virtual void DropCollection (const String& name) = 0;
191
192 public:
193 /**
194 */
195 virtual Collection::Ptr GetCollection (const String& name) = 0;
196
197 public:
198 /**
199 * Transaction object factory
200 *
201 * \note operations that happen OUTSIDE of a transaction are IMPLICITLY auto-committed immediately.
202 */
204 };
205
206}
207
208/*
209 ********************************************************************************
210 ***************************** Implementation Details ***************************
211 ********************************************************************************
212 */
213#include "Connection.inl"
214
215#endif /*_Stroika_Foundation_Database_Document_Connection_h_*/
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
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...