Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

157 linhas
5.8 KiB

  1. #pragma once
  2. #include <cppmariadb.h>
  3. #include "../../types.h"
  4. #include "../attributes/attributes.h"
  5. namespace cpphibernate {
  6. namespace mariadb {
  7. struct table_t;
  8. struct data_context;
  9. struct create_update_context;
  10. /**
  11. * @brief Abstract field class.
  12. */
  13. struct field_t
  14. {
  15. public:
  16. size_t id { 0 }; //!< unique id of the field
  17. size_t value_id { 0 }; //!< unique id of the value type
  18. size_t real_value_id { 0 }; //!< unique id of the real/unwrapped value type
  19. bool value_is_nullable { false }; //!< value is stored in a nullable container
  20. bool value_is_pointer { false }; //!< value is stored in a pointer container
  21. bool value_is_container { false }; //!< value is stored in a container
  22. bool value_is_ordered { false }; //!< value is stored in a ordered container (vector, list, ...)
  23. bool value_is_auto_incremented { false }; //!< value is a auto incremented field
  24. const table_t& table; //!< table this field belongs to
  25. const table_t* referenced_table { nullptr }; //!< table that belongs to the value (if exists)
  26. std::string name; //!< name of the SQL field
  27. std::string type; //!< SQL type name
  28. std::string create_arguments; //!< additional arguments for CREATE TABLE command
  29. std::string convert_to_open; //!< SQL code to open the "convert to" operation
  30. std::string convert_to_close; //!< SQL code to close the "convert to" operation
  31. std::string convert_from_open; //!< SQL code to open the "convert from" operation
  32. std::string convert_from_close; //!< SQL code to close the "convert from" operation
  33. attributes_t attributes; //!< attributes for the field
  34. public:
  35. /**
  36. * @brief Value constructor. Creates a mariadb field from the cpphibernate field.
  37. *
  38. * @param[in] p_owner Owner of the field.
  39. * @param[in] p_schema Cpphibernate schema the mariadb field belongs to.
  40. * @param[in] p_table Cpphibernate table the mariadb field belongs to.
  41. * @param[in] p_field Cpphibernate field to create mariadb field for.
  42. */
  43. template<
  44. typename T_schema,
  45. typename T_table,
  46. typename T_field>
  47. inline field_t(
  48. const table_t& p_owner,
  49. const T_schema& p_schema,
  50. const T_table& p_table,
  51. const T_field& p_field);
  52. /**
  53. * @brief Move constructor.
  54. */
  55. inline field_t(field_t&& other) = delete;
  56. /**
  57. * @brief Copy constructor.
  58. */
  59. inline field_t(const field_t&) = delete;
  60. /**
  61. * @brief Destructor.
  62. */
  63. virtual ~field_t() = 0;
  64. /**
  65. * @brief Print the field values to the passed stream.
  66. */
  67. std::ostream& print(std::ostream& os) const;
  68. public:
  69. /**
  70. * @brief Get the value of this field from the current dataset.
  71. *
  72. * @param[in] context Data context to get the dataset from.
  73. *
  74. * @return Value of the field from the current dataset.
  75. */
  76. virtual value_t get(const data_context& context) const;
  77. /**
  78. * @brief Set a new value of this field in the current dataset.
  79. *
  80. * @param[in] context Data context to get the dataset from.
  81. * @param[in] value Value of the field to assign to the dataset.
  82. */
  83. virtual void set(const data_context& context, const value_t& value) const;
  84. /**
  85. * @brief Check if the value that is represented by this field has the default value.
  86. *
  87. * @param[in] context Data context to receive the value of the assigned dataset.
  88. *
  89. * @retval true If the dataset in the context is the default value for this field.
  90. * @retval false If the dataset in the context is not the default value for this field.
  91. */
  92. virtual bool is_default(const data_context& context) const;
  93. /**
  94. * @brief Create a new value that is represented by this field.
  95. *
  96. * @param[in] connection Connection to use to create the value.
  97. *
  98. * @return New created value for this field.
  99. */
  100. virtual std::string generate_value(::cppmariadb::connection& connection) const;
  101. public:
  102. /**
  103. * @brief Execute a create/update operation on the foreign table this field represents (if it is a foreign field)
  104. *
  105. * @param[in] context Create/Update context with the needed data for the operation.
  106. *
  107. * @return Key of the created/updated foreign dataset.
  108. */
  109. virtual value_t foreign_create_update(const create_update_context& context) const;
  110. private:
  111. /**
  112. * @brief Initialize the field.
  113. */
  114. void init();
  115. };
  116. using field_ptr_u = std::unique_ptr<const field_t>;
  117. namespace __impl
  118. {
  119. /**
  120. * @brief Helper type to build table.
  121. */
  122. template<typename X, typename = void>
  123. struct field_builder;
  124. }
  125. /**
  126. * @brief Predicate to create mariadb table class.
  127. */
  128. constexpr decltype(auto) make_field = mp::generic_predicate<__impl::field_builder> { };
  129. } }