field_view.ipp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_IMPL_FIELD_VIEW_IPP
  8. #define BOOST_MYSQL_IMPL_FIELD_VIEW_IPP
  9. #pragma once
  10. #include <boost/mysql/field_view.hpp>
  11. #include <boost/mysql/string_view.hpp>
  12. #include <boost/mysql/impl/internal/byte_to_hex.hpp>
  13. #include <boost/mysql/impl/internal/dt_to_string.hpp>
  14. #include <boost/assert.hpp>
  15. #include <cstddef>
  16. #include <ostream>
  17. namespace boost {
  18. namespace mysql {
  19. namespace detail {
  20. inline std::ostream& print_blob(std::ostream& os, blob_view value)
  21. {
  22. if (value.empty())
  23. return os << "{}";
  24. char buffer[16]{'0', 'x'};
  25. os << "{ ";
  26. for (std::size_t i = 0; i < value.size(); ++i)
  27. {
  28. // Separating comma
  29. if (i != 0)
  30. os << ", ";
  31. // Convert to hex
  32. byte_to_hex(value[i], buffer + 2);
  33. // Insert
  34. os << string_view(buffer, 4);
  35. }
  36. os << " }";
  37. return os;
  38. }
  39. inline std::ostream& print_time(std::ostream& os, const boost::mysql::time& value)
  40. {
  41. char buffer[64]{};
  42. std::size_t sz = detail::time_to_string(value, buffer);
  43. os << string_view(buffer, sz);
  44. return os;
  45. }
  46. } // namespace detail
  47. } // namespace mysql
  48. } // namespace boost
  49. std::ostream& boost::mysql::operator<<(std::ostream& os, const field_view& value)
  50. {
  51. // Make operator<< work for detail::string_view_offset types
  52. if (value.impl_.is_string_offset() || value.impl_.is_blob_offset())
  53. {
  54. return os << "<sv_offset>";
  55. }
  56. switch (value.kind())
  57. {
  58. case field_kind::null: return os << "<NULL>";
  59. case field_kind::int64: return os << value.get_int64();
  60. case field_kind::uint64: return os << value.get_uint64();
  61. case field_kind::string: return os << value.get_string();
  62. case field_kind::blob: return detail::print_blob(os, value.get_blob());
  63. case field_kind::float_: return os << value.get_float();
  64. case field_kind::double_: return os << value.get_double();
  65. case field_kind::date: return os << value.get_date();
  66. case field_kind::datetime: return os << value.get_datetime();
  67. case field_kind::time: return detail::print_time(os, value.get_time());
  68. default: BOOST_ASSERT(false); return os;
  69. }
  70. }
  71. #endif