mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 21:04:15 +08:00
Untab everything and improve formatting
This commit is contained in:
parent
1987614bfe
commit
628b0bbaff
@ -42,196 +42,196 @@
|
|||||||
|
|
||||||
namespace sqlpp
|
namespace sqlpp
|
||||||
{
|
{
|
||||||
namespace connection_validator
|
namespace connection_validator
|
||||||
{
|
{
|
||||||
struct automatic
|
struct automatic
|
||||||
{
|
{
|
||||||
template<typename Connection>
|
template<typename Connection>
|
||||||
void validate(Connection* connection)
|
void validate(Connection* connection)
|
||||||
{
|
{
|
||||||
if (!connection->is_valid())
|
if (!connection->is_valid())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
connection->reconnect();
|
connection->reconnect();
|
||||||
}
|
}
|
||||||
catch (const sqlpp::exception&)
|
catch (const sqlpp::exception&)
|
||||||
{
|
{
|
||||||
throw sqlpp::exception("Failed to reconnect to database.");
|
throw sqlpp::exception("Failed to reconnect to database.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Connection>
|
template<typename Connection>
|
||||||
void deregister(Connection* connection) {}
|
void deregister(Connection* connection) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
class periodic
|
class periodic
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::chrono::seconds revalidate_interval;
|
std::chrono::seconds revalidate_interval;
|
||||||
std::unordered_map<void*,std::chrono::time_point<std::chrono::system_clock>> last_checked;
|
std::unordered_map<void*, std::chrono::time_point<std::chrono::system_clock>> last_checked;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
periodic(const std::chrono::seconds r = 28800s) //default wait_timeout in MySQL
|
periodic(const std::chrono::seconds r = 28800s) //default wait_timeout in MySQL
|
||||||
: revalidate_interval(r), last_checked() {}
|
: revalidate_interval(r), last_checked() {}
|
||||||
|
|
||||||
template<typename Connection>
|
template<typename Connection>
|
||||||
void validate(Connection* connection)
|
void validate(Connection* connection)
|
||||||
{
|
{
|
||||||
auto last = last_checked.find(connection);
|
auto last = last_checked.find(connection);
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
if (last == last_checked.end())
|
if (last == last_checked.end())
|
||||||
{
|
{
|
||||||
last_checked.emplace_hint(last, connection, now);
|
last_checked.emplace_hint(last, connection, now);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (now - last->second < revalidate_interval)
|
if (now - last->second < revalidate_interval)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!connection->is_valid())
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
connection->reconnect();
|
|
||||||
}
|
|
||||||
catch (const sqlpp::exception& e)
|
|
||||||
{
|
|
||||||
throw sqlpp::exception("Failed to reconnect to database.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
last = now;
|
if (!connection->is_valid())
|
||||||
}
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
connection->reconnect();
|
||||||
|
}
|
||||||
|
catch (const sqlpp::exception& e)
|
||||||
|
{
|
||||||
|
throw sqlpp::exception("Failed to reconnect to database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Connection>
|
last = now;
|
||||||
void deregister(Connection* con)
|
}
|
||||||
{
|
|
||||||
auto itr = last_checked.find(con);
|
|
||||||
if(itr != last_checked.end())
|
|
||||||
{
|
|
||||||
last_checked.erase(itr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct none
|
template<typename Connection>
|
||||||
{
|
void deregister(Connection* con)
|
||||||
template<typename Connection>
|
{
|
||||||
void validate(Connection*) {}
|
auto itr = last_checked.find(con);
|
||||||
|
if (itr != last_checked.end())
|
||||||
|
{
|
||||||
|
last_checked.erase(itr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template<typename Connection>
|
struct none
|
||||||
void deregister(Connection*) {}
|
{
|
||||||
};
|
template<typename Connection>
|
||||||
}
|
void validate(Connection*) {}
|
||||||
|
|
||||||
template <typename Connection_config,
|
template<typename Connection>
|
||||||
typename Connection_validator = connection_validator::automatic,
|
void deregister(Connection*) {}
|
||||||
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type>
|
};
|
||||||
class connection_pool
|
}
|
||||||
{
|
|
||||||
friend pool_connection<Connection_config, Connection_validator, Connection>;
|
|
||||||
|
|
||||||
private:
|
template <typename Connection_config,
|
||||||
std::mutex connection_pool_mutex;
|
typename Connection_validator = connection_validator::automatic,
|
||||||
const std::shared_ptr<Connection_config> config;
|
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type>
|
||||||
size_t maximum_pool_size = 0;
|
class connection_pool
|
||||||
std::stack<std::unique_ptr<Connection>> free_connections;
|
{
|
||||||
Connection_validator connection_validator;
|
friend pool_connection<Connection_config, Connection_validator, Connection>;
|
||||||
|
|
||||||
void free_connection(std::unique_ptr<Connection>& connection)
|
private:
|
||||||
{
|
std::mutex connection_pool_mutex;
|
||||||
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
const std::shared_ptr<Connection_config> config;
|
||||||
if (free_connections.size() >= maximum_pool_size)
|
size_t maximum_pool_size = 0;
|
||||||
{
|
std::stack<std::unique_ptr<Connection>> free_connections;
|
||||||
// Exceeds default size, deregister left over info in the connection_validator and let connection self destroy.
|
Connection_validator connection_validator;
|
||||||
connection_validator.deregister(connection.get());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (connection.get())
|
|
||||||
{
|
|
||||||
free_connections.push(std::move(connection));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw sqlpp::exception("Trying to free an empty connection.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
void free_connection(std::unique_ptr<Connection>& connection)
|
||||||
connection_pool(const std::shared_ptr<Connection_config>& config, size_t pool_size)
|
{
|
||||||
: config(config), maximum_pool_size(pool_size), connection_validator(Connection_validator()) {}
|
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
||||||
~connection_pool() = default;
|
if (free_connections.size() >= maximum_pool_size)
|
||||||
connection_pool(const connection_pool&) = delete;
|
{
|
||||||
connection_pool(connection_pool&& other)
|
// Exceeds default size, deregister left over info in the connection_validator and let connection self destroy.
|
||||||
: config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)),
|
connection_validator.deregister(connection.get());
|
||||||
connection_validator(std::move(other.connection_validator)) {}
|
}
|
||||||
connection_pool& operator=(const connection_pool&) = delete;
|
else
|
||||||
connection_pool& operator=(connection_pool&&) = delete;
|
{
|
||||||
|
if (connection.get())
|
||||||
|
{
|
||||||
|
free_connections.push(std::move(connection));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw sqlpp::exception("Trying to free an empty connection.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pool_connection<Connection_config, Connection_validator, Connection> get_connection()
|
public:
|
||||||
{
|
connection_pool(const std::shared_ptr<Connection_config>& config, size_t pool_size)
|
||||||
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
: config(config), maximum_pool_size(pool_size), connection_validator(Connection_validator()) {}
|
||||||
while (true)
|
~connection_pool() = default;
|
||||||
{
|
connection_pool(const connection_pool&) = delete;
|
||||||
try
|
connection_pool(connection_pool&& other)
|
||||||
{
|
: config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)),
|
||||||
if (!free_connections.empty())
|
connection_validator(std::move(other.connection_validator)) {}
|
||||||
{
|
connection_pool& operator=(const connection_pool&) = delete;
|
||||||
auto connection = std::move(free_connections.top());
|
connection_pool& operator=(connection_pool&&) = delete;
|
||||||
free_connections.pop();
|
|
||||||
connection_validator.validate(connection.get());
|
|
||||||
|
|
||||||
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(connection), this);
|
auto get_connection()
|
||||||
}
|
-> pool_connection<Connection_config, Connection_validator, Connection>
|
||||||
else
|
{
|
||||||
{
|
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
||||||
break;
|
while (true)
|
||||||
}
|
{
|
||||||
}
|
try
|
||||||
catch (const sqlpp::exception&)
|
{
|
||||||
{
|
if (!free_connections.empty())
|
||||||
throw sqlpp::exception("Failed to retrieve a valid connection.");
|
{
|
||||||
}
|
auto connection = std::move(free_connections.top());
|
||||||
}
|
free_connections.pop();
|
||||||
|
connection_validator.validate(connection.get());
|
||||||
|
|
||||||
try
|
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(connection), this);
|
||||||
{
|
}
|
||||||
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(std::make_unique<Connection>(config)), this);
|
else
|
||||||
}
|
{
|
||||||
catch (const sqlpp::exception&)
|
break;
|
||||||
{
|
}
|
||||||
throw sqlpp::exception("Failed to spawn a new connection.");
|
}
|
||||||
}
|
catch (const sqlpp::exception&)
|
||||||
}
|
{
|
||||||
|
throw sqlpp::exception("Failed to retrieve a valid connection.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Query, typename Lambda>
|
try
|
||||||
void operator()(Query query, Lambda callback)
|
{
|
||||||
{
|
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(std::make_unique<Connection>(config)), this);
|
||||||
query_task<connection_pool, Query, Lambda>(*this, query, callback)();
|
}
|
||||||
}
|
catch (const sqlpp::exception&)
|
||||||
|
{
|
||||||
|
throw sqlpp::exception("Failed to spawn a new connection.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Query>
|
template<typename Query, typename Lambda>
|
||||||
void operator()(Query query)
|
void operator()(Query query, Lambda callback)
|
||||||
{
|
{
|
||||||
operator()(query, [](){});
|
query_task<connection_pool, Query, Lambda>(*this, query, callback)();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
template<typename Connection_config,
|
template<typename Query>
|
||||||
typename Connection_validator = connection_validator::automatic,
|
void operator()(Query query)
|
||||||
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value,Connection_config::connection>::type>
|
{
|
||||||
connection_pool<Connection_config, Connection_validator, Connection> make_connection_pool(
|
operator()(query, []() {});
|
||||||
const std::shared_ptr<Connection_config>& config,
|
}
|
||||||
size_t max_pool_size)
|
};
|
||||||
{
|
|
||||||
return connection_pool<Connection_config, Connection_validator, Connection>(config, max_pool_size);
|
template<typename Connection_config,
|
||||||
}
|
typename Connection_validator = connection_validator::automatic,
|
||||||
|
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type>
|
||||||
|
auto make_connection_pool(const std::shared_ptr<Connection_config>& config, size_t max_pool_size)
|
||||||
|
-> connection_pool<Connection_config, Connection_validator, Connection>
|
||||||
|
{
|
||||||
|
return connection_pool<Connection_config, Connection_validator, Connection>(config, max_pool_size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -32,63 +32,67 @@
|
|||||||
|
|
||||||
namespace sqlpp
|
namespace sqlpp
|
||||||
{
|
{
|
||||||
template <typename Connection_config, typename Connection_validator, typename Connection,
|
template <typename Connection_config, typename Connection_validator, typename Connection,
|
||||||
typename Connection_pool = connection_pool<Connection_config, Connection_validator, Connection>>
|
typename Connection_pool = connection_pool<Connection_config, Connection_validator, Connection>>
|
||||||
struct pool_connection : public sqlpp::connection
|
struct pool_connection : public sqlpp::connection
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Connection> _impl;
|
std::unique_ptr<Connection> _impl;
|
||||||
Connection_pool* origin;
|
Connection_pool* origin;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
pool_connection() : _impl(nullptr), origin(nullptr) {}
|
pool_connection() : _impl(nullptr), origin(nullptr)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
pool_connection(std::unique_ptr<Connection>& connection, Connection_pool* origin)
|
pool_connection(std::unique_ptr<Connection>& connection, Connection_pool* origin) : _impl(std::move(connection)), origin(origin)
|
||||||
: _impl(std::move(connection)), origin(origin) {}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
~pool_connection()
|
~pool_connection()
|
||||||
{
|
{
|
||||||
if (_impl.get())
|
if (_impl.get())
|
||||||
{
|
{
|
||||||
origin->free_connection(_impl);
|
origin->free_connection(_impl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
auto operator()(Args&&... args) -> decltype(_impl->args(std::forward<Args>(args)...))
|
auto operator()(Args&&... args) -> decltype(_impl->args(std::forward<Args>(args)...))
|
||||||
{
|
{
|
||||||
return _impl->args(std::forward<Args>(args)...);
|
return _impl->args(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
auto operator()(const T& t) -> decltype(_impl->run(t))
|
auto operator()(const T& t) -> decltype(_impl->run(t))
|
||||||
{
|
{
|
||||||
return _impl->run(t);
|
return _impl->run(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
auto execute(const T& t) -> decltype(_impl->execute(t))
|
auto execute(const T& t) -> decltype(_impl->execute(t))
|
||||||
{
|
{
|
||||||
return _impl->execute(t);
|
return _impl->execute(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
auto prepare(const T& t) -> decltype(_impl->prepare(t))
|
auto prepare(const T& t) -> decltype(_impl->prepare(t))
|
||||||
{
|
{
|
||||||
return _impl->prepare(t);
|
return _impl->prepare(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
pool_connection(const pool_connection&) = delete;
|
pool_connection(const pool_connection&) = delete;
|
||||||
pool_connection(pool_connection&& other)
|
pool_connection(pool_connection&& other) : _impl(std::move(other._impl)), origin(other.origin)
|
||||||
: _impl(std::move(other._impl)), origin(other.origin) {}
|
{
|
||||||
pool_connection& operator=(const pool_connection&) = delete;
|
}
|
||||||
pool_connection& operator=(pool_connection&& other)
|
pool_connection& operator=(const pool_connection&) = delete;
|
||||||
{
|
pool_connection& operator=(pool_connection&& other)
|
||||||
_impl = std::move(other._impl);
|
{
|
||||||
origin = other.origin;
|
_impl = std::move(other._impl);
|
||||||
return *this;
|
origin = other.origin;
|
||||||
}
|
return *this;
|
||||||
};
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user