implemented the first version of statement_cache

This commit is contained in:
2025-07-30 10:34:33 +02:00
parent f90a670fb3
commit ebd8bccfb3
31 changed files with 575 additions and 278 deletions
+2
View File
@@ -26,6 +26,8 @@ add_executable(OrmTests
sql/FieldTest.cpp
utils/auto_reset_event.cpp
utils/auto_reset_event.hpp
sql/StatementCacheTest.cpp
sql/ConnectionPoolFixture.hpp
)
target_link_libraries(OrmTests matador-orm matador-core Catch2::Catch2WithMain)
+1 -2
View File
@@ -7,8 +7,7 @@
namespace matador::test::orm {
sql::connection_impl *test_backend_service::create(const sql::connection_info &info)
{
sql::connection_impl *test_backend_service::create(const sql::connection_info &info) {
return noop_connections_.insert(std::make_unique<test_connection>(info)).first->get();
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef CONNECTION_POOL_FIXTURE_HPP
#define CONNECTION_POOL_FIXTURE_HPP
#include "matador/sql/backend_provider.hpp"
#include "matador/sql/connection.hpp"
#include "matador/sql/interface/connection_impl.hpp"
#include "../backend/test_backend_service.hpp"
namespace matador::test::orm {
class ConnectionPoolFixture {
public:
ConnectionPoolFixture() {
sql::backend_provider::instance().register_backend("noop", std::make_unique<test_backend_service>());
db = std::make_unique<sql::connection>("noop://noop.db");
}
~ConnectionPoolFixture() = default;
protected:
std::unique_ptr<sql::connection> db;
};
}
#endif //CONNECTION_POOL_FIXTURE_HPP
+8 -30
View File
@@ -1,39 +1,21 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/backend_provider.hpp"
#include "matador/sql/connection.hpp"
#include "matador/sql/connection_pool.hpp"
#include "../backend/test_connection.hpp"
#include "../backend/test_backend_service.hpp"
#include "../utils/auto_reset_event.hpp"
#include "ConnectionPoolFixture.hpp"
#include <thread>
using namespace matador::sql;
using namespace matador::test::utils;
using namespace matador::test::orm;
namespace matador::test::orm {
class ConnectionPoolFixture {
public:
ConnectionPoolFixture() {
backend_provider::instance().register_backend("noop", std::make_unique<test_backend_service>());
db = std::make_unique<sql::connection>("noop://noop.db");
}
~ConnectionPoolFixture() = default;
protected:
std::unique_ptr<connection> db;
};
}
TEST_CASE_METHOD(ConnectionPoolFixture, "Create connection pool", "[connection pool]") {
using pool_t = connection_pool<test_connection>;
pool_t pool("noop://noop.db", 4);
connection_pool pool("noop://noop.db", 4);
REQUIRE(pool.size() == 4);
REQUIRE(pool.idle() == 4);
@@ -75,9 +57,7 @@ TEST_CASE_METHOD(ConnectionPoolFixture, "Create connection pool", "[connection p
}
TEST_CASE_METHOD(ConnectionPoolFixture, "Acquire connection by id", "[connection pool]") {
using pool_t = connection_pool<test_connection>;
pool_t pool("noop://noop.db", 4);
connection_pool pool("noop://noop.db", 4);
REQUIRE(pool.size() == 4);
REQUIRE(pool.idle() == 4);
@@ -105,9 +85,7 @@ TEST_CASE_METHOD(ConnectionPoolFixture, "Acquire connection by id", "[connection
}
TEST_CASE("Try acquire connection", "[connection pool][try acquire]") {
using pool_t = connection_pool<test_connection>;
pool_t pool("noop://noop.db", 1);
connection_pool pool("noop://noop.db", 1);
REQUIRE(pool.size() == 1);
REQUIRE(pool.idle() == 1);
@@ -145,7 +123,7 @@ TEST_CASE("Try acquire connection", "[connection pool][try acquire]") {
auto_reset_event reset_main_event;
auto_reset_event reset_thread_event;
std::thread t([&reset_main_event, &reset_thread_event, &pool]() {
std::thread t([&reset_main_event, &reset_thread_event, &pool] {
auto c1 = pool.acquire();
REQUIRE(c1.valid());
REQUIRE(c1.id());
+36
View File
@@ -0,0 +1,36 @@
#include <catch2/catch_test_macros.hpp>
#include <matador/query/query.hpp>
#include "matador/sql/connection_pool.hpp"
#include "matador/sql/statement_cache.hpp"
#include "../backend/test_backend_service.hpp"
#include "ConnectionPoolFixture.hpp"
using namespace matador::test;
using namespace matador::sql;
using namespace matador::query;
TEST_CASE("Test statement cache", "[statement][cache]") {
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
connection_pool pool("noop://noop.db", 4);
statement_cache cache(pool, 4);
query_context ctx;
ctx.sql = "SELECT * FROM person";
REQUIRE(cache.capacity() == 4);
REQUIRE(cache.empty());
auto result = cache.acquire(ctx);
REQUIRE(result);
REQUIRE(cache.size() == 1);
REQUIRE(!cache.empty());
REQUIRE(cache.capacity() == 4);
const auto stmt = result.value();
}