0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 12:51:13 +08:00
This commit is contained in:
Frank Park 2017-04-12 16:49:58 -04:00
parent d1fbab9e47
commit 3bbaf39c68
2 changed files with 56 additions and 58 deletions

View File

@ -133,8 +133,6 @@ namespace sqlpp
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type>
class connection_pool_t
{
friend pool_connection<Connection_config, Connection_validator, Connection>;
private:
std::mutex connection_pool_mutex;
const std::shared_ptr<Connection_config> config;
@ -142,6 +140,55 @@ namespace sqlpp
std::stack<std::unique_ptr<Connection>> free_connections;
Connection_validator connection_validator;
public:
connection_pool_t(const std::shared_ptr<Connection_config>& config, size_t pool_size)
: config(config), maximum_pool_size(pool_size), connection_validator(Connection_validator()) {}
~connection_pool_t() = default;
connection_pool_t(const connection_pool_t&) = delete;
connection_pool_t(connection_pool_t&& other)
: config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)),
connection_validator(std::move(other.connection_validator)) {}
connection_pool_t& operator=(const connection_pool_t&) = delete;
connection_pool_t& operator=(connection_pool_t&&) = delete;
template <typename Pool_connection = pool_connection<Connection_config, Connection_validator, Connection, connection_pool_t>>
auto get_connection()
-> Pool_connection
{
std::lock_guard<std::mutex> lock(connection_pool_mutex);
while (true)
{
try
{
if (!free_connections.empty())
{
auto connection = std::move(free_connections.top());
free_connections.pop();
connection_validator.validate(connection.get());
return Pool_connection(std::move(connection), this);
}
else
{
break;
}
}
catch (const sqlpp::exception&)
{
throw sqlpp::exception("Failed to retrieve a valid connection.");
}
}
try
{
return Pool_connection(std::move(std::make_unique<Connection>(config)), this);
}
catch (const sqlpp::exception&)
{
throw sqlpp::exception("Failed to spawn a new connection.");
}
}
void free_connection(std::unique_ptr<Connection>& connection)
{
std::lock_guard<std::mutex> lock(connection_pool_mutex);
@ -163,54 +210,6 @@ namespace sqlpp
}
}
public:
connection_pool_t(const std::shared_ptr<Connection_config>& config, size_t pool_size)
: config(config), maximum_pool_size(pool_size), connection_validator(Connection_validator()) {}
~connection_pool_t() = default;
connection_pool_t(const connection_pool_t&) = delete;
connection_pool_t(connection_pool_t&& other)
: config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)),
connection_validator(std::move(other.connection_validator)) {}
connection_pool_t& operator=(const connection_pool_t&) = delete;
connection_pool_t& operator=(connection_pool_t&&) = delete;
auto get_connection()
-> pool_connection<Connection_config, Connection_validator, Connection>
{
std::lock_guard<std::mutex> lock(connection_pool_mutex);
while (true)
{
try
{
if (!free_connections.empty())
{
auto connection = std::move(free_connections.top());
free_connections.pop();
connection_validator.validate(connection.get());
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(connection), this);
}
else
{
break;
}
}
catch (const sqlpp::exception&)
{
throw sqlpp::exception("Failed to retrieve a valid connection.");
}
}
try
{
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(std::make_unique<Connection>(config)), this);
}
catch (const sqlpp::exception&)
{
throw sqlpp::exception("Failed to spawn a new connection.");
}
}
template<typename Query, typename Lambda>
void operator()(Query query, Lambda callback)
{

View File

@ -34,8 +34,7 @@
namespace sqlpp
{
template <typename Connection_config, typename Connection_validator, typename Connection,
typename Connection_pool = connection_pool_t<Connection_config, Connection_validator, Connection>>
template <typename Connection_config, typename Connection_validator, typename Connection, typename Connection_pool>
struct pool_connection : public sqlpp::connection
{
private:
@ -90,11 +89,11 @@ namespace sqlpp
return _impl->prepare(t);
}
template<typename Query, typename Lambda,
template<typename Query, typename Lambda, typename Result = decltype(pool_connection()(Query())),
typename std::enable_if<is_invocable<Lambda>::value ||
is_invocable<Lambda, sqlpp::exception>::value ||
is_invocable<Lambda, sqlpp::exception, decltype(pool_connection()(Query()))>::value &&
!is_invocable<Lambda, sqlpp::exception, decltype(pool_connection()(Query())), pool_connection>::value, int>::type = 0>
is_invocable<Lambda, sqlpp::exception, Result>::value &&
!is_invocable<Lambda, sqlpp::exception, Result, pool_connection>::value, int>::type = 0>
void operator()(Query query, Lambda callback)
{
try
@ -103,12 +102,12 @@ namespace sqlpp
}
catch (const std::exception& e)
{
invoke_callback(sqlpp::exception(sqlpp::exception::query_error, e.what()), pool_connection(), decltype(pool_connection()(Query()))(), callback);
invoke_callback(sqlpp::exception(sqlpp::exception::query_error, e.what()), pool_connection(), Result(), callback);
}
}
template<typename Query, typename Lambda,
typename std::enable_if<is_invocable<Lambda, sqlpp::exception, decltype(pool_connection()(Query())), pool_connection>::value, int>::type = 0>
template<typename Query, typename Lambda, typename Result = decltype(pool_connection()(Query())),
typename std::enable_if<is_invocable<Lambda, sqlpp::exception, Result, pool_connection>::value, int>::type = 0>
void operator()(Query query, Lambda callback)
{
static_assert(false, "Direct query with pool connection forbids callback with parameter of type connection.");