metadata.hpp 12 KB

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