metadata.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // Copyright (c) 2019-2023 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_IMPL_METADATA_HPP
  8. #define BOOST_MYSQL_IMPL_METADATA_HPP
  9. #pragma once
  10. #include <boost/mysql/metadata.hpp>
  11. #include <boost/mysql/detail/auxiliar/access_fwd.hpp>
  12. #include <boost/mysql/detail/protocol/constants.hpp>
  13. namespace boost {
  14. namespace mysql {
  15. namespace detail {
  16. inline column_type compute_field_type_string(std::uint32_t flags, std::uint16_t collation)
  17. {
  18. if (flags & column_flags::set)
  19. return column_type::set;
  20. else if (flags & column_flags::enum_)
  21. return column_type::enum_;
  22. else if (collation == binary_collation)
  23. return column_type::binary;
  24. else
  25. return column_type::char_;
  26. }
  27. inline column_type compute_field_type_var_string(std::uint16_t collation)
  28. {
  29. return collation == binary_collation ? column_type::varbinary : column_type::varchar;
  30. }
  31. inline column_type compute_field_type_blob(std::uint16_t collation)
  32. {
  33. return collation == binary_collation ? column_type::blob : column_type::text;
  34. }
  35. inline column_type compute_field_type(
  36. protocol_field_type protocol_type,
  37. std::uint32_t flags,
  38. std::uint16_t collation
  39. )
  40. {
  41. switch (protocol_type)
  42. {
  43. case protocol_field_type::decimal:
  44. case protocol_field_type::newdecimal: return column_type::decimal;
  45. case protocol_field_type::geometry: return column_type::geometry;
  46. case protocol_field_type::tiny: return column_type::tinyint;
  47. case protocol_field_type::short_: return column_type::smallint;
  48. case protocol_field_type::int24: return column_type::mediumint;
  49. case protocol_field_type::long_: return column_type::int_;
  50. case protocol_field_type::longlong: return column_type::bigint;
  51. case protocol_field_type::float_: return column_type::float_;
  52. case protocol_field_type::double_: return column_type::double_;
  53. case protocol_field_type::bit: return column_type::bit;
  54. case protocol_field_type::date: return column_type::date;
  55. case protocol_field_type::datetime: return column_type::datetime;
  56. case protocol_field_type::timestamp: return column_type::timestamp;
  57. case protocol_field_type::time: return column_type::time;
  58. case protocol_field_type::year: return column_type::year;
  59. case protocol_field_type::json: return column_type::json;
  60. case protocol_field_type::string: return compute_field_type_string(flags, collation);
  61. case protocol_field_type::var_string: return compute_field_type_var_string(collation);
  62. case protocol_field_type::blob: return compute_field_type_blob(collation);
  63. default: return column_type::unknown;
  64. }
  65. }
  66. } // namespace detail
  67. } // namespace mysql
  68. } // namespace boost
  69. boost::mysql::metadata::metadata(const detail::column_definition_packet& msg, bool copy_strings)
  70. : schema_(copy_strings ? msg.schema.value : string_view()),
  71. table_(copy_strings ? msg.table.value : string_view()),
  72. org_table_(copy_strings ? msg.org_table.value : string_view()),
  73. name_(copy_strings ? msg.name.value : string_view()),
  74. org_name_(copy_strings ? msg.org_name.value : string_view()),
  75. character_set_(msg.character_set),
  76. column_length_(msg.column_length),
  77. type_(detail::compute_field_type(msg.type, msg.flags, msg.character_set)),
  78. flags_(msg.flags),
  79. decimals_(msg.decimals)
  80. {
  81. }
  82. struct boost::mysql::detail::metadata_access
  83. {
  84. static metadata construct(const detail::column_definition_packet& msg, bool copy_strings)
  85. {
  86. return metadata(msg, copy_strings);
  87. }
  88. };
  89. #endif