database_connection.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Common database connection class.
  41. ///
  42. /// This class provides functions that are common for establishing
  43. /// connection with different types of databases; enables operations
  44. /// on access parameters strings. In particular, it provides a way
  45. /// to parse parameters in key=value format. This class is expected
  46. /// to be a base class for all @ref LeaseMgr and possibly
  47. /// @ref BaseHostDataSource derived classes.
  48. class DatabaseConnection : public boost::noncopyable {
  49. public:
  50. /// @brief Defines maximum value for time that can be reliably stored.
  51. ///
  52. /// @todo: Is this common for MySQL and Postgres? Maybe we should have
  53. /// specific values for each backend?
  54. ///
  55. /// If I'm still alive I'll be too old to care. You fix it.
  56. static const time_t MAX_DB_TIME;
  57. /// @brief Database configuration parameter map
  58. typedef std::map<std::string, std::string> ParameterMap;
  59. /// @brief Constructor
  60. ///
  61. /// @param parameters A data structure relating keywords and values
  62. /// concerned with the database.
  63. DatabaseConnection(const ParameterMap& parameters)
  64. :parameters_(parameters) {
  65. }
  66. /// @brief Returns value of a connection parameter.
  67. ///
  68. /// @param name Name of the parameter which value should be returned.
  69. /// @return Value of one of the connection parameters.
  70. /// @throw BadValue if parameter is not found
  71. std::string getParameter(const std::string& name) const;
  72. /// @brief Parse database access string
  73. ///
  74. /// Parses the string of "keyword=value" pairs and separates them
  75. /// out into the map.
  76. ///
  77. /// @param dbaccess Database access string.
  78. ///
  79. /// @return @ref ParameterMap of keyword/value pairs.
  80. static ParameterMap parse(const std::string& dbaccess);
  81. /// @brief Redact database access string
  82. ///
  83. /// Takes the database parameters and returns a database access string
  84. /// passwords replaced by asterisks. This string is used in log messages.
  85. ///
  86. /// @param parameters Database access parameters (output of "parse").
  87. ///
  88. /// @return Redacted database access string.
  89. static std::string redactedAccessString(const ParameterMap& parameters);
  90. private:
  91. /// @brief List of parameters passed in dbconfig
  92. ///
  93. /// That will be mostly used for storing database name, username,
  94. /// password and other parameters required for DB access. It is not
  95. /// intended to keep any DHCP-related parameters.
  96. ParameterMap parameters_;
  97. };
  98. }; // end of isc::dhcp namespace
  99. }; // end of isc namespace
  100. #endif // DATABASE_CONNECTION_H