rename compile to build and added sql_hash to query_context

This commit is contained in:
Sascha Kühl 2026-04-06 11:52:55 +02:00
parent 57246d8742
commit d59b41d06c
6 changed files with 18 additions and 18 deletions

View File

@ -29,7 +29,7 @@ struct value_visitor;
class query_builder final : public query_part_visitor {
public:
sql::query_context compile(const query_data &data,
sql::query_context build(const query_data &data,
const sql::dialect &d,
std::optional<std::reference_wrapper<const sql::connection_impl>> conn);

View File

@ -37,6 +37,7 @@ enum class return_mode {
struct query_context {
std::string sql;
size_t sql_hash{};
sql_command command{};
std::string command_name{};
std::string schema_name{};

View File

@ -9,22 +9,22 @@ namespace matador::query {
utils::result<sql::execute_result, utils::error> executable_query::execute(const sql::executor &exec) const {
query_builder compiler;
return exec.execute(compiler.compile(*context_, exec.dialect(), std::nullopt));
return exec.execute(compiler.build(*context_, exec.dialect(), std::nullopt));
}
utils::result<sql::statement, utils::error> executable_query::prepare(sql::executor &exec) const {
query_builder compiler;
return exec.prepare(compiler.compile(*context_, exec.dialect(), std::nullopt));
return exec.prepare(compiler.build(*context_, exec.dialect(), std::nullopt));
}
sql::query_context executable_query::compile( const sql::executor& exec ) const {
query_builder compiler;
return compiler.compile(*context_, exec.dialect(), std::nullopt);
return compiler.build(*context_, exec.dialect(), std::nullopt);
}
std::string executable_query::str(const sql::executor &exec) const {
query_builder compiler;
return exec.str(compiler.compile(*context_, exec.dialect(), std::nullopt));
return exec.str(compiler.build(*context_, exec.dialect(), std::nullopt));
}
}

View File

@ -27,7 +27,7 @@ sql::record *create_prototype(const std::vector<object::attribute> &prototype) {
}
utils::result<sql::query_result<sql::record>, utils::error> fetchable_query::fetch_all(const sql::executor &exec) const {
query_builder compiler;
auto ctx = compiler.compile(*context_, exec.dialect(), std::nullopt);
auto ctx = compiler.build(*context_, exec.dialect(), std::nullopt);
ctx.resolver = exec.resolver();
return exec.fetch(ctx)
.and_then([](auto &&res) {
@ -38,7 +38,7 @@ utils::result<sql::query_result<sql::record>, utils::error> fetchable_query::fet
utils::result<std::optional<sql::record>, utils::error> fetchable_query::fetch_one(const sql::executor &exec) const {
query_builder compiler;
auto ctx = compiler.compile(*context_, exec.dialect(), std::nullopt);
auto ctx = compiler.build(*context_, exec.dialect(), std::nullopt);
ctx.resolver = exec.resolver();
auto result = exec.fetch(ctx);
if (!result.is_ok()) {
@ -64,7 +64,7 @@ std::string fetchable_query::str(const sql::dialect &d) const {
sql::query_context fetchable_query::compile(const sql::dialect &d) const {
query_builder compiler;
return compiler.compile(*context_, d, std::nullopt);
return compiler.build(*context_, d, std::nullopt);
}
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetchable_query::fetch(const sql::executor &exec, const std::type_index& index) const {

View File

@ -17,10 +17,9 @@
#include "matador/sql/dialect.hpp"
namespace matador::query {
sql::query_context query_builder::compile(const query_data& data,
sql::query_context query_builder::build(const query_data& data,
const sql::dialect& d,
const std::optional<std::reference_wrapper<const sql::connection_impl>>
conn) {
const std::optional<std::reference_wrapper<const sql::connection_impl>> conn) {
data_ = &data;
dialect_ = &d;
connection_ = conn;
@ -33,6 +32,7 @@ sql::query_context query_builder::compile(const query_data& data,
dialect_ = nullptr;
data_ = nullptr;
query_.sql_hash = std::hash<std::string>{}(query_.sql);
return {query_};
}

View File

@ -151,10 +151,9 @@ statement_cache::statement_cache(utils::message_bus &bus, connection_pool &pool,
utils::result<statement, utils::error> statement_cache::acquire(const query_context& ctx) {
std::unique_lock lock(mutex_);
// hash statement
const auto key = std::hash<std::string>{}(ctx.sql);
const auto now = std::chrono::steady_clock::now();
// Found in cache. Move it to of the LRU list
if (const auto it = cache_map_.find(key); it != cache_map_.end()) {
if (const auto it = cache_map_.find(ctx.sql_hash); it != cache_map_.end()) {
usage_list_.splice(usage_list_.begin(), usage_list_, it->second.position);
it->second.last_access = now;
bus_.publish<statement_accessed_event>({ctx.sql, std::chrono::steady_clock::now()});
@ -181,9 +180,9 @@ utils::result<statement, utils::error> statement_cache::acquire(const query_cont
bus_.publish<statement_evicted_event>({ctx.sql, std::chrono::steady_clock::now()});
}
usage_list_.push_front(key);
usage_list_.push_front(ctx.sql_hash);
const auto it = cache_map_.insert({
key,
ctx.sql_hash,
{statement{
std::make_shared<internal::statement_cache_proxy>(bus_, std::move(stmt), pool_, id)},
std::chrono::steady_clock::now(),