Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Foundation/Database/SQL/Connection.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Database_SQL_Connection_h_
5#define _Stroika_Foundation_Database_SQL_Connection_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <optional>
10
12#include "Stroika/Foundation/Common/Property.h"
14
16
17/**
18 * \file
19 *
20 * The point of this module is to define a Connection abstraction that can be used for different kinds
21 * of connections (e.g. SQLite local database object, and ODBC remote database object). This generic API
22 * can then be used in places where either backend database might be in use.
23 *
24 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
25 */
26
27namespace Stroika::Foundation::Database::SQL {
28
29 using Characters::String;
30
31 class Transaction;
32 class Statement;
33
34 namespace Connection {
35
36 class IRep;
37
38 /**
39 * Connection::Ptr provides an API for accessing an SQLite database.
40 *
41 * A new Connection::Ptr is typically created with SOME_SERVICE::Connection::New () (e..g SQLite::Connection::New())
42 *
43 * \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>
44 * But though each connection can only be accessed from a single thread at a time, the underlying database may be
45 * threadsafe (even if accessed across processes).
46 *
47 * The Connection::Ptr itself is standardC++ thread safety. The thread-safety of the underlying database depends on how the underlying
48 * shared_ptr<IRep> was created.
49 */
50 class Ptr {
51 public:
52 /**
53 */
54 Ptr (const Ptr& src);
55 Ptr (const shared_ptr<IRep>& src = nullptr);
56
57 public:
58 ~Ptr () = default;
59
60 public:
61 /**
62 */
63 nonvirtual Ptr& operator= (const Ptr& src);
64 nonvirtual Ptr& operator= (Ptr&& src) noexcept;
65
66 public:
67 /**
68 */
69 nonvirtual IRep* operator->() const noexcept;
70
71 public:
72 /**
73 * The return the EngineProperties of the database being talked to via the Connection::Ptr.
74 *
75 * \note for some Connections, this can change dynamically (like ODBC depending on connection state)
76 */
77 nonvirtual shared_ptr<const EngineProperties> GetEngineProperties () const;
78
79 public:
80 /**
81 * Statement object factory
82 *
83 * The reason you might use this instead of SQLite::Statement{} - is in writing generic code
84 * that doesn't depend on the particular kind of SQL database you are connected to (e.g. that
85 * might be used for ODBC or SQLite).
86 */
87 nonvirtual Statement mkStatement (const String& sql);
88
89 public:
90 /**
91 * Transaction object factory
92 *
93 * The reason you might use this instead of SQLite::Transaction{} - is in writing generic code
94 * that doesn't depend on the particular kind of SQL database you are connected to (e.g. that
95 * might be used for ODBC or SQLite).
96 */
97 nonvirtual Transaction mkTransaction ();
98
99 public:
100 /**
101 * This returns nothing, but raises exceptions on errors.
102 *
103 * \todo - EXTEND this to write the RESPONSE (use the callback) to DbgTrace () calls - perhaps optionally?)
104 */
105 nonvirtual void Exec (const String& sql) const;
106
107 public:
108 /**
109 * @see Characters::ToString ()
110 */
111 nonvirtual String ToString () const;
112
113 public:
114 nonvirtual auto operator== (const Ptr& rhs) const;
115 nonvirtual bool operator== (nullptr_t) const noexcept;
116
117 public:
118 [[no_unique_address]] Debug::AssertExternallySynchronizedMutex fAssertExternallySynchronizedMutex;
119
120 protected:
121 shared_ptr<IRep> _fRep;
122 };
123
124 /**
125 * Connection::IRep provides an (abstract) API for accessing an SQL database.
126 *
127 * \note \em Thread-Safety <a href="Thread-Safety.md#Thread-Safety-Rules-Depends-On-Subtype">Thread-Safety-Rules-Depends-On-Subtype</a>
128 */
129 class IRep : public enable_shared_from_this<IRep> {
130 public:
131 /**
132 */
133 virtual ~IRep () = default;
134
135 public:
136 /**
137 * The return the EngineProperties of the database being talked to via the Connection::Ptr.
138 *
139 * \note for some Connections, this can change dynamically (like ODBC depending on connection state)
140 */
141 virtual shared_ptr<const EngineProperties> GetEngineProperties () const = 0;
142
143 public:
144 /**
145 * Statement object factory
146 */
147 virtual Statement mkStatement (const String& sql) = 0;
148
149 public:
150 /**
151 * Transaction object factory
152 */
154
155 public:
156 /**
157 * This returns nothing, but raises exceptions on errors.
158 */
159 virtual void Exec (const String& sql) = 0;
160 };
161
162 }
163
164}
165
166/*
167 ********************************************************************************
168 ***************************** Implementation Details ***************************
169 ********************************************************************************
170 */
171#include "Connection.inl"
172
173#endif /*_Stroika_Foundation_Database_SQL_Connection_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
virtual Statement mkStatement(const String &sql)=0
virtual shared_ptr< const EngineProperties > GetEngineProperties() const =0
nonvirtual shared_ptr< const EngineProperties > GetEngineProperties() const
NOT a real mutex - just a debugging infrastructure support tool so in debug builds can be assured thr...