connection-parameters.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Copyright (C) 2013 Vadim Zeitlin
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef SOCI_CONNECTION_PARAMETERS_H_INCLUDED
  8. #define SOCI_CONNECTION_PARAMETERS_H_INCLUDED
  9. #include "soci/soci-platform.h"
  10. #include <map>
  11. #include <string>
  12. namespace soci
  13. {
  14. // Names of some predefined options and their values.
  15. extern SOCI_DECL char const * option_reconnect;
  16. extern SOCI_DECL char const * option_true;
  17. namespace details
  18. {
  19. class dynamic_backend_ref;
  20. } // namespace details
  21. class backend_factory;
  22. // Simple container for the information used when opening a session.
  23. class SOCI_DECL connection_parameters
  24. {
  25. public:
  26. connection_parameters();
  27. connection_parameters(backend_factory const & factory, std::string const & connectString);
  28. connection_parameters(std::string const & backendName, std::string const & connectString);
  29. explicit connection_parameters(std::string const & fullConnectString);
  30. connection_parameters(connection_parameters const& other);
  31. connection_parameters(connection_parameters && other);
  32. connection_parameters& operator=(connection_parameters const& other);
  33. connection_parameters& operator=(connection_parameters && other);
  34. ~connection_parameters();
  35. // Retrieve the backend and the connection strings specified in the ctor.
  36. backend_factory const * get_factory() const { return factory_; }
  37. void set_connect_string(const std::string & connectString) { connectString_ = connectString; }
  38. std::string const & get_connect_string() const { return connectString_; }
  39. // Set the value of the given option, overwriting any previous value.
  40. void set_option(const char * name, std::string const & value)
  41. {
  42. options_[name] = value;
  43. }
  44. // Return true if the option with the given name was found and fill the
  45. // provided parameter with its value.
  46. bool get_option(const char * name, std::string & value) const
  47. {
  48. Options::const_iterator const it = options_.find(name);
  49. if (it == options_.end())
  50. return false;
  51. value = it->second;
  52. return true;
  53. }
  54. // Return true if the option with the given name was found with option_true
  55. // value.
  56. bool is_option_on(const char * name) const
  57. {
  58. std::string value;
  59. return get_option(name, value) && value == option_true;
  60. }
  61. private:
  62. void reset_after_move();
  63. // The backend and connection string specified in our ctor.
  64. backend_factory const * factory_;
  65. std::string connectString_;
  66. // References the backend name used for obtaining the factor from
  67. // dynamic_backends.
  68. details::dynamic_backend_ref * backendRef_;
  69. // We store all the values as strings for simplicity.
  70. typedef std::map<std::string, std::string> Options;
  71. Options options_;
  72. };
  73. } // namespace soci
  74. #endif // SOCI_CONNECTION_PARAMETERS_H_INCLUDED