25 template <
typename... Models>
29 template <auto Member, db::BackendType Backend, Uu
idStorage Storage>
33 inline static constexpr auto member = Member;
34 inline static constexpr auto backend = Backend;
35 inline static constexpr auto storage = Storage;
37 "ORM_MODEL_UUID_STORAGE: storage overrides are supported for MySQL and SQLite only");
40 "ORM_MODEL_UUID_STORAGE: invalid UUID storage representation");
43 template <
typename... Definitions>
46 template <
typename Model>
47 [[nodiscard]]
static consteval auto isValidFor() ->
bool
49 if constexpr (not(std::same_as<detail::member_owner_t<Definitions::member>, Model> && ...))
53 constexpr std::array<
db::BackendType,
sizeof...(Definitions)> backends{Definitions::backend...};
59 const auto nameStorage = std::tuple{detail::reflectedMemberName<Definitions::member>()...};
60 const auto names = [&]<std::size_t... I>(std::index_sequence<I...>)
consteval {
61 return std::array<std::string_view,
sizeof...(Definitions)>{std::get<I>(nameStorage).view()...};
62 }(std::make_index_sequence<
sizeof...(Definitions)>{});
63 for (std::size_t left = 0; left < names.size(); ++left)
64 for (std::size_t right = left + 1; right < names.size(); ++right)
65 if (names[left] == names[right] && backends[left] == backends[right])
67 return (detail::memberExists<Model, Definitions::member>() && ...) &&
68 ((orm::UuidDomainValue<
69 detail::static_optional_value_t<detail::member_value_t<Definitions::member>>>) &&
77 ((detail::reflectedMemberName<Definitions::member>() == name && Definitions::backend == backend ?
78 static_cast<void>(result = Definitions::storage) :
79 static_cast<void>(0)),
88struct IsUuidStorages : std::false_type
92template <
typename... Definitions>
93struct IsUuidStorages<UuidStorages<Definitions...>> : std::true_type
97template <
typename Model>
98consteval auto hasValidUuidStorageDefinition() ->
bool
100 if constexpr (
requires { Model::uuid_storage; })
102 using definition_t = std::remove_cvref_t<
decltype(Model::uuid_storage)>;
103 if constexpr (IsUuidStorages<definition_t>::value)
104 return definition_t::template isValidFor<Model>();
112template <
typename Model>
115 if constexpr (
requires { Model::uuid_storage; })
117 using definition_t = std::remove_cvref_t<
decltype(Model::uuid_storage)>;
118 if constexpr (IsUuidStorages<definition_t>::value && definition_t::template isValidFor<Model>())
119 return Model::uuid_storage.find(name, backend);
125struct StaticSchemaTraits;
127template <
typename... Models>
128struct StaticSchemaTraits<
Schema<Models...>>
130 template <
typename Model>
131 inline static constexpr bool contains = (std::same_as<Model, Models> || ...);
133 template <
typename Model, std::size_t... Is>
134 [[nodiscard]]
static consteval auto indexOfImpl(std::index_sequence<Is...>) -> std::size_t
136 std::size_t result = 0;
137 (void)(((std::same_as<Model, Models>) ? (result = Is,
true) :
false) || ...);
141 template <
typename Model>
142 [[nodiscard]]
static consteval auto indexOf() -> std::size_t
144 static_assert(contains<Model>,
"ORM_SCHEMA_MODEL_MISSING: requested model type does not belong to this schema");
145 return indexOfImpl<Model>(std::make_index_sequence<
sizeof...(Models)>{});
150struct IsFixedString : std::false_type
154template <std::
size_t Size>
155struct IsFixedString<reflection::
FixedString<Size>> : std::true_type
160struct IsColumnNames : std::false_type
164template <
typename... Definitions>
165struct IsColumnNames<ColumnNames<Definitions...>> : std::true_type
170struct IsPrimaryKey : std::false_type
175struct IsStringLengths : std::false_type
179template <
typename... Definitions>
180struct IsStringLengths<StringLengths<Definitions...>> : std::true_type
184template <
typename Model>
185consteval auto hasValidStringLengthsDefinition() ->
bool
187 if constexpr (
requires { Model::string_lengths; })
189 using definition_t = std::remove_cvref_t<
decltype(Model::string_lengths)>;
190 if constexpr (IsStringLengths<definition_t>::value)
191 return definition_t::template isValidFor<Model>();
199template <
typename Model>
200consteval auto mappedStringLength(std::string_view name) -> std::optional<std::size_t>
202 if constexpr (
requires { Model::string_lengths; })
204 using definition_t = std::remove_cvref_t<
decltype(Model::string_lengths)>;
205 if constexpr (IsStringLengths<definition_t>::value)
206 return Model::string_lengths.find(name);
211template <
auto... Members>
212struct IsPrimaryKey<
PrimaryKey<Members...>> : std::true_type
217struct IsAutoIncrement : std::false_type
221template <
auto... Members>
222struct IsAutoIncrement<AutoIncrement<Members...>> : std::true_type
227struct IsRelations : std::false_type
231template <
typename... Descriptors>
232struct IsRelations<std::tuple<Descriptors...>>
233 : std::bool_constant<(orm::detail::IsRelationDescriptor<Descriptors>::value && ...)>
237template <
typename Model>
238consteval auto hasValidTableNameDefinition() ->
bool
240 if constexpr (
requires { Model::table_name; })
242 using definition_t = std::remove_cvref_t<
decltype(Model::table_name)>;
243 if constexpr (IsFixedString<definition_t>::value)
245 return not Model::table_name.view().empty();
258template <
typename Model>
259consteval auto hasValidColumnNamesDefinition() ->
bool
261 if constexpr (
requires { Model::columns_names; })
263 using definition_t = std::remove_cvref_t<
decltype(Model::columns_names)>;
264 if constexpr (IsColumnNames<definition_t>::value)
266 return definition_t::template isValidFor<Model>();
279template <
typename Model>
280consteval auto hasValidPrimaryKeyDefinition() ->
bool
282 if constexpr (
requires { Model::id_columns; })
284 using definition_t = std::remove_cvref_t<
decltype(Model::id_columns)>;
285 if constexpr (IsPrimaryKey<definition_t>::value)
287 return definition_t::template isValidFor<Model>();
300template <
typename Model>
301consteval auto hasValidAutoIncrementDefinition() ->
bool
303 if constexpr (
requires { Model::auto_increment_columns; })
305 using definition_t = std::remove_cvref_t<
decltype(Model::auto_increment_columns)>;
306 if constexpr (IsAutoIncrement<definition_t>::value)
308 return definition_t::template isValidFor<Model>();
321template <
typename Model>
322consteval auto normalizedDefaultTableName()
325 constexpr auto prefixSize = source.starts_with(
"class ") ? std::string_view{
"class "}.size() :
326 source.starts_with(
"struct ") ? std::string_view{
"struct "}.size() :
328 constexpr auto resultSize = [source]
330 std::size_t result{};
331 for (std::size_t index = prefixSize; index < source.size(); ++index)
334 if (source[index] ==
':' && index + 1 < source.size() && source[index + 1] ==
':')
342 std::array<char, resultSize + 1> result{};
343 std::size_t output{};
344 for (std::size_t index = prefixSize; index < source.size(); ++index)
346 if (source[index] ==
':' && index + 1 < source.size() && source[index + 1] ==
':')
348 result[output++] =
'_';
353 result[output++] = source[index];
359template <
typename Model>
360inline constexpr auto defaultTableNameStorage = normalizedDefaultTableName<Model>();
362template <
typename Model>
363consteval auto modelTypeName() -> std::string_view
368template <
typename Model>
369consteval auto mappedTableName() -> std::string_view
371 if constexpr (
requires { Model::table_name; })
373 using definition_t = std::remove_cvref_t<
decltype(Model::table_name)>;
374 if constexpr (IsFixedString<definition_t>::value)
376 return Model::table_name.view();
379 constexpr auto& storage = defaultTableNameStorage<Model>;
380 return std::string_view{storage.data(), storage.size() - 1};
383template <
typename Model>
384consteval auto mappedColumnName(std::string_view fieldName) -> std::string_view
386 if constexpr (
requires { Model::columns_names; })
388 using definition_t = std::remove_cvref_t<
decltype(Model::columns_names)>;
389 if constexpr (IsColumnNames<definition_t>::value && definition_t::template isValidFor<Model>())
391 if (
const auto mapped = Model::columns_names.find(fieldName); mapped.has_value())
393 return mapped.value();
400template <
typename Model>
401consteval auto isPrimaryKey(std::string_view fieldName) ->
bool
403 if constexpr (
requires { Model::id_columns; })
405 using definition_t = std::remove_cvref_t<
decltype(Model::id_columns)>;
406 if constexpr (IsPrimaryKey<definition_t>::value && definition_t::template isValidFor<Model>())
408 return Model::id_columns.contains(fieldName);
418template <
typename Model>
419consteval auto isAutoIncrement(std::string_view fieldName) ->
bool
421 if constexpr (
requires { Model::auto_increment_columns; })
423 using definition_t = std::remove_cvref_t<
decltype(Model::auto_increment_columns)>;
424 if constexpr (IsAutoIncrement<definition_t>::value && definition_t::template isValidFor<Model>())
426 return Model::auto_increment_columns.contains(fieldName);
436template <
typename Model, std::size_t... Indices>
437consteval auto hasDefaultPrimaryKey(std::index_sequence<Indices...>) ->
bool
439 return (((reflectedFieldName<Model, Indices>().
compare(
"id") == 0) &&
445template <
typename Model>
446consteval auto hasPrimaryKey() ->
bool
448 if constexpr (
requires { Model::id_columns; })
450 using definition_t = std::remove_cvref_t<
decltype(Model::id_columns)>;
451 if constexpr (IsPrimaryKey<definition_t>::value)
453 return definition_t::size != 0 && definition_t::template isValidFor<Model>();
466template <
typename Model,
typename SchemaType, std::
size_t Index>
467consteval auto makeColumn() -> ColumnView
470 using value_t = std::remove_cv_t<static_optional_value_t<std::remove_cvref_t<field_t>>>;
471 constexpr auto fieldName = reflectedFieldName<Model, Index>();
473 "ORM_MODEL_OPTIONAL_COLLECTION: relation collections cannot be optional");
475 if constexpr (StaticSchemaTraits<SchemaType>::template contains<value_t>)
477 static_assert(hasPrimaryKey<value_t>(),
478 "ORM_RELATION_KEYLESS: every to-one target must define a valid primary key");
482 .name = mappedColumnName<Model>(fieldName),
483 .type = std::nullopt,
485 .targetModelIndex = StaticSchemaTraits<SchemaType>::template indexOf<value_t>(),
486 .isPrimaryKey = isPrimaryKey<Model>(fieldName),
487 .isAutoIncrement = isAutoIncrement<Model>(fieldName),
489 .maxStringLength = std::nullopt,
497 .name = mappedColumnName<Model>(fieldName),
501 .isPrimaryKey = isPrimaryKey<Model>(fieldName),
502 .isAutoIncrement = isAutoIncrement<Model>(fieldName),
504 .maxStringLength = mappedStringLength<Model>(fieldName),
511template <
typename Model, std::size_t OutputIndex, std::size_t... FieldIndices>
512consteval auto columnFieldIndex(std::index_sequence<FieldIndices...>) -> std::size_t
514 constexpr std::array fieldIndices{FieldIndices...};
515 constexpr std::array included{
518 std::size_t output{};
520 for (std::size_t index = 0; index < included.size(); ++index)
524 if (output == OutputIndex)
526 result = fieldIndices[index];
535template <
typename Model,
typename SchemaType, std::size_t... FieldIndices, std::size_t... OutputIndices>
536consteval auto makeColumnsDirect(std::index_sequence<FieldIndices...>, std::index_sequence<OutputIndices...>)
538 return std::array<ColumnView,
sizeof...(OutputIndices)>{
539 makeColumn<Model, SchemaType,
540 columnFieldIndex<Model, OutputIndices>(std::index_sequence<FieldIndices...>{})>()...};
543template <
typename Model,
typename SchemaType, std::size_t... Indices>
544consteval auto makeColumns(std::index_sequence<Indices...> fields)
547 "ORM_MODEL_OPTIONAL_COLLECTION: relation collections cannot be optional");
548 constexpr auto columnCount =
550 return makeColumnsDirect<Model, SchemaType>(fields, std::make_index_sequence<columnCount>{});
553template <std::
size_t Size>
554consteval auto hasValidColumnNames(
const std::array<ColumnView, Size>& columns) ->
bool
557 for (std::size_t left = 0; left < columns.size() && valid; ++left)
559 if (columns[left].
fieldName.empty() || columns[left].name.empty())
564 for (std::size_t right = left + 1; right < columns.size() && valid; ++right)
566 if (columns[left].
fieldName.compare(columns[right].fieldName) == 0 ||
567 columns[left].name.compare(columns[right].name) == 0 ||
568 columns[left].fieldName.compare(columns[right].name) == 0 ||
569 columns[left].name.compare(columns[right].fieldName) == 0)
579template <std::
size_t Size>
580consteval auto hasValidPrimaryKey(
const std::array<ColumnView, Size>& columns) ->
bool
583 for (
const auto& column : columns)
585 if (column.isPrimaryKey && (not column.isNotNull || column.kind !=
FieldKind::Scalar))
594template <std::
size_t Size>
595consteval auto hasValidAutoIncrement(
const std::array<ColumnView, Size>& columns) ->
bool
597 std::size_t primaryKeyCount{};
598 std::size_t autoIncrementCount{};
599 const ColumnView* autoIncrementColumn{};
601 for (
const auto& column : columns)
603 primaryKeyCount += column.isPrimaryKey ? 1U : 0U;
604 if (column.isAutoIncrement)
606 ++autoIncrementCount;
607 autoIncrementColumn = &column;
611 if (autoIncrementCount == 0)
615 return autoIncrementCount == 1 && primaryKeyCount == 1 && autoIncrementColumn->isPrimaryKey &&
616 autoIncrementColumn->isNotNull && autoIncrementColumn->kind ==
FieldKind::Scalar &&
620template <
typename Model>
621consteval auto modelPrimaryKeyCount() -> std::size_t
623 if constexpr (
requires { Model::id_columns; })
625 using definition_t = std::remove_cvref_t<
decltype(Model::id_columns)>;
626 if constexpr (IsPrimaryKey<definition_t>::value)
628 return definition_t::size;
641template <std::
size_t ResultSize, std::
size_t ColumnCount>
642consteval auto makePrimaryKeyIndices(
const std::array<ColumnView, ColumnCount>& columns)
644 std::array<std::size_t, ResultSize> result{};
645 std::size_t output{};
646 for (std::size_t index = 0; index < columns.size(); ++index)
648 if (columns[index].isPrimaryKey)
650 result[output++] = index;
656template <
typename Model, std::
size_t ColumnCount>
657consteval auto makeModelPrimaryKeyIndices(
const std::array<ColumnView, ColumnCount>& columns)
659 if constexpr (
requires { Model::id_columns; })
661 using definition_t = std::remove_cvref_t<
decltype(Model::id_columns)>;
662 if constexpr (IsPrimaryKey<definition_t>::value && definition_t::template isValidFor<Model>())
664 constexpr auto fieldIndices = definition_t::template indices<Model>();
665 std::array<std::size_t, fieldIndices.size()> result{};
666 for (std::size_t keyIndex = 0; keyIndex < fieldIndices.size(); ++keyIndex)
669 for (std::size_t columnIndex = 0; columnIndex < columns.size(); ++columnIndex)
671 if (columns[columnIndex].fieldIndex == fieldIndices[keyIndex])
673 result[keyIndex] = columnIndex;
680 result[keyIndex] = columns.size();
687 return std::array<std::size_t, 0>{};
692 return makePrimaryKeyIndices<modelPrimaryKeyCount<Model>()>(columns);
696template <
typename Model,
typename SchemaType>
697inline constexpr auto staticColumns =
700template <
typename Model,
typename SchemaType>
701inline constexpr auto staticPrimaryKeyIndices = makeModelPrimaryKeyIndices<Model>(staticColumns<Model, SchemaType>);
709 template <auto Member, db::BackendType Backend, model::Uu
idStorage Storage>
716 template <
typename... Definitions>
std::vector< query::QueryValue > PrimaryKey
auto compare(const SchemaSnapshot &before, const SchemaSnapshot &after, const GenerationDecisions &decisions={}) -> SchemaDiff
consteval auto logicalType() -> ColumnType
Resolves a C++ field type to its logical database type at compile time. Users may extend the closed s...
constexpr auto noTargetModel
constexpr bool isNullable
std::remove_reference_t< std::tuple_element_t< Index, typename detail::ReflectionTraits< detail::ReflectedType< T > >::Tuple > > field_type_t
FixedString(const char(&)[N]) -> FixedString< N - 1 >
constexpr std::size_t fieldCount
consteval auto fieldName() noexcept
constexpr auto typeNameStorage
constexpr bool is_relation_collection_v
consteval auto uuidStorages(Definitions...) -> model::UuidStorages< Definitions... >
consteval auto uuidStorage() -> model::UuidStorageDefinition< Member, Backend, Storage >
constexpr bool is_optional_relation_collection_v
model::Schema< Models... > Schema
A closed compile-time set of ORM models.
static constexpr auto storage
static constexpr auto member
static constexpr auto backend
consteval auto find(std::string_view name, db::BackendType backend) const -> UuidStorage
static consteval auto isValidFor() -> bool