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

View File

@ -33,7 +33,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace sqlpp namespace sqlpp
{ {
template<typename ConnectionBase> template <typename ConnectionBase>
class connection_pool class connection_pool
{ {
public: public:
@ -44,18 +44,50 @@ namespace sqlpp
class pool_core : public std::enable_shared_from_this<pool_core> class pool_core : public std::enable_shared_from_this<pool_core>
{ {
public: 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() = delete;
pool_core(const pool_core &) = delete; pool_core(const pool_core&) = delete;
pool_core(pool_core &&) = delete; pool_core(pool_core&&) = delete;
pool_core& operator=(const pool_core&) = delete; pool_core& operator=(const pool_core&) = delete;
pool_core& operator=(pool_core&&) = delete; pool_core& operator=(pool_core&&) = delete;
_pooled_connection_t get(); _pooled_connection_t get()
void put(_handle_ptr_t& handle); {
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. // 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: private:
_config_ptr_t _connection_config; _config_ptr_t _connection_config;
@ -64,88 +96,39 @@ namespace sqlpp
}; };
connection_pool() = default; 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(const connection_pool&) = delete;
connection_pool(connection_pool&&) = default; connection_pool(connection_pool&&) = default;
connection_pool& operator=(const connection_pool&) = delete; connection_pool& operator=(const connection_pool&) = delete;
connection_pool& operator=(connection_pool&&) = default; connection_pool& operator=(connection_pool&&) = default;
void initialize(const _config_ptr_t& connection_config, std::size_t capacity); void initialize(const _config_ptr_t& connection_config, std::size_t capacity)
_pooled_connection_t get(); {
if (_core)
{
throw std::runtime_error{"Connection pool already initialized"};
}
_core = std::make_shared<pool_core>(connection_config, capacity);
}
_pooled_connection_t get()
{
return _core->get();
}
// Returns number of connections available in the pool. Only used in tests. // Returns number of connections available in the pool. Only used in tests.
std::size_t available(); std::size_t available()
{
return _core->available();
}
private: private:
std::shared_ptr<pool_core> _core; std::shared_ptr<pool_core> _core;
}; };
} // namespace sqlpp
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}
{
}
template<typename ConnectionBase>
typename connection_pool<ConnectionBase>::_pooled_connection_t connection_pool<ConnectionBase>::pool_core::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()};
}
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()
{
return _core->get();
}
template<typename ConnectionBase>
std::size_t connection_pool<ConnectionBase>::available()
{
return _core->available();
}
} // namespace sqlpp