metadata.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_METADATA_HPP
  8. #define BOOST_MYSQL_METADATA_HPP
  9. #include <boost/mysql/column_type.hpp>
  10. #include <boost/mysql/string_view.hpp>
  11. #include <boost/mysql/detail/access.hpp>
  12. #include <boost/mysql/detail/coldef_view.hpp>
  13. #include <boost/mysql/detail/flags.hpp>
  14. #include <string>
  15. namespace boost {
  16. namespace mysql {
  17. /**
  18. * \brief Metadata about a column in a SQL query.
  19. * \details This is a regular, value type. Instances of this class are not created by the user
  20. * directly, but by the library.
  21. */
  22. class metadata
  23. {
  24. public:
  25. /**
  26. * \brief Default constructor.
  27. * \details The constructed metadata object has undefined
  28. * values for all of its members.
  29. *
  30. * \par Exception safety
  31. * No-throw guarantee.
  32. */
  33. metadata() = default;
  34. /**
  35. * \brief Move constructor.
  36. *
  37. * \par Exception safety
  38. * No-throw guarantee.
  39. *
  40. * \par Object lifetimes
  41. * `string_view`s obtained by calling accessor functions on `other` are invalidated.
  42. */
  43. metadata(metadata&& other) = default;
  44. /**
  45. * \brief Copy constructor.
  46. *
  47. * \par Exception safety
  48. * Strong guarantee. Internal allocations may throw.
  49. */
  50. metadata(const metadata& other) = default;
  51. /**
  52. * \brief Move assignment.
  53. *
  54. * \par Exception safety
  55. * No-throw guarantee.
  56. *
  57. * \par Object lifetimes
  58. * `string_view`s obtained by calling accessor functions on both `*this` and `other`
  59. * are invalidated.
  60. */
  61. metadata& operator=(metadata&& other) = default;
  62. /**
  63. * \brief Copy assignment.
  64. *
  65. * \par Exception safety
  66. * Basic guarantee. Internal allocations may throw.
  67. *
  68. * \par Object lifetimes
  69. * `string_view`s obtained by calling accessor functions on `*this`
  70. * are invalidated.
  71. */
  72. metadata& operator=(const metadata& other) = default;
  73. /// Destructor.
  74. ~metadata() = default;
  75. /**
  76. * \brief Returns the name of the database (schema) the column belongs to.
  77. * \details
  78. * This is optional information - it won't be populated unless
  79. * the connection executing the query has `meta_mode() == metadata_mode::full`.
  80. *
  81. * \par Exception safety
  82. * No-throw guarantee.
  83. *
  84. * \par Object lifetimes
  85. * The returned reference is valid as long as `*this` is alive and hasn't been
  86. * assigned to or moved from.
  87. */
  88. string_view database() const noexcept { return schema_; }
  89. /**
  90. * \brief Returns the name of the virtual table the column belongs to.
  91. * \details If the table was aliased, this will be the name of the alias
  92. * (e.g. in `"SELECT * FROM employees emp"`, `table()` will be `"emp"`).
  93. *\n
  94. * This is optional information - it won't be populated unless
  95. * the connection executing the query has `meta_mode() == metadata_mode::full`.
  96. *
  97. * \par Exception safety
  98. * No-throw guarantee.
  99. *
  100. * \par Object lifetimes
  101. * The returned reference is valid as long as `*this` is alive and hasn't been
  102. * assigned to or moved from.
  103. */
  104. string_view table() const noexcept { return table_; }
  105. /**
  106. * \brief Returns the name of the physical table the column belongs to.
  107. * \details E.g. in `"SELECT * FROM employees emp"`,
  108. * `original_table()` will be `"employees"`.
  109. * \n
  110. * This is optional information - it won't be populated unless
  111. * the connection executing the query has `meta_mode() == metadata_mode::full`.
  112. *
  113. * \par Exception safety
  114. * No-throw guarantee.
  115. *
  116. * \par Object lifetimes
  117. * The returned reference is valid as long as `*this` is alive and hasn't been
  118. * assigned to or moved from.
  119. */
  120. string_view original_table() const noexcept { return org_table_; }
  121. /**
  122. * \brief Returns the actual name of the column.
  123. * \details If the column was aliased, this will be the name of the alias
  124. * (e.g. in `"SELECT id AS employee_id FROM employees"`,
  125. * `column_name()` will be `"employee_id"`).
  126. *\n
  127. * This is optional information - it won't be populated unless
  128. * the connection executing the query has `meta_mode() == metadata_mode::full`.
  129. *
  130. * \par Exception safety
  131. * No-throw guarantee.
  132. *
  133. * \par Object lifetimes
  134. * The returned reference is valid as long as `*this` is alive and hasn't been
  135. * assigned to or moved from.
  136. */
  137. string_view column_name() const noexcept { return name_; }
  138. /**
  139. * \brief Returns the original (physical) name of the column.
  140. * \details E.g. in `"SELECT id AS employee_id FROM employees"`,
  141. * `original_column_name()` will be `"id"`.
  142. * \n
  143. * This is optional information - it won't be populated unless
  144. * the connection executing the query has `meta_mode() == metadata_mode::full`.
  145. *
  146. * \par Exception safety
  147. * No-throw guarantee.
  148. *
  149. * \par Object lifetimes
  150. * The returned reference is valid as long as `*this` is alive and hasn't been
  151. * assigned to or moved from.
  152. */
  153. string_view original_column_name() const noexcept { return org_name_; }
  154. /**
  155. * \brief Returns the ID of the collation that fields belonging to this column use.
  156. * \details This is <b>not</b> the collation used when defining the column
  157. * in a `CREATE TABLE` statement, but the collation that fields that belong to
  158. * this column and are sent to the client have. It usually matches the connection's collation.
  159. *
  160. * \par Exception safety
  161. * No-throw guarantee.
  162. */
  163. std::uint16_t column_collation() const noexcept { return character_set_; }
  164. /**
  165. * \brief Returns the maximum length of the column.
  166. * \par Exception safety
  167. * No-throw guarantee.
  168. */
  169. unsigned column_length() const noexcept { return column_length_; }
  170. /**
  171. * \brief Returns the type of the column (see \ref column_type for more info).
  172. * \par Exception safety
  173. * No-throw guarantee.
  174. */
  175. column_type type() const noexcept { return type_; }
  176. /**
  177. * \brief Returns the number of decimals of the column.
  178. * \par Exception safety
  179. * No-throw guarantee.
  180. */
  181. unsigned decimals() const noexcept { return decimals_; }
  182. /**
  183. * \brief Returns `true` if the column is not allowed to be NULL, `false` if it is nullable.
  184. * \par Exception safety
  185. * No-throw guarantee.
  186. */
  187. bool is_not_null() const noexcept { return flag_set(detail::column_flags::not_null); }
  188. /**
  189. * \brief Returns `true` if the column is part of a `PRIMARY KEY`.
  190. * \par Exception safety
  191. * No-throw guarantee.
  192. */
  193. bool is_primary_key() const noexcept { return flag_set(detail::column_flags::pri_key); }
  194. /**
  195. * \brief Returns `true` if the column is part of a `UNIQUE KEY` (but not a `PRIMARY KEY`).
  196. * \par Exception safety
  197. * No-throw guarantee.
  198. */
  199. bool is_unique_key() const noexcept { return flag_set(detail::column_flags::unique_key); }
  200. /**
  201. * \brief Returns `true` if the column is part of a `KEY` (but not a `UNIQUE KEY` or `PRIMARY KEY`).
  202. * \par Exception safety
  203. * No-throw guarantee.
  204. */
  205. bool is_multiple_key() const noexcept { return flag_set(detail::column_flags::multiple_key); }
  206. /**
  207. * \brief Returns `true` if the column has no sign (is `UNSIGNED`).
  208. * \par Exception safety
  209. * No-throw guarantee.
  210. */
  211. bool is_unsigned() const noexcept { return flag_set(detail::column_flags::unsigned_); }
  212. /**
  213. * \brief Returns `true` if the column is defined as `ZEROFILL` (padded to its maximum length by
  214. * zeros).
  215. * \par Exception safety
  216. * No-throw guarantee.
  217. */
  218. bool is_zerofill() const noexcept { return flag_set(detail::column_flags::zerofill); }
  219. /**
  220. * \brief Returns `true` if the column is defined as `AUTO_INCREMENT`.
  221. * \par Exception safety
  222. * No-throw guarantee.
  223. */
  224. bool is_auto_increment() const noexcept { return flag_set(detail::column_flags::auto_increment); }
  225. /**
  226. * \brief Returns `true` if the column does not have a default value.
  227. * \par Exception safety
  228. * No-throw guarantee.
  229. */
  230. bool has_no_default_value() const noexcept { return flag_set(detail::column_flags::no_default_value); }
  231. /**
  232. * \brief Returns `true` if the column is defined as `ON UPDATE CURRENT_TIMESTAMP`.
  233. * \par Exception safety
  234. * No-throw guarantee.
  235. */
  236. bool is_set_to_now_on_update() const noexcept { return flag_set(detail::column_flags::on_update_now); }
  237. private:
  238. std::string schema_;
  239. std::string table_; // virtual table
  240. std::string org_table_; // physical table
  241. std::string name_; // virtual column name
  242. std::string org_name_; // physical column name
  243. std::uint16_t character_set_;
  244. std::uint32_t column_length_; // maximum length of the field
  245. column_type type_; // type of the column
  246. std::uint16_t flags_; // Flags as defined in Column Definition Flags
  247. std::uint8_t decimals_; // max shown decimal digits. 0x00 for int/static strings; 0x1f for
  248. // dynamic strings, double, float
  249. metadata(const detail::coldef_view& coldef, bool copy_strings)
  250. : schema_(copy_strings ? coldef.database : string_view()),
  251. table_(copy_strings ? coldef.table : string_view()),
  252. org_table_(copy_strings ? coldef.org_table : string_view()),
  253. name_(copy_strings ? coldef.name : string_view()),
  254. org_name_(copy_strings ? coldef.org_name : string_view()),
  255. character_set_(coldef.collation_id),
  256. column_length_(coldef.column_length),
  257. type_(coldef.type),
  258. flags_(coldef.flags),
  259. decimals_(coldef.decimals)
  260. {
  261. }
  262. bool flag_set(std::uint16_t flag) const noexcept { return flags_ & flag; }
  263. #ifndef BOOST_MYSQL_DOXYGEN
  264. friend struct detail::access;
  265. #endif
  266. };
  267. } // namespace mysql
  268. } // namespace boost
  269. #endif