connect_params_helpers.ipp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_CONNECT_PARAMS_HELPERS_IPP
  8. #define BOOST_MYSQL_IMPL_CONNECT_PARAMS_HELPERS_IPP
  9. #pragma once
  10. #include <boost/mysql/handshake_params.hpp>
  11. #include <boost/mysql/detail/connect_params_helpers.hpp>
  12. #include <memory>
  13. namespace boost {
  14. namespace mysql {
  15. namespace detail {
  16. inline string_view copy_string(const std::string& input, char*& it)
  17. {
  18. if (!input.empty())
  19. {
  20. std::memcpy(it, input.data(), input.size());
  21. string_view res(it, input.size());
  22. it += input.size();
  23. return res;
  24. }
  25. return string_view();
  26. }
  27. } // namespace detail
  28. } // namespace mysql
  29. } // namespace boost
  30. boost::mysql::detail::stable_connect_params boost::mysql::detail::make_stable(const connect_params& input)
  31. {
  32. const auto& addr_impl = access::get_impl(input.server_address);
  33. // Calculate required space
  34. std::size_t required_size = addr_impl.address.size() + input.username.size() + input.password.size() +
  35. input.database.size();
  36. // Allocate space for strings
  37. std::unique_ptr<char[]> ptr{required_size ? new char[required_size] : nullptr};
  38. // Copy them to the new buffer
  39. char* it = ptr.get();
  40. auto address = copy_string(addr_impl.address, it);
  41. auto username = copy_string(input.username, it);
  42. auto password = copy_string(input.password, it);
  43. auto database = copy_string(input.database, it);
  44. return {
  45. any_address_view{addr_impl.type, address, addr_impl.port},
  46. handshake_params(
  47. username,
  48. password,
  49. database,
  50. input.connection_collation,
  51. adjust_ssl_mode(input.ssl, input.server_address.type()),
  52. input.multi_queries
  53. ),
  54. std::move(ptr),
  55. };
  56. }
  57. #endif