PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
base_repository.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
21#pragma once
22
23#define PACS_STORAGE_BASE_REPOSITORY_HPP_INCLUDED
24
26
27#include <functional>
28#include <map>
29#include <memory>
30#include <optional>
31#include <string>
32#include <vector>
33
34#ifdef PACS_WITH_DATABASE_SYSTEM
35
36#include <database/core/database_backend.h>
37#include <database/database_types.h>
38
39namespace kcenon::pacs::storage {
40
42using database_value = database::core::database_value;
43
122template <typename Entity, typename PrimaryKey = int64_t>
123class base_repository {
124public:
126 using entity_type = Entity;
127
129 using pk_type = PrimaryKey;
130
132 using result_type = Result<Entity>;
133
135 using list_result_type = Result<std::vector<Entity>>;
136
144 explicit base_repository(std::shared_ptr<pacs_database_adapter> db,
145 std::string table_name,
146 std::string pk_column = "id");
147
149 virtual ~base_repository() = default;
150
151 // Non-copyable
152 base_repository(const base_repository&) = delete;
153 auto operator=(const base_repository&) -> base_repository& = delete;
154
155 // Movable
156 base_repository(base_repository&&) noexcept = default;
157 auto operator=(base_repository&&) noexcept -> base_repository& = default;
158
159 // ========================================================================
160 // Read Operations
161 // ========================================================================
162
172 [[nodiscard]] virtual auto find_by_id(PrimaryKey id) -> result_type;
173
182 [[nodiscard]] virtual auto find_all(std::optional<size_t> limit = std::nullopt)
183 -> list_result_type;
184
201 [[nodiscard]] virtual auto find_where(const std::string& column,
202 const std::string& op,
203 const database_value& value)
204 -> list_result_type;
205
212 [[nodiscard]] virtual auto exists(PrimaryKey id) -> Result<bool>;
213
219 [[nodiscard]] virtual auto count() -> Result<size_t>;
220
221 // ========================================================================
222 // Write Operations
223 // ========================================================================
224
235 [[nodiscard]] virtual auto save(const Entity& entity)
236 -> Result<PrimaryKey>;
237
246 [[nodiscard]] virtual auto insert(const Entity& entity)
247 -> Result<PrimaryKey>;
248
257 [[nodiscard]] virtual auto update(const Entity& entity) -> VoidResult;
258
265 [[nodiscard]] virtual auto remove(PrimaryKey id) -> VoidResult;
266
275 [[nodiscard]] virtual auto remove_where(const std::string& column,
276 const std::string& op,
277 const database_value& value)
278 -> Result<size_t>;
279
280 // ========================================================================
281 // Batch Operations
282 // ========================================================================
283
293 [[nodiscard]] virtual auto insert_batch(const std::vector<Entity>& entities)
294 -> Result<std::vector<PrimaryKey>>;
295
319 template <typename Func>
320 [[nodiscard]] auto in_transaction(Func&& func)
321 -> std::invoke_result_t<Func>;
322
323protected:
324 // ========================================================================
325 // Abstract Methods - Subclass Must Implement
326 // ========================================================================
327
337 [[nodiscard]] virtual auto map_row_to_entity(const database_row& row) const
338 -> Entity = 0;
339
352 [[nodiscard]] virtual auto entity_to_row(const Entity& entity) const
353 -> std::map<std::string, database_value> = 0;
354
361 [[nodiscard]] virtual auto get_pk(const Entity& entity) const
362 -> PrimaryKey = 0;
363
372 [[nodiscard]] virtual auto has_pk(const Entity& entity) const -> bool = 0;
373
382 [[nodiscard]] virtual auto select_columns() const
383 -> std::vector<std::string>;
384
385 // ========================================================================
386 // Utility Methods for Subclasses
387 // ========================================================================
388
394 [[nodiscard]] auto query_builder() -> database::query_builder;
395
399 [[nodiscard]] auto storage_session() -> pacs_storage_session;
400
406 [[nodiscard]] auto db() -> std::shared_ptr<pacs_database_adapter>;
407
413 [[nodiscard]] auto table_name() const -> const std::string&;
414
420 [[nodiscard]] auto pk_column() const -> const std::string&;
421
422private:
424 std::shared_ptr<pacs_database_adapter> db_;
425
427 std::string table_name_;
428
430 std::string pk_column_;
431};
432
433} // namespace kcenon::pacs::storage
434
435// Include template implementation
436#include "base_repository_impl.h"
437
438#endif // PACS_WITH_DATABASE_SYSTEM
Template implementation of base_repository.
Unified database adapter for PACS system.