0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Move method definitions inside corresponding classes (#521)

* Move the method definitions of sqlpp::normal_connection inside the class.
* Move the method definitions of sqlpp::pooled_connection inside the class.
* clang-format include/sqlpp11/connection.h
* clang-format include/sqlpp11/connection_pool.h
This commit is contained in:
MeanSquaredError 2023-09-01 07:19:04 +03:00 committed by GitHub
parent e7b50dadd4
commit 1cd47c77dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 113 additions and 150 deletions

View File

@ -37,7 +37,7 @@ namespace sqlpp
};
// Normal (non-pooled) connection
template<typename ConnectionBase>
template <typename ConnectionBase>
class normal_connection : public ConnectionBase
{
public:
@ -46,8 +46,15 @@ namespace sqlpp
// Constructors
normal_connection() = default;
normal_connection(const _config_t& config);
normal_connection(const _config_ptr_t& config);
normal_connection(const _config_t& config) : normal_connection{std::make_shared<_config_t>(config)}
{
}
normal_connection(const _config_ptr_t& config) : ConnectionBase{std::make_unique<_handle_t>(config)}
{
}
normal_connection(const normal_connection&) = delete;
normal_connection(normal_connection&&) = default;
@ -56,36 +63,21 @@ namespace sqlpp
normal_connection& operator=(normal_connection&&) = default;
// creates a connection handle and connects to database
void connectUsing(const _config_ptr_t& config) noexcept(false);
void connectUsing(const _config_ptr_t& config) noexcept(false)
{
ConnectionBase::_handle = std::make_unique<_handle_t>(config);
}
private:
using _handle_t = typename ConnectionBase::_handle_t;
};
template<typename ConnectionBase>
normal_connection<ConnectionBase>::normal_connection(const _config_t& config) :
normal_connection{std::make_shared<_config_t>(config)}
{
}
template<typename ConnectionBase>
normal_connection<ConnectionBase>::normal_connection(const _config_ptr_t& config) :
ConnectionBase{std::make_unique<_handle_t>(config)}
{
}
template<typename ConnectionBase>
void normal_connection<ConnectionBase>::connectUsing(const _config_ptr_t& config) noexcept(false)
{
ConnectionBase::_handle = std::make_unique<_handle_t>(config);
}
// Forward declaration
template<typename ConnectionBase>
template <typename ConnectionBase>
class connection_pool;
// Pooled connection
template<typename ConnectionBase>
template <typename ConnectionBase>
class pooled_connection : public ConnectionBase
{
friend class connection_pool<ConnectionBase>::pool_core;
@ -99,32 +91,19 @@ namespace sqlpp
// Copy/Move constructors
pooled_connection(const pooled_connection&) = delete;
pooled_connection(pooled_connection&& other) = default;
~pooled_connection();
// Assigment operators
pooled_connection& operator=(const pooled_connection&) = delete;
pooled_connection& operator=(pooled_connection&& other);
private:
_pool_core_ptr_t _pool_core;
// Constructors used by the connection pool
pooled_connection(_handle_ptr_t&& handle, _pool_core_ptr_t pool_core);
pooled_connection(const _config_ptr_t& config, _pool_core_ptr_t pool_core);
void conn_release();
};
template<typename ConnectionBase>
pooled_connection<ConnectionBase>::~pooled_connection()
~pooled_connection()
{
conn_release();
}
template<typename ConnectionBase>
pooled_connection<ConnectionBase>& pooled_connection<ConnectionBase>::operator=(pooled_connection&& other)
// Assigment operators
pooled_connection& operator=(const pooled_connection&) = delete;
pooled_connection& operator=(pooled_connection&& other)
{
if (this != &other)
{
if (this != &other) {
conn_release();
static_cast<ConnectionBase&>(*this) = std::move(static_cast<ConnectionBase&>(other));
_pool_core = std::move(other._pool_core);
@ -132,26 +111,27 @@ namespace sqlpp
return *this;
}
template<typename ConnectionBase>
pooled_connection<ConnectionBase>::pooled_connection(_handle_ptr_t&& handle, _pool_core_ptr_t pool_core) :
ConnectionBase{std::move(handle)},
_pool_core{pool_core}
private:
_pool_core_ptr_t _pool_core;
// Constructors used by the connection pool
pooled_connection(_handle_ptr_t&& handle, _pool_core_ptr_t pool_core)
: ConnectionBase{std::move(handle)}, _pool_core{pool_core}
{
}
template<typename ConnectionBase>
pooled_connection<ConnectionBase>::pooled_connection(const _config_ptr_t& config, _pool_core_ptr_t pool_core) :
ConnectionBase{std::make_unique<_handle_t>(config)},
_pool_core{pool_core}
pooled_connection(const _config_ptr_t& config, _pool_core_ptr_t pool_core)
: ConnectionBase{std::make_unique<_handle_t>(config)}, _pool_core{pool_core}
{
}
template<typename ConnectionBase>
void pooled_connection<ConnectionBase>::conn_release()
void conn_release()
{
if (_pool_core)
{
if (_pool_core) {
_pool_core->put(ConnectionBase::_handle);
_pool_core = nullptr;
}
}
};
} // namespace sqlpp

View File

@ -33,7 +33,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace sqlpp
{
template<typename ConnectionBase>
template <typename ConnectionBase>
class connection_pool
{
public:
@ -44,18 +44,50 @@ namespace sqlpp
class pool_core : public std::enable_shared_from_this<pool_core>
{
public:
pool_core(const _config_ptr_t& connection_config, std::size_t capacity);
pool_core(const _config_ptr_t& connection_config, std::size_t capacity)
: _connection_config{connection_config}, _handles{capacity}
{
}
pool_core() = delete;
pool_core(const pool_core &) = delete;
pool_core(pool_core &&) = delete;
pool_core(const pool_core&) = delete;
pool_core(pool_core&&) = delete;
pool_core& operator=(const pool_core&) = delete;
pool_core& operator=(pool_core&&) = delete;
_pooled_connection_t get();
void put(_handle_ptr_t& handle);
_pooled_connection_t get()
{
std::unique_lock<std::mutex> lock{_mutex};
if (_handles.empty())
{
lock.unlock();
return _pooled_connection_t{_connection_config, this->shared_from_this()};
}
auto handle = std::move(_handles.front());
_handles.pop_front();
lock.unlock();
// If the fetched connection is dead, drop it and create a new one on the fly
return handle->check_connection() ? _pooled_connection_t{std::move(handle), this->shared_from_this()}
: _pooled_connection_t{_connection_config, this->shared_from_this()};
}
void put(_handle_ptr_t& handle)
{
std::unique_lock<std::mutex> lock{_mutex};
if (_handles.full())
{
_handles.set_capacity(_handles.capacity() + 5);
}
_handles.push_back(std::move(handle));
}
// Returns number of connections available in the pool. Only used in tests.
std::size_t available();
std::size_t available()
{
std::unique_lock<std::mutex> lock{_mutex};
return _handles.size();
}
private:
_config_ptr_t _connection_config;
@ -64,88 +96,39 @@ namespace sqlpp
};
connection_pool() = default;
connection_pool(const _config_ptr_t& connection_config, std::size_t capacity);
connection_pool(const _config_ptr_t& connection_config, std::size_t capacity)
: _core{std::make_shared<pool_core>(connection_config, capacity)}
{
}
connection_pool(const connection_pool&) = delete;
connection_pool(connection_pool&&) = default;
connection_pool& operator=(const connection_pool&) = delete;
connection_pool& operator=(connection_pool&&) = default;
void initialize(const _config_ptr_t& connection_config, std::size_t capacity);
_pooled_connection_t get();
// Returns number of connections available in the pool. Only used in tests.
std::size_t available();
private:
std::shared_ptr<pool_core> _core;
};
template<typename ConnectionBase>
connection_pool<ConnectionBase>::pool_core::pool_core(const _config_ptr_t& connection_config, std::size_t capacity) :
_connection_config{connection_config},
_handles{capacity}
void initialize(const _config_ptr_t& connection_config, std::size_t capacity)
{
}
template<typename ConnectionBase>
typename connection_pool<ConnectionBase>::_pooled_connection_t connection_pool<ConnectionBase>::pool_core::get()
if (_core)
{
std::unique_lock<std::mutex> lock{_mutex};
if (_handles.empty()) {
lock.unlock();
return _pooled_connection_t{_connection_config, this->shared_from_this()};
}
auto handle = std::move(_handles.front());
_handles.pop_front();
lock.unlock();
// If the fetched connection is dead, drop it and create a new one on the fly
return
handle->check_connection() ?
_pooled_connection_t{std::move(handle), this->shared_from_this()} :
_pooled_connection_t{_connection_config, this->shared_from_this()};
}
template<typename ConnectionBase>
void connection_pool<ConnectionBase>::pool_core::put(_handle_ptr_t& handle)
{
std::unique_lock<std::mutex> lock{_mutex};
if (_handles.full ()) {
_handles.set_capacity (_handles.capacity () + 5);
}
_handles.push_back(std::move(handle));
}
template<typename ConnectionBase>
std::size_t connection_pool<ConnectionBase>::pool_core::available()
{
std::unique_lock<std::mutex> lock{_mutex};
return _handles.size();
}
template<typename ConnectionBase>
connection_pool<ConnectionBase>::connection_pool(const _config_ptr_t& connection_config, std::size_t capacity) :
_core{std::make_shared<pool_core>(connection_config, capacity)}
{
}
template<typename ConnectionBase>
void connection_pool<ConnectionBase>::initialize(const _config_ptr_t& connection_config, std::size_t capacity)
{
if (_core) {
throw std::runtime_error{"Connection pool already initialized"};
}
_core = std::make_shared<pool_core>(connection_config, capacity);
}
template<typename ConnectionBase>
typename connection_pool<ConnectionBase>::_pooled_connection_t connection_pool<ConnectionBase>::get()
_pooled_connection_t get()
{
return _core->get();
}
template<typename ConnectionBase>
std::size_t connection_pool<ConnectionBase>::available()
// Returns number of connections available in the pool. Only used in tests.
std::size_t available()
{
return _core->available();
}
private:
std::shared_ptr<pool_core> _core;
};
} // namespace sqlpp