22#include <boost/algorithm/string.hpp>
23#include <boost/foreach.hpp>
24#include <boost/scoped_ptr.hpp>
39map<string, HostDataSourceFactory::Factory> HostDataSourceFactory::map_;
43 const string& dbaccess) {
49 DatabaseConnection::ParameterMap::iterator it = parameters.find(
"type");
50 if (it == parameters.end()) {
52 "contain the 'type' keyword");
55 string db_type = it->second;
56 auto index = map_.find(db_type);
59 if (index == map_.end()) {
60 if ((db_type ==
"mysql") ||
61 (db_type ==
"postgresql")) {
62 string with = (db_type ==
"postgresql" ?
"pgsql" : db_type);
64 <<
"' is not compiled in. Did you forget to use --with-"
65 << with <<
" during compilation?");
68 db_type <<
"' is not supported");
72 sources.push_back(index->second(parameters));
75 if (!sources.back()) {
78 " factory returned NULL");
84 const string& db_type) {
85 for (
auto it = sources.begin(); it != sources.end(); ++it) {
86 if ((*it)->getType() != db_type) {
99 const string& db_type,
100 const string& dbaccess,
104 bool deleted =
false;
109 for (
auto it = sources.begin(); it != sources.end(); ++it) {
110 if ((*it)->getType() != db_type || (*it)->getParameters() != parameters) {
113 if (if_unusable && (!(*it)->isUnusable())) {
118 .arg((*it)->getType());
129 if (map_.count(db_type)) {
132 map_.insert(pair<string, Factory>(db_type, factory));
147 auto index = map_.find(db_type);
148 if (index != map_.end()) {
163 auto index = map_.find(db_type);
164 return (index != map_.end());
169 std::stringstream txt;
171 for (
auto x : map_) {
172 txt << x.first <<
" ";
190struct MySqlHostDataSourceInit {
192 MySqlHostDataSourceInit() {
193 HostDataSourceFactory::registerFactory(
"mysql", factory,
true);
197 ~MySqlHostDataSourceInit() {
198 HostDataSourceFactory::deregisterFactory(
"mysql",
true);
205 .arg(DatabaseConnection::redactedAccessString(parameters));
211MySqlHostDataSourceInit mysql_init_;
215struct PgSqlHostDataSourceInit {
217 PgSqlHostDataSourceInit() {
218 HostDataSourceFactory::registerFactory(
"postgresql", factory,
true);
222 ~PgSqlHostDataSourceInit() {
223 HostDataSourceFactory::deregisterFactory(
"postgresql",
true);
230 .arg(DatabaseConnection::redactedAccessString(parameters));
236PgSqlHostDataSourceInit pgsql_init_;
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
A generic exception that is thrown when an unexpected error condition occurs.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
static bool deregisterFactory(const std::string &db_type, bool no_log=false)
Deregister a host data source factory.
static void add(HostDataSourceList &sources, const std::string &dbaccess)
Create and add an instance of a host data source.
static void printRegistered()
Prints out all registered backends.
std::function< HostDataSourcePtr(const db::DatabaseConnection::ParameterMap &)> Factory
Type of host data source factory.
static bool registerFactory(const std::string &db_type, const Factory &factory, bool no_log=false)
Register a host data source factory.
static bool del(HostDataSourceList &sources, const std::string &db_type)
Delete a host data source.
static bool registeredFactory(const std::string &db_type)
Check if a host data source factory was registered.
PostgreSQL Host Data Source.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Logging initialization functions.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
boost::shared_ptr< BaseHostDataSource > HostDataSourcePtr
HostDataSource pointer.
isc::log::Logger hosts_logger("hosts")
Logger for the HostMgr and the code it calls.
const isc::log::MessageID HOSTS_BACKEND_DEREGISTER
std::vector< HostDataSourcePtr > HostDataSourceList
HostDataSource list.
const isc::log::MessageID HOSTS_CFG_CLOSE_HOST_DATA_SOURCE
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB
const isc::log::MessageID HOSTS_BACKEND_REGISTER
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
const isc::log::MessageID HOSTS_BACKENDS_REGISTERED
Defines the logger used by the top-level component of kea-lfc.