|
- #pragma once
-
- #include "field_primary_key.h"
-
- #include "../../context/data_context.inl"
-
- namespace cpphibernate {
- namespace mariadb {
-
- /* field_primary_key_t */
-
- template<
- typename T_field>
- template<
- typename T_schema,
- typename T_table>
- field_primary_key_t<T_field>::field_primary_key_t(
- const table_t& p_owner,
- const T_schema& p_schema,
- const T_table& p_table,
- const T_field& p_field)
- : field_value_t<T_field>::field_value_t(
- p_owner,
- p_schema,
- p_table,
- p_field)
- {
- this->value_is_auto_incremented = key_props.is_auto_generated();
- auto args = key_props.create_table_argument();
- if (args)
- this->create_arguments = args;
- this->name = this->table.name + '_' + this->name;
- }
-
- template<
- typename T_field>
- bool field_primary_key_t<T_field>
- ::is_default(const data_context& context) const
- {
- using dataset_type = typename mp::decay_t<T_field>::dataset_type;
- auto& dataset = context.get<dataset_type>();
- return key_props.is_default(this->_field.getter(dataset));
- }
-
- template<
- typename T_field>
- std::string field_primary_key_t<T_field>
- ::generate_value(::cppmariadb::connection& connection) const
- {
- auto * query = key_props.create_key_query();
- if (!query)
- throw exception("unable to generate key value: no query defined for this key type!");
-
- auto ret = connection.execute_used(query);
- if (!ret || !ret->next())
- throw exception("unable to generate key value: empty result!");
-
- return ret->current()->at(0).template get<std::string>();
- }
-
- } }
|