#include <dhcp/option_definition.h>
#include <dhcp/option_space.h>
#include <dhcpsrv/cfg_option.h>
+#include <dhcpsrv/db_exceptions.h>
#include <dhcpsrv/dhcpsrv_log.h>
#include <dhcpsrv/mysql_host_data_source.h>
#include <dhcpsrv/db_exceptions.h>
StatementIndex stindex,
boost::shared_ptr<MySqlHostExchange> exchange) const;
+ /// @brief Throws exception if database is read only.
+ ///
+ /// This method should be called by the methods which write to the
+ /// database. If the backend is operating in read-only mode this
+ /// method will throw exception.
+ ///
+ /// @throw DbReadOnly if backend is operating in read only mode.
+ void checkReadOnly() const;
+
/// @brief Pointer to the object representing an exchange which
/// can be used to retrieve hosts and DHCPv4 options.
boost::shared_ptr<MySqlHostWithOptionsExchange> host_exchange_;
return (result);
}
+void
+MySqlHostDataSourceImpl::checkReadOnly() const {
+ if (is_readonly_) {
+ isc_throw(ReadOnlyDb, "MySQL host database backend is configured to"
+ " operate in read only mode");
+ }
+}
+
MySqlHostDataSource::
MySqlHostDataSource(const MySqlConnection::ParameterMap& parameters)
- : impl_(new MySqlHostDataSourceImpl(parameters),
- "MySQL host database backend is configured to"
- " operate in read only mode") {
- impl_.allowConstOnly(impl_->is_readonly_);
+ : impl_(new MySqlHostDataSourceImpl(parameters)) {
}
MySqlHostDataSource::~MySqlHostDataSource() {
- delete impl_.getPtr();
+ delete impl_;
}
void
MySqlHostDataSource::add(const HostPtr& host) {
+ // If operating in read-only mode, throw exception.
+ impl_->checkReadOnly();
+
// Initiate MySQL transaction as we will have to make multiple queries
// to insert host information into multiple tables. If that fails on
// any stage, the transaction will be rolled back by the destructor of
void
MySqlHostDataSource::commit() {
+ // If operating in read-only mode, throw exception.
+ impl_->checkReadOnly();
impl_->conn_.commit();
}
void
MySqlHostDataSource::rollback() {
+ // If operating in read-only mode, throw exception.
+ impl_->checkReadOnly();
impl_->conn_.rollback();
}
#include <dhcpsrv/base_host_data_source.h>
#include <dhcpsrv/db_exceptions.h>
#include <dhcpsrv/mysql_connection.h>
-#include <util/pointer_util.h>
namespace isc {
namespace dhcp {
private:
/// @brief Pointer to the implementation of the @ref MySqlHostDataSource.
- util::RestrictedConstPtr<MySqlHostDataSourceImpl, ReadOnlyDb> impl_;
+ MySqlHostDataSourceImpl* impl_;
};
}
#include <dhcp/option.h>
#include <dhcp/option_definition.h>
#include <dhcp/option_space.h>
+#include <dhcpsrv/db_exceptions.h>
#include <dhcpsrv/cfg_option.h>
#include <dhcpsrv/dhcpsrv_log.h>
#include <dhcpsrv/pgsql_host_data_source.h>
StatementIndex stindex,
boost::shared_ptr<PgSqlHostExchange> exchange) const;
+ /// @brief Throws exception if database is read only.
+ ///
+ /// This method should be called by the methods which write to the
+ /// database. If the backend is operating in read-only mode this
+ /// method will throw exception.
+ ///
+ /// @throw DbReadOnly if backend is operating in read only mode.
+ void checkReadOnly() const;
/// @brief Returns PostgreSQL schema version of the open database
///
return (std::make_pair<uint32_t, uint32_t>(version, minor));
}
+void
+PgSqlHostDataSourceImpl::checkReadOnly() const {
+ if (is_readonly_) {
+ isc_throw(ReadOnlyDb, "PostgreSQL host database backend is configured"
+ " to operate in read only mode");
+ }
+}
+
+
/*********** PgSqlHostDataSource *********************/
PgSqlHostDataSource::
PgSqlHostDataSource(const PgSqlConnection::ParameterMap& parameters)
- : impl_(new PgSqlHostDataSourceImpl(parameters),
- "PostgreSQL host database backend is configured to"
- " operate in read only mode") {
- impl_.allowConstOnly(impl_->is_readonly_);
+ : impl_(new PgSqlHostDataSourceImpl(parameters)) {
}
PgSqlHostDataSource::~PgSqlHostDataSource() {
- delete impl_.getPtr();
+ delete impl_;
}
void
PgSqlHostDataSource::add(const HostPtr& host) {
+ // If operating in read-only mode, throw exception.
+ impl_->checkReadOnly();
+
// Initiate PostgreSQL transaction as we will have to make multiple queries
// to insert host information into multiple tables. If that fails on
// any stage, the transaction will be rolled back by the destructor of
void
PgSqlHostDataSource::commit() {
+ // If operating in read-only mode, throw exception.
+ impl_->checkReadOnly();
impl_->conn_.commit();
}
void
PgSqlHostDataSource::rollback() {
+ // If operating in read-only mode, throw exception.
+ impl_->checkReadOnly();
impl_->conn_.rollback();
}
#define PGSQL_HOST_DATA_SOURCE_H
#include <dhcpsrv/base_host_data_source.h>
-#include <dhcpsrv/db_exceptions.h>
#include <dhcpsrv/pgsql_connection.h>
#include <dhcpsrv/pgsql_exchange.h>
-#include <util/pointer_util.h>
namespace isc {
namespace dhcp {
private:
/// @brief Pointer to the implementation of the @ref PgSqlHostDataSource.
- util::RestrictedConstPtr<PgSqlHostDataSourceImpl, ReadOnlyDb> impl_;
+ PgSqlHostDataSourceImpl* impl_;
};
}
-// Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
#ifndef POINTER_UTIL_H
#define POINTER_UTIL_H
-#include <exceptions/exceptions.h>
-#include <string>
-
namespace isc {
namespace util {
-template <typename T, typename E>
-class RestrictedConstPtr {
-public:
-
- RestrictedConstPtr(T* ptr, const std::string& error_text)
- : ptr_(ptr), const_only_(false), error_text_(error_text) {
- }
-
- void allowConstOnly(const bool const_only) {
- const_only_ = const_only;
- }
-
- T* operator->() const {
- return (ptr_);
- }
-
- T* operator->() {
- if (const_only_) {
- isc_throw(E, error_text_);
- }
- return (ptr_);
- }
-
- T* getPtr() const {
- return (ptr_);
- }
-
-private:
-
- T* ptr_;
-
- bool const_only_;
-
- std::string error_text_;
-};
-
-
/// @brief This function checks if two pointers are non-null and values
/// are equal.
///