mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Merge branch 'develop' of https://github.com/rbock/sqlpp11 into develop
This commit is contained in:
commit
3a6e4d93ec
@ -1,28 +1,28 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013 - 2017, Roland Bock, Frank Park
|
* Copyright (c) 2013 - 2017, Roland Bock, Frank Park
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without modification,
|
* Redistribution and use in source and binary forms, with or without modification,
|
||||||
* are permitted provided that the following conditions are met:
|
* are permitted provided that the following conditions are met:
|
||||||
*
|
*
|
||||||
* Redistributions of source code must retain the above copyright notice, this
|
* Redistributions of source code must retain the above copyright notice, this
|
||||||
* list of conditions and the following disclaimer.
|
* list of conditions and the following disclaimer.
|
||||||
*
|
*
|
||||||
* Redistributions in binary form must reproduce the above copyright notice, this
|
* Redistributions in binary form must reproduce the above copyright notice, this
|
||||||
* list of conditions and the following disclaimer in the documentation and/or
|
* list of conditions and the following disclaimer in the documentation and/or
|
||||||
* other materials provided with the distribution.
|
* other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
* 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
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
* 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
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef SQLPP_CONNECTION_POOL_H
|
#ifndef SQLPP_CONNECTION_POOL_H
|
||||||
#define SQLPP_CONNECTION_POOL_H
|
#define SQLPP_CONNECTION_POOL_H
|
||||||
@ -39,154 +39,171 @@
|
|||||||
|
|
||||||
namespace sqlpp
|
namespace sqlpp
|
||||||
{
|
{
|
||||||
namespace reconnect_policy
|
namespace reconnect_policy
|
||||||
{
|
{
|
||||||
struct auto_reconnect {
|
struct auto_reconnect
|
||||||
template<typename Connection>
|
{
|
||||||
void operator()(Connection* connection)
|
template <typename Connection>
|
||||||
{
|
void operator()(Connection* connection)
|
||||||
if(!connection->is_valid())
|
{
|
||||||
connection->reconnect()
|
if (!connection->is_valid())
|
||||||
}
|
connection->reconnect();
|
||||||
template<typename Connection>
|
}
|
||||||
void clean(Connection* connection) {}
|
template <typename Connection>
|
||||||
};
|
void clean(Connection* connection)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
class periodic_reconnect
|
||||||
class periodic_reconnect
|
{
|
||||||
{
|
private:
|
||||||
private:
|
std::chrono::seconds revalidate_after;
|
||||||
std::chrono::seconds revalidate_after;
|
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_reconnect(const std::chrono::seconds r = 28800s) //default wait_timeout in MySQL
|
periodic_reconnect(const std::chrono::seconds r = std::chrono::seconds(28800)) // default wait_timeout in MySQL
|
||||||
: revalidate_after(r), last_checked() {}
|
: revalidate_after(r), last_checked()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Connection>
|
template <typename Connection>
|
||||||
void operator()(Connection* con)
|
void operator()(Connection* con)
|
||||||
{
|
{
|
||||||
auto last = last_checked.find(con);
|
auto last = last_checked.find(con);
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
if(last == last_checked.end())
|
if (last == last_checked.end())
|
||||||
{
|
{
|
||||||
if (!con->is_valid())
|
if (!con->is_valid())
|
||||||
{
|
{
|
||||||
con->reconnect();
|
con->reconnect();
|
||||||
}
|
}
|
||||||
last_checked.emplace_hint(last, con, now);
|
last_checked.emplace_hint(last, con, now);
|
||||||
}
|
}
|
||||||
else if(now - last->second > revalidate_after)
|
else if (now - last->second > revalidate_after)
|
||||||
{
|
{
|
||||||
if (!con->is_valid())
|
if (!con->is_valid())
|
||||||
{
|
{
|
||||||
con->reconnect();
|
con->reconnect();
|
||||||
}
|
}
|
||||||
last = now;
|
last = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
template<typename Connection>
|
template <typename Connection>
|
||||||
void clean(Connection* con) {
|
void clean(Connection* con)
|
||||||
auto itr = last_checked.find(con);
|
{
|
||||||
if(itr != last_checked.end())
|
auto itr = last_checked.find(con);
|
||||||
{
|
if (itr != last_checked.end())
|
||||||
last_checked.erase(itr);
|
{
|
||||||
}
|
last_checked.erase(itr);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct never_reconnect {
|
struct never_reconnect
|
||||||
template<typename Connection>
|
{
|
||||||
void operator()(Connection*) {}
|
template <typename Connection>
|
||||||
template<typename Connection>
|
void operator()(Connection*)
|
||||||
void clean(Connection*) {}
|
{
|
||||||
};
|
}
|
||||||
}
|
template <typename Connection>
|
||||||
|
void clean(Connection*)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Connection_config,
|
template <typename Connection_config,
|
||||||
typename Reconnect_policy = reconnect_policy::auto_reconnect,
|
typename Reconnect_policy = reconnect_policy::auto_reconnect,
|
||||||
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type>
|
typename Connection = typename std::enable_if<std::is_class<typename Connection_config::connection>::value,
|
||||||
class connection_pool
|
typename Connection_config::connection>::type>
|
||||||
{
|
class connection_pool
|
||||||
friend pool_connection<Connection_config, Reconnect_policy, Connection>;
|
{
|
||||||
|
friend pool_connection<Connection_config, Reconnect_policy, Connection>;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex connection_pool_mutex;
|
std::mutex connection_pool_mutex;
|
||||||
const std::shared_ptr<Connection_config> config;
|
const std::shared_ptr<Connection_config> config;
|
||||||
size_t maximum_pool_size = 0;
|
size_t maximum_pool_size = 0;
|
||||||
std::stack<std::unique_ptr<Connection>> free_connections;
|
std::stack<std::unique_ptr<Connection>> free_connections;
|
||||||
Reconnect_policy reconnect_policy;
|
Reconnect_policy reconnect_policy;
|
||||||
|
|
||||||
void free_connection(std::unique_ptr<Connection>& connection)
|
void free_connection(std::unique_ptr<Connection>& connection)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
||||||
if (free_connections.size() >= maximum_pool_size)
|
if (free_connections.size() >= maximum_pool_size)
|
||||||
{
|
{
|
||||||
// Exceeds default size, do nothign and let connection self destroy.
|
// Exceeds default size, do nothign and let connection self destroy.
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (connection.get())
|
if (connection.get())
|
||||||
{
|
{
|
||||||
if (connection->is_valid())
|
if (connection->is_valid())
|
||||||
{
|
{
|
||||||
free_connections.push(std::move(connection));
|
free_connections.push(std::move(connection));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw sqlpp::exception("Trying to free a connection with incompatible config.");
|
throw sqlpp::exception("Trying to free a connection with incompatible config.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw sqlpp::exception("Trying to free an empty connection.");
|
throw sqlpp::exception("Trying to free an empty connection.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
connection_pool(const std::shared_ptr<Connection_config>& config, size_t pool_size)
|
connection_pool(const std::shared_ptr<Connection_config>& config, size_t pool_size)
|
||||||
: config(config), maximum_pool_size(pool_size), reconnect_policy(Reconnect_policy()) {}
|
: config(config), maximum_pool_size(pool_size), reconnect_policy(Reconnect_policy())
|
||||||
~connection_pool() = default;
|
{
|
||||||
connection_pool(const connection_pool&) = delete;
|
}
|
||||||
connection_pool(connection_pool&& other)
|
~connection_pool() = default;
|
||||||
: config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)),
|
connection_pool(const connection_pool&) = delete;
|
||||||
reconnect_policy(std::move(other.reconnect_policy)) {}
|
connection_pool(connection_pool&& other)
|
||||||
connection_pool& operator=(const connection_pool&) = delete;
|
: config(std::move(other.config)),
|
||||||
connection_pool& operator=(connection_pool&&) = delete;
|
maximum_pool_size(std::move(other.maximum_pool_size)),
|
||||||
|
reconnect_policy(std::move(other.reconnect_policy))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
connection_pool& operator=(const connection_pool&) = delete;
|
||||||
|
connection_pool& operator=(connection_pool&&) = delete;
|
||||||
|
|
||||||
pool_connection<Connection_config, Reconnect_policy, Connection> get_connection()
|
pool_connection<Connection_config, Reconnect_policy, Connection> get_connection()
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
std::lock_guard<std::mutex> lock(connection_pool_mutex);
|
||||||
if (!free_connections.empty())
|
if (!free_connections.empty())
|
||||||
{
|
{
|
||||||
auto connection = std::move(free_connections.top());
|
auto connection = std::move(free_connections.top());
|
||||||
free_connections.pop();
|
free_connections.pop();
|
||||||
return pool_connection<Connection_config, Reconnect_policy, Connection>(connection, this);
|
return pool_connection<Connection_config, Reconnect_policy, Connection>(connection, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return pool_connection<Connection_config, Reconnect_policy, Connection>(std::move(std::make_unique<Connection>(config)), this);
|
auto c = std::unique_ptr<Connection>(new Connection(*(config.get())));
|
||||||
}
|
return pool_connection<Connection_config, Reconnect_policy, Connection>(c, this);
|
||||||
catch (const sqlpp::exception& e)
|
}
|
||||||
{
|
catch (const sqlpp::exception& e)
|
||||||
std::cerr << "Failed to spawn a new connection." << std::endl;
|
{
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << "Failed to spawn a new connection." << std::endl;
|
||||||
throw;
|
std::cerr << e.what() << std::endl;
|
||||||
}
|
throw;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template<typename Connection_config,
|
template <typename Connection_config,
|
||||||
typename Reconnect_policy = reconnect_policy::auto_reconnect,
|
typename Reconnect_policy = reconnect_policy::auto_reconnect,
|
||||||
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value,Connection_config::connection>::type>
|
typename Connection = typename std::enable_if<std::is_class<typename Connection_config::connection>::value,
|
||||||
connection_pool<Connection_config, Reconnect_policy, Connection> make_connection_pool(
|
typename Connection_config::connection>::type>
|
||||||
const std::shared_ptr<Connection_config>& config,
|
connection_pool<Connection_config, Reconnect_policy, Connection> make_connection_pool(
|
||||||
size_t max_pool_size)
|
const std::shared_ptr<Connection_config>& config, size_t max_pool_size)
|
||||||
{
|
{
|
||||||
return connection_pool<Connection_config, Reconnect_policy, Connection>(config, max_pool_size);
|
return connection_pool<Connection_config, Reconnect_policy, Connection>(config, max_pool_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,28 +1,28 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013 - 2017, Roland Bock, Frank Park
|
* Copyright (c) 2013 - 2017, Roland Bock, Frank Park
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without modification,
|
* Redistribution and use in source and binary forms, with or without modification,
|
||||||
* are permitted provided that the following conditions are met:
|
* are permitted provided that the following conditions are met:
|
||||||
*
|
*
|
||||||
* Redistributions of source code must retain the above copyright notice, this
|
* Redistributions of source code must retain the above copyright notice, this
|
||||||
* list of conditions and the following disclaimer.
|
* list of conditions and the following disclaimer.
|
||||||
*
|
*
|
||||||
* Redistributions in binary form must reproduce the above copyright notice, this
|
* Redistributions in binary form must reproduce the above copyright notice, this
|
||||||
* list of conditions and the following disclaimer in the documentation and/or
|
* list of conditions and the following disclaimer in the documentation and/or
|
||||||
* other materials provided with the distribution.
|
* other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
* 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
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
* 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
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef SQLPP_POOL_CONNECTION_H
|
#ifndef SQLPP_POOL_CONNECTION_H
|
||||||
#define SQLPP_POOL_CONNECTION_H
|
#define SQLPP_POOL_CONNECTION_H
|
||||||
@ -31,46 +31,54 @@
|
|||||||
|
|
||||||
namespace sqlpp
|
namespace sqlpp
|
||||||
{
|
{
|
||||||
template <typename Connection_config, typename Reconnect_policy, typename Connection,
|
template <typename Connection_config, typename Reconnect_policy, typename Connection>
|
||||||
typename Connection_pool = connection_pool<Connection_config, Reconnect_policy, Connection>>
|
class connection_pool;
|
||||||
struct pool_connection
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
std::unique_ptr<Connection> _impl;
|
|
||||||
Connection_pool* origin;
|
|
||||||
|
|
||||||
public:
|
template <typename Connection_config,
|
||||||
pool_connection(std::unique_ptr<Connection>& connection, Connection_pool* origin)
|
typename Reconnect_policy,
|
||||||
: _impl(std::move(connection)), origin(origin) {}
|
typename Connection,
|
||||||
|
typename Connection_pool = connection_pool<Connection_config, Reconnect_policy, Connection>>
|
||||||
|
struct pool_connection
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Connection> _impl;
|
||||||
|
Connection_pool* origin;
|
||||||
|
|
||||||
~pool_connection()
|
public:
|
||||||
{
|
pool_connection(std::unique_ptr<Connection>& connection, Connection_pool* origin)
|
||||||
origin->free_connection(_impl);
|
: _impl(std::move(connection)), origin(origin)
|
||||||
}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
~pool_connection()
|
||||||
auto operator()(Args&&... args) -> decltype(_impl->args(std::forward<Args>(args)...))
|
{
|
||||||
{
|
origin->free_connection(_impl);
|
||||||
return _impl->args(std::forward<Args>(args)...);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename... Args>
|
||||||
auto operator()(const T& t) -> decltype(_impl->run(t))
|
auto operator()(Args&&... args) -> decltype((*_impl)(std::forward<Args>(args)...))
|
||||||
{
|
{
|
||||||
return _impl->run(t);
|
return (*_impl)(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection* operator->()
|
template <typename T>
|
||||||
{
|
auto operator()(const T& t) -> decltype((*_impl)(t))
|
||||||
return &_impl;
|
{
|
||||||
}
|
return (*_impl)(t);
|
||||||
|
}
|
||||||
|
|
||||||
pool_connection(const pool_connection&) = delete;
|
Connection* operator->()
|
||||||
pool_connection(pool_connection&& other)
|
{
|
||||||
: _impl(std::move(other._impl)), origin(other.origin) {}
|
return _impl.get();
|
||||||
pool_connection& operator=(const pool_connection&) = delete;
|
}
|
||||||
pool_connection& operator=(pool_connection&&) = delete;
|
|
||||||
};
|
pool_connection(const pool_connection&) = delete;
|
||||||
|
pool_connection(pool_connection&& other) : _impl(std::move(other._impl)), origin(other.origin)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
pool_connection& operator=(const pool_connection&) = delete;
|
||||||
|
pool_connection& operator=(pool_connection&&) = delete;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2015, Roland Bock
|
* Copyright (c) 2013-2017, Roland Bock, Aaron Bishop
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without modification,
|
* Redistribution and use in source and binary forms, with or without modification,
|
||||||
@ -38,6 +38,25 @@ namespace sqlpp
|
|||||||
using type = std::input_iterator_tag;
|
using type = std::input_iterator_tag;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
template<class DbResult, class = void>
|
||||||
|
struct result_has_size : std::false_type {};
|
||||||
|
|
||||||
|
template<class DbResult>
|
||||||
|
struct result_has_size<DbResult, void_t<decltype(std::declval<DbResult>().size())>>
|
||||||
|
: std::true_type {};
|
||||||
|
|
||||||
|
template<class DbResult, class = void>
|
||||||
|
struct result_size_type { using type = void; };
|
||||||
|
|
||||||
|
template<class DbResult>
|
||||||
|
struct result_size_type<DbResult, void_t<decltype(std::declval<DbResult>().size())>>
|
||||||
|
{
|
||||||
|
using type = decltype(std::declval<DbResult>().size());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
template <typename DbResult, typename ResultRow>
|
template <typename DbResult, typename ResultRow>
|
||||||
class result_t
|
class result_t
|
||||||
{
|
{
|
||||||
@ -139,6 +158,13 @@ namespace sqlpp
|
|||||||
{
|
{
|
||||||
_result.next(_result_row);
|
_result.next(_result_row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class Size = typename detail::result_size_type<DbResult>::type>
|
||||||
|
Size size() const
|
||||||
|
{
|
||||||
|
static_assert(detail::result_has_size<DbResult>::value, "Underlying connector does not support size()");
|
||||||
|
return _result.size();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
} // namespace sqlpp
|
} // namespace sqlpp
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ namespace
|
|||||||
static_assert(not sqlpp::can_be_null_t<decltype(x.s)>::value, "constant non-null value can not be null");
|
static_assert(not sqlpp::can_be_null_t<decltype(x.s)>::value, "constant non-null value can not be null");
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
const auto& x = db(select(bar.alpha, foo.delta, bar.gamma, seven)
|
const auto& x = db(select(bar.alpha, foo.delta, bar.gamma, seven)
|
||||||
.from(bar.join(foo).on(foo.omega > bar.alpha))
|
.from(bar.join(foo).on(foo.omega > bar.alpha))
|
||||||
.unconditionally()).front();
|
.unconditionally()).front();
|
||||||
static_assert(sqlpp::can_be_null_t<decltype(x.alpha)>::value, "nullable value can always be null");
|
static_assert(sqlpp::can_be_null_t<decltype(x.alpha)>::value, "nullable value can always be null");
|
||||||
@ -73,6 +73,14 @@ namespace
|
|||||||
static_assert(not sqlpp::can_be_null_t<decltype(x.delta)>::value, "right side of (inner) join cannot be null");
|
static_assert(not sqlpp::can_be_null_t<decltype(x.delta)>::value, "right side of (inner) join cannot be null");
|
||||||
static_assert(not sqlpp::can_be_null_t<decltype(x.s)>::value, "constant non-null value can not be null");
|
static_assert(not sqlpp::can_be_null_t<decltype(x.s)>::value, "constant non-null value can not be null");
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
MockSizeDb db2;
|
||||||
|
auto&& result = db2(select(bar.alpha, foo.delta, bar.gamma, seven)
|
||||||
|
.from(bar.join(foo).on(foo.omega > bar.alpha))
|
||||||
|
.unconditionally());
|
||||||
|
result.size();
|
||||||
|
static_assert(std::is_same<size_t, decltype(result.size())>::value, "MockSizeDb size() isn't size_t");
|
||||||
|
}
|
||||||
|
|
||||||
// Inner join
|
// Inner join
|
||||||
{
|
{
|
||||||
|
202
tests/MockDb.h
202
tests/MockDb.h
@ -288,4 +288,206 @@ struct MockDbT : public sqlpp::connection
|
|||||||
using MockDb = MockDbT<false>;
|
using MockDb = MockDbT<false>;
|
||||||
using EnforceDb = MockDbT<true>;
|
using EnforceDb = MockDbT<true>;
|
||||||
|
|
||||||
|
|
||||||
|
struct MockSizeDb : public sqlpp::connection
|
||||||
|
{
|
||||||
|
using _traits = MockDb::_traits;
|
||||||
|
|
||||||
|
using _serializer_context_t = MockDb::_serializer_context_t;
|
||||||
|
|
||||||
|
using _interpreter_context_t = _serializer_context_t;
|
||||||
|
|
||||||
|
_serializer_context_t get_serializer_context()
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static _serializer_context_t& _serialize_interpretable(const T& t, _serializer_context_t& context)
|
||||||
|
{
|
||||||
|
sqlpp::serialize(t, context);
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static _serializer_context_t& _interpret_interpretable(const T& t, _interpreter_context_t& context)
|
||||||
|
{
|
||||||
|
sqlpp::serialize(t, context);
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
class result_t : public MockDb::result_t
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
size_t size() const { return 0; }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Directly executed statements start here
|
||||||
|
template <typename T>
|
||||||
|
auto _run(const T& t, ::sqlpp::consistent_t) -> decltype(t._run(*this))
|
||||||
|
{
|
||||||
|
return t._run(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Check, typename T>
|
||||||
|
auto _run(const T& t, Check) -> Check;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
auto operator()(const T& t) -> decltype(this->_run(t, sqlpp::run_check_t<_serializer_context_t, T>{}))
|
||||||
|
{
|
||||||
|
return _run(t, sqlpp::run_check_t<_serializer_context_t, T>{});
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t execute(const std::string&)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename Statement,
|
||||||
|
typename Enable = typename std::enable_if<not std::is_convertible<Statement, std::string>::value, void>::type>
|
||||||
|
size_t execute(const Statement& x)
|
||||||
|
{
|
||||||
|
_serializer_context_t context;
|
||||||
|
::sqlpp::serialize(x, context);
|
||||||
|
std::cout << "Running execute call with\n" << context.str() << std::endl;
|
||||||
|
return execute(context.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Insert>
|
||||||
|
size_t insert(const Insert& x)
|
||||||
|
{
|
||||||
|
_serializer_context_t context;
|
||||||
|
::sqlpp::serialize(x, context);
|
||||||
|
std::cout << "Running insert call with\n" << context.str() << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Update>
|
||||||
|
size_t update(const Update& x)
|
||||||
|
{
|
||||||
|
_serializer_context_t context;
|
||||||
|
::sqlpp::serialize(x, context);
|
||||||
|
std::cout << "Running update call with\n" << context.str() << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Remove>
|
||||||
|
size_t remove(const Remove& x)
|
||||||
|
{
|
||||||
|
_serializer_context_t context;
|
||||||
|
::sqlpp::serialize(x, context);
|
||||||
|
std::cout << "Running remove call with\n" << context.str() << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Select>
|
||||||
|
result_t select(const Select& x)
|
||||||
|
{
|
||||||
|
_serializer_context_t context;
|
||||||
|
::sqlpp::serialize(x, context);
|
||||||
|
std::cout << "Running select call with\n" << context.str() << std::endl;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepared statements start here
|
||||||
|
using _prepared_statement_t = std::nullptr_t;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
auto _prepare(const T& t, ::sqlpp::consistent_t) -> decltype(t._prepare(*this))
|
||||||
|
{
|
||||||
|
return t._prepare(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Check, typename T>
|
||||||
|
auto _prepare(const T& t, Check) -> Check;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
auto prepare(const T& t) -> decltype(this->_prepare(t, sqlpp::prepare_check_t<_serializer_context_t, T>{}))
|
||||||
|
{
|
||||||
|
return _prepare(t, sqlpp::prepare_check_t<_serializer_context_t, T>{});
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Statement>
|
||||||
|
_prepared_statement_t prepare_execute(Statement& x)
|
||||||
|
{
|
||||||
|
_serializer_context_t context;
|
||||||
|
::sqlpp::serialize(x, context);
|
||||||
|
std::cout << "Running prepare execute call with\n" << context.str() << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Insert>
|
||||||
|
_prepared_statement_t prepare_insert(Insert& x)
|
||||||
|
{
|
||||||
|
_serializer_context_t context;
|
||||||
|
::sqlpp::serialize(x, context);
|
||||||
|
std::cout << "Running prepare insert call with\n" << context.str() << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename PreparedExecute>
|
||||||
|
size_t run_prepared_execute(const PreparedExecute&)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename PreparedInsert>
|
||||||
|
size_t run_prepared_insert(const PreparedInsert&)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Select>
|
||||||
|
_prepared_statement_t prepare_select(Select& x)
|
||||||
|
{
|
||||||
|
_serializer_context_t context;
|
||||||
|
::sqlpp::serialize(x, context);
|
||||||
|
std::cout << "Running prepare select call with\n" << context.str() << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename PreparedSelect>
|
||||||
|
result_t run_prepared_select(PreparedSelect&)
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto attach(std::string name) -> ::sqlpp::schema_t
|
||||||
|
{
|
||||||
|
return {name};
|
||||||
|
}
|
||||||
|
|
||||||
|
void start_transaction()
|
||||||
|
{
|
||||||
|
_mock_data._last_isolation_level = _mock_data._default_isolation_level;
|
||||||
|
}
|
||||||
|
|
||||||
|
void start_transaction(sqlpp::isolation_level level)
|
||||||
|
{
|
||||||
|
_mock_data._last_isolation_level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_default_isolation_level(sqlpp::isolation_level level)
|
||||||
|
{
|
||||||
|
_mock_data._default_isolation_level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlpp::isolation_level get_default_isolation_level()
|
||||||
|
{
|
||||||
|
return _mock_data._default_isolation_level;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rollback_transaction(bool)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void commit_transaction()
|
||||||
|
{}
|
||||||
|
|
||||||
|
void report_rollback_failure(std::string)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// temporary data store to verify the expected results were produced
|
||||||
|
InternalMockData _mock_data;
|
||||||
|
};
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user