From bfb36f3d0ca1ac049eba25ae4e573df7a4c7fbd1 Mon Sep 17 00:00:00 2001 From: Marcin Siodelski Date: Thu, 28 Jul 2016 21:08:43 +0200 Subject: [PATCH] [4489] Removed RestrictedConstPtr class. --- src/lib/dhcpsrv/mysql_host_data_source.cc | 32 ++++++++++++++--- src/lib/dhcpsrv/mysql_host_data_source.h | 3 +- src/lib/dhcpsrv/pgsql_host_data_source.cc | 32 ++++++++++++++--- src/lib/dhcpsrv/pgsql_host_data_source.h | 4 +-- src/lib/util/pointer_util.h | 42 +---------------------- 5 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/lib/dhcpsrv/mysql_host_data_source.cc b/src/lib/dhcpsrv/mysql_host_data_source.cc index c4b97757e0..0b98fdbfb1 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.cc +++ b/src/lib/dhcpsrv/mysql_host_data_source.cc @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -1760,6 +1761,15 @@ public: StatementIndex stindex, boost::shared_ptr 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 host_exchange_; @@ -2202,21 +2212,29 @@ getHost(const SubnetID& subnet_id, 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 @@ -2539,12 +2557,16 @@ std::pair MySqlHostDataSource::getVersion() const { 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(); } diff --git a/src/lib/dhcpsrv/mysql_host_data_source.h b/src/lib/dhcpsrv/mysql_host_data_source.h index 6ee563d83b..d5e56949a0 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.h +++ b/src/lib/dhcpsrv/mysql_host_data_source.h @@ -10,7 +10,6 @@ #include #include #include -#include namespace isc { namespace dhcp { @@ -257,7 +256,7 @@ public: private: /// @brief Pointer to the implementation of the @ref MySqlHostDataSource. - util::RestrictedConstPtr impl_; + MySqlHostDataSourceImpl* impl_; }; } diff --git a/src/lib/dhcpsrv/pgsql_host_data_source.cc b/src/lib/dhcpsrv/pgsql_host_data_source.cc index 2d4e57ba03..5fac1830a6 100644 --- a/src/lib/dhcpsrv/pgsql_host_data_source.cc +++ b/src/lib/dhcpsrv/pgsql_host_data_source.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -1230,6 +1231,14 @@ public: StatementIndex stindex, boost::shared_ptr 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 /// @@ -1661,23 +1670,32 @@ std::pair PgSqlHostDataSourceImpl::getVersion() const { return (std::make_pair(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 @@ -1928,12 +1946,16 @@ std::pair PgSqlHostDataSource::getVersion() const { 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(); } diff --git a/src/lib/dhcpsrv/pgsql_host_data_source.h b/src/lib/dhcpsrv/pgsql_host_data_source.h index ab062efc3c..40ad840c86 100644 --- a/src/lib/dhcpsrv/pgsql_host_data_source.h +++ b/src/lib/dhcpsrv/pgsql_host_data_source.h @@ -8,10 +8,8 @@ #define PGSQL_HOST_DATA_SOURCE_H #include -#include #include #include -#include namespace isc { namespace dhcp { @@ -288,7 +286,7 @@ public: private: /// @brief Pointer to the implementation of the @ref PgSqlHostDataSource. - util::RestrictedConstPtr impl_; + PgSqlHostDataSourceImpl* impl_; }; } diff --git a/src/lib/util/pointer_util.h b/src/lib/util/pointer_util.h index ed8374722f..a775584573 100644 --- a/src/lib/util/pointer_util.h +++ b/src/lib/util/pointer_util.h @@ -1,4 +1,4 @@ -// 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 @@ -7,49 +7,9 @@ #ifndef POINTER_UTIL_H #define POINTER_UTIL_H -#include -#include - namespace isc { namespace util { -template -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. /// -- 2.47.2