17import :model_relations;
23template <
typename... Models>
24consteval auto hasUniqueTypes() ->
bool
26 constexpr std::array<TypeId,
sizeof...(Models)> ids{
typeId<Models>()...};
28 for (std::size_t left = 0; left < ids.size() && unique; ++left)
30 for (std::size_t right = left + 1; right < ids.size() && unique; ++right)
32 if (ids[left] == ids[right])
49 template <
typename T,
typename SchemaType>
52 static_assert(detail::StaticSchemaTraits<SchemaType>::template contains<T>,
53 "ORM_SCHEMA_MODEL_MISSING: a static descriptor requires its model to belong to Schema");
54 static_assert(std::is_aggregate_v<T>,
"ORM_MODEL_AGGREGATE: an ORM model must be an aggregate");
56 "ORM_MODEL_FIELD_LIMIT: automatic model reflection supports at most 128 fields");
57 static_assert(detail::hasValidTableNameDefinition<T>(),
58 "ORM_MODEL_TABLE_NAME: table_name must be a non-empty orm::reflection::FixedString");
59 static_assert(detail::hasValidColumnNamesDefinition<T>(),
60 "ORM_MODEL_COLUMN_MAPPING: columns_names must be a valid typed columnNames(...) definition");
61 static_assert(detail::hasValidStringLengthsDefinition<T>(),
62 "ORM_MODEL_STRING_LENGTH: string_lengths must uniquely map string members of this model");
63 static_assert(detail::hasValidUuidStorageDefinition<T>(),
64 "ORM_MODEL_UUID_STORAGE: uuid_storage must uniquely map UUID members per backend");
66 detail::hasValidPrimaryKeyDefinition<T>(),
67 "ORM_MODEL_PRIMARY_KEY_DEFINITION: id_columns must be a valid typed primaryKey<Members...>() definition");
68 static_assert(detail::hasValidAutoIncrementDefinition<T>(),
69 "ORM_MODEL_AUTO_INCREMENT_DEFINITION: auto_increment_columns must be a valid typed "
70 "autoIncrement<Members...>() definition");
71 static_assert(detail::hasValidRelationsDefinition<T>(),
72 "ORM_MODEL_RELATIONS_DEFINITION: relations must be a typed orm::relations(...) definition");
73 static_assert(detail::hasCompleteRelationDefinitions<T>(),
74 "ORM_RELATION_DESCRIPTOR_COUNT: every collection requires exactly one matching descriptor");
76 inline static constexpr auto columns = detail::staticColumns<T, SchemaType>;
78 static_assert(detail::hasValidColumnNames(
columns),
79 "ORM_MODEL_COLUMN_NAME: model field and SQL column names must be non-empty and unique");
80 static_assert(detail::hasValidPrimaryKey(
columns),
81 "ORM_MODEL_PRIMARY_KEY: primary-key members must be non-null scalar fields");
82 static_assert(detail::hasValidAutoIncrement(
columns),
83 "ORM_MODEL_AUTO_INCREMENT: auto increment requires one "
84 "non-null int member which is the model's only primary key");
86 inline static constexpr auto primaryKeyIndices = detail::staticPrimaryKeyIndices<T, SchemaType>;
99 "ORM_MODEL_PRIMARY_KEY_RELATION: primary keys must refer to scalar columns");
101 inline static constexpr auto relations = detail::makeRelations<T, SchemaType>();
105 .schemaIndex = detail::StaticSchemaTraits<SchemaType>::template indexOf<T>(),
106 .typeName = detail::modelTypeName<T>(),
107 .tableName = detail::mappedTableName<T>(),
114 template <
typename SchemaType,
typename T>
120 template <
typename SchemaType,
typename T>
123 return ModelView{&SchemaType::view, detail::StaticSchemaTraits<SchemaType>::template indexOf<T>()};
126 template <
typename T>
127 [[nodiscard]]
consteval auto tableName() -> std::string_view
129 return detail::mappedTableName<T>();
139template <
typename... Models>
140consteval auto hasUniqueTableNames() ->
bool
142 constexpr std::array<std::string_view,
sizeof...(Models)> names{
tableName<Models>()...};
143 for (std::size_t left = 0; left < names.size(); ++left)
145 for (std::size_t right = left + 1; right < names.size(); ++right)
147 if (names[left].
compare(names[right]) == 0)
156template <std::
size_t Size>
157consteval auto hasValidRelationTargets(
const std::array<const ModelDataView*, Size>& models) ->
bool
159 for (
const auto* owner : models)
161 for (std::size_t relationIndex = 0; relationIndex < owner->relations.size(); ++relationIndex)
163 const auto& relation = owner->relations[relationIndex];
164 if (relation.targetModelIndex >= models.size())
168 const auto* target = models[relation.targetModelIndex];
169 if (target ==
nullptr || target->primaryKeyIndices.empty())
176 if (relation.mappedBy.empty())
180 const auto* inverse = target->findRelationField(relation.mappedBy);
182 inverse->targetModelIndex != owner->schemaIndex)
189 if (relation.mappedBy.empty())
193 const auto* owning = target->findRelationField(relation.mappedBy);
195 owning->targetModelIndex != owner->schemaIndex || not owning->junction.isConfigured() ||
196 not owning->junction.owningSide)
206[[nodiscard]]
consteval auto foreignKeyColumnNameSize(std::string_view relationName,
207 std::string_view targetColumnName) -> std::size_t
209 return relationName.size() + 1U + targetColumnName.size();
212[[nodiscard]]
consteval auto foreignKeyColumnNameCharacter(std::string_view relationName,
213 std::string_view targetColumnName, std::size_t index) ->
char
215 if (index < relationName.size())
217 return relationName[index];
219 if (index == relationName.size())
223 return targetColumnName[index - relationName.size() - 1U];
226[[nodiscard]]
consteval auto equalsForeignKeyColumnName(std::string_view candidate, std::string_view relationName,
227 std::string_view targetColumnName) ->
bool
229 if (candidate.size() != foreignKeyColumnNameSize(relationName, targetColumnName))
233 for (std::size_t index = 0; index < candidate.size(); ++index)
235 if (candidate[index] != foreignKeyColumnNameCharacter(relationName, targetColumnName, index))
243[[nodiscard]]
consteval auto equalForeignKeyColumnNames(std::string_view leftRelation,
244 std::string_view leftTargetColumn,
245 std::string_view rightRelation,
246 std::string_view rightTargetColumn) ->
bool
248 const auto size = foreignKeyColumnNameSize(leftRelation, leftTargetColumn);
249 if (size != foreignKeyColumnNameSize(rightRelation, rightTargetColumn))
253 for (std::size_t index = 0; index < size; ++index)
255 if (foreignKeyColumnNameCharacter(leftRelation, leftTargetColumn, index) !=
256 foreignKeyColumnNameCharacter(rightRelation, rightTargetColumn, index))
264template <std::
size_t Size>
265consteval auto hasUniquePhysicalColumnNames(
const std::array<const ModelDataView*, Size>& models) ->
bool
267 for (
const auto* owner : models)
269 for (std::size_t scalarIndex = 0; scalarIndex < owner->columns.size(); ++scalarIndex)
271 const auto& scalar = owner->columns[scalarIndex];
276 for (std::size_t relationColumnIndex = 0; relationColumnIndex < owner->columns.size();
277 ++relationColumnIndex)
279 const auto& relation = owner->columns[relationColumnIndex];
280 if (relation.kind !=
FieldKind::ToOne || relation.targetModelIndex >= models.size())
284 const auto* target = models[relation.targetModelIndex];
285 for (std::size_t keyIndex = 0; keyIndex < target->primaryKeyIndices.size(); ++keyIndex)
287 const auto primaryKeyIndex = target->primaryKeyIndices[keyIndex];
288 if (primaryKeyIndex >= target->columns.size() ||
289 equalsForeignKeyColumnName(scalar.name, relation.name, target->columns[primaryKeyIndex].name))
297 for (std::size_t leftRelationIndex = 0; leftRelationIndex < owner->columns.size(); ++leftRelationIndex)
299 const auto& leftRelation = owner->columns[leftRelationIndex];
300 if (leftRelation.kind !=
FieldKind::ToOne || leftRelation.targetModelIndex >= models.size())
304 const auto* leftTarget = models[leftRelation.targetModelIndex];
305 for (std::size_t leftKeyIndex = 0; leftKeyIndex < leftTarget->primaryKeyIndices.size(); ++leftKeyIndex)
307 const auto leftPrimaryKeyIndex = leftTarget->primaryKeyIndices[leftKeyIndex];
308 if (leftPrimaryKeyIndex >= leftTarget->columns.size())
312 for (std::size_t rightRelationIndex = leftRelationIndex + 1; rightRelationIndex < owner->columns.size();
313 ++rightRelationIndex)
315 const auto& rightRelation = owner->columns[rightRelationIndex];
316 if (rightRelation.kind !=
FieldKind::ToOne || rightRelation.targetModelIndex >= models.size())
320 const auto* rightTarget = models[rightRelation.targetModelIndex];
321 for (std::size_t rightKeyIndex = 0; rightKeyIndex < rightTarget->primaryKeyIndices.size();
324 const auto rightPrimaryKeyIndex = rightTarget->primaryKeyIndices[rightKeyIndex];
325 if (rightPrimaryKeyIndex >= rightTarget->columns.size() ||
326 equalForeignKeyColumnNames(leftRelation.name, leftTarget->columns[leftPrimaryKeyIndex].name,
328 rightTarget->columns[rightPrimaryKeyIndex].name))
340[[nodiscard]]
consteval auto hasUniqueNames(std::span<const std::string_view> left,
341 std::span<const std::string_view> right = {}) ->
bool
343 for (std::size_t leftIndex = 0; leftIndex < left.size(); ++leftIndex)
345 if (left[leftIndex].empty())
349 for (std::size_t other = leftIndex + 1; other < left.size(); ++other)
351 if (left[leftIndex].
compare(left[other]) == 0)
356 for (std::size_t rightIndex = 0; rightIndex < right.size(); ++rightIndex)
358 const auto rightName = right[rightIndex];
359 if (rightName.empty() || left[leftIndex].compare(rightName) == 0)
365 for (std::size_t rightIndex = 0; rightIndex < right.size(); ++rightIndex)
367 if (right[rightIndex].empty())
371 for (std::size_t other = rightIndex + 1; other < right.size(); ++other)
373 if (right[rightIndex].
compare(right[other]) == 0)
382template <std::
size_t Size>
383consteval auto hasValidJunctions(
const std::array<const ModelDataView*, Size>& models) ->
bool
385 for (std::size_t ownerIndex = 0; ownerIndex < models.size(); ++ownerIndex)
387 const auto* owner = models[ownerIndex];
388 for (std::size_t relationIndex = 0; relationIndex < owner->relations.size(); ++relationIndex)
390 const auto& relation = owner->relations[relationIndex];
395 const auto* target = models[relation.targetModelIndex];
396 if (not relation.mappedBy.empty() || not relation.junction.owningSide ||
397 relation.junction.ownerColumns.size() != owner->primaryKeyIndices.size() ||
398 relation.junction.targetColumns.size() != target->primaryKeyIndices.size() ||
399 not hasUniqueNames(relation.junction.ownerColumns, relation.junction.targetColumns))
403 for (
const auto* model : models)
405 if (model->tableName.compare(relation.junction.tableName) == 0)
410 for (std::size_t otherOwnerIndex = ownerIndex; otherOwnerIndex < models.size(); ++otherOwnerIndex)
412 const auto* otherOwner = models[otherOwnerIndex];
413 const auto firstRelation = otherOwnerIndex == ownerIndex ? relationIndex + 1 : std::size_t{};
414 for (std::size_t otherRelationIndex = firstRelation; otherRelationIndex < otherOwner->relations.size();
415 ++otherRelationIndex)
417 const auto& other = otherOwner->relations[otherRelationIndex];
419 other.junction.tableName.compare(relation.junction.tableName) == 0)
437 template <
typename... Models>
440 static_assert(detail::hasUniqueTypes<Models...>(),
441 "ORM_SCHEMA_DUPLICATE_MODEL: a schema may list a model type only once");
442 static_assert(detail::hasUniqueTableNames<Models...>(),
443 "ORM_SCHEMA_DUPLICATE_TABLE: every model in a schema must use a unique table name");
445 template <
typename Model>
446 inline static constexpr bool contains = (std::same_as<Model, Models> || ...);
448 template <
typename Model>
449 [[nodiscard]]
static consteval auto indexOf() -> std::size_t
452 "ORM_SCHEMA_MODEL_MISSING: requested model type does not belong to this schema");
453 constexpr std::array<bool,
sizeof...(Models)> matches{std::same_as<Model, Models>...};
454 for (std::size_t index = 0; index < matches.size(); ++index)
464 [[nodiscard]]
static constexpr auto modelAt(std::size_t index)
noexcept ->
ModelView
472 static_assert(detail::hasUniquePhysicalColumnNames(
modelStorage),
473 "ORM_SCHEMA_COLUMN_COLLISION: scalar and generated foreign-key column names must be unique");
475 static_assert(detail::hasValidRelationTargets(
modelStorage),
476 "ORM_SCHEMA_RELATION_GRAPH: mappedBy must resolve to a compatible relation in the closed schema");
479 "ORM_SCHEMA_JUNCTION: junction tables and columns must be unique and match endpoint primary keys");
484 template <
typename SchemaType>
487 return SchemaType::view;
490 template <
typename SchemaType,
typename T>
493 static_assert(SchemaType::template contains<std::remove_cv_t<T>>,
494 "ORM_SCHEMA_MODEL_MISSING: requested model type does not belong to the closed schema");
504 template <
typename... Models>
507 template <
typename SchemaType,
typename T>
513 template <
typename SchemaType,
typename T>
514 [[nodiscard]]
constexpr auto modelView() noexcept ->
model::ModelView
auto compare(const SchemaSnapshot &before, const SchemaSnapshot &after, const GenerationDecisions &decisions={}) -> SchemaDiff
constexpr auto schemaView() noexcept -> const SchemaView &
consteval auto requireSchemaModel() -> void
consteval auto modelDescriptor() noexcept -> StaticModel< T, SchemaType >
consteval auto tableName() -> std::string_view
consteval auto typeId() noexcept -> TypeId
constexpr auto modelView() noexcept -> ModelView
constexpr std::size_t fieldCount
constexpr auto modelView() noexcept -> model::ModelView
consteval auto modelDescriptor() noexcept -> model::StaticModel< T, SchemaType >
model::Schema< Models... > Schema
Immutable data stored once for a model inside a closed schema.
Trivially-copyable handle to one model in immutable Schema storage.
Immutable view over a statically materialized schema.
constexpr auto at(std::size_t index) const noexcept -> ModelView
A closed compile-time set of ORM models.
static constexpr std::array< const ModelDataView *, sizeof...(Models)> modelStorage
static constexpr SchemaView view
static constexpr auto modelAt(std::size_t index) noexcept -> ModelView
static consteval auto indexOf() -> std::size_t
static constexpr bool contains
Materializes one aggregate model's complete scalar metadata in static storage.
static constexpr auto primaryKeyIndices
static constexpr auto relations
static constexpr auto columns
static constexpr ModelDataView data