2023-07-25 13:00:05 +08:00
|
|
|
#pragma once
|
|
|
|
|
2013-08-14 04:43:10 +08:00
|
|
|
/*
|
2015-02-16 02:00:21 +08:00
|
|
|
* Copyright (c) 2013-2015, Roland Bock
|
2023-07-17 12:14:23 +08:00
|
|
|
* Copyright (c) 2023, Vesselin Atanasov
|
2013-08-14 04:43:10 +08:00
|
|
|
* All rights reserved.
|
2015-08-05 20:43:21 +08:00
|
|
|
*
|
2013-08-14 04:43:10 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
2015-08-05 20:43:21 +08:00
|
|
|
*
|
2013-08-14 04:43:10 +08:00
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
2015-08-05 20:43:21 +08:00
|
|
|
*
|
2013-08-14 04:43:10 +08:00
|
|
|
* Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
* other materials provided with the distribution.
|
2015-08-05 20:43:21 +08:00
|
|
|
*
|
2013-08-14 04:43:10 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2023-09-16 15:21:05 +08:00
|
|
|
#include <sqlpp11/compat/make_unique.h>
|
|
|
|
|
2023-07-17 12:14:23 +08:00
|
|
|
#include <memory>
|
2023-09-20 12:38:16 +08:00
|
|
|
#include <utility>
|
2023-07-17 12:14:23 +08:00
|
|
|
|
2013-08-14 04:43:10 +08:00
|
|
|
namespace sqlpp
|
|
|
|
{
|
2015-09-14 03:33:19 +08:00
|
|
|
struct connection
|
|
|
|
{
|
|
|
|
};
|
2023-07-17 12:14:23 +08:00
|
|
|
|
2023-09-20 12:38:16 +08:00
|
|
|
template <typename ConnectionBase>
|
|
|
|
class common_connection : public ConnectionBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool is_connected() const
|
|
|
|
{
|
|
|
|
return ConnectionBase::_handle ? ConnectionBase::_handle->is_connected() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ping_server() const
|
|
|
|
{
|
|
|
|
return ConnectionBase::_handle ? ConnectionBase::_handle->ping_server() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
using ConnectionBase::ConnectionBase;
|
|
|
|
};
|
|
|
|
|
2023-07-17 12:14:23 +08:00
|
|
|
// Normal (non-pooled) connection
|
2023-09-01 12:19:04 +08:00
|
|
|
template <typename ConnectionBase>
|
2023-09-20 12:38:16 +08:00
|
|
|
class normal_connection : public common_connection<ConnectionBase>
|
2023-07-17 12:14:23 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using _config_t = typename ConnectionBase::_config_t;
|
|
|
|
using _config_ptr_t = typename ConnectionBase::_config_ptr_t;
|
|
|
|
|
|
|
|
// Constructors
|
|
|
|
normal_connection() = default;
|
2023-09-01 12:19:04 +08:00
|
|
|
|
|
|
|
normal_connection(const _config_t& config) : normal_connection{std::make_shared<_config_t>(config)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-09-20 12:38:16 +08:00
|
|
|
normal_connection(const _config_ptr_t& config)
|
2023-10-21 15:20:07 +08:00
|
|
|
: common_connection<ConnectionBase>(compat::make_unique<_handle_t>(config))
|
2023-09-01 12:19:04 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-07-17 12:14:23 +08:00
|
|
|
normal_connection(const normal_connection&) = delete;
|
|
|
|
normal_connection(normal_connection&&) = default;
|
|
|
|
|
|
|
|
// Assigment operators
|
|
|
|
normal_connection& operator=(const normal_connection&) = delete;
|
|
|
|
normal_connection& operator=(normal_connection&&) = default;
|
|
|
|
|
|
|
|
// creates a connection handle and connects to database
|
2023-09-01 12:19:04 +08:00
|
|
|
void connectUsing(const _config_ptr_t& config) noexcept(false)
|
|
|
|
{
|
2023-09-16 15:21:05 +08:00
|
|
|
ConnectionBase::_handle = compat::make_unique<_handle_t>(config);
|
2023-09-01 12:19:04 +08:00
|
|
|
}
|
2023-07-17 12:14:23 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
using _handle_t = typename ConnectionBase::_handle_t;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Forward declaration
|
2023-09-01 12:19:04 +08:00
|
|
|
template <typename ConnectionBase>
|
2023-07-17 12:14:23 +08:00
|
|
|
class connection_pool;
|
|
|
|
|
|
|
|
// Pooled connection
|
2023-09-01 12:19:04 +08:00
|
|
|
template <typename ConnectionBase>
|
2023-09-20 12:38:16 +08:00
|
|
|
class pooled_connection : public common_connection<ConnectionBase>
|
2023-07-17 12:14:23 +08:00
|
|
|
{
|
|
|
|
friend class connection_pool<ConnectionBase>::pool_core;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using _config_ptr_t = typename ConnectionBase::_config_ptr_t;
|
|
|
|
using _handle_t = typename ConnectionBase::_handle_t;
|
|
|
|
using _handle_ptr_t = typename ConnectionBase::_handle_ptr_t;
|
|
|
|
using _pool_core_ptr_t = std::shared_ptr<typename connection_pool<ConnectionBase>::pool_core>;
|
|
|
|
|
|
|
|
// Copy/Move constructors
|
|
|
|
pooled_connection(const pooled_connection&) = delete;
|
|
|
|
pooled_connection(pooled_connection&& other) = default;
|
2023-09-01 12:19:04 +08:00
|
|
|
|
|
|
|
~pooled_connection()
|
|
|
|
{
|
|
|
|
conn_release();
|
|
|
|
}
|
2023-07-17 12:14:23 +08:00
|
|
|
|
|
|
|
// Assigment operators
|
|
|
|
pooled_connection& operator=(const pooled_connection&) = delete;
|
2023-09-01 12:19:04 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-07-17 12:14:23 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
_pool_core_ptr_t _pool_core;
|
|
|
|
|
|
|
|
// Constructors used by the connection pool
|
2023-09-01 12:19:04 +08:00
|
|
|
pooled_connection(_handle_ptr_t&& handle, _pool_core_ptr_t pool_core)
|
2023-10-21 15:20:07 +08:00
|
|
|
: common_connection<ConnectionBase>(std::move(handle)), _pool_core(pool_core)
|
2023-09-01 12:19:04 +08:00
|
|
|
{
|
2023-07-17 12:14:23 +08:00
|
|
|
}
|
|
|
|
|
2023-09-01 12:19:04 +08:00
|
|
|
pooled_connection(const _config_ptr_t& config, _pool_core_ptr_t pool_core)
|
2023-10-21 15:20:07 +08:00
|
|
|
: common_connection<ConnectionBase>(compat::make_unique<_handle_t>(config)), _pool_core(pool_core)
|
2023-09-01 12:19:04 +08:00
|
|
|
{
|
|
|
|
}
|
2023-07-17 12:14:23 +08:00
|
|
|
|
2023-09-01 12:19:04 +08:00
|
|
|
void conn_release()
|
|
|
|
{
|
|
|
|
if (_pool_core)
|
|
|
|
{
|
|
|
|
_pool_core->put(ConnectionBase::_handle);
|
|
|
|
_pool_core = nullptr;
|
|
|
|
}
|
2023-07-17 12:14:23 +08:00
|
|
|
}
|
2023-09-01 12:19:04 +08:00
|
|
|
};
|
2017-09-10 20:56:51 +08:00
|
|
|
} // namespace sqlpp
|