database_connection.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef DATABASE_CONNECTION_H
  7. #define DATABASE_CONNECTION_H
  8. #include <boost/noncopyable.hpp>
  9. #include <exceptions/exceptions.h>
  10. #include <map>
  11. #include <string>
  12. namespace isc {
  13. namespace dhcp {
  14. /// @brief Exception thrown if name of database is not specified
  15. class NoDatabaseName : public Exception {
  16. public:
  17. NoDatabaseName(const char* file, size_t line, const char* what) :
  18. isc::Exception(file, line, what) {}
  19. };
  20. /// @brief Exception thrown on failure to open database
  21. class DbOpenError : public Exception {
  22. public:
  23. DbOpenError(const char* file, size_t line, const char* what) :
  24. isc::Exception(file, line, what) {}
  25. };
  26. /// @brief Exception thrown on failure to execute a database function
  27. class DbOperationError : public Exception {
  28. public:
  29. DbOperationError(const char* file, size_t line, const char* what) :
  30. isc::Exception(file, line, what) {}
  31. };
  32. /// @brief Invalid type exception
  33. ///
  34. /// Thrown when the factory doesn't recognize the type of the backend.
  35. class InvalidType : public Exception {
  36. public:
  37. InvalidType(const char* file, size_t line, const char* what) :
  38. isc::Exception(file, line, what) {}
  39. };
  40. /// @brief Invalid Timeout
  41. ///
  42. /// Thrown when the timeout specified for the database connection is invalid.
  43. class DbInvalidTimeout : public Exception {
  44. public:
  45. DbInvalidTimeout(const char* file, size_t line, const char* what) :
  46. isc::Exception(file, line, what) {}
  47. };
  48. /// @brief Common database connection class.
  49. ///
  50. /// This class provides functions that are common for establishing
  51. /// connection with different types of databases; enables operations
  52. /// on access parameters strings. In particular, it provides a way
  53. /// to parse parameters in key=value format. This class is expected
  54. /// to be a base class for all @ref LeaseMgr and possibly
  55. /// @ref BaseHostDataSource derived classes.
  56. class DatabaseConnection : public boost::noncopyable {
  57. public:
  58. /// @brief Defines maximum value for time that can be reliably stored.
  59. ///
  60. /// @todo: Is this common for MySQL and Postgres? Maybe we should have
  61. /// specific values for each backend?
  62. ///
  63. /// If I'm still alive I'll be too old to care. You fix it.
  64. static const time_t MAX_DB_TIME;
  65. /// @brief Database configuration parameter map
  66. typedef std::map<std::string, std::string> ParameterMap;
  67. /// @brief Constructor
  68. ///
  69. /// @param parameters A data structure relating keywords and values
  70. /// concerned with the database.
  71. DatabaseConnection(const ParameterMap& parameters)
  72. :parameters_(parameters) {
  73. }
  74. /// @brief Returns value of a connection parameter.
  75. ///
  76. /// @param name Name of the parameter which value should be returned.
  77. /// @return Value of one of the connection parameters.
  78. /// @throw BadValue if parameter is not found
  79. std::string getParameter(const std::string& name) const;
  80. /// @brief Parse database access string
  81. ///
  82. /// Parses the string of "keyword=value" pairs and separates them
  83. /// out into the map.
  84. ///
  85. /// @param dbaccess Database access string.
  86. ///
  87. /// @return @ref ParameterMap of keyword/value pairs.
  88. static ParameterMap parse(const std::string& dbaccess);
  89. /// @brief Redact database access string
  90. ///
  91. /// Takes the database parameters and returns a database access string
  92. /// passwords replaced by asterisks. This string is used in log messages.
  93. ///
  94. /// @param parameters Database access parameters (output of "parse").
  95. ///
  96. /// @return Redacted database access string.
  97. static std::string redactedAccessString(const ParameterMap& parameters);
  98. private:
  99. /// @brief List of parameters passed in dbconfig
  100. ///
  101. /// That will be mostly used for storing database name, username,
  102. /// password and other parameters required for DB access. It is not
  103. /// intended to keep any DHCP-related parameters.
  104. ParameterMap parameters_;
  105. };
  106. }; // end of isc::dhcp namespace
  107. }; // end of isc namespace
  108. #endif // DATABASE_CONNECTION_H