ORM C++
Loading...
Searching...
No Matches
connection.cppm
Go to the documentation of this file.
1module;
2
3#include <cstddef>
4#include <memory>
5#include <optional>
6#include <string>
7#include <string_view>
8#include <utility>
9
10export module orm:database_connection;
11
12import :foundation;
13import :connection_config;
14import :model;
15import :sql;
16import :query_value;
17import :database_payload;
18
19namespace orm::migrations::detail
20{
21struct DatabaseAccess;
22}
23
24export namespace orm
25{
26class Database;
27
28/** Owns an application transaction. The Database must outlive this guard. */
30{
31public:
35 auto operator=(const ScopedTransaction&) -> ScopedTransaction& = delete;
36 auto operator=(ScopedTransaction&&) -> ScopedTransaction& = delete;
37
38 auto commit() -> void;
39 auto rollback() -> void;
40
41private:
42 friend class Database;
43 explicit ScopedTransaction(Database& database);
44 Database& database;
45};
46
47class Database final
48{
49public:
50 /**
51 * @brief Constructs a new Database object.
52 */
53 template <db::ConnectionConfiguration Config>
54 explicit Database(const Config& config, db::CommandGeneratorFactory registry = {})
55 : Database{std::move(registry), db::ConnectionConfigView{config}}
56 {
57 }
58 ~Database() noexcept;
59 Database(const Database&) = delete;
60 Database(Database&&) = delete;
61 auto operator=(const Database&) -> Database& = delete;
62 auto operator=(Database&&) -> Database& = delete;
63
64 /**
65 * @brief Connects to a database.
66 *
67 * @param config The typed connection parameters, borrowed during opening.
68 */
69 template <db::ConnectionConfiguration Config>
70 auto connect(const Config& config) -> void
71 {
73 }
74
75 /**
76 * @brief Disconnects from the database.
77 */
78 auto disconnect() -> void;
79
80 /** Creates a borrowed ORM context. This Database must outlive the context. */
81 template <typename SchemaType>
82 [[nodiscard]] auto orm() & noexcept -> OrmContext<SchemaType>
83 {
84 return OrmContext<SchemaType>{*this};
85 }
86
87 template <typename SchemaType>
88 auto orm() && -> OrmContext<SchemaType> = delete;
89
90 /**
91 * @brief Get the backend type of the database.
92 *
93 * @return The backend type of the database.
94 */
95 [[nodiscard]] auto getBackendType() const noexcept -> db::BackendType;
96
97 /**
98 * @brief Returns whether a backend session is currently open.
99 */
100 [[nodiscard]] auto isConnected() const noexcept -> bool;
101
102 /**
103 * @brief Returns the capabilities advertised by the connected backend.
104 * @throws DatabaseError when no backend is connected.
105 */
106 [[nodiscard]] auto getBackendCapabilities() const -> const db::BackendCapabilities&;
107
108 /** Starts a transaction that rolls back on scope exit unless explicitly finished. */
109 [[nodiscard]] auto scopedTransaction() & -> ScopedTransaction;
110
111 /**
112 * @brief Starts a transaction.
113 */
114 auto beginTransaction() -> void;
115
116 /**
117 * @brief Commits a transaction.
118 */
119 auto commitTransaction() -> void;
120
121 /**
122 * @brief Rollbacks a transaction.
123 */
124 auto rollbackTransaction() -> void;
125
126private:
127 explicit Database(db::CommandGeneratorFactory registry, db::ConnectionConfigView config);
128 auto connect(db::ConnectionConfigView config) -> void;
129 friend class ScopedTransaction;
130 friend struct migrations::detail::DatabaseAccess;
131 template <typename>
132 friend class OrmContext;
133
134 auto beginInsertManySavepoint() -> std::string;
135 auto rollbackToInsertManySavepoint(std::string_view name) -> void;
136 auto releaseInsertManySavepoint(std::string_view name) -> void;
137 auto executeSavepointCommand(std::string_view command, std::string_view operation) -> void;
138 auto markTransactionFailed() noexcept -> void;
139 auto restoreHealthyTransactionState() noexcept -> void;
140 [[nodiscard]] auto hasApplicationTransaction() const noexcept -> bool;
141 auto checkTransactionStart() const -> void;
142 auto startScopedTransaction(const ScopedTransaction& owner) -> void;
143 auto finishScopedTransaction(const ScopedTransaction& owner, bool commit) -> void;
144 auto closeFailedTransactionSession() noexcept -> void;
145
146 auto executeMutation(const db::Statement& statement, std::string_view operation) -> std::size_t;
147 auto executeMutation(db::StatementView statement, std::string_view operation) -> std::size_t;
148 auto executeInsertIfAbsent(const db::Statement& statement, const db::InsertIfAbsentSpec& conflict,
149 std::string_view operation) -> std::size_t;
150 auto executeSql(std::string_view statement, std::string_view operation) -> void;
151 auto ensureSessionUsable(std::string_view operation) const -> void;
152 auto ensureBoundValueSupported(const db::BoundValue& value, std::string_view operation) -> void;
153 auto ensureDdlSupported(std::string_view operation) const -> void;
154 auto ensureSchemaSupported(model::ModelView model, bool includeJunctions, std::string_view operation) -> void;
155 auto ensureStringValueSupported(const model::ColumnView& column, const std::optional<query::QueryValue>& value,
156 std::string_view operation) const -> void;
157 auto relationEndpointExists(model::ModelView model, const db::binding::PrimaryKey& key) -> bool;
158 auto tableExists(std::string_view tableName) -> bool;
159 auto ensureEndpointKeySupported(model::ModelView model, const db::binding::PrimaryKey& key,
160 std::string_view operation) const -> void;
161 auto ensureRelationTableEndpointsExist(model::ModelView owner) -> void;
162 [[nodiscard]] auto getBackend() const -> const db::BackendProvider&;
163 [[nodiscard]] auto getCommandGenerator() const -> const db::CommandGenerator&;
164 [[nodiscard]] auto getBackendRuntimeLimits() -> db::BackendRuntimeLimits;
165 auto ensureStatementWithinBindLimit(std::size_t parameterCount, std::string_view operation) -> void;
166 auto ensureModelSupported(model::ModelView model, std::string_view operation) const -> void;
167 auto ensureQuerySupported(model::ModelView model, const query::detail::SelectSpec& spec) const -> void;
168 auto ensureQuerySupported(model::ModelView model, db::detail::SqlQueryView query) const -> void;
169 auto ensureQueryTypesSupported(model::ModelView model, db::detail::SqlQueryView query,
170 std::string_view operation) const -> void;
171 auto ensurePredicateSupported(model::ModelView model, const query::detail::Predicate& predicate,
172 std::string_view operation) const -> void;
173 auto ensurePredicateSupported(const query::detail::Predicate& predicate, std::string_view operation) const -> void;
174 auto ensurePredicateSupported(bool containsCollection, std::string_view operation) const -> void;
175 auto ensureAffectedRowsAvailable(std::string_view operation) const -> void;
176 auto requireCapability(bool supported, std::string_view operation, std::string_view message) const -> void;
177 [[noreturn]] auto throwTranslatedError(const soci::soci_error& error, DatabaseErrorCode fallback,
178 std::string_view operation) -> void;
179
180 soci::session sql;
181 std::unique_ptr<soci::transaction> transaction;
182 const ScopedTransaction* scopedOwner = nullptr;
183 bool transactionFailed = false;
184 bool migrationActive = false;
185 std::size_t insertManySavepointSequence = 0;
186 db::BackendType backendType;
187 db::CommandGeneratorFactory commandGeneratorFactory;
188 const db::BackendProvider* backend = nullptr;
189};
190} // namespace orm
auto connect(const Config &config) -> void
Connects to a database.
~Database() noexcept
auto scopedTransaction() &-> ScopedTransaction
auto getBackendCapabilities() const -> const db::BackendCapabilities &
Returns the capabilities advertised by the connected backend.
auto orm() &noexcept -> OrmContext< SchemaType >
Database(const Config &config, db::CommandGeneratorFactory registry={})
Constructs a new Database object.
friend class ScopedTransaction
auto beginTransaction() -> void
Starts a transaction.
auto isConnected() const noexcept -> bool
Returns whether a backend session is currently open.
friend class OrmContext
auto commitTransaction() -> void
Commits a transaction.
auto orm() &&-> OrmContext< SchemaType >=delete
auto getBackendType() const noexcept -> db::BackendType
Get the backend type of the database.
auto rollbackTransaction() -> void
Rollbacks a transaction.
auto disconnect() -> void
Disconnects from the database.
~ScopedTransaction() noexcept
auto rollback() -> void
auto commit() -> void
ScopedTransaction(const ScopedTransaction &)=delete
DatabaseErrorCode
Definition types.cppm:99