statement cache progress

This commit is contained in:
Sascha Kühl 2024-01-04 15:52:29 +01:00
parent 94b1c356a5
commit 4504c783e1
4 changed files with 52 additions and 12 deletions

View File

@ -6,6 +6,7 @@
#include <chrono> #include <chrono>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <optional>
#include <thread> #include <thread>
#include <unordered_map> #include <unordered_map>
@ -48,8 +49,14 @@ public:
Connection* operator->() { return &connection_->second; } Connection* operator->() { return &connection_->second; }
Connection& operator*() { return connection_->second; } Connection& operator*() { return connection_->second; }
[[nodiscard]] size_t id() const { return connection_->first; } [[nodiscard]] std::optional<size_t> id() const
{
if (connection_) {
return connection_->first;
} else {
return std::nullopt;
}
}
[[nodiscard]] bool valid() const { return connection_ != nullptr; } [[nodiscard]] bool valid() const { return connection_ != nullptr; }
private: private:

View File

@ -27,15 +27,8 @@ public:
data_type_traits<Type>::bind_value(binder(), pos, val); data_type_traits<Type>::bind_value(binder(), pos, val);
} }
void bind(size_t pos, const char *value, size_t size) void bind(size_t pos, const char *value, size_t size);
{ void bind(size_t pos, std::string &val, size_t size);
data_type_traits<const char*>::bind_value(binder(), pos, value, size);
}
void bind(size_t pos, std::string &val, size_t size)
{
data_type_traits<std::string>::bind_value(binder(), pos, val, size);
}
virtual void reset() = 0; virtual void reset() = 0;

View File

@ -6,4 +6,14 @@ statement_impl::statement_impl(query_context query)
: query_(std::move(query)) : query_(std::move(query))
{} {}
void statement_impl::bind(size_t pos, const char *value, size_t size)
{
data_type_traits<const char*>::bind_value(binder(), pos, value, size);
}
void statement_impl::bind(size_t pos, std::string &val, size_t size)
{
data_type_traits<std::string>::bind_value(binder(), pos, val, size);
}
} }

View File

@ -16,7 +16,7 @@ TEST_CASE("Create connection pool", "[connection pool]") {
auto ptr = pool.acquire(); auto ptr = pool.acquire();
REQUIRE(ptr.valid()); REQUIRE(ptr.valid());
REQUIRE(ptr.id() > 0); REQUIRE(ptr.id().value() > 0);
REQUIRE(ptr->is_open()); REQUIRE(ptr->is_open());
REQUIRE(pool.idle() == 3); REQUIRE(pool.idle() == 3);
@ -48,3 +48,33 @@ TEST_CASE("Create connection pool", "[connection pool]") {
REQUIRE(pool.idle() == 4); REQUIRE(pool.idle() == 4);
REQUIRE(pool.inuse() == 0); REQUIRE(pool.inuse() == 0);
} }
TEST_CASE("Acquire connection by id", "[connection pool]") {
using pool_t = connection_pool<connection>;
pool_t pool("sqlite://sqlite.db", 4);
REQUIRE(pool.size() == 4);
REQUIRE(pool.idle() == 4);
REQUIRE(pool.inuse() == 0);
auto ptr = pool.acquire();
REQUIRE(ptr.valid());
REQUIRE(ptr.id());
REQUIRE(ptr.id().value() > 0);
REQUIRE(ptr->is_open());
auto same_ptr = pool.acquire(ptr.id().value());
REQUIRE(!same_ptr.valid());
const auto connection_id = ptr.id().value();
pool.release(ptr);
REQUIRE(!ptr.valid());
same_ptr = pool.acquire(connection_id);
REQUIRE(same_ptr.valid());
REQUIRE(same_ptr.id() == connection_id);
}