Commit bfb36f3d authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[4489] Removed RestrictedConstPtr class.

parent fe0424b0
......@@ -11,6 +11,7 @@
#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>
......@@ -1760,6 +1761,15 @@ public:
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_;
......@@ -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<uint32_t, uint32_t> 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();
}
......
......@@ -10,7 +10,6 @@
#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 {
......@@ -257,7 +256,7 @@ public:
private:
/// @brief Pointer to the implementation of the @ref MySqlHostDataSource.
util::RestrictedConstPtr<MySqlHostDataSourceImpl, ReadOnlyDb> impl_;
MySqlHostDataSourceImpl* impl_;
};
}
......
......@@ -10,6 +10,7 @@
#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>
......@@ -1230,6 +1231,14 @@ public:
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
///
......@@ -1661,23 +1670,32 @@ std::pair<uint32_t, uint32_t> PgSqlHostDataSourceImpl::getVersion() const {
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
......@@ -1928,12 +1946,16 @@ std::pair<uint32_t, uint32_t> 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();
}
......
......@@ -8,10 +8,8 @@
#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 {
......@@ -288,7 +286,7 @@ public:
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
......@@ -7,49 +7,9 @@
#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.
///
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment