From 4504c783e18610e87ec82b3019bf457871ece1cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Thu, 4 Jan 2024 15:52:29 +0100 Subject: [PATCH] statement cache progress --- include/matador/sql/connection_pool.hpp | 11 +++++++-- include/matador/sql/statement_impl.hpp | 11 ++------- src/sql/statement_impl.cpp | 10 ++++++++ test/ConnectionPoolTest.cpp | 32 ++++++++++++++++++++++++- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/include/matador/sql/connection_pool.hpp b/include/matador/sql/connection_pool.hpp index dbd8094..dd05dcf 100644 --- a/include/matador/sql/connection_pool.hpp +++ b/include/matador/sql/connection_pool.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -48,8 +49,14 @@ public: Connection* operator->() { return &connection_->second; } Connection& operator*() { return connection_->second; } - [[nodiscard]] size_t id() const { return connection_->first; } - + [[nodiscard]] std::optional id() const + { + if (connection_) { + return connection_->first; + } else { + return std::nullopt; + } + } [[nodiscard]] bool valid() const { return connection_ != nullptr; } private: diff --git a/include/matador/sql/statement_impl.hpp b/include/matador/sql/statement_impl.hpp index 68881aa..c56d191 100644 --- a/include/matador/sql/statement_impl.hpp +++ b/include/matador/sql/statement_impl.hpp @@ -27,15 +27,8 @@ public: data_type_traits::bind_value(binder(), pos, val); } - void bind(size_t pos, const char *value, size_t size) - { - data_type_traits::bind_value(binder(), pos, value, size); - } - - void bind(size_t pos, std::string &val, size_t size) - { - data_type_traits::bind_value(binder(), pos, val, size); - } + void bind(size_t pos, const char *value, size_t size); + void bind(size_t pos, std::string &val, size_t size); virtual void reset() = 0; diff --git a/src/sql/statement_impl.cpp b/src/sql/statement_impl.cpp index b23c496..da32133 100644 --- a/src/sql/statement_impl.cpp +++ b/src/sql/statement_impl.cpp @@ -6,4 +6,14 @@ statement_impl::statement_impl(query_context query) : query_(std::move(query)) {} +void statement_impl::bind(size_t pos, const char *value, size_t size) +{ + data_type_traits::bind_value(binder(), pos, value, size); +} + +void statement_impl::bind(size_t pos, std::string &val, size_t size) +{ + data_type_traits::bind_value(binder(), pos, val, size); +} + } \ No newline at end of file diff --git a/test/ConnectionPoolTest.cpp b/test/ConnectionPoolTest.cpp index 62a6ce4..6ef0537 100644 --- a/test/ConnectionPoolTest.cpp +++ b/test/ConnectionPoolTest.cpp @@ -16,7 +16,7 @@ TEST_CASE("Create connection pool", "[connection pool]") { auto ptr = pool.acquire(); REQUIRE(ptr.valid()); - REQUIRE(ptr.id() > 0); + REQUIRE(ptr.id().value() > 0); REQUIRE(ptr->is_open()); REQUIRE(pool.idle() == 3); @@ -48,3 +48,33 @@ TEST_CASE("Create connection pool", "[connection pool]") { REQUIRE(pool.idle() == 4); REQUIRE(pool.inuse() == 0); } + +TEST_CASE("Acquire connection by id", "[connection pool]") { + using pool_t = connection_pool; + + 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); +} \ No newline at end of file