ORM C++
Loading...
Searching...
No Matches
commands.cppm
Go to the documentation of this file.
1module;
2
3#include <cstddef>
4#include <format>
5#include <memory>
6#include <stdexcept>
7#include <string>
8#include <string_view>
9#include <unordered_map>
10#include <vector>
11
12export module orm:sql_commands;
13
14import :foundation;
15import :model;
16import :sql_contracts;
17
18export namespace orm::db
19{
21
23{
24 std::size_t rowCount{};
25
26 auto operator==(const InsertManySpec&) const -> bool = default;
27};
28
29/** Stable parameter name for one row of a batch insert; the row index scopes it inside a chunk. */
30[[nodiscard]] inline auto insertManyParameterName(std::size_t rowIndex, std::string_view logicalName) -> std::string
31{
32 return std::format("orm_m{}_{}", rowIndex, logicalName);
33}
34
35namespace detail
36{
37/** One bound insert column: its logical parameter name and the physical column it maps to. */
38struct InsertColumnBinding
39{
40 std::string logicalName;
41 const model::ColumnView* column;
42};
43
44/**
45 * Internal mapping from a model to the columns a plain or batch insert binds, in statement order.
46 *
47 * Auto-increment columns are omitted; a to-one column contributes one logical name per primary-key
48 * column of its target ("field_pk") and binds the target column's type. Legacy insert, insertMany
49 * and the parameter binders all use this single derivation. This is an implementation seam, not
50 * part of the documented backend-extension surface.
51 */
52[[nodiscard]] inline auto insertColumnBindings(model::ModelView model) -> std::vector<InsertColumnBinding>
53{
54 std::vector<InsertColumnBinding> bindings;
55
56 for (const auto& column : model->columns)
57 {
58 if (column.kind == model::FieldKind::ToOne)
59 {
60 const auto target = model.resolveTarget(column);
61 if (target == nullptr)
62 {
63 throw std::invalid_argument{"To-one column has no target model in the schema"};
64 }
65
66 for (const auto& relatedColumn : target->columns)
67 {
68 if (relatedColumn.isPrimaryKey)
69 {
70 bindings.push_back({std::format("{}_{}", column.name, relatedColumn.name), &relatedColumn});
71 }
72 }
73 }
74 else if (not column.isAutoIncrement)
75 {
76 bindings.push_back({std::string{column.name}, &column});
77 }
78 }
79
80 return bindings;
81}
82} // namespace detail
83
85{
86public:
87 virtual ~CommandGenerator() = default;
88
89 [[nodiscard]] virtual auto createTable(model::ModelView model) const -> std::string = 0;
90 [[nodiscard]] virtual auto dropTable(model::ModelView model) const -> std::string = 0;
91 [[nodiscard]] virtual auto insert(model::ModelView model) const -> std::string = 0;
92 /** Non-owning batch statement source; custom generators may keep the default rejection. */
93 [[nodiscard]] virtual auto insertMany(model::ModelView /*model*/,
94 const InsertManySpec& /*spec*/) const -> std::string
95 {
97 "batch inserts are not supported by this command generator"};
98 }
99 [[nodiscard]] virtual auto select(model::ModelView model, const SelectSpec& spec) const -> SelectStatement = 0;
100 [[nodiscard]] virtual auto update(model::ModelView model, const UpdateSpec& spec) const -> Statement = 0;
101 [[nodiscard]] virtual auto remove(model::ModelView model, const Predicate& predicate) const -> Statement = 0;
102};
103
105{
106public:
107 /** The dialect must outlive this generator. */
109
110 [[nodiscard]] auto createTable(model::ModelView model) const -> std::string override;
111 [[nodiscard]] auto dropTable(model::ModelView model) const -> std::string override;
112 [[nodiscard]] auto insert(model::ModelView model) const -> std::string override;
113 [[nodiscard]] auto insertMany(model::ModelView model, const InsertManySpec& spec) const -> std::string override;
114 [[nodiscard]] auto select(model::ModelView model, const SelectSpec& spec) const -> SelectStatement override;
115 [[nodiscard]] auto update(model::ModelView model, const UpdateSpec& spec) const -> Statement override;
116 [[nodiscard]] auto remove(model::ModelView model, const Predicate& predicate) const -> Statement override;
117
118private:
119 const SqlDialect& dialect;
120 BackendType backend;
121};
122} // namespace orm::db
123
124namespace orm::db
125{
126export
127{
130 {
131 public:
133 /** Registers exactly the supplied providers, without adding built-ins. */
134 explicit CommandGeneratorFactory(std::vector<std::unique_ptr<BackendProvider>> providers);
140 auto registerBackend(std::unique_ptr<BackendProvider> backend) -> void;
141 [[nodiscard]] auto getBackend(BackendType backendType) const -> const BackendProvider&;
142 auto getCommandGenerator(BackendType backendType) const -> const CommandGenerator&;
143
144 private:
145 std::unordered_map<BackendType, std::unique_ptr<BackendProvider>> backends;
146 };
147}
148} // namespace orm::db
150{
151export
152{
154 [[nodiscard]] auto createTableStatements(const SqlDialect& dialect, model::ModelView owner,
155 BackendType backend = BackendType::Empty) -> std::vector<std::string>;
156 [[nodiscard]] auto dropTableStatements(const SqlDialect& dialect,
157 model::ModelView owner) -> std::vector<std::string>;
159 [[nodiscard]] auto linkStatement(const SqlDialect& dialect, model::ModelView owner, model::RelationView relation,
160 const binding::PrimaryKey& ownerKey, const binding::PrimaryKey& targetKey,
162 [[nodiscard]] auto linkInsertSpec(model::ModelView owner, model::RelationView relation,
163 const binding::PrimaryKey& ownerKey,
165 [[nodiscard]] auto unlinkStatement(const SqlDialect& dialect, model::ModelView owner, model::RelationView relation,
166 const binding::PrimaryKey& ownerKey, const binding::PrimaryKey& targetKey,
169 [[nodiscard]] auto collectionSelectStatement(const SqlDialect& dialect, model::ModelView owner,
170 model::RelationView relation, std::string targetSelectSql,
171 const std::vector<binding::PrimaryKey>& ownerKeys, bool joinedValues,
173}
174} // namespace orm::db::relations
A backend-neutral database runtime error.
Definition types.cppm:121
CommandGeneratorFactory(CommandGeneratorFactory &&)=default
auto getCommandGenerator(BackendType backendType) const -> const CommandGenerator &
auto getBackend(BackendType backendType) const -> const BackendProvider &
CommandGeneratorFactory(const CommandGeneratorFactory &)=delete
auto registerBackend(std::unique_ptr< BackendProvider > backend) -> void
auto operator=(const CommandGeneratorFactory &) -> CommandGeneratorFactory &=delete
auto operator=(CommandGeneratorFactory &&) -> CommandGeneratorFactory &=default
virtual auto insert(model::ModelView model) const -> std::string=0
virtual auto select(model::ModelView model, const SelectSpec &spec) const -> SelectStatement=0
virtual auto update(model::ModelView model, const UpdateSpec &spec) const -> Statement=0
virtual auto insertMany(model::ModelView, const InsertManySpec &) const -> std::string
Definition commands.cppm:93
virtual ~CommandGenerator()=default
virtual auto dropTable(model::ModelView model) const -> std::string=0
virtual auto createTable(model::ModelView model) const -> std::string=0
virtual auto remove(model::ModelView model, const Predicate &predicate) const -> Statement=0
auto remove(model::ModelView model, const Predicate &predicate) const -> Statement override
auto dropTable(model::ModelView model) const -> std::string override
auto insert(model::ModelView model) const -> std::string override
auto createTable(model::ModelView model) const -> std::string override
auto insertMany(model::ModelView model, const InsertManySpec &spec) const -> std::string override
auto select(model::ModelView model, const SelectSpec &spec) const -> SelectStatement override
DefaultCommandGenerator(const SqlDialect &dialect, BackendType backend=BackendType::Empty)
auto update(model::ModelView model, const UpdateSpec &spec) const -> Statement override
std::vector< query::QueryValue > PrimaryKey
auto linkInsertSpec(model::ModelView owner, model::RelationView relation, const binding::PrimaryKey &ownerKey, const binding::PrimaryKey &targetKey) -> InsertIfAbsentSpec
auto collectionSelectStatement(const SqlDialect &dialect, model::ModelView owner, model::RelationView relation, std::string targetSelectSql, const std::vector< binding::PrimaryKey > &ownerKeys, bool joinedValues, BackendType backend=BackendType::Empty) -> Statement
auto unlinkStatement(const SqlDialect &dialect, model::ModelView owner, model::RelationView relation, const binding::PrimaryKey &ownerKey, const binding::PrimaryKey &targetKey, BackendType backend=BackendType::Empty) -> Statement
auto linkStatement(const SqlDialect &dialect, model::ModelView owner, model::RelationView relation, const binding::PrimaryKey &ownerKey, const binding::PrimaryKey &targetKey, BackendType backend=BackendType::Empty) -> Statement
auto dropTableStatements(const SqlDialect &dialect, model::ModelView owner) -> std::vector< std::string >
auto createTableStatements(const SqlDialect &dialect, model::ModelView owner, BackendType backend=BackendType::Empty) -> std::vector< std::string >
query::detail::UpdateSpec UpdateSpec
BackendType
Definition types.cppm:62
query::detail::Predicate Predicate
auto insertManyParameterName(std::size_t rowIndex, std::string_view logicalName) -> std::string
Definition commands.cppm:30
query::detail::SelectSpec SelectSpec
Statement SelectStatement
Definition commands.cppm:20
auto operator==(const InsertManySpec &) const -> bool=default
Trivially-copyable handle to one model in immutable Schema storage.
Definition views.cppm:181